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

Add unit tests for qi address derivation #337

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
113 changes: 113 additions & 0 deletions src/_tests/unit/qihdwallet-address-derivation.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import assert from 'assert';
import { loadTests } from '../utils.js';
import { Mnemonic, QiHDWallet, Zone, QiAddressInfo } from '../../index.js';

interface TestCaseQiAddressDerivation {
mnemonic: string;
externalAddresses: Array<{
zone: string;
addresses: Array<QiAddressInfo>;
}>;
changeAddresses: Array<{
zone: string;
addresses: Array<QiAddressInfo>;
}>;
paymentCodeAddresses: {
bobMnemonic: string;
sendAddresses: Array<{
zone: string;
addresses: Array<QiAddressInfo>;
}>;
receiveAddresses: Array<{
zone: string;
addresses: Array<QiAddressInfo>;
}>;
};
}

describe('QiHDWallet Address Derivation', function () {
this.timeout(2 * 60 * 1000);
const tests = loadTests<TestCaseQiAddressDerivation>('qi-address-derivation');

for (const test of tests) {
it('derives external addresses correctly', function () {
const mnemonic = Mnemonic.fromPhrase(test.mnemonic);
const qiWallet = QiHDWallet.fromMnemonic(mnemonic);

for (const externalAddressesInfo of test.externalAddresses) {
const zone = externalAddressesInfo.zone as Zone;
for (const expectedAddressInfo of externalAddressesInfo.addresses) {
const derivedAddressInfo = qiWallet.getNextAddressSync(0, zone);
assert.deepEqual(
derivedAddressInfo,
expectedAddressInfo,
`External address mismatch for zone ${zone}, expected: ${JSON.stringify(expectedAddressInfo)}, derived: ${JSON.stringify(derivedAddressInfo)}`,
);
}
}
});

it('derives change addresses correctly', function () {
const mnemonic = Mnemonic.fromPhrase(test.mnemonic);
const qiWallet = QiHDWallet.fromMnemonic(mnemonic);

for (const changeAddressesInfo of test.changeAddresses) {
const zone = changeAddressesInfo.zone as Zone;
for (const expectedAddressInfo of changeAddressesInfo.addresses) {
const derivedAddressInfo = qiWallet.getNextChangeAddressSync(0, zone);
assert.deepEqual(
derivedAddressInfo,
expectedAddressInfo,
`Change address mismatch for zone ${zone}, expected: ${JSON.stringify(expectedAddressInfo)}, derived: ${JSON.stringify(derivedAddressInfo)}`,
);
}
}
});

it('derives payment code send addresses correctly', function () {
const mnemonic = Mnemonic.fromPhrase(test.mnemonic);
const qiWallet = QiHDWallet.fromMnemonic(mnemonic);

const bobMnemonic = Mnemonic.fromPhrase(test.paymentCodeAddresses.bobMnemonic);
const bobQiWallet = QiHDWallet.fromMnemonic(bobMnemonic);
const bobPaymentCode = bobQiWallet.getPaymentCode(0);

qiWallet.openChannel(bobPaymentCode);

for (const sendAddressesInfo of test.paymentCodeAddresses.sendAddresses) {
const zone = sendAddressesInfo.zone as Zone;
for (const expectedAddressInfo of sendAddressesInfo.addresses) {
const derivedAddressInfo = qiWallet.getNextSendAddress(bobPaymentCode, zone);
assert.deepEqual(
derivedAddressInfo,
expectedAddressInfo,
`Payment code send address mismatch, expected: ${JSON.stringify(expectedAddressInfo)}, derived: ${JSON.stringify(derivedAddressInfo)}`,
);
}
}
});

it('derives payment code receive addresses correctly', function () {
const mnemonic = Mnemonic.fromPhrase(test.mnemonic);
const qiWallet = QiHDWallet.fromMnemonic(mnemonic);

const bobMnemonic = Mnemonic.fromPhrase(test.paymentCodeAddresses.bobMnemonic);
const bobQiWallet = QiHDWallet.fromMnemonic(bobMnemonic);
const bobPaymentCode = bobQiWallet.getPaymentCode(0);

qiWallet.openChannel(bobPaymentCode);

for (const receiveAddressesInfo of test.paymentCodeAddresses.receiveAddresses) {
const zone = receiveAddressesInfo.zone as Zone;
for (const expectedAddressInfo of receiveAddressesInfo.addresses) {
const derivedAddressInfo = qiWallet.getNextReceiveAddress(bobPaymentCode, zone);
assert.deepEqual(
derivedAddressInfo,
expectedAddressInfo,
`Payment code receive address mismatch, expected: ${JSON.stringify(expectedAddressInfo)}, derived: ${JSON.stringify(derivedAddressInfo)}`,
);
}
}
});
}
});
2 changes: 2 additions & 0 deletions src/quais.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ export {
encryptKeystoreJsonSync,
SerializedHDWallet,
SerializedQiHDWallet,
QiAddressInfo,
NeuteredAddressInfo,
} from './wallet/index.js';

// WORDLIST
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

export { BaseWallet } from './base-wallet.js';

export type { SerializedHDWallet } from './hdwallet.js';
export type { SerializedHDWallet, NeuteredAddressInfo } from './hdwallet.js';

export { QuaiHDWallet } from './quai-hdwallet.js';

Expand All @@ -31,6 +31,6 @@ export { Wallet } from './wallet.js';

export type { KeystoreAccount, EncryptOptions } from './json-keystore.js';

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

export { HDNodeVoidWallet, HDNodeWallet } from './hdnodewallet.js';
2 changes: 1 addition & 1 deletion src/wallet/qi-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type DerivationPath = 'BIP44:external' | 'BIP44:change' | string; // string for
*
* @extends NeuteredAddressInfo
*/
interface QiAddressInfo extends NeuteredAddressInfo {
export interface QiAddressInfo extends NeuteredAddressInfo {
status: AddressStatus;
derivationPath: DerivationPath;
}
Expand Down
Binary file added testcases/qi-address-derivation.json.gz
Binary file not shown.
Loading