Skip to content

Commit 611b2aa

Browse files
committed
renepay: make self payments with renesendpay
Changelog-None. Signed-off-by: Lagrang3 <[email protected]>
1 parent 4004804 commit 611b2aa

File tree

2 files changed

+91
-49
lines changed

2 files changed

+91
-49
lines changed

plugins/renepay/mods.c

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,8 @@ static struct command_result *selfpay_success(struct command *cmd,
258258
const char *method UNUSED,
259259
const char *buf,
260260
const jsmntok_t *tok,
261-
struct route *route)
261+
struct payment *payment)
262262
{
263-
tal_steal(tmpctx, route); // discard this route when tmpctx clears
264-
struct payment *payment =
265-
payment_map_get(pay_plugin->payment_map, route->key.payment_hash);
266-
assert(payment);
267-
268263
struct preimage preimage;
269264
const char *err;
270265
err = json_scan(tmpctx, buf, tok, "{payment_preimage:%}",
@@ -282,18 +277,17 @@ static struct command_result *selfpay_failure(struct command *cmd,
282277
const char *method UNUSED,
283278
const char *buf,
284279
const jsmntok_t *tok,
285-
struct route *route)
280+
struct payment *payment)
286281
{
287-
tal_steal(tmpctx, route); // discard this route when tmpctx clears
288-
struct payment *payment =
289-
payment_map_get(pay_plugin->payment_map, route->key.payment_hash);
290-
assert(payment);
291-
struct payment_result *result = tal_sendpay_result_from_json(tmpctx, buf, tok);
292-
if (result == NULL)
293-
plugin_err(pay_plugin->plugin,
282+
struct payment_result *result =
283+
tal_sendpay_result_from_json(tmpctx, buf, tok);
284+
if (result == NULL) {
285+
plugin_log(pay_plugin->plugin, LOG_UNUSUAL,
294286
"Unable to parse sendpay failure: %.*s",
295287
json_tok_full_len(tok), json_tok_full(buf, tok));
296-
288+
return payment_fail(payment, LIGHTNINGD,
289+
"Self pay failed for unknown reason");
290+
}
297291
return payment_fail(payment, result->code, "%s", result->message);
298292
}
299293

@@ -310,17 +304,32 @@ static struct command_result *selfpay_cb(struct payment *payment)
310304
"Selfpay: cannot get a valid cmd.");
311305

312306
struct payment_info *pinfo = &payment->payment_info;
313-
/* Self-payment routes are not part of the routetracker, we build them
314-
* on-the-fly here and release them on success or failure. */
315-
struct route *route =
316-
new_route(payment, payment->groupid,
317-
/*partid=*/0, pinfo->payment_hash,
318-
pinfo->amount, pinfo->amount);
319307
struct out_req *req;
320-
req = jsonrpc_request_start(cmd, "sendpay",
321-
selfpay_success, selfpay_failure, route);
322-
route->hops = tal_arr(route, struct route_hop, 0);
323-
json_add_route(req->js, route, payment);
308+
req = jsonrpc_request_start(cmd, "renesendpay", selfpay_success,
309+
selfpay_failure, payment);
310+
json_add_sha256(req->js, "payment_hash", &pinfo->payment_hash);
311+
json_add_u64(req->js, "partid", 0);
312+
json_add_u64(req->js, "groupid", payment->groupid);
313+
json_add_string(req->js, "invoice", pinfo->invstr);
314+
json_add_node_id(req->js, "destination", &pinfo->destination);
315+
json_add_amount_msat(req->js, "amount_msat", pinfo->amount);
316+
json_add_amount_msat(req->js, "total_amount_msat", pinfo->amount);
317+
json_add_u32(req->js, "final_cltv", pinfo->final_cltv);
318+
if (pinfo->label)
319+
json_add_string(req->js, "label", pinfo->label);
320+
if (pinfo->description)
321+
json_add_string(req->js, "description", pinfo->description);
322+
/* An empty route means a payment to oneself, pathlen=0 */
323+
json_array_start(req->js, "route");
324+
json_array_end(req->js);
325+
if (pinfo->payment_secret)
326+
json_add_secret(req->js, "payment_secret",
327+
pinfo->payment_secret);
328+
else {
329+
assert(pinfo->blinded_paths);
330+
const struct blinded_path *bpath = pinfo->blinded_paths[0];
331+
json_myadd_blinded_path(req->js, "blinded_path", bpath);
332+
}
324333
return send_outreq(req);
325334
}
326335

plugins/renepay/sendpay.c

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static struct command_result *sendpay_rpc_failure(struct command *cmd,
145145
json_to_u32(buffer, codetok, &errcode);
146146
else
147147
errcode = LIGHTNINGD;
148-
148+
// FIXME: make this response more useful, maybe similar to waitsendpay
149149
return command_fail(
150150
cmd, errcode, "renesendpay failed to error in RPC: %.*s",
151151
json_tok_full_len(toks), json_tok_full(buffer, toks));
@@ -297,6 +297,11 @@ static struct command_result *sendonion_done(struct command *cmd,
297297
renesendpay->total_amount);
298298
json_add_string(response, "invoice", renesendpay->invoice);
299299

300+
const jsmntok_t *preimagetok =
301+
json_get_member(buffer, toks, "payment_preimage");
302+
if (preimagetok)
303+
json_add_tok(response, "payment_preimage", preimagetok, buffer);
304+
300305
if (renesendpay->label)
301306
json_add_string(response, "label", renesendpay->label);
302307
if (renesendpay->description)
@@ -338,31 +343,55 @@ static struct command_result *waitblockheight_done(struct command *cmd,
338343
const u8 *onion;
339344
struct out_req *req;
340345

341-
onion =
342-
create_onion(tmpctx, renesendpay, renesendpay->route[0].node_id, 1);
343-
req = jsonrpc_request_start(cmd, "sendonion", sendonion_done,
344-
sendpay_rpc_failure, renesendpay);
345-
json_add_hex_talarr(req->js, "onion", onion);
346+
if (tal_count(renesendpay->route) > 0) {
347+
onion = create_onion(tmpctx, renesendpay,
348+
renesendpay->route[0].node_id, 1);
349+
req = jsonrpc_request_start(cmd, "sendonion", sendonion_done,
350+
sendpay_rpc_failure, renesendpay);
351+
json_add_hex_talarr(req->js, "onion", onion);
352+
353+
json_add_amount_msat(req->js, "amount_msat",
354+
renesendpay->deliver_amount);
355+
356+
const struct route_hop *hop = &renesendpay->route[0];
357+
json_object_start(req->js, "first_hop");
358+
json_add_amount_msat(req->js, "amount_msat", hop->amount);
359+
json_add_num(req->js, "delay",
360+
hop->delay + renesendpay->blockheight);
361+
json_add_node_id(req->js, "id", &hop->node_id);
362+
json_add_short_channel_id(req->js, "channel", hop->scid);
363+
json_object_end(req->js);
364+
365+
json_array_start(req->js, "shared_secrets");
366+
for (size_t i = 0; i < tal_count(renesendpay->shared_secrets);
367+
i++) {
368+
json_add_secret(req->js, NULL,
369+
&renesendpay->shared_secrets[i]);
370+
}
371+
json_array_end(req->js);
372+
373+
json_add_node_id(req->js, "destination",
374+
&renesendpay->destination);
375+
376+
} else {
377+
/* self payment */
378+
onion = NULL;
379+
req = jsonrpc_request_start(cmd, "sendpay", sendonion_done,
380+
sendpay_rpc_failure, renesendpay);
381+
json_array_start(req->js, "route");
382+
json_array_end(req->js);
383+
json_add_amount_msat(req->js, "amount_msat",
384+
renesendpay->total_amount);
385+
if(renesendpay->payment_secret)
386+
json_add_secret(req->js, "payment_secret", renesendpay->payment_secret);
387+
else{
388+
// FIXME: get payment_secret from blinded path
389+
}
390+
}
391+
346392
json_add_sha256(req->js, "payment_hash", &renesendpay->payment_hash);
347393
json_add_u64(req->js, "partid", renesendpay->partid);
348394
json_add_u64(req->js, "groupid", renesendpay->groupid);
349-
json_add_node_id(req->js, "destination", &renesendpay->destination);
350-
json_add_amount_msat(req->js, "amount_msat", renesendpay->deliver_amount);
351-
352-
const struct route_hop *hop = &renesendpay->route[0];
353-
json_object_start(req->js, "first_hop");
354-
json_add_amount_msat(req->js, "amount_msat", hop->amount);
355-
json_add_num(req->js, "delay", hop->delay + renesendpay->blockheight);
356-
json_add_node_id(req->js, "id", &hop->node_id);
357-
json_add_short_channel_id(req->js, "channel", hop->scid);
358-
json_object_end(req->js);
359-
360-
json_array_start(req->js, "shared_secrets");
361-
for (size_t i = 0; i < tal_count(renesendpay->shared_secrets); i++) {
362-
json_add_secret(req->js, NULL, &renesendpay->shared_secrets[i]);
363-
}
364-
json_array_end(req->js);
365-
366395
if (renesendpay->label)
367396
json_add_string(req->js, "label", renesendpay->label);
368397
if (renesendpay->description)
@@ -431,7 +460,11 @@ struct command_result *json_renesendpay(struct command *cmd,
431460
renesendpay->partid = *partid;
432461
renesendpay->groupid = *groupid;
433462

434-
renesendpay->sent_amount = renesendpay->route[0].amount;
463+
if (tal_count(renesendpay->route) > 0)
464+
renesendpay->sent_amount = renesendpay->route[0].amount;
465+
else /* this might be a self pay */
466+
renesendpay->sent_amount = *amount;
467+
435468
renesendpay->total_amount = *total_amount;
436469
renesendpay->deliver_amount = *amount;
437470
renesendpay->final_cltv = *final_cltv;

0 commit comments

Comments
 (0)