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