@@ -12,7 +12,7 @@ namespace p2p
1212namespace
1313{
1414constexpr char c_keyID[] = " id" ;
15- constexpr char c_keySec256k1 [] = " secp256k1" ;
15+ constexpr char c_keySecp256k1 [] = " secp256k1" ;
1616constexpr char c_keyIP[] = " ip" ;
1717constexpr char c_keyTCP[] = " tcp" ;
1818constexpr char c_keyUDP[] = " udp" ;
@@ -88,29 +88,57 @@ void ENR::streamContent(RLPStream& _s) const
8888 }
8989}
9090
91- ENR createV4ENR (Secret const & _secret, boost::asio::ip::address const & _ip, uint16_t _tcpPort, uint16_t _udpPort)
91+ ENR ENR::update (
92+ std::map<std::string, bytes> const & _keyValuePairs, SignFunction const & _signFunction) const
9293{
93- ENR::SignFunction signFunction = [&_secret](bytesConstRef _data) {
94- // dev::sign returns 65 bytes signature containing r,s,v values
95- Signature s = dev::sign (_secret, sha3 (_data));
96- // The resulting 64-byte signature is encoded as the concatenation of the r and s signature values.
97- return bytes (&s[0 ], &s[64 ]);
98- };
94+ return ENR (m_seq + 1 , _keyValuePairs, _signFunction);
95+ }
96+
97+ ENR IdentitySchemeV4::createENR (Secret const & _secret, boost::asio::ip::address const & _ip,
98+ uint16_t _tcpPort, uint16_t _udpPort)
99+ {
100+ ENR::SignFunction signFunction = [&_secret](
101+ bytesConstRef _data) { return sign (_data, _secret); };
102+
103+ auto const keyValuePairs = createKeyValuePairs (_secret, _ip, _tcpPort, _udpPort);
99104
105+ return ENR{0 /* sequence number */ , keyValuePairs, signFunction};
106+ }
107+
108+ bytes IdentitySchemeV4::sign (bytesConstRef _data, Secret const & _secret)
109+ {
110+ // dev::sign returns 65 bytes signature containing r,s,v values
111+ Signature s = dev::sign (_secret, sha3 (_data));
112+ // The resulting 64-byte signature is encoded as the concatenation of the r and s signature
113+ // values.
114+ return bytes (&s[0 ], &s[64 ]);
115+ }
116+
117+ std::map<std::string, bytes> IdentitySchemeV4::createKeyValuePairs (Secret const & _secret,
118+ boost::asio::ip::address const & _ip, uint16_t _tcpPort, uint16_t _udpPort)
119+ {
100120 PublicCompressed const publicKey = toPublicCompressed (_secret);
101121
102122 auto const address = _ip.is_v4 () ? addressToBytes (_ip.to_v4 ()) : addressToBytes (_ip.to_v6 ());
103123
104124 // Values are of different types (string, bytes, uint16_t),
105125 // so we store them as RLP representation
106- std::map<std::string, bytes> const keyValuePairs = {{c_keyID, rlp (c_IDV4)},
107- {c_keySec256k1 , rlp (publicKey. asBytes ()) }, {c_keyIP , rlp (address )},
108- {c_keyTCP, rlp (_tcpPort)}, {c_keyUDP, rlp (_udpPort)}};
126+ return {{c_keyID, rlp (c_IDV4)}, {c_keySecp256k1, rlp (publicKey. asBytes () )},
127+ {c_keyIP , rlp (address) }, {c_keyTCP , rlp (_tcpPort )}, {c_keyUDP, rlp (_udpPort)}};
128+ }
109129
110- return ENR{0 /* sequence number */ , keyValuePairs, signFunction};
130+ ENR IdentitySchemeV4::updateENR (ENR const & _enr, Secret const & _secret,
131+ boost::asio::ip::address const & _ip, uint16_t _tcpPort, uint16_t _udpPort)
132+ {
133+ ENR::SignFunction signFunction = [&_secret](
134+ bytesConstRef _data) { return sign (_data, _secret); };
135+
136+ auto const keyValuePairs = createKeyValuePairs (_secret, _ip, _tcpPort, _udpPort);
137+
138+ return _enr.update (keyValuePairs, signFunction);
111139}
112140
113- ENR parseV4ENR (RLP const & _rlp)
141+ ENR IdentitySchemeV4::parseENR (RLP const & _rlp)
114142{
115143 ENR::VerifyFunction verifyFunction = [](std::map<std::string, bytes> const & _keyValuePairs,
116144 bytesConstRef _signature, bytesConstRef _data) {
0 commit comments