Encode a Bech32 or Bech32m string. */
| 356 | |
| 357 | /** Encode a Bech32 or Bech32m string. */ |
| 358 | std::string Encode(Encoding encoding, const std::string& hrp, const data& values) { |
| 359 | // First ensure that the HRP is all lowercase. BIP-173 and BIP350 require an encoder |
| 360 | // to return a lowercase Bech32/Bech32m string, but if given an uppercase HRP, the |
| 361 | // result will always be invalid. |
| 362 | for (const char& c : hrp) assert(c < 'A' || c > 'Z'); |
| 363 | |
| 364 | std::string ret; |
| 365 | ret.reserve(hrp.size() + 1 + values.size() + CHECKSUM_SIZE); |
| 366 | ret += hrp; |
| 367 | ret += SEPARATOR; |
| 368 | for (const uint8_t& i : values) ret += CHARSET[i]; |
| 369 | for (const uint8_t& i : CreateChecksum(encoding, hrp, values)) ret += CHARSET[i]; |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | /** Decode a Bech32 or Bech32m string. */ |
| 374 | DecodeResult Decode(const std::string& str, CharLimit limit) { |