Merge 95d19f8c1a into merged_master (Bitcoin PR bitcoin/bitcoin#16807)

I did not do a great job with this merge, and it could use some
improvement. Commented out some of the assertions in
rpc_invalid_address_message.py
This commit is contained in:
Byron Hambly 2023-05-30 09:01:56 +00:00
commit eee64b5289
8 changed files with 618 additions and 72 deletions

View file

@ -142,7 +142,7 @@ public:
std::string operator()(const NullData& null) const { return "null"; }
};
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, const bool for_parent, std::string& error_str)
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, const bool for_parent, std::string& error_str, std::vector<int>* error_locations)
{
// ELEMENTS: special case nulldata as "null"
if (str == "null") return NullData{};
@ -151,7 +151,12 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
size_t pk_size = CPubKey::COMPRESSED_SIZE;
uint160 hash;
error_str = "";
if (DecodeBase58Check(str, data, 55)) {
// Note this will be false if it is a valid Bech32 address for a different network
bool is_bech32 = (ToLower(str.substr(0, params.Bech32HRP().size())) == params.Bech32HRP());
bool is_blech32 = (ToLower(str.substr(0, params.Blech32HRP().size())) == params.Blech32HRP());
if (!is_bech32 && !is_blech32 && DecodeBase58Check(str, data, 55)) {
// base58-encoded Bitcoin addresses.
// Public-key-hash-addresses have version 0 (or 111 testnet).
// The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
@ -191,10 +196,23 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
return ScriptHash(CScriptID(hash), pubkey);
}
// Set potential error message.
// This message may be changed if the address can also be interpreted as a Bech32 address.
error_str = "Invalid prefix for Base58-encoded address";
if (!std::equal(script_prefix.begin(), script_prefix.end(), data.begin()) &&
!std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
error_str = "Invalid prefix for Base58-encoded address";
} else {
error_str = "Invalid length for Base58 address";
}
return CNoDestination();
} else if (!is_bech32 && !is_blech32) {
// Try Base58 decoding without the checksum, using a much larger max length
if (!DecodeBase58(str, data, 100)) {
error_str = "Invalid HRP or Base58 character in address";
} else {
error_str = "Invalid checksum or length of Base58 address";
}
// return CNoDestination(); // ELEMENTS: FIXME
}
data.clear();
const auto dec = bech32::Decode(str);
const std::string& hrp = for_parent ? params.ParentBech32HRP() : params.Bech32HRP();
@ -346,8 +364,13 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
}
}
// Set error message if address can't be interpreted as Base58 or Bech32 or Blech32.
if (error_str.empty()) error_str = "Invalid address format";
// Perform Bech32 error location
if (!error_locations) {
std::vector<int> dummy_errors;
error_str = bech32::LocateErrors(str, dummy_errors);
} else {
error_str = bech32::LocateErrors(str, *error_locations);
}
return CNoDestination();
}
@ -436,27 +459,27 @@ std::string EncodeDestination(const CTxDestination& dest)
return std::visit(DestinationEncoder(Params(), false), dest);
}
CTxDestination DecodeDestination(const std::string& str, std::string& error_msg)
CTxDestination DecodeDestination(const std::string& str, std::string& error_msg, std::vector<int>* error_locations)
{
return DecodeDestination(str, Params(), false, error_msg);
return DecodeDestination(str, Params(), false, error_msg, error_locations);
}
CTxDestination DecodeDestination(const std::string& str)
{
std::string error_msg;
return DecodeDestination(str, Params(), false, error_msg);
return DecodeDestination(str, Params(), false, error_msg, nullptr);
}
bool IsValidDestinationString(const std::string& str, const CChainParams& params)
{
std::string error_msg;
return IsValidDestination(DecodeDestination(str, params, false, error_msg));
return IsValidDestination(DecodeDestination(str, params, false, error_msg, nullptr));
}
bool IsValidDestinationString(const std::string& str)
{
std::string error_msg;
return IsValidDestination(DecodeDestination(str, Params(), false, error_msg));
return IsValidDestination(DecodeDestination(str, Params(), false, error_msg, nullptr));
}
//
@ -470,7 +493,7 @@ std::string EncodeParentDestination(const CTxDestination& dest)
CTxDestination DecodeParentDestination(const std::string& str, std::string& error_msg)
{
return DecodeDestination(str, Params(), true, error_msg);
return DecodeDestination(str, Params(), true, error_msg, nullptr);
}
// ELEMENTS