Browse Source

Merge pull request #1487 from bitcoinjs/addError

Add sanity check for arguments
psbt-tx-getters
d-yokoi 5 years ago
committed by GitHub
parent
commit
3f6f5ef97a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      src/psbt.js
  2. 2
      test/fixtures/psbt.json
  3. 23
      ts_src/psbt.ts

22
src/psbt.js

@ -143,6 +143,17 @@ class Psbt {
return this; return this;
} }
addInput(inputData) { 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'); checkInputsForPartialSig(this.data.inputs, 'addInput');
const c = this.__CACHE; const c = this.__CACHE;
this.data.addInput(inputData); this.data.addInput(inputData);
@ -163,6 +174,17 @@ class Psbt {
return this; return this;
} }
addOutput(outputData) { 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'); checkInputsForPartialSig(this.data.inputs, 'addOutput');
const { address } = outputData; const { address } = outputData;
if (typeof address === 'string') { if (typeof address === 'string') {

2
test/fixtures/psbt.json

@ -311,7 +311,7 @@
"inputData": { "inputData": {
"hash": 42 "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", "description": "should be equal",

23
ts_src/psbt.ts

@ -182,6 +182,17 @@ export class Psbt {
} }
addInput(inputData: PsbtInputExtended): this { 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'); checkInputsForPartialSig(this.data.inputs, 'addInput');
const c = this.__CACHE; const c = this.__CACHE;
this.data.addInput(inputData); this.data.addInput(inputData);
@ -205,6 +216,18 @@ export class Psbt {
} }
addOutput(outputData: PsbtOutputExtended): this { 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'); checkInputsForPartialSig(this.data.inputs, 'addOutput');
const { address } = outputData as any; const { address } = outputData as any;
if (typeof address === 'string') { if (typeof address === 'string') {

Loading…
Cancel
Save