| 321 | } |
| 322 | |
| 323 | const u8 *b12_string_to_data(const tal_t *ctx, |
| 324 | const char *str, |
| 325 | size_t str_len, |
| 326 | const char *hrp_expected, |
| 327 | size_t *dlen, |
| 328 | const char **fail) |
| 329 | { |
| 330 | char *hrp; |
| 331 | u8 *data; |
| 332 | char *bech32; |
| 333 | size_t bech32_len; |
| 334 | bool have_plus = false; |
| 335 | |
| 336 | /* First we collapse +\s*, except at start/end. */ |
| 337 | bech32 = tal_arr(tmpctx, char, str_len); |
| 338 | bech32_len = 0; |
| 339 | for (size_t i = 0; i < str_len; i++) { |
| 340 | if (i != 0 && i+1 != str_len && !have_plus && str[i] == '+') { |
| 341 | have_plus = true; |
| 342 | continue; |
| 343 | } |
| 344 | if (have_plus && cisspace(str[i])) |
| 345 | continue; |
| 346 | have_plus = false; |
| 347 | bech32[bech32_len++] = str[i]; |
| 348 | } |
| 349 | |
| 350 | if (have_plus) { |
| 351 | *fail = tal_fmt(ctx, "unfinished string"); |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | if (!from_bech32_charset(ctx, bech32, bech32_len, &hrp, &data)) { |
| 356 | *fail = tal_fmt(ctx, "invalid bech32 string"); |
| 357 | return NULL; |
| 358 | } |
| 359 | if (!streq(hrp, hrp_expected)) { |
| 360 | *fail = tal_fmt(ctx, "unexpected prefix %s", hrp); |
| 361 | data = tal_free(data); |
| 362 | } else |
| 363 | *dlen = tal_bytelen(data); |
| 364 | |
| 365 | tal_free(hrp); |
| 366 | return data; |
| 367 | } |
| 368 | |
| 369 | char *offer_encode(const tal_t *ctx, const struct tlv_offer *offer_tlv) |
| 370 | { |