Skip to content

Commit 16c29d1

Browse files
davidkaplanbitgollm-git
andcommitted
feat(utxo-core): improve bip322 transaction handling
Change buildToSignPsbt to directly accept a Transaction object instead of hex string, and return a Psbt object instead of hex. Similarly update buildToSpendTransaction to return a Transaction object. Issue: BTC-2362 Co-authored-by: llm-git <[email protected]>
1 parent 11993ae commit 16c29d1

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ export type AddressDetails = {
1414
* @param {AddressDetails} addressDetails - The details of the address, including redeemScript and/or witnessScript.
1515
* @returns {string} - The hex representation of the constructed PSBT.
1616
*/
17-
export function buildToSignPsbt(toSpendTxHex: string, addressDetails: AddressDetails): string {
17+
export function buildToSignPsbt(toSpendTx: Transaction<bigint>, addressDetails: AddressDetails): Psbt {
1818
if (!addressDetails.redeemScript && !addressDetails.witnessScript) {
1919
throw new Error('redeemScript and/or witnessScript must be provided');
2020
}
2121

22-
const toSpendTx = Transaction.fromHex(toSpendTxHex);
23-
2422
// Create PSBT object for constructing the transaction
2523
const psbt = new Psbt();
2624
// Set default value for nVersion and nLockTime
@@ -37,13 +35,15 @@ export function buildToSignPsbt(toSpendTxHex: string, addressDetails: AddressDet
3735
psbt.updateInput(0, { redeemScript: addressDetails.redeemScript });
3836
}
3937
if (addressDetails.witnessScript) {
40-
psbt.updateInput(0, { witnessUtxo: { value: BigInt(0), script: addressDetails.witnessScript } });
38+
psbt.updateInput(0, {
39+
witnessUtxo: { value: BigInt(0), script: addressDetails.witnessScript },
40+
});
4141
}
4242

4343
// Set the output
4444
psbt.addOutput({
4545
value: BigInt(0), // vout[0].nValue = 0
4646
script: Buffer.from([0x6a]), // vout[0].scriptPubKey = OP_RETURN
4747
});
48-
return psbt.toHex();
48+
return psbt;
4949
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Hash } from 'fast-sha256';
2-
import { Psbt } from '@bitgo/utxo-lib';
2+
import { Psbt, Transaction } from '@bitgo/utxo-lib';
33

44
export const BIP322_TAG = 'BIP0322-signed-message';
55

@@ -31,9 +31,13 @@ export function hashMessageWithTag(message: string | Buffer, tag = BIP322_TAG):
3131
* @param {Buffer} scriptPubKey - The scriptPubKey to use for the output
3232
* @param {string | Buffer} message - The message to include in the transaction
3333
* @param {Buffer} [tag=BIP322_TAG] - The tag to use for hashing, defaults to BIP322_TAG.
34-
* @returns {string} - The hex representation of the constructed transaction
34+
* @returns {Transaction} - The constructed transaction
3535
*/
36-
export function buildToSpendTransaction(scriptPubKey: Buffer, message: string | Buffer, tag = BIP322_TAG): string {
36+
export function buildToSpendTransaction(
37+
scriptPubKey: Buffer,
38+
message: string | Buffer,
39+
tag = BIP322_TAG
40+
): Transaction<bigint> {
3741
// Create PSBT object for constructing the transaction
3842
const psbt = new Psbt();
3943
// Set default value for nVersion and nLockTime
@@ -60,5 +64,5 @@ export function buildToSpendTransaction(scriptPubKey: Buffer, message: string |
6064
script: scriptPubKey, // vout[0].scriptPubKey = message_challenge
6165
});
6266
// Return transaction
63-
return psbt.extractTransaction().toHex();
67+
return psbt.extractTransaction();
6468
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'assert';
22

3-
import { payments, Psbt, ECPair, Transaction } from '@bitgo/utxo-lib';
3+
import { payments, ECPair, Transaction } from '@bitgo/utxo-lib';
44

55
import * as bip322 from '../../src/bip322';
66

@@ -26,12 +26,12 @@ describe('BIP322 toSign', function () {
2626

2727
fixtures.forEach(({ message, txid }) => {
2828
it(`should build a to_sign PSBT for message "${message}"`, function () {
29-
const toSpendTxHex = bip322.buildToSpendTransaction(scriptPubKey, Buffer.from(message));
29+
const toSpendTx = bip322.buildToSpendTransaction(scriptPubKey, Buffer.from(message));
3030
const addressDetails = {
3131
witnessScript: scriptPubKey,
3232
};
33-
const result = bip322.buildToSignPsbt(toSpendTxHex, addressDetails);
34-
const computedTxid = Psbt.fromHex(result)
33+
const result = bip322.buildToSignPsbt(toSpendTx, addressDetails);
34+
const computedTxid = result
3535
.signAllInputs(prv, [Transaction.SIGHASH_ALL])
3636
.finalizeAllInputs()
3737
.extractTransaction()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'assert';
22

3-
import { payments, Transaction } from '@bitgo/utxo-lib';
3+
import { payments } from '@bitgo/utxo-lib';
44

55
import { buildToSpendTransaction, hashMessageWithTag } from '../../src/bip322';
66

@@ -50,7 +50,7 @@ describe('to_spend', function () {
5050
fixtures.forEach(({ message, txid }) => {
5151
it(`should build a to_spend transaction for message "${message}"`, function () {
5252
const result = buildToSpendTransaction(scriptPubKey, Buffer.from(message));
53-
const computedTxid = Transaction.fromHex(result).getId();
53+
const computedTxid = result.getId();
5454
assert.strictEqual(computedTxid, txid, `Transaction ID for message "${message}" does not match expected value`);
5555
});
5656
});

0 commit comments

Comments
 (0)