Refactor: Create cache in constructor

This commit is contained in:
junderw 2019-07-19 15:51:38 +09:00
parent d05806fe69
commit 6e447b1f1b
No known key found for this signature in database
GPG key ID: B256185D3A971908
2 changed files with 12 additions and 20 deletions

View file

@ -62,16 +62,14 @@ const DEFAULT_OPTS = {
class Psbt {
constructor(opts = {}, data = new bip174_1.Psbt(new PsbtTransaction())) {
this.data = data;
// set defaults
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
this.__CACHE = {
__NON_WITNESS_UTXO_TX_CACHE: [],
__NON_WITNESS_UTXO_BUF_CACHE: [],
__TX_IN_CACHE: {},
__TX: new transaction_1.Transaction(),
__TX: this.data.globalMap.unsignedTx.tx,
};
// set defaults
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
const c = this.__CACHE;
c.__TX = this.data.globalMap.unsignedTx.tx;
if (this.data.inputs.length === 0) this.setVersion(2);
// Make data hidden when enumerating
const dpew = (obj, attr, enumerable, writable) =>
@ -92,10 +90,8 @@ class Psbt {
}
static fromBuffer(buffer, opts = {}) {
const psbtBase = bip174_1.Psbt.fromBuffer(buffer, transactionFromBuffer);
const tx = psbtBase.globalMap.unsignedTx.tx;
const psbt = new Psbt(opts, psbtBase);
psbt.__CACHE.__TX = tx;
checkTxForDupeIns(tx, psbt.__CACHE);
checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
return psbt;
}
get inputCount() {

View file

@ -88,20 +88,12 @@ export class Psbt {
static fromBuffer(buffer: Buffer, opts: PsbtOptsOptional = {}): Psbt {
const psbtBase = PsbtBase.fromBuffer(buffer, transactionFromBuffer);
const tx: Transaction = (psbtBase.globalMap.unsignedTx as PsbtTransaction)
.tx;
const psbt = new Psbt(opts, psbtBase);
psbt.__CACHE.__TX = tx;
checkTxForDupeIns(tx, psbt.__CACHE);
checkTxForDupeIns(psbt.__CACHE.__TX, psbt.__CACHE);
return psbt;
}
private __CACHE: PsbtCache = {
__NON_WITNESS_UTXO_TX_CACHE: [],
__NON_WITNESS_UTXO_BUF_CACHE: [],
__TX_IN_CACHE: {},
__TX: new Transaction(),
};
private __CACHE: PsbtCache;
private opts: PsbtOpts;
constructor(
@ -110,8 +102,12 @@ export class Psbt {
) {
// set defaults
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
const c = this.__CACHE;
c.__TX = (this.data.globalMap.unsignedTx as PsbtTransaction).tx;
this.__CACHE = {
__NON_WITNESS_UTXO_TX_CACHE: [],
__NON_WITNESS_UTXO_BUF_CACHE: [],
__TX_IN_CACHE: {},
__TX: (this.data.globalMap.unsignedTx as PsbtTransaction).tx,
};
if (this.data.inputs.length === 0) this.setVersion(2);
// Make data hidden when enumerating