Decode a Bech32 or Bech32m string. */
| 366 | |
| 367 | /** Decode a Bech32 or Bech32m string. */ |
| 368 | DecodeResult Decode(const std::string& str) { |
| 369 | std::vector<int> errors; |
| 370 | if (!CheckCharacters(str, errors)) return {}; |
| 371 | size_t pos = str.rfind('1'); |
| 372 | if (str.size() > 90 || pos == str.npos || pos == 0 || pos + 7 > str.size()) { |
| 373 | return {}; |
| 374 | } |
| 375 | data values(str.size() - 1 - pos); |
| 376 | for (size_t i = 0; i < str.size() - 1 - pos; ++i) { |
| 377 | unsigned char c = str[i + pos + 1]; |
| 378 | int8_t rev = CHARSET_REV[c]; |
| 379 | |
| 380 | if (rev == -1) { |
| 381 | return {}; |
| 382 | } |
| 383 | values[i] = rev; |
| 384 | } |
| 385 | std::string hrp; |
| 386 | for (size_t i = 0; i < pos; ++i) { |
| 387 | hrp += LowerCase(str[i]); |
| 388 | } |
| 389 | Encoding result = VerifyChecksum(hrp, values); |
| 390 | if (result == Encoding::INVALID) return {}; |
| 391 | return {result, std::move(hrp), data(values.begin(), values.end() - 6)}; |
| 392 | } |
| 393 | |
| 394 | /** Find index of an incorrect character in a Bech32 string. */ |
| 395 | std::pair<std::string, std::vector<int>> LocateErrors(const std::string& str) { |