Skip to content

Commit a931383

Browse files
mainnet-patrkalis
authored andcommitted
Fix TransactionBuilder failing to debug P2PKH-only transactions
1 parent 248870b commit a931383

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
@@ -18,6 +18,7 @@ import {
1818
isUnlockableUtxo,
1919
isStandardUnlockableUtxo,
2020
StandardUnlockableUtxo,
21+
isP2PKHUnlocker,
2122
} from './interfaces.js';
2223
import { NetworkProvider } from './network/index.js';
2324
import {
@@ -157,6 +158,11 @@ export class TransactionBuilder {
157158
}
158159

159160
debug(): DebugResults {
161+
// do not debug a pure P2PKH-spend transaction
162+
if (this.inputs.every((input) => isP2PKHUnlocker(input.unlocker))) {
163+
return {};
164+
}
165+
160166
if (this.inputs.some((input) => !isStandardUnlockableUtxo(input))) {
161167
throw new Error('Cannot debug a transaction with custom unlocker');
162168
}

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';
@@ -293,4 +295,19 @@ describe('Transaction Builder', () => {
293295
expect(JSON.parse(stringify(wcTransactionObj))).toEqual(expectedResult);
294296
});
295297
});
298+
299+
it('should not fail when spending from only P2PKH inputs', async () => {
300+
const aliceUtxos = (await provider.getUtxos(aliceAddress)).filter(isNonTokenUtxo);
301+
const sigTemplate = new SignatureTemplate(alicePriv);
302+
303+
expect(aliceUtxos.length).toBeGreaterThan(2);
304+
305+
const change = aliceUtxos[0].satoshis + aliceUtxos[1].satoshis - 1000n;
306+
307+
await expect(new TransactionBuilder({ provider })
308+
.addInput(aliceUtxos[0], sigTemplate.unlockP2PKH())
309+
.addInput(aliceUtxos[1], sigTemplate.unlockP2PKH())
310+
.addOutput({ to: aliceAddress, amount: change })
311+
.send()).resolves.not.toThrow();
312+
});
296313
});

0 commit comments

Comments
 (0)