Skip to content

Commit

Permalink
fix unit tests for HDNodeWallet
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoacosta74 committed Jun 24, 2024
1 parent 0b2f8cb commit d4b91b3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 67 deletions.
130 changes: 64 additions & 66 deletions src/_tests/test-wallet-hd.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
//! TODO: Fix this test
/*
import assert from 'assert';

import { loadTests } from './utils.js';

import { HDNodeWallet, HDNodeVoidWallet } from '../wallet/index.js';

import {
getBytes, wordlists,
QuaiHDWallet, Mnemonic,
QiHDWallet,
} from "../index.js";
import { getBytes, wordlists, Mnemonic } from '../quais.js';

import type { Wordlist } from "../wordlists/index.js";
import type { TestCaseMnemonic, TestCaseMnemonicNode } from "./types.js";
// import type { HDWalletStatic } from "../wallet/hdwallet.js";
import type { Wordlist } from '../wordlists/index.js';

import type { TestCaseMnemonic, TestCaseMnemonicNode } from './types.js';

const decoder = new TextDecoder();
function fromHex(hex: string): string {
Expand All @@ -33,21 +25,21 @@ type Test = {
test: TestCaseMnemonic;
};

describe("Test HDWallets", function() {
function checkWallet(wallet: QuaiHDWallet | QiHDWallet, test: TestCaseMnemonicNode): void {
assert.equal(wallet.chainCode, test.chainCode, "chainCode");
assert.equal(wallet.depth, test.depth, "depth");
assert.equal(wallet.index, test.index, "index");
assert.equal(wallet.fingerprint, test.fingerprint, "fingerprint");
assert.equal(wallet.accountFingerprint, test.parentFingerprint, "parentFingerprint");
assert.equal(wallet.publicKey, test.publicKey, "publicKey");
if (wallet instanceof QuaiHDWallet) {
assert.equal(wallet.extendedKey, test.xpriv, "xpriv");
assert.equal(wallet.privateKey, test.privateKey, "privateKey");
assert.equal(wallet.neuter().extendedKey, test.xpub, "xpub");
} else if (wallet instanceof QiHDWallet) {
assert.equal(wallet.extendedKey, test.xpub, "xpub");
describe('Test HDWallets', function () {
function checkWallet(wallet: HDNodeWallet | HDNodeVoidWallet, test: TestCaseMnemonicNode): void {
assert.equal(wallet.chainCode, test.chainCode, 'chainCode');
assert.equal(wallet.depth, test.depth, 'depth');
assert.equal(wallet.index, test.index, 'index');
assert.equal(wallet.fingerprint, test.fingerprint, 'fingerprint');
assert.equal(wallet.parentFingerprint, test.parentFingerprint, 'parentFingerprint');
assert.equal(wallet.publicKey, test.publicKey, 'publicKey');

if (wallet instanceof HDNodeWallet) {
assert.equal(wallet.extendedKey, test.xpriv, 'xpriv');
assert.equal(wallet.privateKey, test.privateKey, 'privateKey');
assert.equal(wallet.neuter().extendedKey, test.xpub, 'xpub');
} else if (wallet instanceof HDNodeVoidWallet) {
assert.equal(wallet.extendedKey, test.xpub, 'xpub');
}
}

Expand Down Expand Up @@ -90,10 +82,9 @@ describe("Test HDWallets", function() {
for (const { test, checkMnemonic, phrase, password, wordlist } of checks) {
it(`computes the HD keys by mnemonic: ${test.name}`, function () {
for (const subtest of test.nodes) {
const WalletClass = QuaiHDWallet as typeof QuaiHDWallet & HDWalletStatic<QuaiHDWallet>;
const w = WalletClass.fromPhrase(phrase, password, subtest.path, wordlist);
assert.ok(w instanceof QuaiHDWallet, "instanceof QuaiHDWallet");
assert.equal(w.path, subtest.path, "path")
const w = HDNodeWallet.fromPhrase(phrase, subtest.path, password, wordlist);
assert.ok(w instanceof HDNodeWallet, 'instanceof HDNodeWallet');
assert.equal(w.path, subtest.path, 'path');
checkWallet(w, subtest);
assert.ok(!!w.mnemonic, 'has mnemonic');
checkMnemonic(w.mnemonic as Mnemonic);
Expand All @@ -102,12 +93,11 @@ describe("Test HDWallets", function() {
}

for (const { test } of checks) {
it(`computes the HD keys by entropy: ${ test.name }`, function() {
const WalletClass = QuaiHDWallet as typeof QuaiHDWallet & HDWalletStatic<QuaiHDWallet>;
const seedRoot = WalletClass.fromSeed(test.seed);
it(`computes the HD keys by entropy: ${test.name}`, function () {
const seedRoot = HDNodeWallet.fromSeed(test.seed);
for (const subtest of test.nodes) {
const w = seedRoot.derivePath(subtest.path);
assert.ok(w instanceof QuaiHDWallet, 'instanceof QuaiHDWallet');
assert.ok(w instanceof HDNodeWallet, 'instanceof HDNodeWallet');
assert.equal(w.path, subtest.path, 'path');
checkWallet(w, subtest);
assert.equal(w.mnemonic, null);
Expand All @@ -118,41 +108,49 @@ describe("Test HDWallets", function() {
for (const { test } of checks) {
it(`computes the HD keys by enxtended private key: ${test.name}`, function () {
for (const subtest of test.nodes) {
const w = QuaiHDWallet.fromExtendedKey(subtest.xpriv);
assert.ok(w instanceof QuaiHDWallet, 'instanceof QuaiHDWallet');
const w = HDNodeWallet.fromExtendedKey(subtest.xpriv);
assert.ok(w instanceof HDNodeWallet, 'instanceof HDNodeWallet');
checkWallet(w, subtest);
assert.equal(w.mnemonic, null);
}
});
}

// ! TODO: Fix this test
// for (const { test, phrase, password, wordlist } of checks) {
// it(`computes the neutered HD keys by paths: ${ test.name }`, function() {
// const root = QuaiHDWallet.fromPhrase(phrase, "m", password, wordlist).neuter();
// for (const subtest of test.nodes) {
// if (subtest.path.indexOf("'") >= 0) {
// assert.throws(
// () => {
// const w = root.derivePath(subtest.path);
// console.log(w);
// },
// (error: any) => {
// return (
// error.code === 'UNSUPPORTED_OPERATION' &&
// error.message.match(/^cannot derive child of neutered node/) &&
// error.operation === 'deriveChild'
// );
// },
// );
// } else {
// const w = root.derivePath(subtest.path);
// assert.ok(w instanceof HDNodeVoidWallet, 'instanceof HDNodeVoidWallet');
// assert.equal(w.path, subtest.path, 'path');
// checkWallet(w, subtest);
// }
// }
// });
// }
for (const { test, phrase, password, wordlist } of checks) {
it(`computes the neutered HD keys by paths: ${test.name}`, function () {
const root = HDNodeWallet.fromPhrase(phrase, 'm', password, wordlist).neuter();
for (const subtest of test.nodes) {
if (subtest.path.indexOf("'") >= 0) {
assert.throws(
() => {
const w = root.derivePath(subtest.path);
console.log(w);
},
(error: any) => {
return (
error.code === 'UNSUPPORTED_OPERATION' &&
error.message.match(/^cannot derive child of neutered node/) &&
error.operation === 'deriveChild'
);
},
);
} else {
const w = root.derivePath(subtest.path);
assert.ok(w instanceof HDNodeVoidWallet, 'instanceof HDNodeVoidWallet');
assert.equal(w.path, subtest.path, 'path');
checkWallet(w, subtest);
}
}
});
}

for (const { test } of checks) {
it(`computes the neutered HD keys by enxtended public key: ${test.name}`, function () {
for (const subtest of test.nodes) {
const w = HDNodeWallet.fromExtendedKey(subtest.xpub);
assert.ok(w instanceof HDNodeVoidWallet, 'instanceof HDNodeVoidWallet');
checkWallet(w, subtest);
}
});
}
});
*/
2 changes: 1 addition & 1 deletion src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export type { KeystoreAccount, EncryptOptions } from './json-keystore.js';

export { QiHDWallet } from './qi-hdwallet.js';

export { HDNodeVoidWallet } from "./hdnodewallet.js";
export { HDNodeVoidWallet, HDNodeWallet } from './hdnodewallet.js';

0 comments on commit d4b91b3

Please sign in to comment.