psbt: fix rendering for invalid long sighash type field

The PSBT sighash type field is a 32 bit unsigned integer in BIP 174,
signed in PSBTInput, and it is not validated when deserialized.

decodepsbt incorrectly truncates this field before looking up its
name. Fix that and add a test.
This commit is contained in:
Sjors Provoost
2026-08-28 11:43:48 +02:00
parent ea7d459ac0
commit 1fca81960a
4 changed files with 18 additions and 4 deletions

View File

@@ -338,9 +338,13 @@ const std::map<unsigned char, std::string> mapSigHashTypes = {
{static_cast<unsigned char>(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY), std::string("SINGLE|ANYONECANPAY")},
};
std::string SighashToStr(unsigned char sighash_type)
std::string SighashToStr(int32_t sighash_type)
{
const auto& it = mapSigHashTypes.find(sighash_type);
// Signatures encode the sighash type in a single byte, but the PSBT field
// for it is a 32 bit unsigned integer in BIP 174 (signed in PSBTInput)
if (sighash_type < 0 || sighash_type > 0xff) return "";
const uint8_t sighash_byte(sighash_type);
const auto& it = mapSigHashTypes.find(sighash_byte);
if (it == mapSigHashTypes.end()) return "";
return it->second;
}