Skip to content

Commit c3c0bfa

Browse files
fix(utxo-core): update toSignPsbt to properly set witnessUtxo
Update buildToSignPsbt to correctly handle scriptPubKey for witnessUtxo instead of using witnessScript, which was incorrectly used in this context. The function now requires scriptPubKey in AddressDetails and properly sets up both witnessUtxo and witnessScript when present. Co-authored-by: llm-git <[email protected]> TICKET: BTC-2372
1 parent 30c6d78 commit c3c0bfa

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

modules/utxo-core/src/bip322/toSign.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Psbt, Transaction } from '@bitgo/utxo-lib';
33
export type AddressDetails = {
44
redeemScript?: Buffer;
55
witnessScript?: Buffer;
6+
scriptPubKey: Buffer;
67
};
78

89
/**
@@ -15,10 +16,6 @@ export type AddressDetails = {
1516
* @returns {string} - The hex representation of the constructed PSBT.
1617
*/
1718
export function buildToSignPsbt(toSpendTx: Transaction<bigint>, addressDetails: AddressDetails): Psbt {
18-
if (!addressDetails.redeemScript && !addressDetails.witnessScript) {
19-
throw new Error('redeemScript and/or witnessScript must be provided');
20-
}
21-
2219
// Create PSBT object for constructing the transaction
2320
const psbt = new Psbt();
2421
// Set default value for nVersion and nLockTime
@@ -31,13 +28,15 @@ export function buildToSignPsbt(toSpendTx: Transaction<bigint>, addressDetails:
3128
sequence: 0, // vin[0].nSequence = 0
3229
nonWitnessUtxo: toSpendTx.toBuffer(), // previous transaction for us to rebuild later to verify
3330
});
31+
psbt.updateInput(0, {
32+
witnessUtxo: { value: BigInt(0), script: addressDetails.scriptPubKey },
33+
});
34+
3435
if (addressDetails.redeemScript) {
3536
psbt.updateInput(0, { redeemScript: addressDetails.redeemScript });
3637
}
3738
if (addressDetails.witnessScript) {
38-
psbt.updateInput(0, {
39-
witnessUtxo: { value: BigInt(0), script: addressDetails.witnessScript },
40-
});
39+
psbt.updateInput(0, { witnessScript: addressDetails.witnessScript });
4140
}
4241

4342
// Set the output

modules/utxo-core/test/bip322/bip322.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export const BIP322_FIXTURE_HELLO_WORLD_TOSPEND_TX = buildToSpendTransaction(
1414
);
1515

1616
export const BIP322_FIXTURE_HELLOW_WORLD_TOSIGN_PSBT = buildToSignPsbt(BIP322_FIXTURE_HELLO_WORLD_TOSPEND_TX, {
17-
witnessScript: BIP322_PAYMENT_P2WPKH_FIXTURE.output as Buffer,
17+
scriptPubKey: BIP322_PAYMENT_P2WPKH_FIXTURE.output as Buffer,
1818
});

modules/utxo-core/test/bip322/toSign.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('BIP322 toSign', function () {
2424
it(`should build a to_sign PSBT for message "${message}"`, function () {
2525
const toSpendTx = bip322.buildToSpendTransaction(scriptPubKey, Buffer.from(message));
2626
const addressDetails = {
27-
witnessScript: scriptPubKey,
27+
scriptPubKey,
2828
};
2929
const result = bip322.buildToSignPsbt(toSpendTx, addressDetails);
3030
const computedTxid = result

0 commit comments

Comments
 (0)