1 /* Stolen from https://github.com/sipa/bech32/blob/master/ref/c/segwit_addr.h,
2  * with only the two ' > 90' checks hoisted */
3 
4 /* Copyright (c) 2017, 2021 Pieter Wuille
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef LIGHTNING_COMMON_BECH32_H
26 #define LIGHTNING_COMMON_BECH32_H
27 #include "config.h"
28 
29 #include <stdint.h>
30 #include <stdlib.h>
31 
32 /** Encode a SegWit address
33  *
34  *  Out: output:   Pointer to a buffer of size 73 + strlen(hrp) that will be
35  *                 updated to contain the null-terminated address.
36  *  In:  hrp:      Pointer to the null-terminated human readable part to use
37  *                 (chain/network specific).
38  *       ver:      Version of the witness program (between 0 and 16 inclusive).
39  *       prog:     Data bytes for the witness program (between 2 and 40 bytes).
40  *       prog_len: Number of data bytes in prog.
41  *  Returns 1 if successful.
42  */
43 int segwit_addr_encode(
44     char *output,
45     const char *hrp,
46     int ver,
47     const uint8_t *prog,
48     size_t prog_len
49 );
50 
51 /** Decode a SegWit address
52  *
53  *  Out: ver:      Pointer to an int that will be updated to contain the witness
54  *                 program version (between 0 and 16 inclusive).
55  *       prog:     Pointer to a buffer of size 40 that will be updated to
56  *                 contain the witness program bytes.
57  *       prog_len: Pointer to a size_t that will be updated to contain the length
58  *                 of bytes in prog.
59  *       hrp:      Pointer to the null-terminated human readable part that is
60  *                 expected (chain/network specific).
61  *       addr:     Pointer to the null-terminated address.
62  *  Returns 1 if successful.
63  */
64 int segwit_addr_decode(
65     int* ver,
66     uint8_t* prog,
67     size_t* prog_len,
68     const char* hrp,
69     const char* addr
70 );
71 
72 /** Supported encodings. */
73 typedef enum {
74     BECH32_ENCODING_NONE,
75     BECH32_ENCODING_BECH32,
76     BECH32_ENCODING_BECH32M
77 } bech32_encoding;
78 
79 /** Encode a Bech32 or Bech32m string
80  *
81  *  Out: output:  Pointer to a buffer of size strlen(hrp) + data_len + 8 that
82  *                will be updated to contain the null-terminated Bech32 string.
83  *  In: hrp :     Pointer to the null-terminated human readable part.
84  *      data :    Pointer to an array of 5-bit values.
85  *      data_len: Length of the data array.
86  *      max_input_len: Maximum valid length of input (90 for segwit usage).
87  *      enc:      Which encoding to use (BECH32_ENCODING_BECH32{,M}).
88  *  Returns 1 if successful.
89  */
90 int bech32_encode(
91     char *output,
92     const char *hrp,
93     const uint8_t *data,
94     size_t data_len,
95     size_t max_input_len,
96     bech32_encoding enc
97 );
98 
99 /** Decode a Bech32 or Bech32m string
100  *
101  *  Out: hrp:      Pointer to a buffer of size strlen(input) - 6. Will be
102  *                 updated to contain the null-terminated human readable part.
103  *       data:     Pointer to a buffer of size strlen(input) - 8 that will
104  *                 hold the encoded 5-bit data values.
105  *       data_len: Pointer to a size_t that will be updated to be the number
106  *                 of entries in data.
107  *  In: input:     Pointer to a null-terminated Bech32 string.
108  *      max_input_len: Maximum valid length of input (90 for segwit usage).
109  *  Returns BECH32_ENCODING_BECH32{,M} to indicate decoding was successful
110  *  with the specified encoding standard. BECH32_ENCODING_NONE is returned if
111  *  decoding failed.
112  */
113 bech32_encoding bech32_decode(
114     char *hrp,
115     uint8_t *data,
116     size_t *data_len,
117     const char *input,
118     size_t max_input_len
119 );
120 
121 /* Helper from bech32: translates inbits-bit bytes to outbits-bit bytes.
122  * @outlen is incremented as bytes are added.
123  * @pad is true if we're to pad, otherwise truncate last byte if necessary
124  */
125 int bech32_convert_bits(uint8_t* out, size_t* outlen, int outbits,
126 			const uint8_t* in, size_t inlen, int inbits,
127 			int pad);
128 
129 /* The charset, and reverse mapping */
130 extern const char bech32_charset[32];
131 extern const int8_t bech32_charset_rev[128];
132 
133 #endif /* LIGHTNING_COMMON_BECH32_H */
134 
135