Skip to content

Commit

Permalink
add unit test for Qi address derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoacosta74 committed Oct 29, 2024
1 parent b5a7c9c commit ed9b11f
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
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)}`,
);
}
}
});
}
});
Binary file added testcases/qi-address-derivation.json.gz
Binary file not shown.

0 comments on commit ed9b11f

Please sign in to comment.