aboutsummaryrefslogtreecommitdiff
path: root/src/bech32.cpp
diff options
context:
space:
mode:
authorjosibake <josibake@protonmail.com>2023-07-18 22:22:26 +0200
committerjosibake <josibake@protonmail.com>2024-05-13 12:07:47 +0200
commit5676aec1e1a6d2c6fd3099e120e263a0a7def089 (patch)
tree0d09ba5b3efeed4304a988aaf13cb36ca86465bc /src/bech32.cpp
parent2cedb42a928fbf3a1e0e8715e918497cbe64af0d (diff)
refactor: Model the bech32 charlimit as an Enum
Bech32(m) was defined with a 90 character limit so that certain guarantees for error detection could be made for segwit addresses. However, there is nothing about the encoding scheme itself that requires a limit and in practice bech32(m) has been used without the 90 char limit (e.g. lightning invoices). Further, increasing the character limit doesn't do away with error detection, it simply lessons the guarantees. Model charlimit as an Enum, so that if a different address scheme is using bech32(m), the character limit for that address scheme can be used, rather than always using the 90 charlimit defined for segwit addresses. upate comment
Diffstat (limited to 'src/bech32.cpp')
-rw-r--r--src/bech32.cpp13
1 files changed, 7 insertions, 6 deletions
diff --git a/src/bech32.cpp b/src/bech32.cpp
index ba3c419d8b..6a0956f1b6 100644
--- a/src/bech32.cpp
+++ b/src/bech32.cpp
@@ -370,11 +370,12 @@ std::string Encode(Encoding encoding, const std::string& hrp, const data& values
}
/** Decode a Bech32 or Bech32m string. */
-DecodeResult Decode(const std::string& str) {
+DecodeResult Decode(const std::string& str, CharLimit limit) {
std::vector<int> errors;
if (!CheckCharacters(str, errors)) return {};
size_t pos = str.rfind('1');
- if (str.size() > 90 || pos == str.npos || pos == 0 || pos + 7 > str.size()) {
+ if (str.size() > limit) return {};
+ if (pos == str.npos || pos == 0 || pos + 7 > str.size()) {
return {};
}
data values(str.size() - 1 - pos);
@@ -397,12 +398,12 @@ DecodeResult Decode(const std::string& str) {
}
/** Find index of an incorrect character in a Bech32 string. */
-std::pair<std::string, std::vector<int>> LocateErrors(const std::string& str) {
+std::pair<std::string, std::vector<int>> LocateErrors(const std::string& str, CharLimit limit) {
std::vector<int> error_locations{};
- if (str.size() > 90) {
- error_locations.resize(str.size() - 90);
- std::iota(error_locations.begin(), error_locations.end(), 90);
+ if (str.size() > limit) {
+ error_locations.resize(str.size() - limit);
+ std::iota(error_locations.begin(), error_locations.end(), static_cast<int>(limit));
return std::make_pair("Bech32 string too long", std::move(error_locations));
}