Skip to content

Commit 806ff79

Browse files
committed
feat(psbt): allow use of non-btc nonWitnessUtxos
Enables extension of Psbt to support other chains. Issue: BG-55848
1 parent 3838e7f commit 806ff79

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/psbt.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export declare class Psbt {
5656
static fromBase64(data: string, opts?: PsbtOptsOptional): Psbt;
5757
static fromHex(data: string, opts?: PsbtOptsOptional): Psbt;
5858
static fromBuffer(buffer: Buffer, opts?: PsbtOptsOptional): Psbt;
59+
protected static transactionFromBuffer(buffer: Buffer, _network: Network): Transaction<bigint>;
5960
private __CACHE;
6061
private opts;
6162
constructor(opts?: PsbtOptsOptional, data?: PsbtBase);

src/psbt.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class Psbt {
7878
// We will disable exporting the Psbt when unsafe sign is active.
7979
// because it is not BIP174 compliant.
8080
__UNSAFE_SIGN_NONSEGWIT: false,
81+
__TX_FROM_BUFFER: buf =>
82+
this.constructor.transactionFromBuffer(buf, this.opts.network),
8183
};
8284
if (this.data.inputs.length === 0) this.setVersion(2);
8385
// Make data hidden when enumerating
@@ -103,6 +105,9 @@ class Psbt {
103105
checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
104106
return psbt;
105107
}
108+
static transactionFromBuffer(buffer, _network) {
109+
return transaction_1.Transaction.fromBuffer(buffer, undefined, 'bigint');
110+
}
106111
get inputCount() {
107112
return this.data.inputs.length;
108113
}
@@ -1219,11 +1224,7 @@ function witnessStackToScriptWitness(witness) {
12191224
}
12201225
function addNonWitnessTxCache(cache, input, inputIndex) {
12211226
cache.__NON_WITNESS_UTXO_BUF_CACHE[inputIndex] = input.nonWitnessUtxo;
1222-
const tx = transaction_1.Transaction.fromBuffer(
1223-
input.nonWitnessUtxo,
1224-
undefined,
1225-
'bigint',
1226-
);
1227+
const tx = cache.__TX_FROM_BUFFER(input.nonWitnessUtxo);
12271228
cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex] = tx;
12281229
const self = cache;
12291230
const selfIndex = inputIndex;

ts_src/psbt.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ export class Psbt {
114114
return psbt;
115115
}
116116

117+
protected static transactionFromBuffer(
118+
buffer: Buffer,
119+
_network: Network,
120+
): Transaction<bigint> {
121+
return Transaction.fromBuffer<bigint>(buffer, undefined, 'bigint');
122+
}
123+
117124
private __CACHE: PsbtCache;
118125
private opts: PsbtOpts;
119126

@@ -137,6 +144,11 @@ export class Psbt {
137144
// We will disable exporting the Psbt when unsafe sign is active.
138145
// because it is not BIP174 compliant.
139146
__UNSAFE_SIGN_NONSEGWIT: false,
147+
__TX_FROM_BUFFER: buf =>
148+
(this.constructor as typeof Psbt).transactionFromBuffer(
149+
buf,
150+
this.opts.network,
151+
),
140152
};
141153
if (this.data.inputs.length === 0) this.setVersion(2);
142154

@@ -752,6 +764,7 @@ interface PsbtCache {
752764
__FEE?: bigint;
753765
__EXTRACTED_TX?: Transaction<bigint>;
754766
__UNSAFE_SIGN_NONSEGWIT: boolean;
767+
__TX_FROM_BUFFER: (buf: Buffer) => Transaction<bigint>;
755768
}
756769

757770
interface PsbtOptsOptional {
@@ -1590,11 +1603,7 @@ function addNonWitnessTxCache(
15901603
): void {
15911604
cache.__NON_WITNESS_UTXO_BUF_CACHE[inputIndex] = input.nonWitnessUtxo!;
15921605

1593-
const tx = Transaction.fromBuffer<bigint>(
1594-
input.nonWitnessUtxo!,
1595-
undefined,
1596-
'bigint',
1597-
);
1606+
const tx = cache.__TX_FROM_BUFFER(input.nonWitnessUtxo!);
15981607
cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex] = tx;
15991608

16001609
const self = cache;

0 commit comments

Comments
 (0)