@@ -15,7 +15,7 @@ import (
15
15
"github.com/sammyne/base58"
16
16
)
17
17
18
- // PrivateKey represents an extended private key.
18
+ // PrivateKey houses all the information of an extended private key.
19
19
type PrivateKey struct {
20
20
PublicKey
21
21
Data []byte
@@ -27,7 +27,7 @@ func (priv *PrivateKey) AddressPubKeyHash() []byte {
27
27
return btcutil .Hash160 (priv .publicKeyData ())
28
28
}
29
29
30
- // Child implements ExtendedKey
30
+ // Child implements ExtendedKey
31
31
func (priv * PrivateKey ) Child (i uint32 ) (ExtendedKey , error ) {
32
32
// Prevent derivation of children beyond the max allowed depth.
33
33
if priv .Level == math .MaxUint8 {
@@ -44,7 +44,7 @@ func (priv *PrivateKey) Child(i uint32) (ExtendedKey, error) {
44
44
data := make ([]byte , KeyDataLen + ChildIndexLen )
45
45
if i < HardenedKeyStart { // normal
46
46
copy (data , priv .publicKeyData ())
47
- } else { // harden
47
+ } else { // harden, where 0x00 prefix plus 32-byte data
48
48
data [0 ] = 0x00
49
49
ReverseCopy (data [1 :KeyDataLen ], priv .Data )
50
50
}
@@ -60,7 +60,7 @@ func (priv *PrivateKey) Child(i uint32) (ExtendedKey, error) {
60
60
IL , chainCode := I [:len (I )/ 2 ], I [len (I )/ 2 :]
61
61
62
62
// 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
64
64
// within the valid range for a secp256k1 private key. There is a small
65
65
// chance (< 1 in 2^127) this condition will not hold, and in that case,
66
66
// 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) {
74
74
// Add the parent private key to the intermediate private key to
75
75
// derive the final child key.
76
76
//
77
- // childKey = parse256(Il ) + parenKey
77
+ // childKey = parse256(IL ) + parenKey
78
78
k := new (big.Int ).SetBytes (priv .Data )
79
79
z .Add (z , k )
80
80
z .Mod (z , secp256k1Curve .N )
@@ -88,21 +88,6 @@ func (priv *PrivateKey) Child(i uint32) (ExtendedKey, error) {
88
88
chainCode , childData ), nil
89
89
}
90
90
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
-
106
91
// IsForNet implements ExtendedKey
107
92
func (priv * PrivateKey ) IsForNet (keyID Magic ) bool {
108
93
return bytes .Equal (priv .Version , keyID [:])
@@ -135,11 +120,6 @@ func (priv *PrivateKey) Neuter() (*PublicKey, error) {
135
120
return & priv .PublicKey , nil
136
121
}
137
122
138
- // ParentFingerprint implements ExtendedKey
139
- func (priv * PrivateKey ) ParentFingerprint () uint32 {
140
- return priv .PublicKey .ParentFingerprint ()
141
- }
142
-
143
123
// Public implements ExtendedKey
144
124
func (priv * PrivateKey ) Public () (* btcec.PublicKey , error ) {
145
125
return btcec .ParsePubKey (priv .publicKeyData (), secp256k1Curve )
@@ -169,7 +149,7 @@ func (priv *PrivateKey) String() string {
169
149
170
150
// ToECPrivate converts the extended key to a btcec private key and returns it.
171
151
// 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.
173
153
func (priv * PrivateKey ) ToECPrivate () * btcec.PrivateKey {
174
154
privKey , _ := btcec .PrivKeyFromBytes (secp256k1Curve , priv .Data )
175
155
@@ -216,14 +196,15 @@ func NewPrivateKey(version []byte, depth uint8, parentFP []byte, index uint32,
216
196
// ParsePrivateKey a new extended private key instance out of a base58-encoded
217
197
// extended key.
218
198
func ParsePrivateKey (data58 string ) (* PrivateKey , error ) {
199
+ // decodePublicKey is applicable here too !!!
219
200
pub , err := decodePublicKey (data58 )
220
201
if nil != err {
221
202
return nil , err
222
203
}
223
204
224
205
priv := & PrivateKey {
225
206
PublicKey : * pub ,
226
- Data : pub .Data [1 :],
207
+ Data : pub .Data [1 :], // simply trims out the 0x00 prefix
227
208
}
228
209
priv .Version = priv .PublicKey .Version
229
210
priv .PublicKey .Data , priv .PublicKey .Version = nil , nil
0 commit comments