Skip to content

Commit 083d2c7

Browse files
committed
feat(psbt): easier custom transaction types
Issue: BG-47820
1 parent 6624bd4 commit 083d2c7

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as ScriptSignature from './script_signature';
88
export { ScriptSignature, address, crypto, networks, payments, script, taproot, };
99
export { Block } from './block';
1010
export { TaggedHashPrefix } from './crypto';
11-
export { Psbt, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, } from './psbt';
11+
export { Psbt, PsbtTransaction, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, } from './psbt';
1212
export { OPS as opcodes } from './ops';
1313
export { Transaction } from './transaction';
1414
export { Network } from './networks';

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
3-
exports.Transaction = exports.opcodes = exports.Psbt = exports.Block = exports.taproot = exports.script = exports.payments = exports.networks = exports.crypto = exports.address = exports.ScriptSignature = void 0;
3+
exports.Transaction = exports.opcodes = exports.PsbtTransaction = exports.Psbt = exports.Block = exports.taproot = exports.script = exports.payments = exports.networks = exports.crypto = exports.address = exports.ScriptSignature = void 0;
44
const address = require('./address');
55
exports.address = address;
66
const crypto = require('./crypto');
@@ -29,6 +29,12 @@ Object.defineProperty(exports, 'Psbt', {
2929
return psbt_1.Psbt;
3030
},
3131
});
32+
Object.defineProperty(exports, 'PsbtTransaction', {
33+
enumerable: true,
34+
get: function() {
35+
return psbt_1.PsbtTransaction;
36+
},
37+
});
3238
var ops_1 = require('./ops');
3339
Object.defineProperty(exports, 'opcodes', {
3440
enumerable: true,

src/psbt.d.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="node" />
22
import { Psbt as PsbtBase } from 'bip174';
3-
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate } from 'bip174/src/lib/interfaces';
3+
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate, Transaction as ITransaction } from 'bip174/src/lib/interfaces';
44
import { Network } from './networks';
55
import { Transaction } from './transaction';
66
export interface TransactionInput {
@@ -163,6 +163,24 @@ export interface SignerAsync {
163163
sign(hash: Buffer, lowR?: boolean): Promise<Buffer>;
164164
getPublicKey?(): Buffer;
165165
}
166+
/**
167+
* This class implements the Transaction interface from bip174 library.
168+
* It contains a bitcoinjs-lib Transaction object.
169+
*/
170+
export declare class PsbtTransaction implements ITransaction {
171+
tx: Transaction<bigint>;
172+
constructor({ tx, buffer, }?: {
173+
tx?: Transaction<bigint>;
174+
buffer?: Buffer;
175+
});
176+
getInputOutputCounts(): {
177+
inputCount: number;
178+
outputCount: number;
179+
};
180+
addInput(input: any): void;
181+
addOutput(output: any): void;
182+
toBuffer(): Buffer;
183+
}
166184
/**
167185
* This function must do two things:
168186
* 1. Check if the `input` can be finalized. If it can not be finalized, throw.

src/psbt.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
3-
exports.Psbt = void 0;
3+
exports.PsbtTransaction = exports.Psbt = void 0;
44
const bip174_1 = require('bip174');
55
const varuint = require('bip174/src/lib/converter/varint');
66
const utils_1 = require('bip174/src/lib/utils');
@@ -613,14 +613,23 @@ exports.Psbt = Psbt;
613613
* It takes the "transaction buffer" portion of the psbt buffer and returns a
614614
* Transaction (From the bip174 library) interface.
615615
*/
616-
const transactionFromBuffer = buffer => new PsbtTransaction(buffer);
616+
const transactionFromBuffer = buffer => new PsbtTransaction({ buffer });
617617
/**
618618
* This class implements the Transaction interface from bip174 library.
619619
* It contains a bitcoinjs-lib Transaction object.
620620
*/
621621
class PsbtTransaction {
622-
constructor(buffer = Buffer.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0])) {
623-
this.tx = transaction_1.Transaction.fromBuffer(buffer, undefined, 'bigint');
622+
constructor({ tx, buffer } = {}) {
623+
if (tx !== undefined) {
624+
this.tx = tx;
625+
} else {
626+
buffer = buffer || Buffer.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
627+
this.tx = transaction_1.Transaction.fromBuffer(
628+
buffer,
629+
undefined,
630+
'bigint',
631+
);
632+
}
624633
checkTxEmpty(this.tx);
625634
Object.defineProperty(this, 'tx', {
626635
enumerable: false,
@@ -663,6 +672,7 @@ class PsbtTransaction {
663672
return this.tx.toBuffer();
664673
}
665674
}
675+
exports.PsbtTransaction = PsbtTransaction;
666676
function canFinalize(input, script, scriptType) {
667677
switch (scriptType) {
668678
case 'pubkey':

ts_src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export { Block } from './block';
2020
export { TaggedHashPrefix } from './crypto';
2121
export {
2222
Psbt,
23+
PsbtTransaction,
2324
PsbtTxInput,
2425
PsbtTxOutput,
2526
Signer,

ts_src/psbt.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,16 +831,24 @@ export interface SignerAsync {
831831
*/
832832
const transactionFromBuffer: TransactionFromBuffer = (
833833
buffer: Buffer,
834-
): ITransaction => new PsbtTransaction(buffer);
834+
): ITransaction => new PsbtTransaction({ buffer });
835835

836836
/**
837837
* This class implements the Transaction interface from bip174 library.
838838
* It contains a bitcoinjs-lib Transaction object.
839839
*/
840-
class PsbtTransaction implements ITransaction {
840+
export class PsbtTransaction implements ITransaction {
841841
tx: Transaction<bigint>;
842-
constructor(buffer: Buffer = Buffer.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0])) {
843-
this.tx = Transaction.fromBuffer<bigint>(buffer, undefined, 'bigint');
842+
constructor({
843+
tx,
844+
buffer,
845+
}: { tx?: Transaction<bigint>; buffer?: Buffer } = {}) {
846+
if (tx !== undefined) {
847+
this.tx = tx;
848+
} else {
849+
buffer = buffer || Buffer.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
850+
this.tx = Transaction.fromBuffer<bigint>(buffer, undefined, 'bigint');
851+
}
844852
checkTxEmpty(this.tx);
845853
Object.defineProperty(this, 'tx', {
846854
enumerable: false,

0 commit comments

Comments
 (0)