Skip to content

Commit 3d670e0

Browse files
committed
Fix TransactionBuilder failing to debug P2PKH-only transactions
1 parent 47a9dbc commit 3d670e0

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

packages/cashscript/src/TransactionBuilder.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
isUnlockableUtxo,
1818
isStandardUnlockableUtxo,
1919
StandardUnlockableUtxo,
20+
isP2PKHUnlocker,
2021
} from './interfaces.js';
2122
import { NetworkProvider } from './network/index.js';
2223
import {
@@ -161,6 +162,11 @@ export class TransactionBuilder {
161162
}
162163

163164
debug(): DebugResults {
165+
// do not debug a pure P2PKH-spend transaction
166+
if (this.inputs.every((input) => isP2PKHUnlocker(input.unlocker))) {
167+
return {};
168+
}
169+
164170
if (this.inputs.some((input) => !isStandardUnlockableUtxo(input))) {
165171
throw new Error('Cannot debug a transaction with custom unlocker');
166172
}

packages/cashscript/test/TransactionBuilder.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
carolAddress,
1010
carolPriv,
1111
bobTokenAddress,
12+
aliceAddress,
13+
alicePriv,
1214
} from './fixture/vars.js';
1315
import { Network } from '../src/interfaces.js';
1416
import { utxoComparator, calculateDust, randomUtxo, randomToken, isNonTokenUtxo, isFungibleTokenUtxo } from '../src/utils.js';
@@ -263,4 +265,19 @@ describe('Transaction Builder', () => {
263265
.build();
264266
}).toThrow('Input is undefined');
265267
});
268+
269+
it('should not fail when spending from only P2PKH inputs', async () => {
270+
const aliceUtxos = (await provider.getUtxos(aliceAddress)).filter(isNonTokenUtxo);
271+
const sigTemplate = new SignatureTemplate(alicePriv);
272+
273+
expect(aliceUtxos.length).toBeGreaterThan(2);
274+
275+
const change = aliceUtxos[0].satoshis + aliceUtxos[1].satoshis - 1000n;
276+
277+
await expect(new TransactionBuilder({ provider })
278+
.addInput(aliceUtxos[0], sigTemplate.unlockP2PKH())
279+
.addInput(aliceUtxos[1], sigTemplate.unlockP2PKH())
280+
.addOutput({ to: aliceAddress, amount: change })
281+
.send()).resolves.not.toThrow();
282+
});
266283
});

0 commit comments

Comments
 (0)