Skip to content

Commit 86c37b4

Browse files
author
AJ ONeal
committed
chore: cleanup serialize
1 parent 5c59caa commit 86c37b4

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

dashhd.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ var DashHd = ("object" === typeof module && exports) || {};
212212
let HARDENED_OFFSET = 0x80000000;
213213
let KEY_SIZE = 33;
214214
let INDEXED_KEY_SIZE = 4 + KEY_SIZE;
215-
let XKEY_SIZE = 78;
215+
let XKEY_SIZE = 74;
216216

217217
// Bitcoin hardcoded by default, can use package `coininfo` for others
218218
let BITCOIN_VERSIONS = { private: 0x0488ade4, public: 0x0488b21e };
@@ -247,19 +247,15 @@ var DashHd = ("object" === typeof module && exports) || {};
247247
key.set([0], 0);
248248
key.set(_privateKey, 1);
249249
//@ts-ignore - wth?
250-
return await Utils.encodeXPrv(
251-
serialize(hdkey, 0 /*hdkey.versions.private*/, key),
252-
);
250+
return await Utils.encodeXPrv(serialize(hdkey, key));
253251
};
254252

255253
hdkey.getPublicExtendedKey = async function () {
256254
if (!hdkey.publicKey) {
257255
throw new Error("Missing public key");
258256
}
259257

260-
return await Utils.encodeXPub(
261-
serialize(hdkey, 0 /*hdkey.versions.public*/, hdkey.publicKey),
262-
);
258+
return await Utils.encodeXPub(serialize(hdkey, hdkey.publicKey));
263259
};
264260

265261
hdkey.derive = async function (path) {
@@ -475,27 +471,21 @@ var DashHd = ("object" === typeof module && exports) || {};
475471

476472
/**
477473
* @param {HDKey} hdkey - TODO attach to hdkey
478-
* @param {Number} version
479474
* @param {Uint8Array} key
480475
*/
481-
function serialize(hdkey, version, key) {
476+
function serialize(hdkey, key) {
482477
// => version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33)
483-
let myOffset = -4;
484-
let xkey = new Uint8Array(XKEY_SIZE + myOffset);
478+
let xkey = new Uint8Array(XKEY_SIZE);
485479
let xkeyDv = new DataView(xkey.buffer);
486480

487-
if (version) {
488-
myOffset += 4;
489-
xkeyDv.setUint32(myOffset, version, BUFFER_BE);
490-
}
491-
xkeyDv.setUint8(myOffset + 4, hdkey.depth);
481+
xkeyDv.setUint8(0, hdkey.depth);
492482

493483
let fingerprint = (hdkey.depth && hdkey.parentFingerprint) || 0x00000000;
494-
xkeyDv.setUint32(myOffset + 5, fingerprint, BUFFER_BE);
495-
xkeyDv.setUint32(myOffset + 9, hdkey.index, BUFFER_BE);
484+
xkeyDv.setUint32(1, fingerprint, BUFFER_BE);
485+
xkeyDv.setUint32(5, hdkey.index, BUFFER_BE);
496486

497-
xkey.set(hdkey.chainCode, myOffset + 13);
498-
xkey.set(key, myOffset + 45);
487+
xkey.set(hdkey.chainCode, 9);
488+
xkey.set(key, 41);
499489

500490
return xkey;
501491
}

0 commit comments

Comments
 (0)