Skip to content

Commit

Permalink
"createonion" to accept an optional custom onion_size.
Browse files Browse the repository at this point in the history
Changelog-Added: `createonion` RPC command now accepts an optional `onion_size`.
  • Loading branch information
fiatjaf committed May 10, 2021
1 parent 9825f32 commit 4971fd9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
13 changes: 9 additions & 4 deletions doc/lightning-createonion.7

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions doc/lightning-createonion.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lightning-createonion -- Low-level command to create a custom onion
SYNOPSIS
--------

**createonion** *hops* *assocdata* \[*session_key*\]
**createonion** *hops* *assocdata* \[*session_key*\] \[*onion_size*\]

DESCRIPTION
-----------
Expand Down Expand Up @@ -68,10 +68,10 @@ which the above *hops* parameter was generated:

- Notice that the payload in the *hops* parameter is the hex-encoded version
of the parameters in the `getroute` response.
- The payloads are shifted left by one, i.e., payload 0 in `createonion`
corresponds to payload 1 from `getroute`.
- Except for the pubkey, the values are shifted left by one, i.e., the 1st
payload in `createonion` corresponds to the 2nd set of values from `getroute`.
- The final payload is a copy of the last payload sans `channel`

These rules are directly derived from the onion construction. Please refer
[BOLT 04][bolt04] for details and rationale.

Expand All @@ -85,6 +85,10 @@ should only be used for testing or if a specific shared secret is
important. If not specified it will be securely generated internally, and the
shared secrets will be returned.

The optional *onion_size* parameter specifies a size different from the default
payment onion (1300 bytes). May be used for custom protocols like trampoline
routing.

RETURN VALUE
------------

Expand Down Expand Up @@ -122,4 +126,3 @@ RESOURCES
Main web site: <https://github.com/ElementsProject/lightning>

[bolt04]: https://github.com/lightningnetwork/lightning-rfc/blob/master/04-onion-routing.md

11 changes: 9 additions & 2 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -1670,13 +1670,16 @@ static struct command_result *json_createonion(struct command *cmd,
struct secret *session_key, *shared_secrets;
struct sphinx_path *sp;
u8 *assocdata, *serialized;
u32 *packet_size;
struct onionpacket *packet;
struct sphinx_hop *hops;
u32 default_packet_size = ROUTING_INFO_SIZE;

if (!param(cmd, buffer, params,
p_req("hops", param_hops_array, &hops),
p_req("assocdata", param_bin_from_hex, &assocdata),
p_opt("session_key", param_secret, &session_key),
p_opt("onion_size", param_number, &packet_size),
NULL)) {
return command_param_failed();
}
Expand All @@ -1686,15 +1689,19 @@ static struct command_result *json_createonion(struct command *cmd,
else
sp = sphinx_path_new_with_key(cmd, assocdata, session_key);

if (packet_size == NULL || *packet_size == 0) {
packet_size = &default_packet_size;
}

for (size_t i=0; i<tal_count(hops); i++)
sphinx_add_hop(sp, &hops[i].pubkey, hops[i].raw_payload);

if (sphinx_path_payloads_size(sp) > ROUTING_INFO_SIZE)
if (sphinx_path_payloads_size(sp) > *packet_size)
return command_fail(
cmd, JSONRPC2_INVALID_PARAMS,
"Payloads exceed maximum onion packet size.");

packet = create_onionpacket(cmd, sp, ROUTING_INFO_SIZE, &shared_secrets);
packet = create_onionpacket(cmd, sp, *packet_size, &shared_secrets);
if (!packet)
return command_fail(cmd, LIGHTNINGD,
"Could not create onion packet");
Expand Down

0 comments on commit 4971fd9

Please sign in to comment.