Skip to content

Commit 73c380f

Browse files
author
sammyne
committed
chore(PrivateKey): remove the redundant methods of duplicated implementation
1 parent f32e14b commit 73c380f

File tree

5 files changed

+20
-58
lines changed

5 files changed

+20
-58
lines changed

extended_key.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ type ExtendedKey interface {
1717
// bound to it for private keys
1818
AddressPubKeyHash() []byte
1919
// Child returns a derived child extended key at the given index. When this
20-
// extended key is a private extended key (as determined by the IsPrivate
21-
// function), a private extended key will be derived. Otherwise, the derived
22-
// extended key will be also be a public extended key.
20+
// extended key is a private extended key, a private extended key will be
21+
// derived. Otherwise, the derived extended key will be also be a public
22+
// extended key.
2323
//
2424
// When the index is greater to or equal than the HardenedKeyStart constant,
2525
// the derived extended key will be a hardened extended key. It is only
@@ -62,6 +62,9 @@ type ExtendedKey interface {
6262
Neuter() (*PublicKey, error)
6363
// ParentFingerprint returns a fingerprint of the parent extended key from
6464
// which this one was derived.
65+
//
66+
// It's defined the be the first 32 bits of the key identifier as specified by
67+
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#key-identifiers
6568
ParentFingerprint() uint32
6669
// Public converts the extended key to a btcec public key and returns it.
6770
Public() (*btcec.PublicKey, error)

internal.go

-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func appendMeta(buf []byte, pub *PublicKey) []byte {
3232
// key string.
3333
// Note: the decoded key goes through format check only, no on-curve check
3434
func decodePublicKey(data58 string) (*PublicKey, error) {
35-
//version, decoded, err := base58.CheckDecodeX(data58, VersionLen)
3635
decoded, version, err := base58.CheckDecodeX(data58, VersionLen)
3736
if nil != err {
3837
return nil, err
@@ -49,11 +48,8 @@ func decodePublicKey(data58 string) (*PublicKey, error) {
4948
// where the version has separated from decoded
5049

5150
// decompose the decoded payload into fields
52-
//a, b := 0, VersionLen
53-
//pub.Version = decoded[a:b]
5451
pub.Version = version
5552

56-
//a, b = b, b+DepthLen
5753
a, b := 0, DepthLen
5854
pub.Level = decoded[a:b][0]
5955

magics.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const MagicLen = 4
77
// Magic represents the buffer to hold the magic bytes.
88
type Magic [MagicLen]byte
99

10-
// magic bytes as version prefix for serialization, and their application goes
10+
// magic bytes as version prefix for base58 encoding, and their application goes
1111
// as named.
1212
var (
1313
MainNetPrivateKey = &Magic{0x04, 0x88, 0xad, 0xe4} // starts with xprv

master_key.go

+5-23
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,13 @@ func GenerateMasterKey(rand io.Reader, keyID Magic,
2323
return nil, ErrNoEnoughEntropy
2424
}
2525

26-
/*
27-
// I = HMAC-SHA512(Key = "Bitcoin seed", Data = S)
28-
hmac512 := hmac.New(sha512.New, masterKey)
29-
hmac512.Write(seed)
30-
I := hmac512.Sum(nil)
31-
32-
secretKey, chainCode := I[:len(I)/2], I[len(I)/2:]
33-
// Ensure the key in usable.
34-
if x := new(big.Int).SetBytes(secretKey); 0 == x.Sign() ||
35-
x.Cmp(btcec.S256().N) >= 0 {
36-
return nil, ErrUnusableSeed
37-
}
38-
39-
// fingerprint of parent
40-
parentFP := []byte{0x00, 0x00, 0x00, 0x00}
41-
42-
//return NewExtendedKey(keyID[:], secretKey, chainCode,
43-
// parentFP, 0, 0, true), nil
44-
45-
return NewPrivateKey(keyID[:], 0, parentFP, 0, chainCode, secretKey), nil
46-
*/
26+
// delegate the common derivation routine
4727
return newMaster(seed, keyID)
4828
}
4929

5030
// NewMasterKey creates a new master node for use in creating a hierarchical
5131
// deterministic key chain. The seed must be between 128 and 512 bits and
52-
// should be generated by a cryptographically secure random generation source.
32+
// should be generated by a cryptographically secure random source.
5333
//
5434
// NOTE: There is an extremely small chance (< 1 in 2^127) the provided seed
5535
// will derive to an unusable secret key. The ErrUnusable error will be
@@ -65,7 +45,9 @@ func NewMasterKey(seed []byte, keyID Magic) (*PrivateKey, error) {
6545
}
6646

6747
// newMaster is helper function to derives a extended private key based on the
68-
// given valid seed and the provided keyID
48+
// given valid seed and the provided keyID.
49+
//
50+
// Official specification goes as https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#master-key-generation
6951
func newMaster(seed []byte, keyID Magic) (*PrivateKey, error) {
7052
// I = HMAC-SHA512(Key = "Bitcoin seed", Data = S)
7153
hmac512 := hmac.New(sha512.New, masterHMACKey)

private_key.go

+8-27
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/sammyne/base58"
1616
)
1717

18-
// PrivateKey represents an extended private key.
18+
// PrivateKey houses all the information of an extended private key.
1919
type PrivateKey struct {
2020
PublicKey
2121
Data []byte
@@ -27,7 +27,7 @@ func (priv *PrivateKey) AddressPubKeyHash() []byte {
2727
return btcutil.Hash160(priv.publicKeyData())
2828
}
2929

30-
// Child implements ExtendedKey
30+
// Child implements ExtendedKey
3131
func (priv *PrivateKey) Child(i uint32) (ExtendedKey, error) {
3232
// Prevent derivation of children beyond the max allowed depth.
3333
if priv.Level == math.MaxUint8 {
@@ -44,7 +44,7 @@ func (priv *PrivateKey) Child(i uint32) (ExtendedKey, error) {
4444
data := make([]byte, KeyDataLen+ChildIndexLen)
4545
if i < HardenedKeyStart { // normal
4646
copy(data, priv.publicKeyData())
47-
} else { // harden
47+
} else { // harden, where 0x00 prefix plus 32-byte data
4848
data[0] = 0x00
4949
ReverseCopy(data[1:KeyDataLen], priv.Data)
5050
}
@@ -60,7 +60,7 @@ func (priv *PrivateKey) Child(i uint32) (ExtendedKey, error) {
6060
IL, chainCode := I[:len(I)/2], I[len(I)/2:]
6161

6262
// Both derived public or private keys rely on treating the left 32-byte
63-
// sequence calculated above (Il) as a 256-bit integer that must be
63+
// sequence calculated above (IL) as a 256-bit integer that must be
6464
// within the valid range for a secp256k1 private key. There is a small
6565
// chance (< 1 in 2^127) this condition will not hold, and in that case,
6666
// a child extended key can't be created for this index and the caller
@@ -74,7 +74,7 @@ func (priv *PrivateKey) Child(i uint32) (ExtendedKey, error) {
7474
// Add the parent private key to the intermediate private key to
7575
// derive the final child key.
7676
//
77-
// childKey = parse256(Il) + parenKey
77+
// childKey = parse256(IL) + parenKey
7878
k := new(big.Int).SetBytes(priv.Data)
7979
z.Add(z, k)
8080
z.Mod(z, secp256k1Curve.N)
@@ -88,21 +88,6 @@ func (priv *PrivateKey) Child(i uint32) (ExtendedKey, error) {
8888
chainCode, childData), nil
8989
}
9090

91-
// Depth implements ExtendedKey
92-
func (priv *PrivateKey) Depth() uint8 {
93-
return priv.Level
94-
}
95-
96-
// Hardened implements ExtendedKey
97-
func (priv *PrivateKey) Hardened() bool {
98-
return priv.PublicKey.Hardened()
99-
}
100-
101-
// Index implements ExtendedKey
102-
func (priv *PrivateKey) Index() uint32 {
103-
return priv.PublicKey.Index()
104-
}
105-
10691
// IsForNet implements ExtendedKey
10792
func (priv *PrivateKey) IsForNet(keyID Magic) bool {
10893
return bytes.Equal(priv.Version, keyID[:])
@@ -135,11 +120,6 @@ func (priv *PrivateKey) Neuter() (*PublicKey, error) {
135120
return &priv.PublicKey, nil
136121
}
137122

138-
// ParentFingerprint implements ExtendedKey
139-
func (priv *PrivateKey) ParentFingerprint() uint32 {
140-
return priv.PublicKey.ParentFingerprint()
141-
}
142-
143123
// Public implements ExtendedKey
144124
func (priv *PrivateKey) Public() (*btcec.PublicKey, error) {
145125
return btcec.ParsePubKey(priv.publicKeyData(), secp256k1Curve)
@@ -169,7 +149,7 @@ func (priv *PrivateKey) String() string {
169149

170150
// ToECPrivate converts the extended key to a btcec private key and returns it.
171151
// As you might imagine this is only possible if the extended key is a private
172-
// extended key (as determined by the IsPrivate function).
152+
// extended key.
173153
func (priv *PrivateKey) ToECPrivate() *btcec.PrivateKey {
174154
privKey, _ := btcec.PrivKeyFromBytes(secp256k1Curve, priv.Data)
175155

@@ -216,14 +196,15 @@ func NewPrivateKey(version []byte, depth uint8, parentFP []byte, index uint32,
216196
// ParsePrivateKey a new extended private key instance out of a base58-encoded
217197
// extended key.
218198
func ParsePrivateKey(data58 string) (*PrivateKey, error) {
199+
// decodePublicKey is applicable here too !!!
219200
pub, err := decodePublicKey(data58)
220201
if nil != err {
221202
return nil, err
222203
}
223204

224205
priv := &PrivateKey{
225206
PublicKey: *pub,
226-
Data: pub.Data[1:],
207+
Data: pub.Data[1:], // simply trims out the 0x00 prefix
227208
}
228209
priv.Version = priv.PublicKey.Version
229210
priv.PublicKey.Data, priv.PublicKey.Version = nil, nil

0 commit comments

Comments
 (0)