Skip to content

Commit

Permalink
Merge pull request #4932 from BitGo/BTC-1351.utxo-bin-tweaks
Browse files Browse the repository at this point in the history
fix: handle signature parsing errors more gracefully
  • Loading branch information
OttoAllmendinger authored Sep 24, 2024
2 parents 76ffc3f + 0e73cd5 commit a87b33c
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions modules/utxo-bin/src/InputParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ export class InputParser extends Parser {
return [this.node('type', 'placeholder (0)')];
}

if (buf.length === 0) {
return [this.node('type', 'placeholder (empty Buffer)')];
}

const nodes = [this.node('bytes', buf)];
if (signerIndex !== undefined) {
nodes.push(this.node('valid', 0 <= signerIndex));
Expand Down Expand Up @@ -182,13 +186,17 @@ export class InputParser extends Parser {
const nodes = signedByLabels ? [this.node('signed by', `[${signedByLabels.join(', ')}]`)] : [];
if (this.params.parseSignatureData.ecdsa || this.params.parseSignatureData.schnorr) {
nodes.push(
...parsed.signatures.map((s: Buffer | 0, i: number) =>
this.node(
i,
undefined,
this.parseSignatureBuffer(parsed.scriptType, s, signerIndex ? signerIndex[i] : undefined)
)
)
...parsed.signatures.map((s: Buffer | 0, i: number) => {
try {
return this.node(
i,
undefined,
this.parseSignatureBuffer(parsed.scriptType, s, signerIndex ? signerIndex[i] : undefined)
);
} catch (e) {
return this.node(i, undefined, [this.node('buf', s), this.handleParseError(e)]);
}
})
);
}
return nodes;
Expand All @@ -210,13 +218,17 @@ export class InputParser extends Parser {
this.chainInfo.prevOutputs,
parsed.publicKeys
);
nodes.push(
...this.parseSignaturesWithSigners(
parsed,
signedBy.flatMap((v, i) => (v ? [i.toString()] : [])),
parsed.signatures.map((k: Buffer | 0) => (k === 0 ? -1 : signedBy.indexOf(k)))
)
);
try {
nodes.push(
...this.parseSignaturesWithSigners(
parsed,
signedBy.flatMap((v, i) => (v ? [i.toString()] : [])),
parsed.signatures.map((k: Buffer | 0) => (k === 0 ? -1 : signedBy.indexOf(k)))
)
);
} catch (e) {
nodes.push(this.node('parseSignaturesWithSigners', undefined, [this.handleParseError(e)]));
}
}
if (this.tx instanceof utxolib.bitgo.UtxoPsbt) {
let signedByLabels: string[] | undefined;
Expand Down Expand Up @@ -271,7 +283,7 @@ export class InputParser extends Parser {
try {
return this.parseSigScriptWithType(parseSignatureScript(this.tx, this.inputIndex, this.tx.network));
} catch (e) {
return this.handleParseError(e);
return this.node('sigScript', undefined, [this.handleParseError(e)]);
}
}

Expand Down

0 comments on commit a87b33c

Please sign in to comment.