Skip to content

Commit 7ef3fe4

Browse files
committed
Add sanity check for arguments
1 parent 41bf2cd commit 7ef3fe4

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/psbt.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ class Psbt {
143143
return this;
144144
}
145145
addInput(inputData) {
146+
if (
147+
arguments.length > 1 ||
148+
!inputData ||
149+
inputData.hash === undefined ||
150+
inputData.index === undefined
151+
) {
152+
throw new Error(
153+
`Invalid arguments for Psbt.addInput. ` +
154+
`Requires single object with at least [hash] and [index]`,
155+
);
156+
}
146157
checkInputsForPartialSig(this.data.inputs, 'addInput');
147158
const c = this.__CACHE;
148159
this.data.addInput(inputData);
@@ -163,6 +174,17 @@ class Psbt {
163174
return this;
164175
}
165176
addOutput(outputData) {
177+
if (
178+
arguments.length > 1 ||
179+
!outputData ||
180+
outputData.value === undefined ||
181+
(outputData.address === undefined && outputData.script === undefined)
182+
) {
183+
throw new Error(
184+
`Invalid arguments for Psbt.addOutput. ` +
185+
`Requires single object with at least [script or address] and [value]`,
186+
);
187+
}
166188
checkInputsForPartialSig(this.data.inputs, 'addOutput');
167189
const { address } = outputData;
168190
if (typeof address === 'string') {

test/fixtures/psbt.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311
"inputData": {
312312
"hash": 42
313313
},
314-
"exception": "Error adding input."
314+
"exception": "Invalid arguments for Psbt\\.addInput\\. Requires single object with at least \\[hash\\] and \\[index\\]"
315315
},
316316
{
317317
"description": "should be equal",

ts_src/psbt.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ export class Psbt {
182182
}
183183

184184
addInput(inputData: PsbtInputExtended): this {
185+
if (
186+
arguments.length > 1 ||
187+
!inputData ||
188+
inputData.hash === undefined ||
189+
inputData.index === undefined
190+
) {
191+
throw new Error(
192+
`Invalid arguments for Psbt.addInput. ` +
193+
`Requires single object with at least [hash] and [index]`,
194+
);
195+
}
185196
checkInputsForPartialSig(this.data.inputs, 'addInput');
186197
const c = this.__CACHE;
187198
this.data.addInput(inputData);
@@ -205,6 +216,18 @@ export class Psbt {
205216
}
206217

207218
addOutput(outputData: PsbtOutputExtended): this {
219+
if (
220+
arguments.length > 1 ||
221+
!outputData ||
222+
outputData.value === undefined ||
223+
((outputData as any).address === undefined &&
224+
(outputData as any).script === undefined)
225+
) {
226+
throw new Error(
227+
`Invalid arguments for Psbt.addOutput. ` +
228+
`Requires single object with at least [script or address] and [value]`,
229+
);
230+
}
208231
checkInputsForPartialSig(this.data.inputs, 'addOutput');
209232
const { address } = outputData as any;
210233
if (typeof address === 'string') {

0 commit comments

Comments
 (0)