Check signatures for sighash type before finalize

This commit is contained in:
junderw 2019-07-10 10:19:26 +09:00
parent 0d9fa87943
commit fa897cf78e
No known key found for this signature in database
GPG key ID: B256185D3A971908
2 changed files with 24 additions and 0 deletions

View file

@ -216,6 +216,7 @@ class Psbt extends bip174_1.Psbt {
const scriptType = classifyScript(script);
if (!canFinalize(input, script, scriptType))
throw new Error(`Can not finalize input #${inputIndex}`);
checkPartialSigSighashes(input);
const { finalScriptSig, finalScriptWitness } = getFinalScripts(
script,
scriptType,
@ -446,6 +447,16 @@ function checkInputsForPartialSig(inputs, action) {
}
});
}
function checkPartialSigSighashes(input) {
if (!input.sighashType || !input.partialSig) return;
const { partialSig, sighashType } = input;
partialSig.forEach(pSig => {
const { hashType } = bscript.signature.decode(pSig.signature);
if (sighashType !== hashType) {
throw new Error('Signature sighash does not match input sighash type');
}
});
}
function checkScriptForPubkey(pubkey, script, action) {
const pubkeyHash = crypto_1.hash160(pubkey);
const decompiled = bscript.decompile(script);

View file

@ -269,6 +269,8 @@ export class Psbt extends PsbtBase {
if (!canFinalize(input, script, scriptType))
throw new Error(`Can not finalize input #${inputIndex}`);
checkPartialSigSighashes(input);
const { finalScriptSig, finalScriptWitness } = getFinalScripts(
script,
scriptType,
@ -547,6 +549,17 @@ function checkInputsForPartialSig(inputs: PsbtInput[], action: string): void {
});
}
function checkPartialSigSighashes(input: PsbtInput): void {
if (!input.sighashType || !input.partialSig) return;
const { partialSig, sighashType } = input;
partialSig.forEach(pSig => {
const { hashType } = bscript.signature.decode(pSig.signature);
if (sighashType !== hashType) {
throw new Error('Signature sighash does not match input sighash type');
}
});
}
function checkScriptForPubkey(
pubkey: Buffer,
script: Buffer,