2019-08-27 13:29:42 +00:00
|
|
|
package keys
|
2018-03-02 15:24:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2018-12-05 19:04:31 +00:00
|
|
|
"crypto/x509"
|
2018-03-02 15:24:09 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
2019-08-27 10:00:03 +00:00
|
|
|
"github.com/nspcc-dev/rfc6979"
|
2018-03-02 15:24:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PrivateKey represents a NEO private key.
|
|
|
|
type PrivateKey struct {
|
|
|
|
b []byte
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// NewPrivateKey creates a new random private key.
|
2018-03-02 15:24:09 +00:00
|
|
|
func NewPrivateKey() (*PrivateKey, error) {
|
2019-09-04 21:12:39 +00:00
|
|
|
priv, _, _, err := elliptic.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
2018-03-02 15:24:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-04 21:12:39 +00:00
|
|
|
return &PrivateKey{b: priv}, nil
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPrivateKeyFromHex returns a PrivateKey created from the
|
|
|
|
// given hex string.
|
|
|
|
func NewPrivateKeyFromHex(str string) (*PrivateKey, error) {
|
|
|
|
b, err := hex.DecodeString(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewPrivateKeyFromBytes(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPrivateKeyFromBytes returns a NEO PrivateKey from the given byte slice.
|
|
|
|
func NewPrivateKeyFromBytes(b []byte) (*PrivateKey, error) {
|
|
|
|
if len(b) != 32 {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"invalid byte length: expected %d bytes got %d", 32, len(b),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return &PrivateKey{b}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 19:04:31 +00:00
|
|
|
// NewPrivateKeyFromRawBytes returns a NEO PrivateKey from the ASN.1 serialized keys.
|
|
|
|
func NewPrivateKeyFromRawBytes(b []byte) (*PrivateKey, error) {
|
|
|
|
privkey, err := x509.ParseECPrivateKey(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewPrivateKeyFromBytes(privkey.D.Bytes())
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:24:09 +00:00
|
|
|
// PublicKey derives the public key from the private key.
|
2019-09-05 06:35:02 +00:00
|
|
|
func (p *PrivateKey) PublicKey() *PublicKey {
|
2018-03-02 15:24:09 +00:00
|
|
|
var (
|
2019-09-04 21:12:39 +00:00
|
|
|
c = elliptic.P256()
|
2018-03-02 15:24:09 +00:00
|
|
|
q = new(big.Int).SetBytes(p.b)
|
|
|
|
)
|
|
|
|
|
2019-09-04 21:12:39 +00:00
|
|
|
x, y := c.ScalarBaseMult(q.Bytes())
|
2018-03-02 15:24:09 +00:00
|
|
|
|
2019-09-05 06:35:02 +00:00
|
|
|
return &PublicKey{X: x, Y: y}
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPrivateKeyFromWIF returns a NEO PrivateKey from the given
|
|
|
|
// WIF (wallet import format).
|
|
|
|
func NewPrivateKeyFromWIF(wif string) (*PrivateKey, error) {
|
|
|
|
w, err := WIFDecode(wif, WIFVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return w.PrivateKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WIF returns the (wallet import format) of the PrivateKey.
|
|
|
|
// Good documentation about this process can be found here:
|
|
|
|
// https://en.bitcoin.it/wiki/Wallet_import_format
|
2019-09-05 06:35:02 +00:00
|
|
|
func (p *PrivateKey) WIF() string {
|
|
|
|
w, err := WIFEncode(p.b, WIFVersion, true)
|
|
|
|
// The only way WIFEncode() can fail is if we're to give it a key of
|
|
|
|
// wrong size, but we have a proper key here, aren't we?
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return w
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Address derives the public NEO address that is coupled with the private key, and
|
|
|
|
// returns it as a string.
|
2019-09-05 06:35:02 +00:00
|
|
|
func (p *PrivateKey) Address() string {
|
|
|
|
pk := p.PublicKey()
|
|
|
|
return pk.Address()
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Signature creates the signature using the private key.
|
2019-09-05 06:35:02 +00:00
|
|
|
func (p *PrivateKey) Signature() []byte {
|
|
|
|
pk := p.PublicKey()
|
|
|
|
return pk.Signature()
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs arbitrary length data using the private key.
|
|
|
|
func (p *PrivateKey) Sign(data []byte) ([]byte, error) {
|
|
|
|
var (
|
|
|
|
privateKey = p.ecdsa()
|
|
|
|
digest = sha256.Sum256(data)
|
|
|
|
)
|
|
|
|
|
|
|
|
r, s, err := rfc6979.SignECDSA(privateKey, digest[:], sha256.New)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params := privateKey.Curve.Params()
|
|
|
|
curveOrderByteSize := params.P.BitLen() / 8
|
|
|
|
rBytes, sBytes := r.Bytes(), s.Bytes()
|
|
|
|
signature := make([]byte, curveOrderByteSize*2)
|
|
|
|
copy(signature[curveOrderByteSize-len(rBytes):], rBytes)
|
|
|
|
copy(signature[curveOrderByteSize*2-len(sBytes):], sBytes)
|
|
|
|
|
|
|
|
return signature, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ecsda converts the key to a usable ecsda.PrivateKey for signing data.
|
|
|
|
func (p *PrivateKey) ecdsa() *ecdsa.PrivateKey {
|
|
|
|
priv := new(ecdsa.PrivateKey)
|
|
|
|
priv.PublicKey.Curve = elliptic.P256()
|
|
|
|
priv.D = new(big.Int).SetBytes(p.b)
|
|
|
|
priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(p.b)
|
|
|
|
return priv
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the stringer interface.
|
|
|
|
func (p *PrivateKey) String() string {
|
|
|
|
return hex.EncodeToString(p.b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns the underlying bytes of the PrivateKey.
|
|
|
|
func (p *PrivateKey) Bytes() []byte {
|
|
|
|
return p.b
|
|
|
|
}
|