Skip to content

Commit 5c59caa

Browse files
author
AJ ONeal
committed
ref!: simplify setPrivateKey()
1 parent a392d9b commit 5c59caa

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

dashhd.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,9 @@ var DashHd = ("object" === typeof module && exports) || {};
233233
hdkey.getPrivateKey = function () {
234234
return _privateKey;
235235
};
236-
hdkey.setPrivateKey = async function (privBytes) {
237-
assert(privBytes.length === 32, "Private key must be 32 bytes.");
238-
236+
hdkey.setPrivateKey = function (privBytes) {
239237
_privateKey = privBytes;
240-
hdkey.publicKey = await Utils.toPublicKey(_privateKey);
238+
return null;
241239
};
242240

243241
hdkey.getPrivateExtendedKey = async function () {
@@ -345,7 +343,8 @@ var DashHd = ("object" === typeof module && exports) || {};
345343

346344
if (_privateKey) {
347345
let nextPrivKey = await Utils.privateKeyTweakAdd(_privateKey, IL);
348-
await _hdkey.setPrivateKey(nextPrivKey);
346+
_hdkey.setPrivateKey(nextPrivKey);
347+
_hdkey.publicKey = await Utils.toPublicKey(nextPrivKey);
349348
} else {
350349
_hdkey.publicKey = await Utils.publicKeyTweakAdd(hdkey.publicKey, IL);
351350
}
@@ -402,6 +401,7 @@ var DashHd = ("object" === typeof module && exports) || {};
402401
let hdkey = DashHd.create(versions);
403402
hdkey.chainCode = IR;
404403
await hdkey.setPrivateKey(IL);
404+
hdkey.publicKey = await Utils.toPublicKey(IL);
405405

406406
return hdkey;
407407
};
@@ -442,7 +442,9 @@ var DashHd = ("object" === typeof module && exports) || {};
442442
version === versions.private,
443443
"Version mismatch: version does not match private",
444444
);
445-
await hdkey.setPrivateKey(key.subarray(1)); // cut off first 0x0 byte
445+
let privBytes = key.subarray(1); // cut off first 0x0 byte
446+
await hdkey.setPrivateKey(privBytes);
447+
hdkey.publicKey = await Utils.toPublicKey(privBytes);
446448
} else {
447449
assert(
448450
version === versions.public,

test/hdkey.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,21 @@ describe("hdkey", function () {
5252
}
5353
});
5454

55+
/*
5556
describe("- privateKey", function () {
5657
it("should throw an error if incorrect key size", async function () {
5758
var hdkey = DashHd.create();
5859
assert.rejects(async function () {
59-
await hdkey.setPrivateKey(Uint8Array.from([1, 2, 3, 4]));
60+
try {
61+
await hdkey.setPrivateKey(Uint8Array.from([1, 2, 3, 4]));
62+
} catch (e) {
63+
console.error(e);
64+
process.exit(1);
65+
}
6066
}, /key must be 32/);
6167
});
6268
});
69+
*/
6370

6471
/*
6572
// TODO adapt to XPub Key data

0 commit comments

Comments
 (0)