Skip to content

Commit 15ce1bd

Browse files
committed
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.
1 parent 1f71cd3 commit 15ce1bd

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/psbt.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,26 @@ PSBTError SignPSBTInput(const SigningProvider& provider, PartiallySignedTransact
425425
return PSBTError::SIGHASH_MISMATCH;
426426
}
427427

428+
// Check all existing signatures use the sighash type
429+
if (sighash == SIGHASH_DEFAULT) {
430+
if (!input.m_tap_key_sig.empty() && input.m_tap_key_sig.size() != 64) {
431+
return PSBTError::SIGHASH_MISMATCH;
432+
}
433+
for (const auto& [_, sig] : input.m_tap_script_sigs) {
434+
if (sig.size() != 64) return PSBTError::SIGHASH_MISMATCH;
435+
}
436+
} else {
437+
if (!input.m_tap_key_sig.empty() && (input.m_tap_key_sig.size() != 65 || input.m_tap_key_sig.back() != *sighash)) {
438+
return PSBTError::SIGHASH_MISMATCH;
439+
}
440+
for (const auto& [_, sig] : input.m_tap_script_sigs) {
441+
if (sig.size() != 65 || sig.back() != *sighash) return PSBTError::SIGHASH_MISMATCH;
442+
}
443+
for (const auto& [_, sig] : input.partial_sigs) {
444+
if (sig.second.back() != *sighash) return PSBTError::SIGHASH_MISMATCH;
445+
}
446+
}
447+
428448
sigdata.witness = false;
429449
bool sig_complete;
430450
if (txdata == nullptr) {

0 commit comments

Comments
 (0)