Fix: P2WPKH was signing with nonWitnessUtxo

This commit is contained in:
junderw 2019-07-11 11:28:09 +09:00
commit 2f1609b918
No known key found for this signature in database
GPG key ID: B256185D3A971908
3 changed files with 29 additions and 21 deletions
ts_src

View file

@ -509,6 +509,7 @@ const isP2MS = isPaymentFactory(payments.p2ms);
const isP2PK = isPaymentFactory(payments.p2pk);
const isP2PKH = isPaymentFactory(payments.p2pkh);
const isP2WPKH = isPaymentFactory(payments.p2wpkh);
const isP2WSHScript = isPaymentFactory(payments.p2wsh);
function check32Bit(num: number): void {
if (
@ -764,19 +765,18 @@ function getHashForSig(
// If a redeemScript is provided, the scriptPubKey must be for that redeemScript
checkRedeemScript(inputIndex, prevout.script, input.redeemScript);
script = input.redeemScript;
hash = unsignedTx.hashForSignature(
inputIndex,
input.redeemScript,
sighashType,
);
} else {
script = prevout.script;
hash = unsignedTx.hashForSignature(
inputIndex,
prevout.script,
sighashType,
}
if (isP2WPKH(script) || isP2WSHScript(script)) {
throw new Error(
`Input #${inputIndex} has nonWitnessUtxo but segwit script: ` +
`${script.toString('hex')}`,
);
}
hash = unsignedTx.hashForSignature(inputIndex, script, sighashType);
} else if (input.witnessUtxo) {
let _script: Buffer; // so we don't shadow the `let script` above
if (input.redeemScript) {
@ -800,7 +800,7 @@ function getHashForSig(
sighashType,
);
script = _script;
} else {
} else if (isP2WSHScript(_script)) {
if (!input.witnessScript)
throw new Error('Segwit input needs witnessScript if not P2WPKH');
checkWitnessScript(inputIndex, _script, input.witnessScript);
@ -812,6 +812,11 @@ function getHashForSig(
);
// want to make sure the script we return is the actual meaningful script
script = input.witnessScript;
} else {
throw new Error(
`Input #${inputIndex} has witnessUtxo but non-segwit script: ` +
`${_script.toString('hex')}`,
);
}
} else {
throw new Error('Need a Utxo input item for signing');