LCOV - code coverage report
Current view: top level - src - bech32.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 3 3
Test Date: 2024-11-04 04:45:35 Functions: - 0 0
Branches: - 0 0

             Branch data     Line data    Source code
       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                 :             : /** The Bech32 and Bech32m checksum size */
      25                 :             : constexpr size_t CHECKSUM_SIZE = 6;
      26                 :             : 
      27                 :             : enum class Encoding {
      28                 :             :     INVALID, //!< Failed decoding
      29                 :             : 
      30                 :             :     BECH32,  //!< Bech32 encoding as defined in BIP173
      31                 :             :     BECH32M, //!< Bech32m encoding as defined in BIP350
      32                 :             : };
      33                 :             : 
      34                 :             : /** Character limits for Bech32(m) encoded strings. Character limits are how we provide error location guarantees.
      35                 :             :  *  These values should never exceed 2^31 - 1 (max value for a 32-bit int), since there are places where we may need to
      36                 :             :  *  convert the CharLimit::VALUE to an int. In practice, this should never happen since this CharLimit applies to an address encoding
      37                 :             :  *  and we would never encode an address with such a massive value */
      38                 :             : enum CharLimit : size_t {
      39                 :             :     BECH32 = 90,            //!< BIP173/350 imposed character limit for Bech32(m) encoded addresses. This guarantees finding up to 4 errors.
      40                 :             : };
      41                 :             : 
      42                 :             : /** Encode a Bech32 or Bech32m string. If hrp contains uppercase characters, this will cause an
      43                 :             :  *  assertion error. Encoding must be one of BECH32 or BECH32M. */
      44                 :             : std::string Encode(Encoding encoding, const std::string& hrp, const std::vector<uint8_t>& values);
      45                 :             : 
      46                 :         174 : struct DecodeResult
      47                 :             : {
      48                 :             :     Encoding encoding;         //!< What encoding was detected in the result; Encoding::INVALID if failed.
      49                 :             :     std::string hrp;           //!< The human readable part
      50                 :             :     std::vector<uint8_t> data; //!< The payload (excluding checksum)
      51                 :             : 
      52                 :          42 :     DecodeResult() : encoding(Encoding::INVALID) {}
      53                 :         132 :     DecodeResult(Encoding enc, std::string&& h, std::vector<uint8_t>&& d) : encoding(enc), hrp(std::move(h)), data(std::move(d)) {}
      54                 :             : };
      55                 :             : 
      56                 :             : /** Decode a Bech32 or Bech32m string. */
      57                 :             : DecodeResult Decode(const std::string& str, CharLimit limit = CharLimit::BECH32);
      58                 :             : 
      59                 :             : /** Return the positions of errors in a Bech32 string. */
      60                 :             : std::pair<std::string, std::vector<int>> LocateErrors(const std::string& str, CharLimit limit = CharLimit::BECH32);
      61                 :             : 
      62                 :             : } // namespace bech32
      63                 :             : 
      64                 :             : #endif // BITCOIN_BECH32_H
        

Generated by: LCOV version 2.0-1