From c1a5d5c100b1628456acfa6129e303737f0ad4d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Sat, 10 Aug 2024 09:39:20 +0200 Subject: [PATCH] Split out bech32 separator char to header --- src/bech32.cpp | 6 +++--- src/bech32.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bech32.cpp b/src/bech32.cpp index 5694ad54c8f..81695acebaa 100644 --- a/src/bech32.cpp +++ b/src/bech32.cpp @@ -364,7 +364,7 @@ std::string Encode(Encoding encoding, const std::string& hrp, const data& values std::string ret; ret.reserve(hrp.size() + 1 + values.size() + CHECKSUM_SIZE); ret += hrp; - ret += '1'; + ret += SEPARATOR; for (const uint8_t& i : values) ret += CHARSET[i]; for (const uint8_t& i : CreateChecksum(encoding, hrp, values)) ret += CHARSET[i]; return ret; @@ -374,7 +374,7 @@ std::string Encode(Encoding encoding, const std::string& hrp, const data& values DecodeResult Decode(const std::string& str, CharLimit limit) { std::vector errors; if (!CheckCharacters(str, errors)) return {}; - size_t pos = str.rfind('1'); + size_t pos = str.rfind(SEPARATOR); if (str.size() > limit) return {}; if (pos == str.npos || pos == 0 || pos + CHECKSUM_SIZE >= str.size()) { return {}; @@ -413,7 +413,7 @@ std::pair> LocateErrors(const std::string& str, Ch return std::make_pair("Invalid character or mixed case", std::move(error_locations)); } - size_t pos = str.rfind('1'); + size_t pos = str.rfind(SEPARATOR); if (pos == str.npos) { return std::make_pair("Missing separator", std::vector{}); } diff --git a/src/bech32.h b/src/bech32.h index 33d1ca1935c..6d5a68ec5a1 100644 --- a/src/bech32.h +++ b/src/bech32.h @@ -21,8 +21,8 @@ namespace bech32 { -/** The Bech32 and Bech32m checksum size */ -constexpr size_t CHECKSUM_SIZE = 6; +static constexpr size_t CHECKSUM_SIZE = 6; +static constexpr char SEPARATOR = '1'; enum class Encoding { INVALID, //!< Failed decoding