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
This commit is contained in:
Anastasia Prasolova 2019-02-20 16:34:28 +03:00 committed by fabwa
parent 2fd2866f7f
commit a56511ced3

View file

@ -2,7 +2,9 @@ package crypto
import ( import (
"bytes" "bytes"
"crypto/ecdsa"
"crypto/sha256" "crypto/sha256"
"crypto/x509"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"io" "io"
@ -72,6 +74,28 @@ func (p *PublicKey) Bytes() []byte {
return append([]byte{prefix}, paddedX...) 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. // DecodeBytes decodes a PublicKey from the given slice of bytes.
func (p *PublicKey) DecodeBytes(data []byte) error { func (p *PublicKey) DecodeBytes(data []byte) error {
l := len(data) l := len(data)