Refactor: pass only cache to certain functions
This commit is contained in:
parent
9749a216b8
commit
2fd4b9dc54
2 changed files with 28 additions and 46 deletions
38
src/psbt.js
38
src/psbt.js
|
@ -26,25 +26,24 @@ class Psbt extends bip174_1.Psbt {
|
||||||
};
|
};
|
||||||
// set defaults
|
// set defaults
|
||||||
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
|
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
|
||||||
this.__CACHE.__TX = transaction_1.Transaction.fromBuffer(
|
const c = this.__CACHE;
|
||||||
this.globalMap.unsignedTx,
|
c.__TX = transaction_1.Transaction.fromBuffer(this.globalMap.unsignedTx);
|
||||||
);
|
|
||||||
this.setVersion(2);
|
this.setVersion(2);
|
||||||
// set cache
|
// set cache
|
||||||
const self = this;
|
|
||||||
delete this.globalMap.unsignedTx;
|
delete this.globalMap.unsignedTx;
|
||||||
Object.defineProperty(this.globalMap, 'unsignedTx', {
|
Object.defineProperty(this.globalMap, 'unsignedTx', {
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
get() {
|
get() {
|
||||||
if (self.__CACHE.__TX_BUF_CACHE !== undefined) {
|
const buf = c.__TX_BUF_CACHE;
|
||||||
return self.__CACHE.__TX_BUF_CACHE;
|
if (buf !== undefined) {
|
||||||
|
return buf;
|
||||||
} else {
|
} else {
|
||||||
self.__CACHE.__TX_BUF_CACHE = self.__CACHE.__TX.toBuffer();
|
c.__TX_BUF_CACHE = c.__TX.toBuffer();
|
||||||
return self.__CACHE.__TX_BUF_CACHE;
|
return c.__TX_BUF_CACHE;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
set(data) {
|
set(data) {
|
||||||
self.__CACHE.__TX_BUF_CACHE = data;
|
c.__TX_BUF_CACHE = data;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
// Make data hidden when enumerating
|
// Make data hidden when enumerating
|
||||||
|
@ -53,8 +52,6 @@ class Psbt extends bip174_1.Psbt {
|
||||||
enumerable,
|
enumerable,
|
||||||
writable,
|
writable,
|
||||||
});
|
});
|
||||||
dpew(this, '__TX', false, true);
|
|
||||||
dpew(this, '__EXTRACTED_TX', false, true);
|
|
||||||
dpew(this, '__CACHE', false, true);
|
dpew(this, '__CACHE', false, true);
|
||||||
dpew(this, 'opts', false, true);
|
dpew(this, 'opts', false, true);
|
||||||
}
|
}
|
||||||
|
@ -216,7 +213,6 @@ class Psbt extends bip174_1.Psbt {
|
||||||
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput(
|
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput(
|
||||||
inputIndex,
|
inputIndex,
|
||||||
input,
|
input,
|
||||||
this.__CACHE.__TX,
|
|
||||||
this.__CACHE,
|
this.__CACHE,
|
||||||
);
|
);
|
||||||
if (!script) return false;
|
if (!script) return false;
|
||||||
|
@ -258,7 +254,6 @@ class Psbt extends bip174_1.Psbt {
|
||||||
? getHashForSig(
|
? getHashForSig(
|
||||||
inputIndex,
|
inputIndex,
|
||||||
Object.assign({}, input, { sighashType: sig.hashType }),
|
Object.assign({}, input, { sighashType: sig.hashType }),
|
||||||
this.__CACHE.__TX,
|
|
||||||
this.__CACHE,
|
this.__CACHE,
|
||||||
)
|
)
|
||||||
: { hash: hashCache, script: scriptCache };
|
: { hash: hashCache, script: scriptCache };
|
||||||
|
@ -327,7 +322,6 @@ class Psbt extends bip174_1.Psbt {
|
||||||
this.inputs,
|
this.inputs,
|
||||||
inputIndex,
|
inputIndex,
|
||||||
keyPair.publicKey,
|
keyPair.publicKey,
|
||||||
this.__CACHE.__TX,
|
|
||||||
this.__CACHE,
|
this.__CACHE,
|
||||||
);
|
);
|
||||||
const partialSig = {
|
const partialSig = {
|
||||||
|
@ -344,7 +338,6 @@ class Psbt extends bip174_1.Psbt {
|
||||||
this.inputs,
|
this.inputs,
|
||||||
inputIndex,
|
inputIndex,
|
||||||
keyPair.publicKey,
|
keyPair.publicKey,
|
||||||
this.__CACHE.__TX,
|
|
||||||
this.__CACHE,
|
this.__CACHE,
|
||||||
);
|
);
|
||||||
Promise.resolve(keyPair.sign(hash)).then(signature => {
|
Promise.resolve(keyPair.sign(hash)).then(signature => {
|
||||||
|
@ -400,14 +393,9 @@ function checkTxInputCache(cache, input) {
|
||||||
function isFinalized(input) {
|
function isFinalized(input) {
|
||||||
return !!input.finalScriptSig || !!input.finalScriptWitness;
|
return !!input.finalScriptSig || !!input.finalScriptWitness;
|
||||||
}
|
}
|
||||||
function getHashAndSighashType(inputs, inputIndex, pubkey, unsignedTx, cache) {
|
function getHashAndSighashType(inputs, inputIndex, pubkey, cache) {
|
||||||
const input = utils_1.checkForInput(inputs, inputIndex);
|
const input = utils_1.checkForInput(inputs, inputIndex);
|
||||||
const { hash, sighashType, script } = getHashForSig(
|
const { hash, sighashType, script } = getHashForSig(inputIndex, input, cache);
|
||||||
inputIndex,
|
|
||||||
input,
|
|
||||||
unsignedTx,
|
|
||||||
cache,
|
|
||||||
);
|
|
||||||
checkScriptForPubkey(pubkey, script, 'sign');
|
checkScriptForPubkey(pubkey, script, 'sign');
|
||||||
return {
|
return {
|
||||||
hash,
|
hash,
|
||||||
|
@ -525,7 +513,8 @@ function checkScriptForPubkey(pubkey, script, action) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function getHashForSig(inputIndex, input, unsignedTx, cache) {
|
function getHashForSig(inputIndex, input, cache) {
|
||||||
|
const unsignedTx = cache.__TX;
|
||||||
const sighashType =
|
const sighashType =
|
||||||
input.sighashType || transaction_1.Transaction.SIGHASH_ALL;
|
input.sighashType || transaction_1.Transaction.SIGHASH_ALL;
|
||||||
let hash;
|
let hash;
|
||||||
|
@ -646,7 +635,8 @@ function classifyScript(script) {
|
||||||
if (isP2PK(script)) return 'pubkey';
|
if (isP2PK(script)) return 'pubkey';
|
||||||
return 'nonstandard';
|
return 'nonstandard';
|
||||||
}
|
}
|
||||||
function getScriptFromInput(inputIndex, input, unsignedTx, cache) {
|
function getScriptFromInput(inputIndex, input, cache) {
|
||||||
|
const unsignedTx = cache.__TX;
|
||||||
const res = {
|
const res = {
|
||||||
script: null,
|
script: null,
|
||||||
isSegwit: false,
|
isSegwit: false,
|
||||||
|
|
|
@ -52,6 +52,7 @@ export class Psbt extends PsbtBase {
|
||||||
}
|
}
|
||||||
return psbt as InstanceType<T>;
|
return psbt as InstanceType<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromBuffer<T extends typeof PsbtBase>(
|
static fromBuffer<T extends typeof PsbtBase>(
|
||||||
this: T,
|
this: T,
|
||||||
buffer: Buffer,
|
buffer: Buffer,
|
||||||
|
@ -75,6 +76,7 @@ export class Psbt extends PsbtBase {
|
||||||
checkTxForDupeIns(tx!, psbt.__CACHE);
|
checkTxForDupeIns(tx!, psbt.__CACHE);
|
||||||
return psbt as InstanceType<T>;
|
return psbt as InstanceType<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
private __CACHE: PsbtCache = {
|
private __CACHE: PsbtCache = {
|
||||||
__NON_WITNESS_UTXO_TX_CACHE: [],
|
__NON_WITNESS_UTXO_TX_CACHE: [],
|
||||||
__NON_WITNESS_UTXO_BUF_CACHE: [],
|
__NON_WITNESS_UTXO_BUF_CACHE: [],
|
||||||
|
@ -82,28 +84,30 @@ export class Psbt extends PsbtBase {
|
||||||
__TX: new Transaction(),
|
__TX: new Transaction(),
|
||||||
};
|
};
|
||||||
private opts: PsbtOpts;
|
private opts: PsbtOpts;
|
||||||
|
|
||||||
constructor(opts: PsbtOptsOptional = {}) {
|
constructor(opts: PsbtOptsOptional = {}) {
|
||||||
super();
|
super();
|
||||||
// set defaults
|
// set defaults
|
||||||
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
|
this.opts = Object.assign({}, DEFAULT_OPTS, opts);
|
||||||
this.__CACHE.__TX = Transaction.fromBuffer(this.globalMap.unsignedTx!);
|
const c = this.__CACHE;
|
||||||
|
c.__TX = Transaction.fromBuffer(this.globalMap.unsignedTx!);
|
||||||
this.setVersion(2);
|
this.setVersion(2);
|
||||||
|
|
||||||
// set cache
|
// set cache
|
||||||
const self = this;
|
|
||||||
delete this.globalMap.unsignedTx;
|
delete this.globalMap.unsignedTx;
|
||||||
Object.defineProperty(this.globalMap, 'unsignedTx', {
|
Object.defineProperty(this.globalMap, 'unsignedTx', {
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
get(): Buffer {
|
get(): Buffer {
|
||||||
if (self.__CACHE.__TX_BUF_CACHE !== undefined) {
|
const buf = c.__TX_BUF_CACHE;
|
||||||
return self.__CACHE.__TX_BUF_CACHE;
|
if (buf !== undefined) {
|
||||||
|
return buf;
|
||||||
} else {
|
} else {
|
||||||
self.__CACHE.__TX_BUF_CACHE = self.__CACHE.__TX.toBuffer();
|
c.__TX_BUF_CACHE = c.__TX.toBuffer();
|
||||||
return self.__CACHE.__TX_BUF_CACHE;
|
return c.__TX_BUF_CACHE;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
set(data: Buffer): void {
|
set(data: Buffer): void {
|
||||||
self.__CACHE.__TX_BUF_CACHE = data;
|
c.__TX_BUF_CACHE = data;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -118,8 +122,6 @@ export class Psbt extends PsbtBase {
|
||||||
enumerable,
|
enumerable,
|
||||||
writable,
|
writable,
|
||||||
});
|
});
|
||||||
dpew(this, '__TX', false, true);
|
|
||||||
dpew(this, '__EXTRACTED_TX', false, true);
|
|
||||||
dpew(this, '__CACHE', false, true);
|
dpew(this, '__CACHE', false, true);
|
||||||
dpew(this, 'opts', false, true);
|
dpew(this, 'opts', false, true);
|
||||||
}
|
}
|
||||||
|
@ -265,7 +267,6 @@ export class Psbt extends PsbtBase {
|
||||||
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput(
|
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput(
|
||||||
inputIndex,
|
inputIndex,
|
||||||
input,
|
input,
|
||||||
this.__CACHE.__TX,
|
|
||||||
this.__CACHE,
|
this.__CACHE,
|
||||||
);
|
);
|
||||||
if (!script) return false;
|
if (!script) return false;
|
||||||
|
@ -312,7 +313,6 @@ export class Psbt extends PsbtBase {
|
||||||
? getHashForSig(
|
? getHashForSig(
|
||||||
inputIndex,
|
inputIndex,
|
||||||
Object.assign({}, input, { sighashType: sig.hashType }),
|
Object.assign({}, input, { sighashType: sig.hashType }),
|
||||||
this.__CACHE.__TX,
|
|
||||||
this.__CACHE,
|
this.__CACHE,
|
||||||
)
|
)
|
||||||
: { hash: hashCache!, script: scriptCache! };
|
: { hash: hashCache!, script: scriptCache! };
|
||||||
|
@ -388,7 +388,6 @@ export class Psbt extends PsbtBase {
|
||||||
this.inputs,
|
this.inputs,
|
||||||
inputIndex,
|
inputIndex,
|
||||||
keyPair.publicKey,
|
keyPair.publicKey,
|
||||||
this.__CACHE.__TX,
|
|
||||||
this.__CACHE,
|
this.__CACHE,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -409,7 +408,6 @@ export class Psbt extends PsbtBase {
|
||||||
this.inputs,
|
this.inputs,
|
||||||
inputIndex,
|
inputIndex,
|
||||||
keyPair.publicKey,
|
keyPair.publicKey,
|
||||||
this.__CACHE.__TX,
|
|
||||||
this.__CACHE,
|
this.__CACHE,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -513,19 +511,13 @@ function getHashAndSighashType(
|
||||||
inputs: PsbtInput[],
|
inputs: PsbtInput[],
|
||||||
inputIndex: number,
|
inputIndex: number,
|
||||||
pubkey: Buffer,
|
pubkey: Buffer,
|
||||||
unsignedTx: Transaction,
|
|
||||||
cache: PsbtCache,
|
cache: PsbtCache,
|
||||||
): {
|
): {
|
||||||
hash: Buffer;
|
hash: Buffer;
|
||||||
sighashType: number;
|
sighashType: number;
|
||||||
} {
|
} {
|
||||||
const input = checkForInput(inputs, inputIndex);
|
const input = checkForInput(inputs, inputIndex);
|
||||||
const { hash, sighashType, script } = getHashForSig(
|
const { hash, sighashType, script } = getHashForSig(inputIndex, input, cache);
|
||||||
inputIndex,
|
|
||||||
input,
|
|
||||||
unsignedTx,
|
|
||||||
cache,
|
|
||||||
);
|
|
||||||
checkScriptForPubkey(pubkey, script, 'sign');
|
checkScriptForPubkey(pubkey, script, 'sign');
|
||||||
return {
|
return {
|
||||||
hash,
|
hash,
|
||||||
|
@ -678,9 +670,9 @@ interface HashForSigData {
|
||||||
function getHashForSig(
|
function getHashForSig(
|
||||||
inputIndex: number,
|
inputIndex: number,
|
||||||
input: PsbtInput,
|
input: PsbtInput,
|
||||||
unsignedTx: Transaction,
|
|
||||||
cache: PsbtCache,
|
cache: PsbtCache,
|
||||||
): HashForSigData {
|
): HashForSigData {
|
||||||
|
const unsignedTx = cache.__TX;
|
||||||
const sighashType = input.sighashType || Transaction.SIGHASH_ALL;
|
const sighashType = input.sighashType || Transaction.SIGHASH_ALL;
|
||||||
let hash: Buffer;
|
let hash: Buffer;
|
||||||
let script: Buffer;
|
let script: Buffer;
|
||||||
|
@ -831,9 +823,9 @@ interface GetScriptReturn {
|
||||||
function getScriptFromInput(
|
function getScriptFromInput(
|
||||||
inputIndex: number,
|
inputIndex: number,
|
||||||
input: PsbtInput,
|
input: PsbtInput,
|
||||||
unsignedTx: Transaction,
|
|
||||||
cache: PsbtCache,
|
cache: PsbtCache,
|
||||||
): GetScriptReturn {
|
): GetScriptReturn {
|
||||||
|
const unsignedTx = cache.__TX;
|
||||||
const res: GetScriptReturn = {
|
const res: GetScriptReturn = {
|
||||||
script: null,
|
script: null,
|
||||||
isSegwit: false,
|
isSegwit: false,
|
||||||
|
|
Loading…
Add table
Reference in a new issue