segwit_addr.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (c) 2017, 2021 Pieter Wuille
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. #ifndef _SEGWIT_ADDR_H_
  22. #define _SEGWIT_ADDR_H_ 1
  23. #include <stdint.h>
  24. // The maximum length of the Bech32 human-readable part according to BIP-173.
  25. #define BECH32_MAX_HRP_LEN 83
  26. /** Encode a SegWit address
  27. *
  28. * Out: output: Pointer to a buffer of size 73 + strlen(hrp) that will be
  29. * updated to contain the null-terminated address.
  30. * In: hrp: Pointer to the null-terminated human readable part to use
  31. * (chain/network specific).
  32. * ver: Version of the witness program (between 0 and 16 inclusive).
  33. * prog: Data bytes for the witness program (between 2 and 40 bytes).
  34. * prog_len: Number of data bytes in prog.
  35. * Returns 1 if successful.
  36. */
  37. int segwit_addr_encode(char* output, const char* hrp, int ver, const uint8_t* prog, size_t prog_len);
  38. /** Decode a SegWit address
  39. *
  40. * Out: ver: Pointer to an int that will be updated to contain the witness
  41. * program version (between 0 and 16 inclusive).
  42. * prog: Pointer to a buffer of size 40 that will be updated to
  43. * contain the witness program bytes.
  44. * prog_len: Pointer to a size_t that will be updated to contain the length
  45. * of bytes in prog.
  46. * hrp: Pointer to the null-terminated human readable part that is
  47. * expected (chain/network specific).
  48. * addr: Pointer to the null-terminated address.
  49. * Returns 1 if successful.
  50. */
  51. int segwit_addr_decode(int* ver, uint8_t* prog, size_t* prog_len, const char* hrp, const char* addr);
  52. /** Supported encodings. */
  53. typedef enum {
  54. BECH32_ENCODING_NONE,
  55. BECH32_ENCODING_BECH32,
  56. BECH32_ENCODING_BECH32M
  57. } bech32_encoding;
  58. /** Encode a Bech32 or Bech32m string
  59. *
  60. * Out: output: Pointer to a buffer of size strlen(hrp) + data_len + 8 that
  61. * will be updated to contain the null-terminated Bech32 string.
  62. * In: hrp : Pointer to the null-terminated human readable part.
  63. * data : Pointer to an array of 5-bit values.
  64. * data_len: Length of the data array.
  65. * enc: Which encoding to use (BECH32_ENCODING_BECH32{,M}).
  66. * Returns 1 if successful.
  67. */
  68. int bech32_encode(
  69. char* output,
  70. const char* hrp,
  71. const uint8_t* data,
  72. size_t data_len,
  73. bech32_encoding enc);
  74. /** Decode a Bech32 or Bech32m string
  75. *
  76. * Out: hrp: Pointer to a buffer of size BECH32_MAX_HRP_LEN + 1. Will be
  77. * updated to contain the null-terminated human readable part.
  78. * data: Pointer to a buffer of size strlen(input) - 8 that will
  79. * hold the encoded 5-bit data values.
  80. * data_len: Pointer to a size_t that will be updated to be the number
  81. * of entries in data.
  82. * In: input: Pointer to a null-terminated Bech32 string.
  83. * Returns BECH32_ENCODING_BECH32{,M} to indicate decoding was successful
  84. * with the specified encoding standard. BECH32_ENCODING_NONE is returned if
  85. * decoding failed.
  86. */
  87. bech32_encoding bech32_decode(char* hrp, uint8_t* data, size_t* data_len, const char* input);
  88. #endif