Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2017, 2021 Pieter Wuille2// Copyright (c) 2021-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6// Bech32 and Bech32m are string encoding formats used in newer7// address types. The outputs consist of a human-readable part8// (alphanumeric), a separator character (1), and a base32 data9// section, the last 6 characters of which are a checksum. The10// module is namespaced under bech32 for historical reasons.11//12// For more information, see BIP 173 and BIP 350.14#ifndef BITCOIN_BECH32_H15#define BITCOIN_BECH32_H17#include <cstddef>18#include <cstdint>19#include <string>20#include <utility>21#include <vector>23namespace bech3224{26inline constexpr size_t CHECKSUM_SIZE = 6;27inline constexpr char SEPARATOR = '1';29enum class Encoding {30INVALID, //!< Failed decoding32BECH32, //!< Bech32 encoding as defined in BIP17333BECH32M, //!< Bech32m encoding as defined in BIP35034};36/** Character limits for Bech32(m) encoded strings. Character limits are how we provide error location guarantees.37* These values should never exceed 2^31 - 1 (max value for a 32-bit int), since there are places where we may need to38* convert the CharLimit::VALUE to an int. In practice, this should never happen since this CharLimit applies to an address encoding39* and we would never encode an address with such a massive value */40enum CharLimit : size_t {41BECH32 = 90, //!< BIP173/350 imposed character limit for Bech32(m) encoded addresses. This guarantees finding up to 4 errors.42};44/** Encode a Bech32 or Bech32m string. If hrp contains uppercase characters, this will cause an45* assertion error. Encoding must be one of BECH32 or BECH32M. */46std::string Encode(Encoding encoding, const std::string& hrp, const std::vector<uint8_t>& values);48struct DecodeResult49{50Encoding encoding; //!< What encoding was detected in the result; Encoding::INVALID if failed.51std::string hrp; //!< The human readable part52std::vector<uint8_t> data; //!< The payload (excluding checksum)54DecodeResult() : encoding(Encoding::INVALID) {}55DecodeResult(Encoding enc, std::string&& h, std::vector<uint8_t>&& d) : encoding(enc), hrp(std::move(h)), data(std::move(d)) {}56};58/** Decode a Bech32 or Bech32m string. */59DecodeResult Decode(const std::string& str, CharLimit limit = CharLimit::BECH32);61/** Return the positions of errors in a Bech32 string. */62std::pair<std::string, std::vector<int>> LocateErrors(const std::string& str, CharLimit limit = CharLimit::BECH32);64} // namespace bech3266#endif // BITCOIN_BECH32_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.