Skip to content

Commit d6969dc

Browse files
author
AJ ONeal
committed
fix(test): update all tests with new APIs
1 parent 506bf92 commit d6969dc

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

test/hdkey.test.js

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ describe("hdkey", function () {
1616
var hdkey = HDKey.fromMasterSeed(Buffer.from(f.seed, "hex"));
1717
var childkey = hdkey.derive(f.path);
1818

19-
assert.equal(childkey.privateExtendedKey, f.private);
20-
assert.equal(childkey.publicExtendedKey, f.public);
19+
assert.equal(childkey.getPrivateExtendedKey(), f.private);
20+
assert.equal(childkey.getPublicExtendedKey(), f.public);
2121
});
2222

2323
describe("> " + f.path + " toJSON() / fromJSON()", function () {
@@ -33,44 +33,44 @@ describe("hdkey", function () {
3333
assert.deepEqual(childkey.toJSON(), obj);
3434

3535
var newKey = HDKey.fromJSON(obj);
36-
assert.strictEqual(newKey.privateExtendedKey, f.private);
37-
assert.strictEqual(newKey.publicExtendedKey, f.public);
36+
assert.strictEqual(newKey.getPrivateExtendedKey(), f.private);
37+
assert.strictEqual(newKey.getPublicExtendedKey(), f.public);
3838
});
3939
});
4040
});
4141
});
4242

4343
describe("- privateKey", function () {
4444
it("should throw an error if incorrect key size", function () {
45-
var hdkey = new HDKey();
45+
var hdkey = HDKey.create();
4646
assert.throws(function () {
47-
hdkey.privateKey = Buffer.from([1, 2, 3, 4]);
47+
hdkey.setPrivateKey(Buffer.from([1, 2, 3, 4]));
4848
}, /key must be 32/);
4949
});
5050
});
5151

5252
describe("- publicKey", function () {
5353
it("should throw an error if incorrect key size", function () {
5454
assert.throws(function () {
55-
var hdkey = new HDKey();
56-
hdkey.publicKey = Buffer.from([1, 2, 3, 4]);
55+
var hdkey = HDKey.create();
56+
hdkey.setPublicKey(Buffer.from([1, 2, 3, 4]));
5757
}, /key must be 33 or 65/);
5858
});
5959

6060
it("should not throw if key is 33 bytes (compressed)", function () {
6161
var priv = secureRandom.randomBuffer(32);
6262
var pub = curve.G.multiply(BigInteger.fromBuffer(priv)).getEncoded(true);
6363
assert.equal(pub.length, 33);
64-
var hdkey = new HDKey();
65-
hdkey.publicKey = pub;
64+
var hdkey = HDKey.create();
65+
hdkey.setPublicKey(pub);
6666
});
6767

6868
it("should not throw if key is 65 bytes (not compressed)", function () {
6969
var priv = secureRandom.randomBuffer(32);
7070
var pub = curve.G.multiply(BigInteger.fromBuffer(priv)).getEncoded(false);
7171
assert.equal(pub.length, 65);
72-
var hdkey = new HDKey();
73-
hdkey.publicKey = pub;
72+
var hdkey = HDKey.create();
73+
hdkey.setPublicKey(pub);
7474
});
7575
});
7676

@@ -91,7 +91,7 @@ describe("hdkey", function () {
9191
"9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271",
9292
);
9393
assert.equal(
94-
hdkey.privateKey.toString("hex"),
94+
hdkey.getPrivateKey().toString("hex"),
9595
"bb7d39bdb83ecf58f2fd82b6d918341cbef428661ef01ab97c28a4842125ac23",
9696
);
9797
assert.equal(
@@ -120,7 +120,7 @@ describe("hdkey", function () {
120120
hdkey.chainCode.toString("hex"),
121121
"9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271",
122122
);
123-
assert.equal(hdkey.privateKey, null);
123+
assert.equal(hdkey.getPrivateKey(), null);
124124
assert.equal(
125125
hdkey.publicKey.toString("hex"),
126126
"024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c",
@@ -145,7 +145,7 @@ describe("hdkey", function () {
145145
hdkey.chainCode.toString("hex"),
146146
"9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271",
147147
);
148-
assert.equal(hdkey.privateKey, null);
148+
assert.equal(hdkey.getPrivateKey(), null);
149149
assert.equal(
150150
hdkey.publicKey.toString("hex"),
151151
"024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c",
@@ -202,7 +202,7 @@ describe("hdkey", function () {
202202

203203
var expected =
204204
"xpub6JdKdVJtdx6sC3nh87pDvnGhotXuU5Kz6Qy7Piy84vUAwWSYShsUGULE8u6gCivTHgz7cCKJHiXaaMeieB4YnoFVAsNgHHKXJ2mN6jCMbH1";
205-
assert.equal(derivedHDKey.publicExtendedKey, expected);
205+
assert.equal(derivedHDKey.getPublicExtendedKey(), expected);
206206
});
207207
});
208208

@@ -214,7 +214,7 @@ describe("hdkey", function () {
214214
var newKey = masterKey.derive("m/44'/6'/4'");
215215
var expected =
216216
"xprv9ymoag6W7cR6KBcJzhCM6qqTrb3rRVVwXKzwNqp1tDWcwierEv3BA9if3ARHMhMPh9u2jNoutcgpUBLMfq3kADDo7LzfoCnhhXMRGX3PXDx";
217-
assert.equal(newKey.privateExtendedKey, expected);
217+
assert.equal(newKey.getPrivateExtendedKey(), expected);
218218
});
219219
});
220220

@@ -230,12 +230,12 @@ describe("hdkey", function () {
230230
"xprv9s21ZrQH143K3ckY9DgU79uMTJkQRLdbCCVDh81SnxTgPzLLGax6uHeBULTtaEtcAvKjXfT7ZWtHzKjTpujMkUd9dDb8msDeAfnJxrgAYhr";
231231
var hdkey = HDKey.fromExtendedKey(key);
232232
assert.equal(
233-
hdkey.privateKey.toString("hex"),
233+
hdkey.getPrivateKey().toString("hex"),
234234
"00000055378cf5fafb56c711c674143f9b0ee82ab0ba2924f19b64f5ae7cdbfd",
235235
);
236236
var derived = hdkey.derive("m/44'/0'/0'/0/0'");
237237
assert.equal(
238-
derived.privateKey.toString("hex"),
238+
derived.getPrivateKey().toString("hex"),
239239
"3348069561d2a0fb925e74bf198762acc47dce7db27372257d2d959a9e6f8aeb",
240240
);
241241
});
@@ -246,14 +246,14 @@ describe("hdkey", function () {
246246
var seed = "000102030405060708090a0b0c0d0e0f";
247247
var masterKey = HDKey.fromMasterSeed(Buffer.from(seed, "hex"));
248248

249-
assert.ok(masterKey.privateExtendedKey, "xpriv is truthy");
250-
masterKey._privateKey = null;
249+
assert.ok(masterKey.getPrivateExtendedKey(), "xpriv is truthy");
250+
masterKey.wipePrivateData();
251251

252252
assert.doesNotThrow(function () {
253-
masterKey.privateExtendedKey;
253+
masterKey.getPrivateExtendedKey();
254254
});
255255

256-
assert.ok(!masterKey.privateExtendedKey, "xpriv is falsy");
256+
assert.ok(!masterKey.getPrivateExtendedKey(), "xpriv is falsy");
257257
});
258258
});
259259

@@ -273,7 +273,8 @@ describe("hdkey", function () {
273273
describe(" - when the path given to derive does not begin with master extended key", function () {
274274
it("should throw an error", function () {
275275
assert.throws(function () {
276-
HDKey.prototype.derive("123");
276+
const hdkey = HDKey.create();
277+
hdkey.derive("123");
277278
}, /Path must start with "m" or "M"/);
278279
});
279280
});
@@ -283,16 +284,16 @@ describe("hdkey", function () {
283284
const hdkey = HDKey.fromMasterSeed(
284285
Buffer.from(fixtures.valid[6].seed, "hex"),
285286
).wipePrivateData();
286-
assert.equal(hdkey.privateKey, null);
287-
assert.equal(hdkey.privateExtendedKey, null);
287+
assert.equal(hdkey.getPrivateKey(), null);
288+
assert.equal(hdkey.getPrivateExtendedKey(), null);
288289
assert.throws(
289290
() => hdkey.sign(Buffer.alloc(32)),
290291
"shouldn't be able to sign",
291292
);
292293
const childKey = hdkey.derive("m/0");
293-
assert.equal(childKey.publicExtendedKey, fixtures.valid[7].public);
294-
assert.equal(childKey.privateKey, null);
295-
assert.equal(childKey.privateExtendedKey, null);
294+
assert.equal(childKey.getPublicExtendedKey(), fixtures.valid[7].public);
295+
assert.equal(childKey.getPrivateKey(), null);
296+
assert.equal(childKey.getPrivateExtendedKey(), null);
296297
});
297298

298299
it("should have correct data", function () {
@@ -331,32 +332,32 @@ describe("hdkey", function () {
331332
it("should not throw if called on hdkey without private data", function () {
332333
const hdkey = HDKey.fromExtendedKey(fixtures.valid[0].public);
333334
assert.doesNotThrow(() => hdkey.wipePrivateData());
334-
assert.equal(hdkey.publicExtendedKey, fixtures.valid[0].public);
335+
assert.equal(hdkey.getPublicExtendedKey(), fixtures.valid[0].public);
335336
});
336337
});
337338

338339
describe("Deriving a child key does not mutate the internal state", function () {
339340
it("should not mutate it when deriving with a private key", function () {
340341
const hdkey = HDKey.fromExtendedKey(fixtures.valid[0].private);
341342
const path = "m/123";
342-
const privateKeyBefore = hdkey.privateKey.toString("hex");
343+
const privateKeyBefore = hdkey.getPrivateKey().toString("hex");
343344

344345
const child = hdkey.derive(path);
345-
assert.equal(hdkey.privateKey.toString("hex"), privateKeyBefore);
346+
assert.equal(hdkey.getPrivateKey().toString("hex"), privateKeyBefore);
346347

347348
const child2 = hdkey.derive(path);
348-
assert.equal(hdkey.privateKey.toString("hex"), privateKeyBefore);
349+
assert.equal(hdkey.getPrivateKey().toString("hex"), privateKeyBefore);
349350

350351
const child3 = hdkey.derive(path);
351-
assert.equal(hdkey.privateKey.toString("hex"), privateKeyBefore);
352+
assert.equal(hdkey.getPrivateKey().toString("hex"), privateKeyBefore);
352353

353354
assert.equal(
354-
child.privateKey.toString("hex"),
355-
child2.privateKey.toString("hex"),
355+
child.getPrivateKey().toString("hex"),
356+
child2.getPrivateKey().toString("hex"),
356357
);
357358
assert.equal(
358-
child2.privateKey.toString("hex"),
359-
child3.privateKey.toString("hex"),
359+
child2.getPrivateKey().toString("hex"),
360+
child3.getPrivateKey().toString("hex"),
360361
);
361362
});
362363

0 commit comments

Comments
 (0)