| 83 | } |
| 84 | |
| 85 | static struct command_result *json_createoffer(struct command *cmd, |
| 86 | const char *buffer, |
| 87 | const jsmntok_t *obj UNNEEDED, |
| 88 | const jsmntok_t *params) |
| 89 | { |
| 90 | struct json_stream *response; |
| 91 | struct json_escape *label; |
| 92 | struct tlv_offer *offer; |
| 93 | struct sha256 merkle; |
| 94 | const char *b12str, *b12str_nosig; |
| 95 | bool *single_use; |
| 96 | enum offer_status status; |
| 97 | struct point32 key; |
| 98 | bool created; |
| 99 | |
| 100 | if (!param(cmd, buffer, params, |
| 101 | p_req("bolt12", param_b12_offer, &offer), |
| 102 | p_opt("label", param_label, &label), |
| 103 | p_opt_def("single_use", param_bool, &single_use, false), |
| 104 | NULL)) |
| 105 | return command_param_failed(); |
| 106 | |
| 107 | if (*single_use) |
| 108 | status = OFFER_SINGLE_USE_UNUSED; |
| 109 | else |
| 110 | status = OFFER_MULTIPLE_USE_UNUSED; |
| 111 | merkle_tlv(offer->fields, &merkle); |
| 112 | offer->signature = tal(offer, struct bip340sig); |
| 113 | if (!point32_from_node_id(&key, &cmd->ld->id)) |
| 114 | fatal("invalid own node_id?"); |
| 115 | hsm_sign_b12(cmd->ld, "offer", "signature", &merkle, NULL, &key, |
| 116 | offer->signature); |
| 117 | b12str = offer_encode(cmd, offer); |
| 118 | |
| 119 | /* If it already exists, we use that one instead (and then |
| 120 | * the offer plugin will complain if it's inactive or expired) */ |
| 121 | if (!wallet_offer_create(cmd->ld->wallet, &merkle, |
| 122 | b12str, label, status)) { |
| 123 | if (!wallet_offer_find(cmd, cmd->ld->wallet, &merkle, |
| 124 | cast_const2(const struct json_escape **, |
| 125 | &label), |
| 126 | &status)) { |
| 127 | return command_fail(cmd, LIGHTNINGD, |
| 128 | "Could not create, nor find offer"); |
| 129 | } |
| 130 | created = false; |
| 131 | } else |
| 132 | created = true; |
| 133 | |
| 134 | offer->signature = tal_free(offer->signature); |
| 135 | b12str_nosig = offer_encode(cmd, offer); |
| 136 | |
| 137 | response = json_stream_success(cmd); |
| 138 | json_populate_offer(response, &merkle, b12str, b12str_nosig, label, status); |
| 139 | json_add_bool(response, "created", created); |
| 140 | return command_success(cmd, response); |
| 141 | } |
| 142 |
nothing calls this directly
no test coverage detected