Skip to content

Commit 5d1dae5

Browse files
OttoAllmendingerllm-git
andcommitted
feat(utxo-lib): split sign verify tests for PSBTs and legacy transactions
Split the combined test file into two separate files - one for PSBT operations and another for legacy transaction signing and verification. Issue: BTC-2732 Co-authored-by: llm-git <[email protected]>
1 parent 64aabd4 commit 5d1dae5

File tree

2 files changed

+57
-38
lines changed

2 files changed

+57
-38
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as assert from 'assert';
2+
3+
import { getStrictSignatureCount, getStrictSignatureCounts } from '../../../src/bitgo';
4+
import { constructTxnBuilder, TxnInput } from '../../../src/testutil';
5+
import { AcidTest, SignStage } from '../../../src/testutil/psbt';
6+
import { getNetworkList, getNetworkName, isMainnet, networks } from '../../../src';
7+
8+
const signs = ['unsigned', 'halfsigned', 'fullsigned'] as const;
9+
10+
function signCount(signStage: SignStage) {
11+
return signStage === 'unsigned' ? 0 : signStage === 'halfsigned' ? 1 : 2;
12+
}
13+
14+
function runTx(acidTest: AcidTest) {
15+
const coin = getNetworkName(acidTest.network);
16+
const signatureCount = signCount(acidTest.signStage);
17+
describe(`tx build, sign and verify for ${coin} ${acidTest.signStage}`, function () {
18+
const inputs = acidTest.inputs.filter(
19+
(input): input is TxnInput<bigint> =>
20+
input.scriptType !== 'taprootKeyPathSpend' && input.scriptType !== 'p2trMusig2'
21+
);
22+
const outputs = acidTest.outputs.filter(
23+
(output) =>
24+
('scriptType' in output && output.scriptType !== undefined) ||
25+
('address' in output && output.address !== undefined)
26+
);
27+
it(`tx signature counts ${coin} ${acidTest.signStage}`, function () {
28+
const txb = constructTxnBuilder(inputs, outputs, acidTest.network, acidTest.rootWalletKeys, acidTest.signStage);
29+
const tx = acidTest.signStage === 'fullsigned' ? txb.build() : txb.buildIncomplete();
30+
31+
const counts = getStrictSignatureCounts(tx);
32+
const countsFromIns = getStrictSignatureCounts(tx.ins);
33+
34+
assert.strictEqual(counts.length, tx.ins.length);
35+
assert.strictEqual(countsFromIns.length, tx.ins.length);
36+
tx.ins.forEach((input, inputIndex) => {
37+
const expectedCount = inputs[inputIndex].scriptType === 'p2shP2pk' && signatureCount > 0 ? 1 : signatureCount;
38+
assert.strictEqual(
39+
getStrictSignatureCount(input),
40+
expectedCount,
41+
`input ${inputIndex} has ${getStrictSignatureCount(input)} signatures, expected ${expectedCount}`
42+
);
43+
assert.strictEqual(counts[inputIndex], expectedCount);
44+
assert.strictEqual(countsFromIns[inputIndex], expectedCount);
45+
});
46+
});
47+
});
48+
}
49+
50+
signs.forEach((sign) => {
51+
getNetworkList()
52+
.filter((v) => isMainnet(v) && v !== networks.bitcoinsv)
53+
.forEach((network) => {
54+
runTx(AcidTest.withDefaults(network, sign, 'psbt'));
55+
});
56+
});

modules/utxo-lib/test/bitgo/psbt/SignVerifyPsbtAndTx.ts renamed to modules/utxo-lib/test/bitgo/psbt/SignVerifyPsbt.ts

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
UtxoPsbt,
1313
UtxoTransaction,
1414
} from '../../../src/bitgo';
15-
import { constructTxnBuilder, Input as TestUtilInput, TxnInput } from '../../../src/testutil';
15+
import { Input as TestUtilInput } from '../../../src/testutil';
1616
import { AcidTest, InputScriptType, SignStage } from '../../../src/testutil/psbt';
1717
import { getNetworkList, getNetworkName, isMainnet, networks } from '../../../src';
1818
import {
@@ -186,48 +186,11 @@ function runPsbt(acidTest: AcidTest) {
186186
});
187187
}
188188

189-
function runTx(acidTest: AcidTest) {
190-
const coin = getNetworkName(acidTest.network);
191-
const signatureCount = signCount(acidTest.signStage);
192-
describe(`tx build, sign and verify for ${coin} ${acidTest.signStage}`, function () {
193-
const inputs = acidTest.inputs.filter(
194-
(input): input is TxnInput<bigint> =>
195-
input.scriptType !== 'taprootKeyPathSpend' && input.scriptType !== 'p2trMusig2'
196-
);
197-
const outputs = acidTest.outputs.filter(
198-
(output) =>
199-
('scriptType' in output && output.scriptType !== undefined) ||
200-
('address' in output && output.address !== undefined)
201-
);
202-
it(`tx signature counts ${coin} ${acidTest.signStage}`, function () {
203-
const txb = constructTxnBuilder(inputs, outputs, acidTest.network, acidTest.rootWalletKeys, acidTest.signStage);
204-
const tx = acidTest.signStage === 'fullsigned' ? txb.build() : txb.buildIncomplete();
205-
206-
const counts = getStrictSignatureCounts(tx);
207-
const countsFromIns = getStrictSignatureCounts(tx.ins);
208-
209-
assert.strictEqual(counts.length, tx.ins.length);
210-
assert.strictEqual(countsFromIns.length, tx.ins.length);
211-
tx.ins.forEach((input, inputIndex) => {
212-
const expectedCount = inputs[inputIndex].scriptType === 'p2shP2pk' && signatureCount > 0 ? 1 : signatureCount;
213-
assert.strictEqual(
214-
getStrictSignatureCount(input),
215-
expectedCount,
216-
`input ${inputIndex} has ${getStrictSignatureCount(input)} signatures, expected ${expectedCount}`
217-
);
218-
assert.strictEqual(counts[inputIndex], expectedCount);
219-
assert.strictEqual(countsFromIns[inputIndex], expectedCount);
220-
});
221-
});
222-
});
223-
}
224-
225189
signs.forEach((sign) => {
226190
getNetworkList()
227191
.filter((v) => isMainnet(v) && v !== networks.bitcoinsv)
228192
.forEach((network) => {
229193
runPsbt(AcidTest.withDefaults(network, sign, 'psbt'));
230194
runPsbt(AcidTest.withDefaults(network, sign, 'psbt-lite'));
231-
runTx(AcidTest.withDefaults(network, sign, 'psbt'));
232195
});
233196
});

0 commit comments

Comments
 (0)