psbt: Enforce sighash type of signatures matches psbt

BIP 174 states that the sighash type of all signatures must match the
type given by the PSBT, so do that.
This commit is contained in:
Ava Chow
2025-03-04 09:05:42 -08:00
parent 1f71cd337a
commit 15ce1bd73f

View File

@@ -425,6 +425,26 @@ PSBTError SignPSBTInput(const SigningProvider& provider, PartiallySignedTransact
return PSBTError::SIGHASH_MISMATCH;
}
// Check all existing signatures use the sighash type
if (sighash == SIGHASH_DEFAULT) {
if (!input.m_tap_key_sig.empty() && input.m_tap_key_sig.size() != 64) {
return PSBTError::SIGHASH_MISMATCH;
}
for (const auto& [_, sig] : input.m_tap_script_sigs) {
if (sig.size() != 64) return PSBTError::SIGHASH_MISMATCH;
}
} else {
if (!input.m_tap_key_sig.empty() && (input.m_tap_key_sig.size() != 65 || input.m_tap_key_sig.back() != *sighash)) {
return PSBTError::SIGHASH_MISMATCH;
}
for (const auto& [_, sig] : input.m_tap_script_sigs) {
if (sig.size() != 65 || sig.back() != *sighash) return PSBTError::SIGHASH_MISMATCH;
}
for (const auto& [_, sig] : input.partial_sigs) {
if (sig.second.back() != *sighash) return PSBTError::SIGHASH_MISMATCH;
}
}
sigdata.witness = false;
bool sig_complete;
if (txdata == nullptr) {