Skip to content

Commit abb677d

Browse files
author
AJ ONeal
committed
ref: simplify fingerprint / identifier
1 parent d54564d commit abb677d

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

dashhd.js

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @typedef DashHD
33
* @prop {HDCreate} create
4+
* @prop {HDFingerprint} fingerprint
45
* @prop {HDFromSeed} fromMasterSeed
56
* @prop {HDFromXKey} fromExtendedKey
67
* @prop {HDUtils} utils
@@ -28,15 +29,13 @@
2829
* @typedef HDKey
2930
* @prop {Uint8Array} chainCode - extra 32-bytes of shared entropy for xkeys
3031
* @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
3232
* @prop {Number} index - the final segment of an HD Path, the index of the wif/addr
3333
* @prop {Number} parentFingerprint - 32-bit int, slice of id, stored in child xkeys
3434
* @prop {Uint8Array} publicKey
3535
* @prop {HDVersions} versions - magic bytes for base58 prefix
3636
* @prop {HDDerivePath} derive - derive a full hd path from the given root
3737
* @prop {HDDeriveChild} deriveChild - get the next child xkey (in a path segment)
3838
* @prop {HDDeriveChild} _deriveChild - helper
39-
* @prop {HDFingerprint} getFingerprint
4039
* @prop {HDMaybeGetString} getPrivateExtendedKey
4140
* @prop {HDMaybeGetBuffer} getPrivateKey
4241
* @prop {HDGetString} getPublicExtendedKey
@@ -230,30 +229,9 @@ var DashHd = ("object" === typeof module && exports) || {};
230229
hdkey.depth = 0;
231230
hdkey.index = 0;
232231
//hdkey.publicKey = null;
233-
//hdkey.identifier = null;
234232
//hdkey.chainCode = null;
235233
hdkey.parentFingerprint = 0;
236234

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-
257235
hdkey.getPrivateKey = function () {
258236
return _privateKey;
259237
};
@@ -262,7 +240,6 @@ var DashHd = ("object" === typeof module && exports) || {};
262240

263241
_privateKey = value;
264242
hdkey.publicKey = await Utils.toPublicKey(value);
265-
hdkey.identifier = await hash160(hdkey.publicKey);
266243
};
267244

268245
hdkey.setPublicKey = async function (value) {
@@ -279,7 +256,6 @@ var DashHd = ("object" === typeof module && exports) || {};
279256
*/
280257
hdkey._setPublicKey = async function (publicKey) {
281258
hdkey.publicKey = publicKey;
282-
hdkey.identifier = await hash160(publicKey);
283259
_privateKey = null;
284260
};
285261

@@ -382,7 +358,7 @@ var DashHd = ("object" === typeof module && exports) || {};
382358

383359
let _hdkey = DashHd.create(hdkey.versions);
384360
_hdkey.depth = hdkey.depth + 1;
385-
_hdkey.parentFingerprint = hdkey.getFingerprint();
361+
_hdkey.parentFingerprint = await DashHd.fingerprint(hdkey.publicKey);
386362
_hdkey.index = index;
387363
_hdkey.chainCode = IR;
388364

@@ -408,6 +384,36 @@ var DashHd = ("object" === typeof module && exports) || {};
408384
return hdkey;
409385
};
410386

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+
411417
DashHd.fromMasterSeed = async function (seedBuffer, versions) {
412418
let I = await Utils.sha512hmac(MASTER_SECRET, seedBuffer);
413419
let IL = I.subarray(0, 32);
@@ -509,15 +515,6 @@ var DashHd = ("object" === typeof module && exports) || {};
509515
return xkey;
510516
}
511517

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-
521518
DashHd.HARDENED_OFFSET = HARDENED_OFFSET;
522519
})(("object" === typeof window && window) || {}, DashHd);
523520
if ("object" === typeof module) {
@@ -549,6 +546,7 @@ if ("object" === typeof module) {
549546

550547
/**
551548
* @callback HDFingerprint
549+
* @param {Uint8Array} pubBytes - Public Key
552550
* @returns {Number}
553551
*/
554552

test/hdkey.test.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ describe("hdkey", function () {
114114
u8ToHex(hdkey.publicKey),
115115
"024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c",
116116
);
117+
let print = await DashHd.fingerprint(hdkey.publicKey);
117118
assert.equal(
118-
u8ToHex(hdkey.identifier),
119-
"26132fdbe7bf89cbc64cf8dafa3f9f88b8666220",
119+
print.toString(16),
120+
"26132fdbe7bf89cbc64cf8dafa3f9f88b8666220".slice(0, 8),
120121
);
121122
});
122123
});
@@ -141,9 +142,10 @@ describe("hdkey", function () {
141142
u8ToHex(hdkey.publicKey),
142143
"024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c",
143144
);
145+
let print = await DashHd.fingerprint(hdkey.publicKey);
144146
assert.equal(
145-
u8ToHex(hdkey.identifier),
146-
"26132fdbe7bf89cbc64cf8dafa3f9f88b8666220",
147+
print.toString(16),
148+
"26132fdbe7bf89cbc64cf8dafa3f9f88b8666220".slice(0, 8),
147149
);
148150
});
149151

@@ -166,9 +168,10 @@ describe("hdkey", function () {
166168
u8ToHex(hdkey.publicKey),
167169
"024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c",
168170
);
171+
let print = await DashHd.fingerprint(hdkey.publicKey);
169172
assert.equal(
170-
u8ToHex(hdkey.identifier),
171-
"26132fdbe7bf89cbc64cf8dafa3f9f88b8666220",
173+
print.toString(16),
174+
"26132fdbe7bf89cbc64cf8dafa3f9f88b8666220".slice(0, 8),
172175
);
173176
});
174177
});
@@ -334,9 +337,10 @@ describe("hdkey", function () {
334337
u8ToHex(hdkey.publicKey),
335338
"024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c",
336339
);
340+
let print = await DashHd.fingerprint(hdkey.publicKey);
337341
assert.equal(
338-
u8ToHex(hdkey.identifier),
339-
"26132fdbe7bf89cbc64cf8dafa3f9f88b8666220",
342+
print.toString(16),
343+
"26132fdbe7bf89cbc64cf8dafa3f9f88b8666220".slice(0, 8),
340344
);
341345
});
342346

0 commit comments

Comments
 (0)