1
1
/**
2
2
* @typedef DashHD
3
3
* @prop {HDCreate } create
4
+ * @prop {HDFingerprint } fingerprint
4
5
* @prop {HDFromSeed } fromMasterSeed
5
6
* @prop {HDFromXKey } fromExtendedKey
6
7
* @prop {HDUtils } utils
28
29
* @typedef HDKey
29
30
* @prop {Uint8Array } chainCode - extra 32-bytes of shared entropy for xkeys
30
31
* @prop {Number } depth - of hd path - typically 0 is seed, 1-3 hardened, 4-5 are not
31
- * @prop {Uint8Array } identifier - same bytes as pubKeyHash, but used for id
32
32
* @prop {Number } index - the final segment of an HD Path, the index of the wif/addr
33
33
* @prop {Number } parentFingerprint - 32-bit int, slice of id, stored in child xkeys
34
34
* @prop {Uint8Array } publicKey
35
35
* @prop {HDVersions } versions - magic bytes for base58 prefix
36
36
* @prop {HDDerivePath } derive - derive a full hd path from the given root
37
37
* @prop {HDDeriveChild } deriveChild - get the next child xkey (in a path segment)
38
38
* @prop {HDDeriveChild } _deriveChild - helper
39
- * @prop {HDFingerprint } getFingerprint
40
39
* @prop {HDMaybeGetString } getPrivateExtendedKey
41
40
* @prop {HDMaybeGetBuffer } getPrivateKey
42
41
* @prop {HDGetString } getPublicExtendedKey
@@ -230,30 +229,9 @@ var DashHd = ("object" === typeof module && exports) || {};
230
229
hdkey . depth = 0 ;
231
230
hdkey . index = 0 ;
232
231
//hdkey.publicKey = null;
233
- //hdkey.identifier = null;
234
232
//hdkey.chainCode = null;
235
233
hdkey . parentFingerprint = 0 ;
236
234
237
- hdkey . getFingerprint = function ( ) {
238
- if ( ! hdkey . identifier ) {
239
- throw new Error ( "Public key has not been set" ) ;
240
- }
241
- let i32be = readUInt32BE ( hdkey . identifier , 0 ) ;
242
- return i32be ;
243
- } ;
244
-
245
- /**
246
- * @param {Uint8Array } u8 - a "web" JS buffer
247
- * @param {Number } offset - where to start reading
248
- * @returns {Number } - a 0-shifted (uint) JS Number
249
- */
250
- function readUInt32BE ( u8 , offset ) {
251
- let dv = new DataView ( u8 . buffer ) ;
252
- // will read offset + 4 bytes (32-bit uint)
253
- let n = dv . getUint32 ( offset , BUFFER_BE ) ;
254
- return n ;
255
- }
256
-
257
235
hdkey . getPrivateKey = function ( ) {
258
236
return _privateKey ;
259
237
} ;
@@ -262,7 +240,6 @@ var DashHd = ("object" === typeof module && exports) || {};
262
240
263
241
_privateKey = value ;
264
242
hdkey . publicKey = await Utils . toPublicKey ( value ) ;
265
- hdkey . identifier = await hash160 ( hdkey . publicKey ) ;
266
243
} ;
267
244
268
245
hdkey . setPublicKey = async function ( value ) {
@@ -279,7 +256,6 @@ var DashHd = ("object" === typeof module && exports) || {};
279
256
*/
280
257
hdkey . _setPublicKey = async function ( publicKey ) {
281
258
hdkey . publicKey = publicKey ;
282
- hdkey . identifier = await hash160 ( publicKey ) ;
283
259
_privateKey = null ;
284
260
} ;
285
261
@@ -382,7 +358,7 @@ var DashHd = ("object" === typeof module && exports) || {};
382
358
383
359
let _hdkey = DashHd . create ( hdkey . versions ) ;
384
360
_hdkey . depth = hdkey . depth + 1 ;
385
- _hdkey . parentFingerprint = hdkey . getFingerprint ( ) ;
361
+ _hdkey . parentFingerprint = await DashHd . fingerprint ( hdkey . publicKey ) ;
386
362
_hdkey . index = index ;
387
363
_hdkey . chainCode = IR ;
388
364
@@ -408,6 +384,36 @@ var DashHd = ("object" === typeof module && exports) || {};
408
384
return hdkey ;
409
385
} ;
410
386
387
+ /** @type {HDFingerprint } */
388
+ DashHd . fingerprint = async function ( pubBytes ) {
389
+ if ( ! pubBytes ) {
390
+ throw new Error ( "Public key has not been set" ) ;
391
+ }
392
+
393
+ /*
394
+ * Note: this *happens* to use the same algorithm
395
+ * as many toPkh() implementations but, semantically,
396
+ * this is NOT toPkh() - it has a different purpose.
397
+ * Furthermore, fingerprint() may change independently of toPkh().
398
+ */
399
+ let sha = await Utils . sha256sum ( pubBytes ) ;
400
+ let identifier = await Utils . ripemd160sum ( sha ) ;
401
+ let i32be = readUInt32BE ( identifier , 0 ) ;
402
+ return i32be ;
403
+ } ;
404
+
405
+ /**
406
+ * @param {Uint8Array } u8 - a "web" JS buffer
407
+ * @param {Number } offset - where to start reading
408
+ * @returns {Number } - a 0-shifted (uint) JS Number
409
+ */
410
+ function readUInt32BE ( u8 , offset ) {
411
+ let dv = new DataView ( u8 . buffer ) ;
412
+ // will read offset + 4 bytes (32-bit uint)
413
+ let n = dv . getUint32 ( offset , BUFFER_BE ) ;
414
+ return n ;
415
+ }
416
+
411
417
DashHd . fromMasterSeed = async function ( seedBuffer , versions ) {
412
418
let I = await Utils . sha512hmac ( MASTER_SECRET , seedBuffer ) ;
413
419
let IL = I . subarray ( 0 , 32 ) ;
@@ -509,15 +515,6 @@ var DashHd = ("object" === typeof module && exports) || {};
509
515
return xkey ;
510
516
}
511
517
512
- /**
513
- * @param {Uint8Array } buf
514
- * @returns {Promise<Uint8Array> }
515
- */
516
- async function hash160 ( buf ) {
517
- let sha = await Utils . sha256sum ( buf ) ;
518
- return await Utils . ripemd160sum ( sha ) ;
519
- }
520
-
521
518
DashHd . HARDENED_OFFSET = HARDENED_OFFSET ;
522
519
} ) ( ( "object" === typeof window && window ) || { } , DashHd ) ;
523
520
if ( "object" === typeof module ) {
@@ -549,6 +546,7 @@ if ("object" === typeof module) {
549
546
550
547
/**
551
548
* @callback HDFingerprint
549
+ * @param {Uint8Array } pubBytes - Public Key
552
550
* @returns {Number }
553
551
*/
554
552
0 commit comments