aboutsummaryrefslogtreecommitdiff
path: root/src/key_io.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter@wuille.net>2021-01-05 13:36:42 -0800
committerPieter Wuille <pieter@wuille.net>2021-03-16 10:48:36 -0700
commitfe5e495c31de47b0ec732b943db11fe345d874af (patch)
tree751ecfb1e191633e0a2f36b01f1ad0d31680a947 /src/key_io.cpp
parent25b1c6e13ddf1626210d5e3d37298d1f3a78a94f (diff)
downloadbitcoin-fe5e495c31de47b0ec732b943db11fe345d874af.tar.xz
Use Bech32m encoding for v1+ segwit addresses
This also includes updates to the Python test framework implementation, test vectors, and release notes.
Diffstat (limited to 'src/key_io.cpp')
-rw-r--r--src/key_io.cpp12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/key_io.cpp b/src/key_io.cpp
index 3b15c9c3b9..dbcbfa1f29 100644
--- a/src/key_io.cpp
+++ b/src/key_io.cpp
@@ -62,7 +62,7 @@ public:
std::vector<unsigned char> data = {(unsigned char)id.version};
data.reserve(1 + (id.length * 8 + 4) / 5);
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.program, id.program + id.length);
- return bech32::Encode(bech32::Encoding::BECH32, m_params.Bech32HRP(), data);
+ return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data);
}
std::string operator()(const CNoDestination& no) const { return {}; }
@@ -96,7 +96,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
}
data.clear();
const auto dec = bech32::Decode(str);
- if (dec.encoding == bech32::Encoding::BECH32 && dec.data.size() > 0) {
+ if ((dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) && dec.data.size() > 0) {
// Bech32 decoding
error_str = "";
if (dec.hrp != params.Bech32HRP()) {
@@ -104,6 +104,14 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
return CNoDestination();
}
int version = dec.data[0]; // The first 5 bit symbol is the witness version (0-16)
+ if (version == 0 && dec.encoding != bech32::Encoding::BECH32) {
+ error_str = "Version 0 witness address must use Bech32 checksum";
+ return CNoDestination();
+ }
+ if (version != 0 && dec.encoding != bech32::Encoding::BECH32M) {
+ error_str = "Version 1+ witness address must use Bech32m checksum";
+ return CNoDestination();
+ }
// The rest of the symbols are converted witness program bytes.
data.reserve(((dec.data.size() - 1) * 5) / 8);
if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, dec.data.begin() + 1, dec.data.end())) {