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) { addInput(inputData) {
checkInputsForPartialSig(this.inputs, 'addInput'); checkInputsForPartialSig(this.inputs, 'addInput');
const inputAdder = getInputAdder(this.__CACHE); const c = this.__CACHE;
const inputAdder = getInputAdder(c);
super.addInput(inputData, inputAdder); super.addInput(inputData, inputAdder);
this.__CACHE.__FEE_RATE = undefined; c.__FEE_RATE = undefined;
this.__CACHE.__EXTRACTED_TX = undefined; c.__EXTRACTED_TX = undefined;
return this; return this;
} }
addOutput(outputData) { addOutput(outputData) {
@ -148,26 +149,11 @@ class Psbt extends bip174_1.Psbt {
const script = address_1.toOutputScript(address, network); const script = address_1.toOutputScript(address, network);
outputData = Object.assign(outputData, { script }); outputData = Object.assign(outputData, { script });
} }
const self = this; const c = this.__CACHE;
const outputAdder = (_outputData, txBuf) => { const outputAdder = getOutputAdder(c);
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();
};
super.addOutput(outputData, true, outputAdder); super.addOutput(outputData, true, outputAdder);
this.__CACHE.__FEE_RATE = undefined; c.__FEE_RATE = undefined;
this.__CACHE.__EXTRACTED_TX = undefined; c.__EXTRACTED_TX = undefined;
return this; return this;
} }
addNonWitnessUtxoToInput(inputIndex, nonWitnessUtxo) { addNonWitnessUtxoToInput(inputIndex, nonWitnessUtxo) {
@ -854,6 +840,25 @@ function getInputAdder(cache) {
return selfCache.__TX.toBuffer(); 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) { function check32Bit(num) {
if ( if (
typeof num !== 'number' || typeof num !== 'number' ||

View file

@ -168,10 +168,11 @@ export class Psbt extends PsbtBase {
addInput(inputData: TransactionInput): this { addInput(inputData: TransactionInput): this {
checkInputsForPartialSig(this.inputs, 'addInput'); checkInputsForPartialSig(this.inputs, 'addInput');
const inputAdder = getInputAdder(this.__CACHE); const c = this.__CACHE;
const inputAdder = getInputAdder(c);
super.addInput(inputData, inputAdder); super.addInput(inputData, inputAdder);
this.__CACHE.__FEE_RATE = undefined; c.__FEE_RATE = undefined;
this.__CACHE.__EXTRACTED_TX = undefined; c.__EXTRACTED_TX = undefined;
return this; return this;
} }
@ -183,29 +184,11 @@ export class Psbt extends PsbtBase {
const script = toOutputScript(address, network); const script = toOutputScript(address, network);
outputData = Object.assign(outputData, { script }); outputData = Object.assign(outputData, { script });
} }
const self = this; const c = this.__CACHE;
const outputAdder = ( const outputAdder = getOutputAdder(c);
_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();
};
super.addOutput(outputData, true, outputAdder); super.addOutput(outputData, true, outputAdder);
this.__CACHE.__FEE_RATE = undefined; c.__FEE_RATE = undefined;
this.__CACHE.__EXTRACTED_TX = undefined; c.__EXTRACTED_TX = undefined;
return this; 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 { function check32Bit(num: number): void {
if ( if (
typeof num !== 'number' || typeof num !== 'number' ||