Skip to content

Commit 3732a6f

Browse files
OttoAllmendingerllm-git
andcommitted
refactor(abstract-utxo): consolidate and refactor unit tests
Consolidate test fixtures and refactor unit tests for better organization. Move address validation tests to their own file, reorganize BTC backup key fixtures and audit tests, and consolidate spoofing protection tests. Issue: BTC-2650 Co-authored-by: llm-git <llm-git@ttll.de>
1 parent 68861ee commit 3732a6f

8 files changed

Lines changed: 162 additions & 200 deletions

File tree

modules/abstract-utxo/test/unit/impl/btc/unit/fixtures/btcBackupKey.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

modules/abstract-utxo/test/unit/impl/btc/unit/fixtures/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

modules/abstract-utxo/test/unit/impl/ltc/unit/index.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

modules/abstract-utxo/test/unit/keychains.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as assert from 'assert';
22

33
import 'should';
4+
import { type TestBitGoAPI, TestBitGo } from '@bitgo/sdk-test';
5+
import { BitGoAPI } from '@bitgo/sdk-api';
6+
47
import { AbstractUtxoCoin } from '../../src';
8+
import { Tbtc } from '../../src/impl/btc';
59

610
import { utxoCoins } from './util';
711

@@ -16,3 +20,54 @@ function run(coin: AbstractUtxoCoin) {
1620
}
1721

1822
utxoCoins.forEach((c) => run(c));
23+
24+
describe('Audit Key', function () {
25+
// Encrypted backup key fixture for testing assertIsValidKey
26+
const btcBackupKey = {
27+
key:
28+
'{"iv":"JgqqE4W45/tKBSMSYqD+qg==","v":1,"iter":10000,"ks":256,"ts":64,"mode"' +
29+
':"ccm","adata":"","cipher":"aes","salt":"kiLPf8VSdI0=","ct":"zUh4Oko/06g02E' +
30+
'wnqOfzJbTwtE2p3b19jDk8Tum07Jv3N/RP7Bo0w/ObLBO1uIJFossO3nJ1JS+7t/vPQhdCtN8oD' +
31+
'6YrZnEZYrRwN6JQkL1uYPnZ1PoWbYI9navK5CLU1KQwDTO9YEN46++OrzFH+CjpQVLblaw="}',
32+
};
33+
34+
let bitgo: TestBitGoAPI;
35+
let coin: Tbtc;
36+
37+
before(function () {
38+
bitgo = TestBitGo.decorate(BitGoAPI, { env: 'test' });
39+
bitgo.safeRegister('tbtc', Tbtc.createInstance);
40+
bitgo.initializeTestVars();
41+
coin = bitgo.coin('tbtc') as Tbtc;
42+
});
43+
44+
it('should return for valid inputs', function () {
45+
coin.assertIsValidKey({
46+
encryptedPrv: btcBackupKey.key,
47+
walletPassphrase: 'kAm[EFQ6o=SxlcLFDw%,',
48+
});
49+
});
50+
51+
it('should throw error if the walletPassphrase is incorrect', function () {
52+
assert.throws(
53+
() =>
54+
coin.assertIsValidKey({
55+
encryptedPrv: btcBackupKey.key,
56+
walletPassphrase: 'foo',
57+
}),
58+
{ message: "failed to decrypt prv: ccm: tag doesn't match" }
59+
);
60+
});
61+
62+
it('should throw if the key is altered', function () {
63+
const alteredKey = btcBackupKey.key.replace(/[0-9]/g, '0');
64+
assert.throws(
65+
() =>
66+
coin.assertIsValidKey({
67+
encryptedPrv: alteredKey,
68+
walletPassphrase: 'kAm[EFQ6o=SxlcLFDw%,',
69+
}),
70+
{ message: 'failed to decrypt prv: json decrypt: invalid parameters' }
71+
);
72+
});
73+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'should';
2+
3+
import { type TestBitGoAPI, TestBitGo } from '@bitgo/sdk-test';
4+
import { BitGoAPI } from '@bitgo/sdk-api';
5+
import { fixedScriptWallet } from '@bitgo/wasm-utxo';
6+
import * as testutils from '@bitgo/wasm-utxo/testutils';
7+
8+
import { Tbtc } from '../../src/impl/btc';
9+
10+
import { constructPsbt } from './util';
11+
12+
const { BitGoPsbt } = fixedScriptWallet;
13+
14+
describe('Post Build Validation', function () {
15+
let bitgo: TestBitGoAPI;
16+
let coin: Tbtc;
17+
18+
before(function () {
19+
bitgo = TestBitGo.decorate(BitGoAPI, { env: 'test' });
20+
bitgo.safeRegister('tbtc', Tbtc.createInstance);
21+
bitgo.initializeTestVars();
22+
coin = bitgo.coin('tbtc') as Tbtc;
23+
});
24+
25+
it('should not modify locktime on postProcessPrebuild', async function () {
26+
const walletKeys = testutils.getDefaultWalletKeys();
27+
28+
// Create a PSBT with lockTime=0 and sequence=0xffffffff
29+
const psbt = constructPsbt(
30+
[{ scriptType: 'p2wsh' as const, value: BigInt(100000) }],
31+
[{ address: 'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7', value: BigInt(90000) }],
32+
'tbtc',
33+
walletKeys,
34+
{ lockTime: 0, sequence: 0xffffffff }
35+
);
36+
37+
const txHex = Buffer.from(psbt.serialize()).toString('hex');
38+
const blockHeight = 100;
39+
const preBuild = { txHex, blockHeight };
40+
const postProcessBuilt = await coin.postProcessPrebuild(preBuild);
41+
42+
// Parse result as PSBT
43+
const resultPsbt = BitGoPsbt.fromBytes(Buffer.from(postProcessBuilt.txHex as string, 'hex'), 'tbtc');
44+
45+
resultPsbt.lockTime.should.equal(0);
46+
47+
// Check sequences via parseTransactionWithWalletKeys
48+
const parsed = resultPsbt.parseTransactionWithWalletKeys(walletKeys, { publicKeys: [] });
49+
for (const input of parsed.inputs) {
50+
input.sequence.should.equal(0xffffffff);
51+
}
52+
});
53+
});

0 commit comments

Comments
 (0)