1 // Copyright (c) 2017, 2021 Pieter Wuille
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 // Bech32 and Bech32m are string encoding formats used in newer
6 // address types. The outputs consist of a human-readable part
7 // (alphanumeric), a separator character (1), and a base32 data
8 // section, the last 6 characters of which are a checksum. The
9 // module is namespaced under bech32 for historical reasons.
10 //
11 // For more information, see BIP 173 and BIP 350.
12 
13 #ifndef BITCOIN_BECH32_H
14 #define BITCOIN_BECH32_H
15 
16 #include <stdint.h>
17 #include <string>
18 #include <vector>
19 
20 namespace bech32
21 {
22 
23 enum class Encoding {
24     INVALID, //!< Failed decoding
25 
26     BECH32,  //!< Bech32 encoding as defined in BIP173
27     BECH32M, //!< Bech32m encoding as defined in BIP350
28 };
29 
30 /** Encode a Bech32 or Bech32m string. If hrp contains uppercase characters, this will cause an
31  *  assertion error. Encoding must be one of BECH32 or BECH32M. */
32 std::string Encode(Encoding encoding, const std::string& hrp, const std::vector<uint8_t>& values);
33 
34 struct DecodeResult
35 {
36     Encoding encoding;         //!< What encoding was detected in the result; Encoding::INVALID if failed.
37     std::string hrp;           //!< The human readable part
38     std::vector<uint8_t> data; //!< The payload (excluding checksum)
39 
DecodeResultDecodeResult40     DecodeResult() : encoding(Encoding::INVALID) {}
DecodeResultDecodeResult41     DecodeResult(Encoding enc, std::string&& h, std::vector<uint8_t>&& d) : encoding(enc), hrp(std::move(h)), data(std::move(d)) {}
42 };
43 
44 /** Decode a Bech32 or Bech32m string. */
45 DecodeResult Decode(const std::string& str);
46 
47 } // namespace bech32
48 
49 #endif // BITCOIN_BECH32_H
50