Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hdnodewallet test #216

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
});
}
});
*/
Loading
Loading