Skip to content

Commit 6e447b1

Browse files
committed
Refactor: Create cache in constructor
1 parent d05806f commit 6e447b1

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

src/psbt.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,14 @@ const DEFAULT_OPTS = {
6262
class Psbt {
6363
constructor(opts = {}, data = new bip174_1.Psbt(new PsbtTransaction())) {
6464
this.data = data;
65+
// set defaults
66+
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
6567
this.__CACHE = {
6668
__NON_WITNESS_UTXO_TX_CACHE: [],
6769
__NON_WITNESS_UTXO_BUF_CACHE: [],
6870
__TX_IN_CACHE: {},
69-
__TX: new transaction_1.Transaction(),
71+
__TX: this.data.globalMap.unsignedTx.tx,
7072
};
71-
// set defaults
72-
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
73-
const c = this.__CACHE;
74-
c.__TX = this.data.globalMap.unsignedTx.tx;
7573
if (this.data.inputs.length === 0) this.setVersion(2);
7674
// Make data hidden when enumerating
7775
const dpew = (obj, attr, enumerable, writable) =>
@@ -92,10 +90,8 @@ class Psbt {
9290
}
9391
static fromBuffer(buffer, opts = {}) {
9492
const psbtBase = bip174_1.Psbt.fromBuffer(buffer, transactionFromBuffer);
95-
const tx = psbtBase.globalMap.unsignedTx.tx;
9693
const psbt = new Psbt(opts, psbtBase);
97-
psbt.__CACHE.__TX = tx;
98-
checkTxForDupeIns(tx, psbt.__CACHE);
94+
checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
9995
return psbt;
10096
}
10197
get inputCount() {

ts_src/psbt.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,12 @@ export class Psbt {
8888

8989
static fromBuffer(buffer: Buffer, opts: PsbtOptsOptional = {}): Psbt {
9090
const psbtBase = PsbtBase.fromBuffer(buffer, transactionFromBuffer);
91-
const tx: Transaction = (psbtBase.globalMap.unsignedTx as PsbtTransaction)
92-
.tx;
9391
const psbt = new Psbt(opts, psbtBase);
94-
psbt.__CACHE.__TX = tx;
95-
checkTxForDupeIns(tx, psbt.__CACHE);
92+
checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
9693
return psbt;
9794
}
9895

99-
private __CACHE: PsbtCache = {
100-
__NON_WITNESS_UTXO_TX_CACHE: [],
101-
__NON_WITNESS_UTXO_BUF_CACHE: [],
102-
__TX_IN_CACHE: {},
103-
__TX: new Transaction(),
104-
};
96+
private __CACHE: PsbtCache;
10597
private opts: PsbtOpts;
10698

10799
constructor(
@@ -110,8 +102,12 @@ export class Psbt {
110102
) {
111103
// set defaults
112104
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
113-
const c = this.__CACHE;
114-
c.__TX = (this.data.globalMap.unsignedTx as PsbtTransaction).tx;
105+
this.__CACHE = {
106+
__NON_WITNESS_UTXO_TX_CACHE: [],
107+
__NON_WITNESS_UTXO_BUF_CACHE: [],
108+
__TX_IN_CACHE: {},
109+
__TX: (this.data.globalMap.unsignedTx as PsbtTransaction).tx,
110+
};
115111
if (this.data.inputs.length === 0) this.setVersion(2);
116112

117113
// Make data hidden when enumerating

0 commit comments

Comments
 (0)