From 593e206627f4fb789de70f55017f71b85d10754d Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 5 Jan 2021 13:36:42 -0800 Subject: Use Bech32m encoding for v1+ segwit addresses This also includes updates to the Python test framework implementation, test vectors, and release notes. Github-Pull: #20861 Rebased-From: fe5e495c31de47b0ec732b943db11fe345d874af --- src/key_io.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/key_io.cpp') diff --git a/src/key_io.cpp b/src/key_io.cpp index f927ebbb42..0cb4d0bf7c 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -63,7 +63,7 @@ public: std::vector 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 {}; } @@ -92,9 +92,15 @@ 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 && dec.hrp == params.Bech32HRP()) { + if ((dec.encoding == bech32::Encoding::BECH32 || dec.encoding == bech32::Encoding::BECH32M) && dec.data.size() > 0 && dec.hrp == params.Bech32HRP()) { // Bech32 decoding int version = dec.data[0]; // The first 5 bit symbol is the witness version (0-16) + if (version == 0 && dec.encoding != bech32::Encoding::BECH32) { + return CNoDestination(); + } + if (version != 0 && dec.encoding != bech32::Encoding::BECH32M) { + 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())) { -- cgit v1.2.3