merge bitcoin#23718: hash preimages fields

This commit is contained in:
Kittywhiskers Van Gogh 2021-12-05 13:07:54 +01:00
parent dc9342b6d9
commit e4530e68c3
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
4 changed files with 178 additions and 2 deletions

View File

@ -137,6 +137,10 @@ void PSBTInput::Merge(const PSBTInput& input)
if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo;
partial_sigs.insert(input.partial_sigs.begin(), input.partial_sigs.end());
ripemd160_preimages.insert(input.ripemd160_preimages.begin(), input.ripemd160_preimages.end());
sha256_preimages.insert(input.sha256_preimages.begin(), input.sha256_preimages.end());
hash160_preimages.insert(input.hash160_preimages.begin(), input.hash160_preimages.end());
hash256_preimages.insert(input.hash256_preimages.begin(), input.hash256_preimages.end());
hd_keypaths.insert(input.hd_keypaths.begin(), input.hd_keypaths.end());
unknown.insert(input.unknown.begin(), input.unknown.end());

View File

@ -34,6 +34,10 @@ static constexpr uint8_t PSBT_IN_SIGHASH = 0x03;
static constexpr uint8_t PSBT_IN_REDEEMSCRIPT = 0x04;
static constexpr uint8_t PSBT_IN_BIP32_DERIVATION = 0x06;
static constexpr uint8_t PSBT_IN_SCRIPTSIG = 0x07;
static constexpr uint8_t PSBT_IN_RIPEMD160 = 0x0A;
static constexpr uint8_t PSBT_IN_SHA256 = 0x0B;
static constexpr uint8_t PSBT_IN_HASH160 = 0x0C;
static constexpr uint8_t PSBT_IN_HASH256 = 0x0D;
static constexpr uint8_t PSBT_IN_PROPRIETARY = 0xFC;
// Output types
@ -164,6 +168,10 @@ struct PSBTInput
CScript final_script_sig;
std::map<CPubKey, KeyOriginInfo> hd_keypaths;
std::map<CKeyID, SigPair> partial_sigs;
std::map<uint160, std::vector<unsigned char>> ripemd160_preimages;
std::map<uint256, std::vector<unsigned char>> sha256_preimages;
std::map<uint160, std::vector<unsigned char>> hash160_preimages;
std::map<uint256, std::vector<unsigned char>> hash256_preimages;
std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
std::set<PSBTProprietary> m_proprietary;
std::optional<int> sighash_type;
@ -203,6 +211,30 @@ struct PSBTInput
// Write any hd keypaths
SerializeHDKeypaths(s, hd_keypaths, CompactSizeWriter(PSBT_IN_BIP32_DERIVATION));
// Write any ripemd160 preimage
for (const auto& [hash, preimage] : ripemd160_preimages) {
SerializeToVector(s, CompactSizeWriter(PSBT_IN_RIPEMD160), Span{hash});
s << preimage;
}
// Write any sha256 preimage
for (const auto& [hash, preimage] : sha256_preimages) {
SerializeToVector(s, CompactSizeWriter(PSBT_IN_SHA256), Span{hash});
s << preimage;
}
// Write any hash160 preimage
for (const auto& [hash, preimage] : hash160_preimages) {
SerializeToVector(s, CompactSizeWriter(PSBT_IN_HASH160), Span{hash});
s << preimage;
}
// Write any hash256 preimage
for (const auto& [hash, preimage] : hash256_preimages) {
SerializeToVector(s, CompactSizeWriter(PSBT_IN_HASH256), Span{hash});
s << preimage;
}
}
// Write script sig
@ -318,6 +350,90 @@ struct PSBTInput
s >> final_script_sig;
break;
}
case PSBT_IN_RIPEMD160:
{
// Make sure that the key is the size of a ripemd160 hash + 1
if (key.size() != CRIPEMD160::OUTPUT_SIZE + 1) {
throw std::ios_base::failure("Size of key was not the expected size for the type ripemd160 preimage");
}
// Read in the hash from key
std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
uint160 hash(hash_vec);
if (ripemd160_preimages.count(hash) > 0) {
throw std::ios_base::failure("Duplicate Key, input ripemd160 preimage already provided");
}
// Read in the preimage from value
std::vector<unsigned char> preimage;
s >> preimage;
// Add to preimages list
ripemd160_preimages.emplace(hash, std::move(preimage));
break;
}
case PSBT_IN_SHA256:
{
// Make sure that the key is the size of a sha256 hash + 1
if (key.size() != CSHA256::OUTPUT_SIZE + 1) {
throw std::ios_base::failure("Size of key was not the expected size for the type sha256 preimage");
}
// Read in the hash from key
std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
uint256 hash(hash_vec);
if (sha256_preimages.count(hash) > 0) {
throw std::ios_base::failure("Duplicate Key, input sha256 preimage already provided");
}
// Read in the preimage from value
std::vector<unsigned char> preimage;
s >> preimage;
// Add to preimages list
sha256_preimages.emplace(hash, std::move(preimage));
break;
}
case PSBT_IN_HASH160:
{
// Make sure that the key is the size of a hash160 hash + 1
if (key.size() != CHash160::OUTPUT_SIZE + 1) {
throw std::ios_base::failure("Size of key was not the expected size for the type hash160 preimage");
}
// Read in the hash from key
std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
uint160 hash(hash_vec);
if (hash160_preimages.count(hash) > 0) {
throw std::ios_base::failure("Duplicate Key, input hash160 preimage already provided");
}
// Read in the preimage from value
std::vector<unsigned char> preimage;
s >> preimage;
// Add to preimages list
hash160_preimages.emplace(hash, std::move(preimage));
break;
}
case PSBT_IN_HASH256:
{
// Make sure that the key is the size of a hash256 hash + 1
if (key.size() != CHash256::OUTPUT_SIZE + 1) {
throw std::ios_base::failure("Size of key was not the expected size for the type hash256 preimage");
}
// Read in the hash from key
std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
uint256 hash(hash_vec);
if (hash256_preimages.count(hash) > 0) {
throw std::ios_base::failure("Duplicate Key, input hash256 preimage already provided");
}
// Read in the preimage from value
std::vector<unsigned char> preimage;
s >> preimage;
// Add to preimages list
hash256_preimages.emplace(hash, std::move(preimage));
break;
}
case PSBT_IN_PROPRIETARY:
{
PSBTProprietary this_prop;

View File

@ -1384,7 +1384,23 @@ static RPCHelpMan decodepsbt()
{RPCResult::Type::STR, "asm", "The asm"},
{RPCResult::Type::STR, "hex", "The hex"},
}},
{RPCResult::Type::OBJ_DYN, "unknown", "The unknown global fields",
{RPCResult::Type::OBJ_DYN, "ripemd160_preimages", /*optional=*/ true, "",
{
{RPCResult::Type::STR, "hash", "The hash and preimage that corresponds to it."},
}},
{RPCResult::Type::OBJ_DYN, "sha256_preimages", /*optional=*/ true, "",
{
{RPCResult::Type::STR, "hash", "The hash and preimage that corresponds to it."},
}},
{RPCResult::Type::OBJ_DYN, "hash160_preimages", /*optional=*/ true, "",
{
{RPCResult::Type::STR, "hash", "The hash and preimage that corresponds to it."},
}},
{RPCResult::Type::OBJ_DYN, "hash256_preimages", /*optional=*/ true, "",
{
{RPCResult::Type::STR, "hash", "The hash and preimage that corresponds to it."},
}},
{RPCResult::Type::OBJ_DYN, "unknown", /*optional=*/ true, "The unknown input fields",
{
{RPCResult::Type::STR_HEX, "key", "(key-value pair) An unknown key-value pair"},
}},
@ -1571,6 +1587,42 @@ static RPCHelpMan decodepsbt()
in.pushKV("final_scriptSig", scriptsig);
}
// Ripemd160 hash preimages
if (!input.ripemd160_preimages.empty()) {
UniValue ripemd160_preimages(UniValue::VOBJ);
for (const auto& [hash, preimage] : input.ripemd160_preimages) {
ripemd160_preimages.pushKV(HexStr(hash), HexStr(preimage));
}
in.pushKV("ripemd160_preimages", ripemd160_preimages);
}
// Sha256 hash preimages
if (!input.sha256_preimages.empty()) {
UniValue sha256_preimages(UniValue::VOBJ);
for (const auto& [hash, preimage] : input.sha256_preimages) {
sha256_preimages.pushKV(HexStr(hash), HexStr(preimage));
}
in.pushKV("sha256_preimages", sha256_preimages);
}
// Hash160 hash preimages
if (!input.hash160_preimages.empty()) {
UniValue hash160_preimages(UniValue::VOBJ);
for (const auto& [hash, preimage] : input.hash160_preimages) {
hash160_preimages.pushKV(HexStr(hash), HexStr(preimage));
}
in.pushKV("hash160_preimages", hash160_preimages);
}
// Hash256 hash preimages
if (!input.hash256_preimages.empty()) {
UniValue hash256_preimages(UniValue::VOBJ);
for (const auto& [hash, preimage] : input.hash256_preimages) {
hash256_preimages.pushKV(HexStr(hash), HexStr(preimage));
}
in.pushKV("hash256_preimages", hash256_preimages);
}
// Proprietary
if (!input.m_proprietary.empty()) {
UniValue proprietary(UniValue::VARR);

View File

@ -39,7 +39,11 @@
"cHNidP8BAHUCAAAAASaBcTce3/KF6Tet7qSze3gADAVmy7OtZGQXE8pCFxv2AAAAAAD+////AtPf9QUAAAAAGXapFNDFmQPFusKGh2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