segwit_addr.h 4.2 KB

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