mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-04 02:02:42 +02:00
Implement Bech32m encoding/decoding
Github-Pull: #20861
Rebased-From: da2bb6976d
This commit is contained in:
@@ -22,9 +22,9 @@ BOOST_AUTO_TEST_CASE(bip173_testvectors_valid)
|
||||
"?1ezyfcl",
|
||||
};
|
||||
for (const std::string& str : CASES) {
|
||||
auto ret = bech32::Decode(str);
|
||||
BOOST_CHECK(!ret.first.empty());
|
||||
std::string recode = bech32::Encode(ret.first, ret.second);
|
||||
const auto dec = bech32::Decode(str);
|
||||
BOOST_CHECK(dec.encoding == bech32::Encoding::BECH32);
|
||||
std::string recode = bech32::Encode(bech32::Encoding::BECH32, dec.hrp, dec.data);
|
||||
BOOST_CHECK(!recode.empty());
|
||||
BOOST_CHECK(CaseInsensitiveEqual(str, recode));
|
||||
}
|
||||
@@ -49,8 +49,8 @@ BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid)
|
||||
"A12uEL5L",
|
||||
};
|
||||
for (const std::string& str : CASES) {
|
||||
auto ret = bech32::Decode(str);
|
||||
BOOST_CHECK(ret.first.empty());
|
||||
const auto dec = bech32::Decode(str);
|
||||
BOOST_CHECK(dec.encoding != bech32::Encoding::BECH32);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,28 +16,28 @@
|
||||
void test_one_input(const std::vector<uint8_t>& buffer)
|
||||
{
|
||||
const std::string random_string(buffer.begin(), buffer.end());
|
||||
const std::pair<std::string, std::vector<uint8_t>> r1 = bech32::Decode(random_string);
|
||||
if (r1.first.empty()) {
|
||||
assert(r1.second.empty());
|
||||
const auto r1 = bech32::Decode(random_string);
|
||||
if (r1.hrp.empty()) {
|
||||
assert(r1.encoding == bech32::Encoding::INVALID);
|
||||
assert(r1.data.empty());
|
||||
} else {
|
||||
const std::string& hrp = r1.first;
|
||||
const std::vector<uint8_t>& data = r1.second;
|
||||
const std::string reencoded = bech32::Encode(hrp, data);
|
||||
assert(r1.encoding != bech32::Encoding::INVALID);
|
||||
const std::string reencoded = bech32::Encode(r1.encoding, r1.hrp, r1.data);
|
||||
assert(CaseInsensitiveEqual(random_string, reencoded));
|
||||
}
|
||||
|
||||
std::vector<unsigned char> input;
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { input.push_back(c); }, buffer.begin(), buffer.end());
|
||||
const std::string encoded = bech32::Encode("bc", input);
|
||||
assert(!encoded.empty());
|
||||
|
||||
const std::pair<std::string, std::vector<uint8_t>> r2 = bech32::Decode(encoded);
|
||||
if (r2.first.empty()) {
|
||||
assert(r2.second.empty());
|
||||
} else {
|
||||
const std::string& hrp = r2.first;
|
||||
const std::vector<uint8_t>& data = r2.second;
|
||||
assert(hrp == "bc");
|
||||
assert(data == input);
|
||||
if (input.size() + 3 + 6 <= 90) {
|
||||
// If it's possible to encode input in Bech32(m) without exceeding the 90-character limit:
|
||||
for (auto encoding : {bech32::Encoding::BECH32, bech32::Encoding::BECH32M}) {
|
||||
const std::string encoded = bech32::Encode(encoding, "bc", input);
|
||||
assert(!encoded.empty());
|
||||
const auto r2 = bech32::Decode(encoded);
|
||||
assert(r2.encoding == encoding);
|
||||
assert(r2.hrp == "bc");
|
||||
assert(r2.data == input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user