| 103 | } |
| 104 | |
| 105 | static void encode(const tal_t *ctx, |
| 106 | struct privkey *privkey, |
| 107 | char *fields[]) |
| 108 | { |
| 109 | struct bolt11 *b11 = talz(ctx, struct bolt11); |
| 110 | struct pubkey me; |
| 111 | bool explicit_n = false; |
| 112 | |
| 113 | b11->timestamp = clock_time().ts.tv_sec; |
| 114 | b11->chain = chainparams_for_network("regtest"); |
| 115 | b11->expiry = 3600; |
| 116 | b11->min_final_cltv_expiry = DEFAULT_FINAL_CLTV_DELTA; |
| 117 | list_head_init(&b11->extra_fields); |
| 118 | |
| 119 | if (!pubkey_from_privkey(privkey, &me)) |
| 120 | errx(ERROR_USAGE, "Invalid privkey!"); |
| 121 | node_id_from_pubkey(&b11->receiver_id, &me); |
| 122 | |
| 123 | while (*fields) { |
| 124 | const char *eq = strchr(*fields, '='); |
| 125 | const char *fname, *val; |
| 126 | char *endp; |
| 127 | unsigned long fieldnum; |
| 128 | |
| 129 | if (!eq) |
| 130 | errx(ERROR_USAGE, "Field name must have =: %s", *fields); |
| 131 | |
| 132 | fname = tal_strndup(ctx, *fields, eq - *fields); |
| 133 | val = eq + 1; |
| 134 | |
| 135 | if (streq(fname, "currency")) { |
| 136 | b11->chain = chainparams_by_lightning_hrp(val); |
| 137 | if (!b11->chain) |
| 138 | errx(ERROR_USAGE, "Unknown currency %s", val); |
| 139 | } else if (streq(fname, "amount")) { |
| 140 | b11->msat = tal(b11, struct amount_msat); |
| 141 | if (!parse_amount_msat(b11->msat, val, strlen(val))) |
| 142 | errx(ERROR_USAGE, "Invalid amount %s", val); |
| 143 | } else if (streq(fname, "timestamp")) { |
| 144 | b11->timestamp = strtoul(val, &endp, 10); |
| 145 | if (!b11->timestamp || *endp != '\0') |
| 146 | errx(ERROR_USAGE, "Invalid amount %s", val); |
| 147 | /* Allow raw numbered fields (except 9, that's below) */ |
| 148 | } else if ((fieldnum = strtoul(fname, &endp, 10)) != 0 |
| 149 | && fieldnum < 256 |
| 150 | && fieldnum != 9) { |
| 151 | struct bolt11_field *extra = tal(b11, struct bolt11_field); |
| 152 | extra->tag = fieldnum; |
| 153 | extra->data = tal_hexdata(extra, val, strlen(val)); |
| 154 | if (!extra->data) |
| 155 | errx(ERROR_USAGE, "Invalid hex %s", val); |
| 156 | list_add_tail(&b11->extra_fields, &extra->list); |
| 157 | } else { |
| 158 | if (strlen(fname) != 1) |
| 159 | errx(ERROR_USAGE, "Unknown field %s", fname); |
| 160 | switch (*fname) { |
| 161 | case 'p': |
| 162 | if (!hex_decode(val, strlen(val), |
no test coverage detected