Skip to content

Commit

Permalink
PublicKey from serialized bytes (#154)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
aprasolova authored and fabwa committed Feb 20, 2019
1 parent 2fd2866 commit a56511c
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pkg/crypto/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package crypto

import (
"bytes"
"crypto/ecdsa"
"crypto/sha256"
"crypto/x509"
"encoding/binary"
"encoding/hex"
"io"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a56511c

Please sign in to comment.