PageRenderTime 104ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/bech32.h

https://github.com/bitcoin/bitcoin
C Header | 53 lines | 25 code | 13 blank | 15 comment | 0 complexity | f5449f16cbaba94e40f7c0d9f44d51f5 MD5 | raw 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. // 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. #ifndef BITCOIN_BECH32_H
  13. #define BITCOIN_BECH32_H
  14. #include <stdint.h>
  15. #include <string>
  16. #include <vector>
  17. namespace bech32
  18. {
  19. enum class Encoding {
  20. INVALID, //!< Failed decoding
  21. BECH32, //!< Bech32 encoding as defined in BIP173
  22. BECH32M, //!< Bech32m encoding as defined in BIP350
  23. };
  24. /** Encode a Bech32 or Bech32m string. If hrp contains uppercase characters, this will cause an
  25. * assertion error. Encoding must be one of BECH32 or BECH32M. */
  26. std::string Encode(Encoding encoding, const std::string& hrp, const std::vector<uint8_t>& values);
  27. struct DecodeResult
  28. {
  29. Encoding encoding; //!< What encoding was detected in the result; Encoding::INVALID if failed.
  30. std::string hrp; //!< The human readable part
  31. std::vector<uint8_t> data; //!< The payload (excluding checksum)
  32. DecodeResult() : encoding(Encoding::INVALID) {}
  33. DecodeResult(Encoding enc, std::string&& h, std::vector<uint8_t>&& d) : encoding(enc), hrp(std::move(h)), data(std::move(d)) {}
  34. };
  35. /** Decode a Bech32 or Bech32m string. */
  36. DecodeResult Decode(const std::string& str);
  37. /** Return the positions of errors in a Bech32 string. */
  38. std::pair<std::string, std::vector<int>> LocateErrors(const std::string& str);
  39. } // namespace bech32
  40. #endif // BITCOIN_BECH32_H