Refactor: externalize outputAdder

This commit is contained in:
junderw 2019-07-09 11:57:50 +09:00
parent e4e5111376
commit 497d048ebf
No known key found for this signature in database
GPG key ID: B256185D3A971908
2 changed files with 57 additions and 47 deletions

View file

@ -134,10 +134,11 @@ class Psbt extends bip174_1.Psbt {
}
addInput(inputData) {
checkInputsForPartialSig(this.inputs, 'addInput');
const inputAdder = getInputAdder(this.__CACHE);
const c = this.__CACHE;
const inputAdder = getInputAdder(c);
super.addInput(inputData, inputAdder);
this.__CACHE.__FEE_RATE = undefined;
this.__CACHE.__EXTRACTED_TX = undefined;
c.__FEE_RATE = undefined;
c.__EXTRACTED_TX = undefined;
return this;
}
addOutput(outputData) {
@ -148,26 +149,11 @@ class Psbt extends bip174_1.Psbt {
const script = address_1.toOutputScript(address, network);
outputData = Object.assign(outputData, { script });
}
const self = this;
const outputAdder = (_outputData, txBuf) => {
if (
!txBuf ||
_outputData.script === undefined ||
_outputData.value === undefined ||
!Buffer.isBuffer(_outputData.script) ||
typeof _outputData.value !== 'number'
) {
throw new Error('Error adding output.');
}
self.__CACHE.__TX.outs.push({
script: _outputData.script,
value: _outputData.value,
});
return self.__CACHE.__TX.toBuffer();
};
const c = this.__CACHE;
const outputAdder = getOutputAdder(c);
super.addOutput(outputData, true, outputAdder);
this.__CACHE.__FEE_RATE = undefined;
this.__CACHE.__EXTRACTED_TX = undefined;
c.__FEE_RATE = undefined;
c.__EXTRACTED_TX = undefined;
return this;
}
addNonWitnessUtxoToInput(inputIndex, nonWitnessUtxo) {
@ -854,6 +840,25 @@ function getInputAdder(cache) {
return selfCache.__TX.toBuffer();
};
}
function getOutputAdder(cache) {
const selfCache = cache;
return (_outputData, txBuf) => {
if (
!txBuf ||
_outputData.script === undefined ||
_outputData.value === undefined ||
!Buffer.isBuffer(_outputData.script) ||
typeof _outputData.value !== 'number'
) {
throw new Error('Error adding output.');
}
selfCache.__TX.outs.push({
script: _outputData.script,
value: _outputData.value,
});
return selfCache.__TX.toBuffer();
};
}
function check32Bit(num) {
if (
typeof num !== 'number' ||

View file

@ -168,10 +168,11 @@ export class Psbt extends PsbtBase {
addInput(inputData: TransactionInput): this {
checkInputsForPartialSig(this.inputs, 'addInput');
const inputAdder = getInputAdder(this.__CACHE);
const c = this.__CACHE;
const inputAdder = getInputAdder(c);
super.addInput(inputData, inputAdder);
this.__CACHE.__FEE_RATE = undefined;
this.__CACHE.__EXTRACTED_TX = undefined;
c.__FEE_RATE = undefined;
c.__EXTRACTED_TX = undefined;
return this;
}
@ -183,29 +184,11 @@ export class Psbt extends PsbtBase {
const script = toOutputScript(address, network);
outputData = Object.assign(outputData, { script });
}
const self = this;
const outputAdder = (
_outputData: TransactionOutput,
txBuf: Buffer,
): Buffer => {
if (
!txBuf ||
(_outputData as any).script === undefined ||
(_outputData as any).value === undefined ||
!Buffer.isBuffer((_outputData as any).script) ||
typeof (_outputData as any).value !== 'number'
) {
throw new Error('Error adding output.');
}
self.__CACHE.__TX.outs.push({
script: (_outputData as any).script!,
value: _outputData.value,
});
return self.__CACHE.__TX.toBuffer();
};
const c = this.__CACHE;
const outputAdder = getOutputAdder(c);
super.addOutput(outputData, true, outputAdder);
this.__CACHE.__FEE_RATE = undefined;
this.__CACHE.__EXTRACTED_TX = undefined;
c.__FEE_RATE = undefined;
c.__EXTRACTED_TX = undefined;
return this;
}
@ -1070,6 +1053,28 @@ function getInputAdder(
};
}
function getOutputAdder(
cache: PsbtCache,
): (_outputData: TransactionOutput, txBuf: Buffer) => Buffer {
const selfCache = cache;
return (_outputData: TransactionOutput, txBuf: Buffer): Buffer => {
if (
!txBuf ||
(_outputData as any).script === undefined ||
(_outputData as any).value === undefined ||
!Buffer.isBuffer((_outputData as any).script) ||
typeof (_outputData as any).value !== 'number'
) {
throw new Error('Error adding output.');
}
selfCache.__TX.outs.push({
script: (_outputData as any).script!,
value: _outputData.value,
});
return selfCache.__TX.toBuffer();
};
}
function check32Bit(num: number): void {
if (
typeof num !== 'number' ||