From 60bc2e8053effd7c28f7057444850e504aebd3a5 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 5 Sep 2019 08:44:08 +0300 Subject: [PATCH] keys: simplify PublicKey() for PrivateKey Public key is just a point, so use the coordinates obtained previously to initialize the PublicKey structure without jumping through the hoops of encoding/decoding. --- pkg/crypto/keys/private_key.go | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/pkg/crypto/keys/private_key.go b/pkg/crypto/keys/private_key.go index bffda5080..75c659cd0 100644 --- a/pkg/crypto/keys/private_key.go +++ b/pkg/crypto/keys/private_key.go @@ -1,7 +1,6 @@ package keys import ( - "bytes" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" @@ -61,8 +60,6 @@ func NewPrivateKeyFromRawBytes(b []byte) (*PrivateKey, error) { // PublicKey derives the public key from the private key. func (p *PrivateKey) PublicKey() (*PublicKey, error) { var ( - err error - pk PublicKey c = elliptic.P256() q = new(big.Int).SetBytes(p.b) ) @@ -72,25 +69,7 @@ func (p *PrivateKey) PublicKey() (*PublicKey, error) { return nil, errors.New("failed to derive public key using elliptic curve") } - bx := x.Bytes() - padded := append( - bytes.Repeat( - []byte{0x00}, - 32-len(bx), - ), - bx..., - ) - - prefix := []byte{0x03} - if y.Bit(0) == 0 { - prefix = []byte{0x02} - } - b := append(prefix, padded...) - - if err = pk.DecodeBytes(b); err != nil { - return nil, err - } - return &pk, nil + return &PublicKey{X: x, Y: y}, nil } // NewPrivateKeyFromWIF returns a NEO PrivateKey from the given