-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathexplainPsbt.ts
More file actions
112 lines (99 loc) · 4.49 KB
/
explainPsbt.ts
File metadata and controls
112 lines (99 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import assert from 'node:assert/strict';
import * as utxolib from '@bitgo/utxo-lib';
import { testutil } from '@bitgo/utxo-lib';
import { fixedScriptWallet, Triple } from '@bitgo/wasm-utxo';
import type { TransactionExplanation } from '../../../../src/transaction/fixedScript/explainTransaction';
import { explainPsbt, explainPsbtWasm } from '../../../../src/transaction/fixedScript';
import { hasWasmUtxoSupport } from './util';
function describeTransactionWith(acidTest: testutil.AcidTest) {
describe(`${acidTest.name}`, function () {
let psbt: utxolib.bitgo.UtxoPsbt;
let psbtBytes: Buffer;
let walletXpubs: Triple<string>;
let customChangeWalletXpubs: Triple<string> | undefined;
let wasmPsbt: fixedScriptWallet.BitGoPsbt;
let refExplanation: TransactionExplanation;
before('prepare', function () {
psbt = acidTest.createPsbt();
refExplanation = explainPsbt(psbt, { pubs: acidTest.rootWalletKeys }, acidTest.network, {
strict: true,
});
psbtBytes = psbt.toBuffer();
const networkName = utxolib.getNetworkName(acidTest.network);
assert(networkName);
walletXpubs = acidTest.rootWalletKeys.triple.map((k) => k.neutered().toBase58()) as Triple<string>;
customChangeWalletXpubs = acidTest.otherWalletKeys.triple.map((k) => k.neutered().toBase58()) as Triple<string>;
if (hasWasmUtxoSupport(acidTest.network)) {
wasmPsbt = fixedScriptWallet.BitGoPsbt.fromBytes(psbtBytes, networkName);
}
});
it('should match the expected values for explainPsbt', function () {
// note: `outputs` means external outputs here
assert.strictEqual(refExplanation.outputs.length, 3);
assert.strictEqual(refExplanation.changeOutputs.length, acidTest.outputs.length - 3);
assert.strictEqual(refExplanation.outputAmount, '1800');
assert.strictEqual(refExplanation.changeOutputs.length, acidTest.outputs.length - 3);
refExplanation.changeOutputs.forEach((change) => {
assert.strictEqual(change.amount, '900');
assert.strictEqual(typeof change.address, 'string');
});
});
it('reference implementation should support custom change outputs', function () {
const customChangeExplanation = explainPsbt(
psbt,
{ pubs: acidTest.rootWalletKeys, customChangePubs: acidTest.otherWalletKeys },
acidTest.network,
{ strict: true }
);
assert.ok(customChangeExplanation.customChangeOutputs);
assert.strictEqual(customChangeExplanation.changeOutputs.length, refExplanation.changeOutputs.length);
assert.strictEqual(customChangeExplanation.outputs.length, refExplanation.outputs.length - 1);
assert.strictEqual(customChangeExplanation.customChangeOutputs.length, 1);
assert.strictEqual(customChangeExplanation.customChangeOutputs[0].amount, '900');
});
it('should match explainPsbtWasm', function () {
if (!hasWasmUtxoSupport(acidTest.network)) {
return this.skip();
}
const wasmExplanation = explainPsbtWasm(wasmPsbt, walletXpubs, {
replayProtection: {
publicKeys: [acidTest.getReplayProtectionPublicKey()],
},
});
for (const key of Object.keys(refExplanation)) {
const refValue = refExplanation[key];
const wasmValue = wasmExplanation[key];
switch (key) {
case 'displayOrder':
case 'inputSignatures':
case 'signatures':
// these are deprecated fields that we want to get rid of
assert.deepStrictEqual(wasmValue, undefined);
break;
default:
assert.deepStrictEqual(wasmValue, refValue, `mismatch for key ${key}`);
break;
}
}
});
if (acidTest.network !== utxolib.networks.bitcoin) {
return;
}
// extended test suite for bitcoin
it('returns custom change outputs when parameter is set', function () {
const wasmExplanation = explainPsbtWasm(wasmPsbt, walletXpubs, {
replayProtection: {
publicKeys: [acidTest.getReplayProtectionPublicKey()],
},
customChangeWalletXpubs,
});
assert.ok(wasmExplanation.customChangeOutputs);
assert.deepStrictEqual(wasmExplanation.outputs.length, 2);
assert.deepStrictEqual(wasmExplanation.customChangeOutputs.length, 1);
assert.deepStrictEqual(wasmExplanation.customChangeOutputs[0].amount, '900');
});
});
}
describe('explainPsbt(Wasm)', function () {
testutil.AcidTest.suite().forEach((test) => describeTransactionWith(test));
});