mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 09:42:22 +00:00
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 (
|
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)
|
||||||
|
|
Loading…
Reference in a new issue