Encode a Bech32 or Bech32m string. */
| 350 | |
| 351 | /** Encode a Bech32 or Bech32m string. */ |
| 352 | std::string Encode(Encoding encoding, const std::string& hrp, const data& values) { |
| 353 | // First ensure that the HRP is all lowercase. BIP-173 and BIP350 require an encoder |
| 354 | // to return a lowercase Bech32/Bech32m string, but if given an uppercase HRP, the |
| 355 | // result will always be invalid. |
| 356 | for (const char& c : hrp) assert(c < 'A' || c > 'Z'); |
| 357 | data checksum = CreateChecksum(encoding, hrp, values); |
| 358 | data combined = Cat(values, checksum); |
| 359 | std::string ret = hrp + '1'; |
| 360 | ret.reserve(ret.size() + combined.size()); |
| 361 | for (const auto c : combined) { |
| 362 | ret += CHARSET[c]; |
| 363 | } |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | /** Decode a Bech32 or Bech32m string. */ |
| 368 | DecodeResult Decode(const std::string& str) { |