/ref/c/segwit_addr.h

https://github.com/sipa/bech32 · C Header · 113 lines · 36 code · 9 blank · 68 comment · 0 complexity · e9a1246db46fc03e1875d09a10a3bae1 MD5 · raw file

  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. /** Encode a SegWit address
  25. *
  26. * Out: output: Pointer to a buffer of size 73 + strlen(hrp) that will be
  27. * updated to contain the null-terminated address.
  28. * In: hrp: Pointer to the null-terminated human readable part to use
  29. * (chain/network specific).
  30. * ver: Version of the witness program (between 0 and 16 inclusive).
  31. * prog: Data bytes for the witness program (between 2 and 40 bytes).
  32. * prog_len: Number of data bytes in prog.
  33. * Returns 1 if successful.
  34. */
  35. int segwit_addr_encode(
  36. char *output,
  37. const char *hrp,
  38. int ver,
  39. const uint8_t *prog,
  40. size_t prog_len
  41. );
  42. /** Decode a SegWit address
  43. *
  44. * Out: ver: Pointer to an int that will be updated to contain the witness
  45. * program version (between 0 and 16 inclusive).
  46. * prog: Pointer to a buffer of size 40 that will be updated to
  47. * contain the witness program bytes.
  48. * prog_len: Pointer to a size_t that will be updated to contain the length
  49. * of bytes in prog.
  50. * hrp: Pointer to the null-terminated human readable part that is
  51. * expected (chain/network specific).
  52. * addr: Pointer to the null-terminated address.
  53. * Returns 1 if successful.
  54. */
  55. int segwit_addr_decode(
  56. int* ver,
  57. uint8_t* prog,
  58. size_t* prog_len,
  59. const char* hrp,
  60. const char* addr
  61. );
  62. /** Supported encodings. */
  63. typedef enum {
  64. BECH32_ENCODING_NONE,
  65. BECH32_ENCODING_BECH32,
  66. BECH32_ENCODING_BECH32M
  67. } bech32_encoding;
  68. /** Encode a Bech32 or Bech32m string
  69. *
  70. * Out: output: Pointer to a buffer of size strlen(hrp) + data_len + 8 that
  71. * will be updated to contain the null-terminated Bech32 string.
  72. * In: hrp : Pointer to the null-terminated human readable part.
  73. * data : Pointer to an array of 5-bit values.
  74. * data_len: Length of the data array.
  75. * enc: Which encoding to use (BECH32_ENCODING_BECH32{,M}).
  76. * Returns 1 if successful.
  77. */
  78. int bech32_encode(
  79. char *output,
  80. const char *hrp,
  81. const uint8_t *data,
  82. size_t data_len,
  83. bech32_encoding enc
  84. );
  85. /** Decode a Bech32 or Bech32m string
  86. *
  87. * Out: hrp: Pointer to a buffer of size strlen(input) - 6. Will be
  88. * updated to contain the null-terminated human readable part.
  89. * data: Pointer to a buffer of size strlen(input) - 8 that will
  90. * hold the encoded 5-bit data values.
  91. * data_len: Pointer to a size_t that will be updated to be the number
  92. * of entries in data.
  93. * In: input: Pointer to a null-terminated Bech32 string.
  94. * Returns BECH32_ENCODING_BECH32{,M} to indicate decoding was successful
  95. * with the specified encoding standard. BECH32_ENCODING_NONE is returned if
  96. * decoding failed.
  97. */
  98. bech32_encoding bech32_decode(
  99. char *hrp,
  100. uint8_t *data,
  101. size_t *data_len,
  102. const char *input
  103. );
  104. #endif