Skip to content

Commit c67d35a

Browse files
committed
lightningd: add description field to offer related responces
1 parent cef11ea commit c67d35a

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

lightningd/offer.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,40 @@ static void json_populate_offer(struct json_stream *response,
1414
const struct sha256 *offer_id,
1515
const char *b12,
1616
const struct json_escape *label,
17+
const char *description,
1718
enum offer_status status)
1819
{
1920
json_add_sha256(response, "offer_id", offer_id);
2021
json_add_bool(response, "active", offer_status_active(status));
2122
json_add_bool(response, "single_use", offer_status_single(status));
2223
json_add_string(response, "bolt12", b12);
24+
if (description)
25+
json_add_string(response, "description", description);
2326
json_add_bool(response, "used", offer_status_used(status));
2427
if (label)
2528
json_add_escaped_string(response, "label", label);
2629
}
2730

31+
static const char *offer_description_from_b12(const tal_t *ctx,
32+
struct lightningd *ld,
33+
const char *b12)
34+
{
35+
struct tlv_offer *offer;
36+
char *fail;
37+
38+
offer = offer_decode(ctx, b12, strlen(b12),
39+
ld->our_features, chainparams, &fail);
40+
if (!offer) {
41+
log_debug(ld->log, "Failed to decode BOLT12: %s", fail);
42+
return NULL;
43+
}
44+
45+
if (!offer->offer_description)
46+
return NULL;
47+
48+
return (const char *)offer->offer_description;
49+
}
50+
2851
static struct command_result *param_b12_offer(struct command *cmd,
2952
const char *name,
3053
const char *buffer,
@@ -114,7 +137,8 @@ static struct command_result *json_createoffer(struct command *cmd,
114137
created = true;
115138

116139
response = json_stream_success(cmd);
117-
json_populate_offer(response, &offer_id, b12str, label, status);
140+
json_populate_offer(response, &offer_id, b12str, label,
141+
offer->offer_description, status);
118142
json_add_bool(response, "created", created);
119143
return command_success(cmd, response);
120144
}
@@ -134,6 +158,7 @@ static struct command_result *json_listoffers(struct command *cmd,
134158
struct json_stream *response;
135159
struct wallet *wallet = cmd->ld->wallet;
136160
const char *b12;
161+
const char *description;
137162
const struct json_escape *label;
138163
bool *active_only;
139164
enum offer_status status;
@@ -149,11 +174,12 @@ static struct command_result *json_listoffers(struct command *cmd,
149174
if (offer_id) {
150175
b12 = wallet_offer_find(tmpctx, wallet, offer_id, &label,
151176
&status);
177+
description = offer_description_from_b12(tmpctx, cmd->ld, b12);
152178
if (b12 && offer_status_active(status) >= *active_only) {
153179
json_object_start(response, NULL);
154180
json_populate_offer(response,
155181
offer_id, b12,
156-
label, status);
182+
label, description, status);
157183
json_object_end(response);
158184
}
159185
} else {
@@ -165,11 +191,12 @@ static struct command_result *json_listoffers(struct command *cmd,
165191
stmt = wallet_offer_id_next(cmd->ld->wallet, stmt, &id)) {
166192
b12 = wallet_offer_find(tmpctx, wallet, &id,
167193
&label, &status);
194+
description = offer_description_from_b12(tmpctx, cmd->ld, b12);
168195
if (offer_status_active(status) >= *active_only) {
169196
json_object_start(response, NULL);
170197
json_populate_offer(response,
171198
&id, b12,
172-
label, status);
199+
label, description, status);
173200
json_object_end(response);
174201
}
175202
}
@@ -193,6 +220,7 @@ static struct command_result *json_disableoffer(struct command *cmd,
193220
struct sha256 *offer_id;
194221
struct wallet *wallet = cmd->ld->wallet;
195222
const char *b12;
223+
const char *description;
196224
const struct json_escape *label;
197225
enum offer_status status;
198226

@@ -202,6 +230,7 @@ static struct command_result *json_disableoffer(struct command *cmd,
202230
return command_param_failed();
203231

204232
b12 = wallet_offer_find(tmpctx, wallet, offer_id, &label, &status);
233+
description = offer_description_from_b12(tmpctx, cmd->ld, b12);
205234
if (!b12)
206235
return command_fail(cmd, LIGHTNINGD, "Unknown offer");
207236

@@ -215,7 +244,7 @@ static struct command_result *json_disableoffer(struct command *cmd,
215244
status = wallet_offer_disable(wallet, offer_id, status);
216245

217246
response = json_stream_success(cmd);
218-
json_populate_offer(response, offer_id, b12, label, status);
247+
json_populate_offer(response, offer_id, b12, label, description, status);
219248
return command_success(cmd, response);
220249
}
221250

@@ -234,6 +263,7 @@ static struct command_result *json_enableoffer(struct command *cmd,
234263
struct sha256 *offer_id;
235264
struct wallet *wallet = cmd->ld->wallet;
236265
const char *b12;
266+
const char *description;
237267
const struct json_escape *label;
238268
enum offer_status status;
239269

@@ -243,6 +273,7 @@ static struct command_result *json_enableoffer(struct command *cmd,
243273
return command_param_failed();
244274

245275
b12 = wallet_offer_find(tmpctx, wallet, offer_id, &label, &status);
276+
description = offer_description_from_b12(tmpctx, cmd->ld, b12);
246277
if (!b12)
247278
return command_fail(cmd, LIGHTNINGD, "Unknown offer");
248279

@@ -256,7 +287,7 @@ static struct command_result *json_enableoffer(struct command *cmd,
256287
status = wallet_offer_enable(wallet, offer_id, status);
257288

258289
response = json_stream_success(cmd);
259-
json_populate_offer(response, offer_id, b12, label, status);
290+
json_populate_offer(response, offer_id, b12, label, description, status);
260291
return command_success(cmd, response);
261292
}
262293

0 commit comments

Comments
 (0)