From a56511ced3973728f998154212b1057f0ef41765 Mon Sep 17 00:00:00 2001 From: Anastasia Prasolova Date: Wed, 20 Feb 2019 16:34:28 +0300 Subject: [PATCH] PublicKey from serialized bytes (#154) * small fixes * gofmt * fix in raw tx build * fixes after review * balance getter interface * moved address and signature calculation to public key * errors handling * PublicKey() returns PublicKey instead of bytes slice * fixes after review * fixes after review * public key creation from asn1 serialized key --- pkg/crypto/public_key.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/crypto/public_key.go b/pkg/crypto/public_key.go index 0ac4c3c01..fd70088ed 100644 --- a/pkg/crypto/public_key.go +++ b/pkg/crypto/public_key.go @@ -2,7 +2,9 @@ package crypto import ( "bytes" + "crypto/ecdsa" "crypto/sha256" + "crypto/x509" "encoding/binary" "encoding/hex" "io" @@ -72,6 +74,28 @@ func (p *PublicKey) Bytes() []byte { return append([]byte{prefix}, paddedX...) } +// NewPublicKeyFromRawBytes returns a NEO PublicKey from the ASN.1 serialized keys. +func NewPublicKeyFromRawBytes(data []byte) (*PublicKey, error) { + var ( + err error + pubkey interface{} + ) + if pubkey, err = x509.ParsePKIXPublicKey(data); err != nil { + return nil, err + } + pk, ok := pubkey.(*ecdsa.PublicKey) + if !ok { + return nil, errors.New("given bytes aren't ECDSA public key") + } + key := PublicKey{ + ECPoint{ + X: pk.X, + Y: pk.Y, + }, + } + return &key, nil +} + // DecodeBytes decodes a PublicKey from the given slice of bytes. func (p *PublicKey) DecodeBytes(data []byte) error { l := len(data) @@ -163,7 +187,7 @@ func (p *PublicKey) Signature() ([]byte, error) { func (p *PublicKey) Address() (string, error) { var ( err error - b []byte + b []byte ) if b, err = p.Signature(); err != nil { return "", err