MCPcopy Create free account
hub / github.com/ElementsProject/lightning / string_to_data

Function string_to_data

common/bolt12.c:108–152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

106}
107
108static const u8 *string_to_data(const tal_t *ctx,
109 const char *str,
110 size_t str_len,
111 const char *hrp_expected,
112 size_t *dlen,
113 char **fail)
114{
115 char *hrp;
116 u8 *data;
117 char *bech32;
118 size_t bech32_len;
119 bool have_plus = false;
120
121 /* First we collapse +\s*, except at start/end. */
122 bech32 = tal_arr(tmpctx, char, str_len);
123 bech32_len = 0;
124 for (size_t i = 0; i < str_len; i++) {
125 if (i != 0 && i+1 != str_len && !have_plus && str[i] == '+') {
126 have_plus = true;
127 continue;
128 }
129 if (have_plus && cisspace(str[i]))
130 continue;
131 have_plus = false;
132 bech32[bech32_len++] = str[i];
133 }
134
135 if (have_plus) {
136 *fail = tal_fmt(ctx, "unfinished string");
137 return NULL;
138 }
139
140 if (!from_bech32_charset(ctx, bech32, bech32_len, &hrp, &data)) {
141 *fail = tal_fmt(ctx, "invalid bech32 string");
142 return NULL;
143 }
144 if (!streq(hrp, hrp_expected)) {
145 *fail = tal_fmt(ctx, "unexpected prefix %s", hrp);
146 data = tal_free(data);
147 } else
148 *dlen = tal_bytelen(data);
149
150 tal_free(hrp);
151 return data;
152}
153
154char *offer_encode(const tal_t *ctx, const struct tlv_offer *offer_tlv)
155{

Callers 4

offer_decodeFunction · 0.85
invrequest_decodeFunction · 0.85
invoice_decode_nosigFunction · 0.85
mainFunction · 0.85

Calls 4

cisspaceFunction · 0.85
tal_freeFunction · 0.85
tal_bytelenFunction · 0.85
from_bech32_charsetFunction · 0.70

Tested by 1

mainFunction · 0.68