@@ -45,28 +45,55 @@ const toU32 = (n: number) => {
45
45
return buf ;
46
46
} ;
47
47
48
- type HDKeyOpt = {
48
+ interface HDKeyOpt {
49
49
versions : Versions ;
50
50
depth ?: number ;
51
51
index ?: number ;
52
52
parentFingerprint ?: number ;
53
53
chainCode : Uint8Array ;
54
54
publicKey ?: Uint8Array ;
55
55
privateKey ?: Uint8Array | bigint ;
56
- } ;
56
+ }
57
57
58
58
export class HDKey {
59
- readonly versions : Versions ;
60
- readonly depth : number = 0 ;
61
- readonly index : number = 0 ;
62
- readonly chainCode : Uint8Array | null = null ;
63
- readonly parentFingerprint : number = 0 ;
64
- private privKey ?: bigint ;
65
- private privKeyBytes ?: Uint8Array ;
66
- private pubKey ?: Uint8Array ;
67
- private pubHash : Uint8Array | undefined ;
59
+ get fingerprint ( ) : number {
60
+ if ( ! this . pubHash ) {
61
+ throw new Error ( "No publicKey set!" ) ;
62
+ }
63
+ return fromU32 ( this . pubHash ) ;
64
+ }
65
+ get identifier ( ) : Uint8Array | undefined {
66
+ return this . pubHash ;
67
+ }
68
+ get pubKeyHash ( ) : Uint8Array | undefined {
69
+ return this . pubHash ;
70
+ }
71
+ get privateKey ( ) : Uint8Array | null {
72
+ return this . privKeyBytes || null ;
73
+ }
74
+ get publicKey ( ) : Uint8Array | null {
75
+ return this . pubKey || null ;
76
+ }
77
+ get privateExtendedKey ( ) : string {
78
+ const priv = this . privateKey ;
79
+ if ( ! priv ) {
80
+ throw new Error ( "No private key" ) ;
81
+ }
82
+ return base58c . encode (
83
+ this . serialize (
84
+ this . versions . private ,
85
+ concatBytes ( new Uint8Array ( [ 0 ] ) , priv )
86
+ )
87
+ ) ;
88
+ }
89
+ get publicExtendedKey ( ) : string {
90
+ if ( ! this . pubKey ) {
91
+ throw new Error ( "No public key" ) ;
92
+ }
93
+ return base58c . encode ( this . serialize ( this . versions . public , this . pubKey ) ) ;
94
+ }
68
95
69
- static fromMasterSeed (
96
+ public static fromMasterSeed (
70
97
seed : Uint8Array ,
71
98
versions : Versions = BITCOIN_VERSIONS
72
99
) : HDKey {
@@ -78,7 +105,7 @@ export class HDKey {
78
105
} ) ;
79
106
}
80
107
81
- static fromExtendedKey (
108
+ public static fromExtendedKey (
82
109
base58key : string ,
83
110
versions : Versions = BITCOIN_VERSIONS
84
111
) : HDKey {
@@ -105,9 +132,18 @@ export class HDKey {
105
132
}
106
133
}
107
134
108
- static fromJSON ( json : { xpriv : string } ) : HDKey {
135
+ public static fromJSON ( json : { xpriv : string } ) : HDKey {
109
136
return HDKey . fromExtendedKey ( json . xpriv ) ;
110
137
}
138
+ public readonly versions : Versions ;
139
+ public readonly depth : number = 0 ;
140
+ public readonly index : number = 0 ;
141
+ public readonly chainCode : Uint8Array | null = null ;
142
+ public readonly parentFingerprint : number = 0 ;
143
+ private privKey ?: bigint ;
144
+ private privKeyBytes ?: Uint8Array ;
145
+ private pubKey ?: Uint8Array ;
146
+ private pubHash : Uint8Array | undefined ;
111
147
112
148
constructor ( opt : HDKeyOpt ) {
113
149
if ( ! opt || typeof opt !== "object" ) {
@@ -118,8 +154,9 @@ export class HDKey {
118
154
this . chainCode = opt . chainCode ;
119
155
this . index = opt . index || 0 ;
120
156
this . parentFingerprint = opt . parentFingerprint || 0 ;
121
- if ( opt . publicKey && opt . privateKey )
157
+ if ( opt . publicKey && opt . privateKey ) {
122
158
throw new Error ( "HDKey: publicKey and privateKey at same time." ) ;
159
+ }
123
160
if ( opt . privateKey ) {
124
161
if ( ! secp . utils . isValidPrivateKey ( opt . privateKey ) ) {
125
162
throw new Error ( "Invalid private key" ) ;
@@ -137,42 +174,6 @@ export class HDKey {
137
174
}
138
175
this . pubHash = hash160 ( this . pubKey ) ;
139
176
}
140
- get fingerprint ( ) : number {
141
- if ( ! this . pubHash ) {
142
- throw new Error ( "No publicKey set!" ) ;
143
- }
144
- return fromU32 ( this . pubHash ) ;
145
- }
146
- get identifier ( ) : Uint8Array | undefined {
147
- return this . pubHash ;
148
- }
149
- get pubKeyHash ( ) : Uint8Array | undefined {
150
- return this . pubHash ;
151
- }
152
- get privateKey ( ) : Uint8Array | null {
153
- return this . privKeyBytes || null ;
154
- }
155
- get publicKey ( ) : Uint8Array | null {
156
- return this . pubKey || null ;
157
- }
158
- get privateExtendedKey ( ) : string {
159
- const priv = this . privateKey ;
160
- if ( ! priv ) {
161
- throw new Error ( "No private key" ) ;
162
- }
163
- return base58c . encode (
164
- this . serialize (
165
- this . versions . private ,
166
- concatBytes ( new Uint8Array ( [ 0 ] ) , priv )
167
- )
168
- ) ;
169
- }
170
- get publicExtendedKey ( ) : string {
171
- if ( ! this . pubKey ) {
172
- throw new Error ( "No public key" ) ;
173
- }
174
- return base58c . encode ( this . serialize ( this . versions . public , this . pubKey ) ) ;
175
- }
176
177
177
178
public derive ( path : string ) : HDKey {
178
179
if ( ! / ^ [ m M ] ' ? / . test ( path ) ) {
0 commit comments