2016-07-08 20:26:09 +00:00
|
|
|
package extkeys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/binary"
|
2016-08-18 00:15:58 +00:00
|
|
|
"encoding/hex"
|
2016-07-08 20:26:09 +00:00
|
|
|
"errors"
|
2016-08-18 00:15:58 +00:00
|
|
|
"fmt"
|
2016-07-08 20:26:09 +00:00
|
|
|
"math/big"
|
2016-08-18 00:15:58 +00:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcutil/base58"
|
2016-07-08 20:26:09 +00:00
|
|
|
)
|
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// Implementation of the following BIPs:
|
|
|
|
// - BIP32 (https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)
|
|
|
|
// - BIP39 (https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki)
|
|
|
|
// - BIP44 (https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)
|
|
|
|
//
|
2016-07-08 20:26:09 +00:00
|
|
|
// Referencing
|
|
|
|
// https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
|
|
|
|
// https://bitcoin.org/en/developer-guide#hardened-keys
|
|
|
|
|
|
|
|
// Reference Implementations
|
|
|
|
// https://github.com/btcsuite/btcutil/tree/master/hdkeychain
|
|
|
|
// https://github.com/WeMeetAgain/go-hdwallet
|
|
|
|
|
|
|
|
// https://github.com/ConsenSys/eth-lightwallet/blob/master/lib/keystore.js
|
|
|
|
// https://github.com/bitpay/bitcore-lib/tree/master/lib
|
|
|
|
|
|
|
|
// MUST CREATE HARDENED CHILDREN OF THE MASTER PRIVATE KEY (M) TO PREVENT
|
|
|
|
// A COMPROMISED CHILD KEY FROM COMPROMISING THE MASTER KEY.
|
|
|
|
// AS THERE ARE NO NORMAL CHILDREN FOR THE MASTER KEYS,
|
|
|
|
// THE MASTER PUBLIC KEY IS NOT USED IN HD WALLETS.
|
|
|
|
// ALL OTHER KEYS CAN HAVE NORMAL CHILDREN,
|
|
|
|
// SO THE CORRESPONDING EXTENDED PUBLIC KEYS MAY BE USED INSTEAD.
|
|
|
|
|
|
|
|
// TODO make sure we're doing this ^^^^ !!!!!!
|
|
|
|
|
|
|
|
const (
|
2017-05-03 14:24:48 +00:00
|
|
|
// HardenedKeyStart defines a starting point for hardened key.
|
2016-08-18 00:15:58 +00:00
|
|
|
// Each extended key has 2^31 normal child keys and 2^31 hardened child keys.
|
|
|
|
// Thus the range for normal child keys is [0, 2^31 - 1] and the range for hardened child keys is [2^31, 2^32 - 1].
|
|
|
|
HardenedKeyStart = 0x80000000 // 2^31
|
2016-07-08 20:26:09 +00:00
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
// MinSeedBytes is the minimum number of bytes allowed for a seed to a master node.
|
|
|
|
MinSeedBytes = 16 // 128 bits
|
2016-07-08 20:26:09 +00:00
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
// MaxSeedBytes is the maximum number of bytes allowed for a seed to a master node.
|
|
|
|
MaxSeedBytes = 64 // 512 bits
|
2016-07-08 20:26:09 +00:00
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
// serializedKeyLen is the length of a serialized public or private
|
|
|
|
// extended key. It consists of 4 bytes version, 1 byte depth, 4 bytes
|
|
|
|
// fingerprint, 4 bytes child number, 32 bytes chain code, and 33 bytes
|
|
|
|
// public/private key data.
|
|
|
|
serializedKeyLen = 4 + 1 + 4 + 4 + 32 + 33 // 78 bytes
|
2016-08-21 06:45:59 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// CoinTypeBTC is BTC coin type
|
|
|
|
CoinTypeBTC = 0 // 0x80000000
|
2016-08-23 21:32:04 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// CoinTypeTestNet is test net coin type
|
|
|
|
CoinTypeTestNet = 1 // 0x80000001
|
|
|
|
|
|
|
|
// CoinTypeETH is ETH coin type
|
|
|
|
CoinTypeETH = 60 // 0x8000003c
|
|
|
|
|
|
|
|
// EmptyExtendedKeyString marker string for zero extended key
|
2016-08-23 21:32:04 +00:00
|
|
|
EmptyExtendedKeyString = "Zeroed extended key"
|
2018-06-22 12:21:49 +00:00
|
|
|
|
|
|
|
// MaxDepth is the maximum depth of an extended key.
|
|
|
|
// Extended keys with depth MaxDepth cannot derive child keys.
|
|
|
|
MaxDepth = 255
|
2016-08-18 00:15:58 +00:00
|
|
|
)
|
2016-07-08 20:26:09 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// errors
|
2016-08-18 00:15:58 +00:00
|
|
|
var (
|
|
|
|
ErrInvalidKey = errors.New("key is invalid")
|
|
|
|
ErrInvalidSeed = errors.New("seed is invalid")
|
|
|
|
ErrInvalidSeedLen = fmt.Errorf("the recommended size of seed is %d-%d bits", MinSeedBytes, MaxSeedBytes)
|
|
|
|
ErrDerivingHardenedFromPublic = errors.New("cannot derive a hardened key from public key")
|
|
|
|
ErrBadChecksum = errors.New("bad extended key checksum")
|
|
|
|
ErrInvalidKeyLen = errors.New("serialized extended key length is invalid")
|
2016-08-21 06:45:59 +00:00
|
|
|
ErrDerivingChild = errors.New("error deriving child key")
|
|
|
|
ErrInvalidMasterKey = errors.New("invalid master key supplied")
|
2018-06-22 12:21:49 +00:00
|
|
|
ErrMaxDepthExceeded = errors.New("max depth exceeded")
|
2017-05-03 14:24:48 +00:00
|
|
|
)
|
2016-08-18 00:15:58 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
var (
|
|
|
|
// PrivateKeyVersion is version for private key
|
2016-08-18 00:15:58 +00:00
|
|
|
PrivateKeyVersion, _ = hex.DecodeString("0488ADE4")
|
2016-07-08 20:26:09 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// PublicKeyVersion is version for public key
|
|
|
|
PublicKeyVersion, _ = hex.DecodeString("0488B21E")
|
|
|
|
)
|
2016-08-21 06:45:59 +00:00
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// ExtendedKey represents BIP44-compliant HD key
|
2016-08-18 00:15:58 +00:00
|
|
|
type ExtendedKey struct {
|
|
|
|
Version []byte // 4 bytes, mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, 0x04358394 private
|
2018-06-22 12:21:49 +00:00
|
|
|
Depth uint8 // 1 byte, depth: 0x00 for master nodes, 0x01 for level-1 derived keys, ....
|
2016-08-18 00:15:58 +00:00
|
|
|
FingerPrint []byte // 4 bytes, fingerprint of the parent's key (0x00000000 if master key)
|
|
|
|
ChildNumber uint32 // 4 bytes, This is ser32(i) for i in xi = xpar/i, with xi the key being serialized. (0x00000000 if master key)
|
|
|
|
KeyData []byte // 33 bytes, the public key or private key data (serP(K) for public keys, 0x00 || ser256(k) for private keys)
|
|
|
|
ChainCode []byte // 32 bytes, the chain code
|
|
|
|
IsPrivate bool // (non-serialized) if false, this chain will only contain a public key and can only create a public key chain.
|
|
|
|
CachedPubKeyData []byte // (non-serialized) used for memoization of public key (calculated from a private key)
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
// nolint: gas
|
2018-05-14 17:13:56 +00:00
|
|
|
const masterSecret = "Bitcoin seed"
|
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
// NewMaster creates new master node, root of HD chain/tree.
|
|
|
|
// Both master and child nodes are of ExtendedKey type, and all the children derive from the root node.
|
2018-05-14 17:13:56 +00:00
|
|
|
func NewMaster(seed []byte) (*ExtendedKey, error) {
|
2016-08-18 00:15:58 +00:00
|
|
|
// Ensure seed is within expected limits
|
2016-07-08 20:26:09 +00:00
|
|
|
lseed := len(seed)
|
2016-08-18 00:15:58 +00:00
|
|
|
if lseed < MinSeedBytes || lseed > MaxSeedBytes {
|
|
|
|
return nil, ErrInvalidSeedLen
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-14 17:13:56 +00:00
|
|
|
secretKey, chainCode, err := splitHMAC(seed, []byte(masterSecret))
|
2016-08-18 00:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
master := &ExtendedKey{
|
|
|
|
Version: PrivateKeyVersion,
|
|
|
|
Depth: 0,
|
2016-07-08 20:26:09 +00:00
|
|
|
FingerPrint: []byte{0x00, 0x00, 0x00, 0x00},
|
2016-08-18 00:15:58 +00:00
|
|
|
ChildNumber: 0,
|
|
|
|
KeyData: secretKey,
|
|
|
|
ChainCode: chainCode,
|
2016-07-08 20:26:09 +00:00
|
|
|
IsPrivate: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return master, nil
|
|
|
|
}
|
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
// Child derives extended key at a given index i.
|
|
|
|
// If parent is private, then derived key is also private. If parent is public, then derived is public.
|
|
|
|
//
|
|
|
|
// If i >= HardenedKeyStart, then hardened key is generated.
|
|
|
|
// You can only generate hardened keys from private parent keys.
|
|
|
|
// If you try generating hardened key form public parent key, ErrDerivingHardenedFromPublic is returned.
|
|
|
|
//
|
|
|
|
// There are four CKD (child key derivation) scenarios:
|
|
|
|
// 1) Private extended key -> Hardened child private extended key
|
|
|
|
// 2) Private extended key -> Non-hardened child private extended key
|
|
|
|
// 3) Public extended key -> Non-hardened child public extended key
|
|
|
|
// 4) Public extended key -> Hardened child public extended key (INVALID!)
|
2017-05-03 14:24:48 +00:00
|
|
|
func (k *ExtendedKey) Child(i uint32) (*ExtendedKey, error) {
|
2018-06-22 12:21:49 +00:00
|
|
|
if k.Depth == MaxDepth {
|
|
|
|
return nil, ErrMaxDepthExceeded
|
|
|
|
}
|
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
// A hardened child may not be created from a public extended key (Case #4).
|
|
|
|
isChildHardened := i >= HardenedKeyStart
|
2017-05-03 14:24:48 +00:00
|
|
|
if !k.IsPrivate && isChildHardened {
|
2016-08-18 00:15:58 +00:00
|
|
|
return nil, ErrDerivingHardenedFromPublic
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
keyLen := 33
|
|
|
|
seed := make([]byte, keyLen+4)
|
2016-07-08 20:26:09 +00:00
|
|
|
if isChildHardened {
|
2016-08-18 00:15:58 +00:00
|
|
|
// Case #1: 0x00 || ser256(parentKey) || ser32(i)
|
2017-05-03 14:24:48 +00:00
|
|
|
copy(seed[1:], k.KeyData) // 0x00 || ser256(parentKey)
|
2016-07-08 20:26:09 +00:00
|
|
|
} else {
|
2016-08-18 00:15:58 +00:00
|
|
|
// Case #2 and #3: serP(parentPubKey) || ser32(i)
|
2017-05-03 14:24:48 +00:00
|
|
|
copy(seed, k.pubKeyBytes())
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|
2016-08-18 00:15:58 +00:00
|
|
|
binary.BigEndian.PutUint32(seed[keyLen:], i)
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
secretKey, chainCode, err := splitHMAC(seed, k.ChainCode)
|
2016-08-18 00:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
child := &ExtendedKey{
|
|
|
|
ChainCode: chainCode,
|
2017-05-03 14:24:48 +00:00
|
|
|
Depth: k.Depth + 1,
|
2016-08-18 00:15:58 +00:00
|
|
|
ChildNumber: i,
|
2017-05-03 14:24:48 +00:00
|
|
|
IsPrivate: k.IsPrivate,
|
2016-08-18 00:15:58 +00:00
|
|
|
// The fingerprint for the derived child is the first 4 bytes of parent's
|
2017-05-03 14:24:48 +00:00
|
|
|
FingerPrint: btcutil.Hash160(k.pubKeyBytes())[:4],
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if k.IsPrivate {
|
2016-08-18 00:15:58 +00:00
|
|
|
// Case #1 or #2: childKey = parse256(IL) + parentKey
|
2017-05-03 14:24:48 +00:00
|
|
|
parentKeyBigInt := new(big.Int).SetBytes(k.KeyData)
|
2016-08-18 00:15:58 +00:00
|
|
|
keyBigInt := new(big.Int).SetBytes(secretKey)
|
2016-07-08 20:26:09 +00:00
|
|
|
keyBigInt.Add(keyBigInt, parentKeyBigInt)
|
2016-08-18 00:15:58 +00:00
|
|
|
keyBigInt.Mod(keyBigInt, btcec.S256().N)
|
|
|
|
|
2018-09-04 07:42:40 +00:00
|
|
|
// Make sure that child.KeyData is 32 bytes of data even if the value is represented with less bytes.
|
|
|
|
// When we derive a child of this key, we call splitHMAC that does a sha512 of a seed that is:
|
|
|
|
// - 1 byte with 0x00
|
|
|
|
// - 32 bytes for the key data
|
|
|
|
// - 4 bytes for the child key index
|
|
|
|
// If we don't padd the KeyData, it will be shifted to left in that 32 bytes space
|
|
|
|
// generating a different seed and different child key.
|
|
|
|
// This part fixes a bug we had previously and described at:
|
|
|
|
// https://medium.com/@alexberegszaszi/why-do-my-bip32-wallets-disagree-6f3254cc5846#.86inuifuq
|
|
|
|
keyData := keyBigInt.Bytes()
|
|
|
|
if len(keyData) < 32 {
|
|
|
|
extra := make([]byte, 32-len(keyData))
|
|
|
|
keyData = append(extra, keyData...)
|
|
|
|
}
|
|
|
|
|
|
|
|
child.KeyData = keyData
|
2016-08-18 00:15:58 +00:00
|
|
|
child.Version = PrivateKeyVersion
|
2016-07-08 20:26:09 +00:00
|
|
|
} else {
|
2016-08-18 00:15:58 +00:00
|
|
|
// Case #3: childKey = serP(point(parse256(IL)) + parentKey)
|
2016-07-08 20:26:09 +00:00
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
// Calculate the corresponding intermediate public key for intermediate private key.
|
|
|
|
keyx, keyy := btcec.S256().ScalarBaseMult(secretKey)
|
2016-07-08 20:26:09 +00:00
|
|
|
if keyx.Sign() == 0 || keyy.Sign() == 0 {
|
2016-08-18 00:15:58 +00:00
|
|
|
return nil, ErrInvalidKey
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
// Convert the serialized compressed parent public key into X and Y coordinates
|
|
|
|
// so it can be added to the intermediate public key.
|
2017-05-03 14:24:48 +00:00
|
|
|
pubKey, err := btcec.ParsePubKey(k.KeyData, btcec.S256())
|
2016-08-18 00:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-08 20:26:09 +00:00
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
// childKey = serP(point(parse256(IL)) + parentKey)
|
|
|
|
childX, childY := btcec.S256().Add(keyx, keyy, pubKey.X, pubKey.Y)
|
|
|
|
pk := btcec.PublicKey{Curve: btcec.S256(), X: childX, Y: childY}
|
|
|
|
child.KeyData = pk.SerializeCompressed()
|
|
|
|
child.Version = PublicKeyVersion
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|
|
|
|
return child, nil
|
|
|
|
}
|
|
|
|
|
2016-08-23 21:32:04 +00:00
|
|
|
// BIP44Child returns Status CKD#i (where i is child index).
|
2016-08-21 06:45:59 +00:00
|
|
|
// BIP44 format is used: m / purpose' / coin_type' / account' / change / address_index
|
2017-05-03 14:24:48 +00:00
|
|
|
func (k *ExtendedKey) BIP44Child(coinType, i uint32) (*ExtendedKey, error) {
|
|
|
|
if !k.IsPrivate {
|
2016-08-21 06:45:59 +00:00
|
|
|
return nil, ErrInvalidMasterKey
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
if k.Depth != 0 {
|
2016-08-21 06:45:59 +00:00
|
|
|
return nil, ErrInvalidMasterKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// m/44'/60'/0'/0/index
|
2017-05-03 14:24:48 +00:00
|
|
|
extKey, err := k.Derive([]uint32{
|
2016-08-21 06:45:59 +00:00
|
|
|
HardenedKeyStart + 44, // purpose
|
|
|
|
HardenedKeyStart + coinType, // cointype
|
|
|
|
HardenedKeyStart + 0, // account
|
|
|
|
0, // 0 - public, 1 - private
|
|
|
|
i, // index
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return extKey, nil
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// Derive returns a derived child key at a given path
|
|
|
|
func (k *ExtendedKey) Derive(path []uint32) (*ExtendedKey, error) {
|
2016-08-21 06:45:59 +00:00
|
|
|
var err error
|
2017-05-03 14:24:48 +00:00
|
|
|
extKey := k
|
2016-08-21 06:45:59 +00:00
|
|
|
for _, i := range path {
|
|
|
|
extKey, err = extKey.Child(i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrDerivingChild
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return extKey, nil
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
// Neuter returns a new extended public key from a give extended private key.
|
|
|
|
// If the input extended key is already public, it will be returned unaltered.
|
2016-08-18 00:15:58 +00:00
|
|
|
func (k *ExtendedKey) Neuter() (*ExtendedKey, error) {
|
|
|
|
// Already an extended public key.
|
|
|
|
if !k.IsPrivate {
|
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the associated public extended key version bytes.
|
|
|
|
version, err := chaincfg.HDPrivateKeyToPublicKeyID(k.Version)
|
2016-07-08 20:26:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-18 00:15:58 +00:00
|
|
|
|
|
|
|
// Convert it to an extended public key. The key for the new extended
|
|
|
|
// key will simply be the pubkey of the current extended private key.
|
|
|
|
return &ExtendedKey{
|
|
|
|
Version: version,
|
|
|
|
KeyData: k.pubKeyBytes(),
|
|
|
|
ChainCode: k.ChainCode,
|
|
|
|
FingerPrint: k.FingerPrint,
|
|
|
|
Depth: k.Depth,
|
|
|
|
ChildNumber: k.ChildNumber,
|
|
|
|
IsPrivate: false,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the extended key as a human-readable base58-encoded string.
|
|
|
|
func (k *ExtendedKey) String() string {
|
2016-08-23 21:32:04 +00:00
|
|
|
if k == nil || len(k.KeyData) == 0 {
|
|
|
|
return EmptyExtendedKeyString
|
2016-08-18 00:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var childNumBytes [4]byte
|
|
|
|
binary.BigEndian.PutUint32(childNumBytes[:], k.ChildNumber)
|
|
|
|
|
|
|
|
// The serialized format is:
|
|
|
|
// version (4) || depth (1) || parent fingerprint (4)) ||
|
|
|
|
// child num (4) || chain code (32) || key data (33) || checksum (4)
|
|
|
|
serializedBytes := make([]byte, 0, serializedKeyLen+4)
|
|
|
|
serializedBytes = append(serializedBytes, k.Version...)
|
2018-06-22 12:21:49 +00:00
|
|
|
serializedBytes = append(serializedBytes, k.Depth)
|
2016-08-18 00:15:58 +00:00
|
|
|
serializedBytes = append(serializedBytes, k.FingerPrint...)
|
|
|
|
serializedBytes = append(serializedBytes, childNumBytes[:]...)
|
|
|
|
serializedBytes = append(serializedBytes, k.ChainCode...)
|
|
|
|
if k.IsPrivate {
|
|
|
|
serializedBytes = append(serializedBytes, 0x00)
|
|
|
|
serializedBytes = paddedAppend(32, serializedBytes, k.KeyData)
|
|
|
|
} else {
|
|
|
|
serializedBytes = append(serializedBytes, k.pubKeyBytes()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkSum := chainhash.DoubleHashB(serializedBytes)[:4]
|
|
|
|
serializedBytes = append(serializedBytes, checkSum...)
|
|
|
|
return base58.Encode(serializedBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// pubKeyBytes returns bytes for the serialized compressed public key associated
|
|
|
|
// with this extended key in an efficient manner including memoization as
|
|
|
|
// necessary.
|
|
|
|
//
|
|
|
|
// When the extended key is already a public key, the key is simply returned as
|
|
|
|
// is since it's already in the correct form. However, when the extended key is
|
|
|
|
// a private key, the public key will be calculated and memoized so future
|
|
|
|
// accesses can simply return the cached result.
|
|
|
|
func (k *ExtendedKey) pubKeyBytes() []byte {
|
|
|
|
// Just return the key if it's already an extended public key.
|
|
|
|
if !k.IsPrivate {
|
|
|
|
return k.KeyData
|
|
|
|
}
|
|
|
|
|
|
|
|
pkx, pky := btcec.S256().ScalarBaseMult(k.KeyData)
|
|
|
|
pubKey := btcec.PublicKey{Curve: btcec.S256(), X: pkx, Y: pky}
|
|
|
|
return pubKey.SerializeCompressed()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToECDSA returns the key data as ecdsa.PrivateKey
|
|
|
|
func (k *ExtendedKey) ToECDSA() *ecdsa.PrivateKey {
|
|
|
|
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), k.KeyData)
|
|
|
|
return privKey.ToECDSA()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeyFromString returns a new extended key instance from a base58-encoded
|
|
|
|
// extended key.
|
|
|
|
func NewKeyFromString(key string) (*ExtendedKey, error) {
|
2016-08-23 21:32:04 +00:00
|
|
|
if key == EmptyExtendedKeyString || len(key) == 0 {
|
|
|
|
return &ExtendedKey{}, nil
|
|
|
|
}
|
|
|
|
|
2016-08-18 00:15:58 +00:00
|
|
|
// The base58-decoded extended key must consist of a serialized payload
|
|
|
|
// plus an additional 4 bytes for the checksum.
|
|
|
|
decoded := base58.Decode(key)
|
|
|
|
if len(decoded) != serializedKeyLen+4 {
|
|
|
|
return nil, ErrInvalidKeyLen
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|
2016-08-18 00:15:58 +00:00
|
|
|
|
|
|
|
// The serialized format is:
|
|
|
|
// version (4) || depth (1) || parent fingerprint (4)) ||
|
|
|
|
// child num (4) || chain code (32) || key data (33) || checksum (4)
|
|
|
|
|
|
|
|
// Split the payload and checksum up and ensure the checksum matches.
|
|
|
|
payload := decoded[:len(decoded)-4]
|
|
|
|
checkSum := decoded[len(decoded)-4:]
|
|
|
|
expectedCheckSum := chainhash.DoubleHashB(payload)[:4]
|
|
|
|
if !bytes.Equal(checkSum, expectedCheckSum) {
|
|
|
|
return nil, ErrBadChecksum
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize each of the payload fields.
|
|
|
|
version := payload[:4]
|
2018-06-22 12:21:49 +00:00
|
|
|
depth := payload[4:5][0]
|
2016-08-18 00:15:58 +00:00
|
|
|
fingerPrint := payload[5:9]
|
|
|
|
childNumber := binary.BigEndian.Uint32(payload[9:13])
|
|
|
|
chainCode := payload[13:45]
|
|
|
|
keyData := payload[45:78]
|
|
|
|
|
|
|
|
// The key data is a private key if it starts with 0x00. Serialized
|
|
|
|
// compressed pubkeys either start with 0x02 or 0x03.
|
|
|
|
isPrivate := keyData[0] == 0x00
|
|
|
|
if isPrivate {
|
|
|
|
// Ensure the private key is valid. It must be within the range
|
|
|
|
// of the order of the secp256k1 curve and not be 0.
|
|
|
|
keyData = keyData[1:]
|
|
|
|
keyNum := new(big.Int).SetBytes(keyData)
|
|
|
|
if keyNum.Cmp(btcec.S256().N) >= 0 || keyNum.Sign() == 0 {
|
|
|
|
return nil, ErrInvalidSeed
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Ensure the public key parses correctly and is actually on the
|
|
|
|
// secp256k1 curve.
|
|
|
|
_, err := btcec.ParsePubKey(keyData, btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ExtendedKey{
|
|
|
|
Version: version,
|
|
|
|
KeyData: keyData,
|
|
|
|
ChainCode: chainCode,
|
|
|
|
FingerPrint: fingerPrint,
|
|
|
|
Depth: depth,
|
|
|
|
ChildNumber: childNumber,
|
|
|
|
IsPrivate: isPrivate,
|
|
|
|
}, nil
|
2016-07-08 20:26:09 +00:00
|
|
|
}
|