Skip to content

Commit 2a77353

Browse files
author
AJ ONeal
committed
ref: simplify private key serialization
1 parent 0c6ad81 commit 2a77353

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

dashhd.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,8 @@ var DashHd = ("object" === typeof module && exports) || {};
293293
return null;
294294
}
295295

296-
let key = new Uint8Array(KEY_SIZE);
297-
key.set([0], 0);
298-
key.set(hdkey.privateKey, 1);
299296
//@ts-ignore - wth?
300-
return await Utils.encodeXPrv(serialize(hdkey, key));
297+
return await Utils.encodeXPrv(serialize(hdkey, hdkey.privateKey));
301298
};
302299

303300
DashHd.toXPub = async function (hdkey) {
@@ -606,9 +603,9 @@ var DashHd = ("object" === typeof module && exports) || {};
606603

607604
/**
608605
* @param {HDKey} hdkey - TODO attach to hdkey
609-
* @param {Uint8Array} key
606+
* @param {Uint8Array} keyBytes
610607
*/
611-
function serialize(hdkey, key) {
608+
function serialize(hdkey, keyBytes) {
612609
// version(4) + depth(1) + fingerprint(4) + index(4) + chain(32) + key(33)
613610
let xkey = new Uint8Array(XKEY_SIZE);
614611
let xkeyDv = new DataView(xkey.buffer);
@@ -623,7 +620,14 @@ var DashHd = ("object" === typeof module && exports) || {};
623620
xkeyDv.setUint32(5, hdkey.index, BUFFER_BE);
624621

625622
xkey.set(hdkey.chainCode, 9);
626-
xkey.set(key, 41);
623+
624+
let keyStart = 41;
625+
let isPrivate = 32 === keyBytes.length;
626+
if (isPrivate) {
627+
xkey[keyStart] = 0x00;
628+
keyStart += 1;
629+
}
630+
xkey.set(keyBytes, keyStart);
627631

628632
return xkey;
629633
}

0 commit comments

Comments
 (0)