From 7ef3fe49966fe54957533970f6356f29aafecf02 Mon Sep 17 00:00:00 2001 From: junderw Date: Tue, 1 Oct 2019 14:54:57 +0900 Subject: [PATCH] Add sanity check for arguments --- src/psbt.js | 22 ++++++++++++++++++++++ test/fixtures/psbt.json | 2 +- ts_src/psbt.ts | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/psbt.js b/src/psbt.js index 57a159c..708dd30 100644 --- a/src/psbt.js +++ b/src/psbt.js @@ -143,6 +143,17 @@ class Psbt { return this; } addInput(inputData) { + if ( + arguments.length > 1 || + !inputData || + inputData.hash === undefined || + inputData.index === undefined + ) { + throw new Error( + `Invalid arguments for Psbt.addInput. ` + + `Requires single object with at least [hash] and [index]`, + ); + } checkInputsForPartialSig(this.data.inputs, 'addInput'); const c = this.__CACHE; this.data.addInput(inputData); @@ -163,6 +174,17 @@ class Psbt { return this; } addOutput(outputData) { + if ( + arguments.length > 1 || + !outputData || + outputData.value === undefined || + (outputData.address === undefined && outputData.script === undefined) + ) { + throw new Error( + `Invalid arguments for Psbt.addOutput. ` + + `Requires single object with at least [script or address] and [value]`, + ); + } checkInputsForPartialSig(this.data.inputs, 'addOutput'); const { address } = outputData; if (typeof address === 'string') { diff --git a/test/fixtures/psbt.json b/test/fixtures/psbt.json index 95c980e..e3062e8 100644 --- a/test/fixtures/psbt.json +++ b/test/fixtures/psbt.json @@ -311,7 +311,7 @@ "inputData": { "hash": 42 }, - "exception": "Error adding input." + "exception": "Invalid arguments for Psbt\\.addInput\\. Requires single object with at least \\[hash\\] and \\[index\\]" }, { "description": "should be equal", diff --git a/ts_src/psbt.ts b/ts_src/psbt.ts index d35fd4c..0a159c9 100644 --- a/ts_src/psbt.ts +++ b/ts_src/psbt.ts @@ -182,6 +182,17 @@ export class Psbt { } addInput(inputData: PsbtInputExtended): this { + if ( + arguments.length > 1 || + !inputData || + inputData.hash === undefined || + inputData.index === undefined + ) { + throw new Error( + `Invalid arguments for Psbt.addInput. ` + + `Requires single object with at least [hash] and [index]`, + ); + } checkInputsForPartialSig(this.data.inputs, 'addInput'); const c = this.__CACHE; this.data.addInput(inputData); @@ -205,6 +216,18 @@ export class Psbt { } addOutput(outputData: PsbtOutputExtended): this { + if ( + arguments.length > 1 || + !outputData || + outputData.value === undefined || + ((outputData as any).address === undefined && + (outputData as any).script === undefined) + ) { + throw new Error( + `Invalid arguments for Psbt.addOutput. ` + + `Requires single object with at least [script or address] and [value]`, + ); + } checkInputsForPartialSig(this.data.inputs, 'addOutput'); const { address } = outputData as any; if (typeof address === 'string') {