aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Chow <github@achow101.com>2023-05-24 12:02:14 -0400
committerAndrew Chow <github@achow101.com>2023-05-24 12:10:55 -0400
commita13f3746dccd9c4ec16d6bfe9b33ebd26e3238e1 (patch)
tree601ece66aa7b6097a1bd4de129f829d3ad835bae /src
parent51c050787fd6bcd016969dd7e245818ebd110b67 (diff)
parenteeee55f9288740747b6e8d806ce8177fd92635cf (diff)
Merge bitcoin/bitcoin#27727: rpc: Fix invalid bech32 address handling
eeee55f9288740747b6e8d806ce8177fd92635cf rpc: Fix invalid bech32 handling (MarcoFalke) Pull request description: Currently the handling of invalid bech32(m) addresses over RPC has many issues: * No error for invalid addresses is reported, leading to internal bugs via `CHECK_NONFATAL`, see https://github.com/bitcoin/bitcoin/issues/27723 * The error messages use "data size" (the meaning of which is unclear to the user, because the witness program data and bech32 section data are related but different) when they mean "program size" Fix all issues. Also, use the BIP 173 and BIP 350 test vectors. ACKs for top commit: achow101: ACK eeee55f9288740747b6e8d806ce8177fd92635cf brunoerg: crACK eeee55f9288740747b6e8d806ce8177fd92635cf Tree-SHA512: c8639ee49e2a54b740b72d66bc4a40352dd553a6e3220dea9f94e48e33124f21f597a2817cb405d0a4c88d21df1013c0a4877a01370a2d326aa2cff1f9c381a8
Diffstat (limited to 'src')
-rw-r--r--src/key_io.cpp13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/key_io.cpp b/src/key_io.cpp
index 4659a59544..33499b0d23 100644
--- a/src/key_io.cpp
+++ b/src/key_io.cpp
@@ -124,7 +124,11 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
data.clear();
const auto dec = bech32::Decode(str);
- if ((dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) && dec.data.size() > 0) {
+ if (dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) {
+ if (dec.data.empty()) {
+ error_str = "Empty Bech32 data section";
+ return CNoDestination();
+ }
// Bech32 decoding
if (dec.hrp != params.Bech32HRP()) {
error_str = strprintf("Invalid or unsupported prefix for Segwit (Bech32) address (expected %s, got %s).", params.Bech32HRP(), dec.hrp);
@@ -158,7 +162,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
}
}
- error_str = "Invalid Bech32 v0 address data size";
+ error_str = strprintf("Invalid Bech32 v0 address program size (%s byte), per BIP141", data.size());
return CNoDestination();
}
@@ -175,7 +179,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
}
if (data.size() < 2 || data.size() > BECH32_WITNESS_PROG_MAX_LEN) {
- error_str = "Invalid Bech32 address data size";
+ error_str = strprintf("Invalid Bech32 address program size (%s byte)", data.size());
return CNoDestination();
}
@@ -184,6 +188,9 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
std::copy(data.begin(), data.end(), unk.program);
unk.length = data.size();
return unk;
+ } else {
+ error_str = strprintf("Invalid padding in Bech32 data section");
+ return CNoDestination();
}
}