Bitcoin Core  27.99.0
P2P Digital Currency
bech32.h
Go to the documentation of this file.
1 // Copyright (c) 2017, 2021 Pieter Wuille
2 // Copyright (c) 2021 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 // Bech32 and Bech32m are string encoding formats used in newer
7 // address types. The outputs consist of a human-readable part
8 // (alphanumeric), a separator character (1), and a base32 data
9 // section, the last 6 characters of which are a checksum. The
10 // module is namespaced under bech32 for historical reasons.
11 //
12 // For more information, see BIP 173 and BIP 350.
13 
14 #ifndef BITCOIN_BECH32_H
15 #define BITCOIN_BECH32_H
16 
17 #include <stdint.h>
18 #include <string>
19 #include <vector>
20 
21 namespace bech32
22 {
23 
24 enum class Encoding {
25  INVALID,
26 
27  BECH32,
28  BECH32M,
29 };
30 
33 std::string Encode(Encoding encoding, const std::string& hrp, const std::vector<uint8_t>& values);
34 
36 {
38  std::string hrp;
39  std::vector<uint8_t> data;
40 
42  DecodeResult(Encoding enc, std::string&& h, std::vector<uint8_t>&& d) : encoding(enc), hrp(std::move(h)), data(std::move(d)) {}
43 };
44 
46 DecodeResult Decode(const std::string& str);
47 
49 std::pair<std::string, std::vector<int>> LocateErrors(const std::string& str);
50 
51 } // namespace bech32
52 
53 #endif // BITCOIN_BECH32_H
Encoding
Definition: bech32.h:24
@ INVALID
Failed decoding.
@ BECH32
Bech32 encoding as defined in BIP173.
@ BECH32M
Bech32m encoding as defined in BIP350.
std::string Encode(Encoding encoding, const std::string &hrp, const data &values)
Encode a Bech32 or Bech32m string.
Definition: bech32.cpp:357
DecodeResult Decode(const std::string &str)
Decode a Bech32 or Bech32m string.
Definition: bech32.cpp:373
std::pair< std::string, std::vector< int > > LocateErrors(const std::string &str)
Find index of an incorrect character in a Bech32 string.
Definition: bech32.cpp:400
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Encoding encoding
What encoding was detected in the result; Encoding::INVALID if failed.
Definition: bech32.h:37
std::vector< uint8_t > data
The payload (excluding checksum)
Definition: bech32.h:39
DecodeResult(Encoding enc, std::string &&h, std::vector< uint8_t > &&d)
Definition: bech32.h:42
std::string hrp
The human readable part.
Definition: bech32.h:38