@@ -33,11 +33,8 @@ import (
3333 "errors"
3434 "fmt"
3535 "io"
36- "math/big"
3736 "sort"
3837
39- "github.com/btcsuite/btcd/btcec"
40- "github.com/ethereum/go-ethereum/common/math"
4138 "github.com/ethereum/go-ethereum/crypto"
4239 "github.com/ethereum/go-ethereum/crypto/sha3"
4340 "github.com/ethereum/go-ethereum/rlp"
@@ -206,25 +203,27 @@ func (r *Record) DecodeRLP(s *rlp.Stream) error {
206203 return nil
207204}
208205
206+ type s256raw []byte
207+
208+ func (s256raw ) ENRKey () string { return "secp256k1" }
209+
209210// NodeAddr returns the node address. The return value will be nil if the record is
210211// unsigned.
211212func (r * Record ) NodeAddr () []byte {
212- var secp256k1 Secp256k1
213- if r .Load (& secp256k1 ) != nil {
213+ var key s256raw
214+ if r .Load (& key ) != nil {
214215 return nil
215216 }
216- pk := btcec .PublicKey (secp256k1 )
217- return crypto .Keccak256 (pk .SerializeCompressed ())
217+ return crypto .Keccak256 (key )
218218}
219219
220220// Sign signs the record with the given private key. It updates the record's identity
221221// scheme, public key and increments the sequence number. Sign returns an error if the
222222// encoded record is larger than the size limit.
223223func (r * Record ) Sign (privkey * ecdsa.PrivateKey ) error {
224- pk := (* btcec .PublicKey )(& privkey .PublicKey )
225224 r .seq = r .seq + 1
226225 r .Set (ID_SECP256k1_KECCAK )
227- r .Set (Secp256k1 (* pk ))
226+ r .Set (Secp256k1 (privkey . PublicKey ))
228227 return r .signAndEncode (privkey )
229228}
230229
@@ -244,14 +243,14 @@ func (r *Record) signAndEncode(privkey *ecdsa.PrivateKey) error {
244243 // Sign the tail of the list.
245244 h := sha3 .NewKeccak256 ()
246245 rlp .Encode (h , list [1 :])
247- sig , err := ( * btcec . PrivateKey )( privkey ). Sign (h .Sum (nil ))
246+ sig , err := crypto . Sign (h .Sum (nil ), privkey )
248247 if err != nil {
249248 return err
250249 }
250+ sig = sig [:len (sig )- 1 ] // remove v
251251
252252 // Put signature in front.
253- r .signature = encodeCompactSignature (sig )
254- list [0 ] = r .signature
253+ r .signature , list [0 ] = sig , sig
255254 r .raw , err = rlp .EncodeToBytes (list )
256255 if err != nil {
257256 return err
@@ -265,41 +264,28 @@ func (r *Record) signAndEncode(privkey *ecdsa.PrivateKey) error {
265264func (r * Record ) verifySignature () error {
266265 // Get identity scheme, public key, signature.
267266 var id ID
268- var secp256k1 Secp256k1
267+ var key s256raw
269268 if err := r .Load (& id ); err != nil {
270269 return err
271270 } else if id != ID_SECP256k1_KECCAK {
272271 return errNoID
273272 }
274- if err := r .Load (& secp256k1 ); err != nil {
275- return err
276- }
277- sig , err := parseCompactSignature (r .signature )
278- if err != nil {
273+ if err := r .Load (& key ); err != nil {
279274 return err
275+ } else if len (key ) != 33 {
276+ return fmt .Errorf ("invalid public key" )
280277 }
281278
282279 // Verify the signature.
283280 list := make ([]interface {}, 0 , len (r .pairs )* 2 + 1 )
284281 list = r .appendPairs (list )
285282 h := sha3 .NewKeccak256 ()
286283 rlp .Encode (h , list )
287- if ! sig .Verify (h .Sum (nil ), (* btcec .PublicKey )(& secp256k1 )) {
284+ fmt .Printf ("sig: %x\n " , r .signature )
285+ fmt .Printf ("key: %x\n " , key )
286+ fmt .Printf ("hash: %x\n " , h .Sum (nil ))
287+ if ! crypto .VerifySignature (key , h .Sum (nil ), r .signature ) {
288288 return errInvalidSig
289289 }
290290 return nil
291291}
292-
293- func encodeCompactSignature (sig * btcec.Signature ) []byte {
294- b := make ([]byte , 64 )
295- math .ReadBits (sig .R , b [:32 ])
296- math .ReadBits (sig .S , b [32 :])
297- return b
298- }
299-
300- func parseCompactSignature (sig []byte ) (* btcec.Signature , error ) {
301- if len (sig ) != 64 {
302- return nil , errInvalidSigsize
303- }
304- return & btcec.Signature {R : new (big.Int ).SetBytes (sig [:32 ]), S : new (big.Int ).SetBytes (sig [32 :])}, nil
305- }
0 commit comments