forked from TrueCloudLab/neoneo-go
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:
parent
2fd2866f7f
commit
a56511ced3
1 changed files with 25 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue