Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createonion to accept an optional custom onion_size #4519

Merged
merged 3 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions devtools/onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static struct route_step *decode_with_privkey(const tal_t *ctx, const u8 *onion,
if (!hex_decode(hexprivkey, strlen(hexprivkey), &seckey, sizeof(seckey)))
errx(1, "Invalid private key hex '%s'", hexprivkey);

packet = parse_onionpacket(tmpctx, onion, TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE), &why_bad);
packet = parse_onionpacket(tmpctx, onion, tal_bytelen(onion), &why_bad);

if (!packet)
errx(1, "Error parsing message: %s", onion_wire_name(why_bad));
Expand All @@ -147,7 +147,7 @@ static struct route_step *decode_with_privkey(const tal_t *ctx, const u8 *onion,
static void do_decode(int argc, char **argv, const u8 *assocdata)
{
const tal_t *ctx = talz(NULL, tal_t);
u8 serialized[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
u8 *serialized;
struct route_step *step;

if (argc != 4)
Expand All @@ -161,7 +161,8 @@ static void do_decode(int argc, char **argv, const u8 *assocdata)
while (isspace(hextemp[hexlen-1]))
hexlen--;

if (!hex_decode(hextemp, hexlen, serialized, sizeof(serialized))) {
serialized = tal_hexdata(hextemp, hextemp, hexlen);
if (!serialized) {
errx(1, "Invalid onion hex '%s'", hextemp);
}

Expand Down
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

6 changes: 4 additions & 2 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -1671,13 +1671,15 @@ 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;

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_def("onion_size", param_number, &packet_size, ROUTING_INFO_SIZE),
NULL)) {
return command_param_failed();
}
Expand All @@ -1690,12 +1692,12 @@ static struct command_result *json_createonion(struct command *cmd,
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
17 changes: 16 additions & 1 deletion tests/test_pay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2816,7 +2816,8 @@ def test_partial_payment_htlc_loss(node_factory, bitcoind):
def test_createonion_limits(node_factory):
l1, = node_factory.get_nodes(1)
hops = [{
"pubkey": "02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619",
# privkey: 41bfd2660762506c9933ade59f1debf7e6495b10c14a92dbcd2d623da2507d3d
"pubkey": "0266e4598d1d3c415f572a8488830b60f7e744ed9235eb0b1ba93283b315c03518",
"payload": "00" * 228
}, {
"pubkey": "0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c",
Expand All @@ -2840,6 +2841,20 @@ def test_createonion_limits(node_factory):
hops[0]['payload'] += '01'
l1.rpc.createonion(hops=hops, assocdata="BB" * 32)

# But with a larger onion, it will work!
oniontool = os.path.join(os.path.dirname(__file__), "..", "devtools", "onion")
onion = l1.rpc.createonion(hops=hops, assocdata="BB" * 32, onion_size=1301)['onion']

# Oniontool wants a filename :(
onionfile = os.path.join(l1.daemon.lightning_dir, 'onion')
with open(onionfile, "w") as f:
f.write(onion)

subprocess.check_output(
[oniontool, '--assoc-data', "BB" * 32,
'decode', onionfile, "41bfd2660762506c9933ade59f1debf7e6495b10c14a92dbcd2d623da2507d3d"]
)


@pytest.mark.developer("needs use_shadow")
def test_blockheight_disagreement(node_factory, bitcoind, executor):
Expand Down