Skip to content

Commit

Permalink
pay: add realm and data elements to sendpay route input
Browse files Browse the repository at this point in the history
This allows the caller to specify non-default realm and per-hop data in the route.
  • Loading branch information
bitonic-cjp committed Jul 17, 2018
1 parent a093d27 commit aa216e4
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ static void json_sendpay(struct command *cmd,
jsmntok_t *routetok, *rhashtok;
jsmntok_t *msatoshitok;
const jsmntok_t *t, *end;
size_t n_hops;
size_t i, n_hops;
struct sha256 rhash;
struct route_hop *route;
struct custom_route_data custom_route_data;
Expand Down Expand Up @@ -997,6 +997,7 @@ static void json_sendpay(struct command *cmd,
n_hops = 0;
route = tal_arr(cmd, struct route_hop, n_hops);

/* First pass through the route input */
for (t = routetok + 1; t < end; t = json_next(t)) {
const jsmntok_t *amttok, *idtok, *delaytok, *chantok;

Expand Down Expand Up @@ -1048,13 +1049,51 @@ static void json_sendpay(struct command *cmd,
n_hops++;
}

custom_route_data = route_to_custom_route_data(cmd->ld, route);

if (n_hops == 0) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS, "Empty route");
return;
}

/* Default (realm = 0) realm / hop_data, and other route data */
custom_route_data = route_to_custom_route_data(cmd->ld, route);

/* Second pass through the route input, to override realm / hop_data */
for (i=0, t = routetok + 1; t < end; i++, t = json_next(t)) {
const jsmntok_t *datatok, *realmtok;

/*
No need to check that t->type != JSMN_OBJECT:
that was already done in the first pass.
*/

realmtok = json_get_member(buffer, t, "realm");
datatok = json_get_member(buffer, t, "data");

if (realmtok) {
unsigned int realm;
if (!json_tok_number(buffer, realmtok, &realm) ||
realm > 0xff) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Route %zu invalid realm",
i);
return;
}
custom_route_data.hop_data[i].realm = realm;
}

if (datatok) {
if (!hex_decode(buffer + datatok->start,
datatok->end - datatok->start,
custom_route_data.hop_data[i].per_hop_data,
sizeof(custom_route_data.hop_data[i].per_hop_data))) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Route %zu invalid data",
i);
return;
}
}
}

if (msatoshitok) {
if (!json_tok_u64(buffer, msatoshitok, &msatoshi)) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
Expand Down

0 comments on commit aa216e4

Please sign in to comment.