Skip to content

Commit f31f7b1

Browse files
committed
common/sphinx: add helper to prepend length to payload.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 6aa520b commit f31f7b1

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

common/sphinx.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ void sphinx_add_hop(struct sphinx_path *path, const struct pubkey *pubkey,
112112
tal_arr_expand(&path->hops, sp);
113113
}
114114

115+
void sphinx_add_modern_hop(struct sphinx_path *path, const struct pubkey *pubkey,
116+
const u8 *payload TAKES)
117+
{
118+
u8 *with_len = tal_arr(NULL, u8, 0);
119+
size_t len = tal_bytelen(payload);
120+
towire_bigsize(&with_len, len);
121+
towire_u8_array(&with_len, payload, len);
122+
if (taken(payload))
123+
tal_free(payload);
124+
125+
sphinx_add_hop(path, pubkey, take(with_len));
126+
}
127+
115128
/* Small helper to append data to a buffer and update the position
116129
* into the buffer
117130
*/

common/sphinx.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx,
228228
void sphinx_add_hop(struct sphinx_path *path, const struct pubkey *pubkey,
229229
const u8 *payload TAKES);
230230

231+
/**
232+
* Prepend length to payload and add: for onionmessage, any size is OK,
233+
* for HTLC onions tal_bytelen(payload) must be > 1.
234+
*/
235+
void sphinx_add_modern_hop(struct sphinx_path *path, const struct pubkey *pubkey,
236+
const u8 *payload TAKES);
237+
231238
/**
232239
* Compute the size of the serialized payloads.
233240
*/

common/test/run-sphinx-xor_cipher_stream.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
110110
/* Generated stub for towire_amount_sat */
111111
void towire_amount_sat(u8 **pptr UNNEEDED, const struct amount_sat sat UNNEEDED)
112112
{ fprintf(stderr, "towire_amount_sat called!\n"); abort(); }
113+
/* Generated stub for towire_bigsize */
114+
void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED)
115+
{ fprintf(stderr, "towire_bigsize called!\n"); abort(); }
113116
/* Generated stub for towire_bool */
114117
void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED)
115118
{ fprintf(stderr, "towire_bool called!\n"); abort(); }

lightningd/onion_message.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,9 @@ static struct command_result *json_send_onion_message(struct command *cmd,
423423
/* Create an onion which encodes this. */
424424
populate_tlvs(hops, reply_path);
425425
sphinx_path = sphinx_path_new(cmd, NULL);
426-
for (size_t i = 0; i < tal_count(hops); i++) {
427-
/* FIXME: Remove legacy, then length prefix can be removed! */
428-
u8 *tlv_with_len = tal_arr(NULL, u8, 0);
429-
towire_bigsize(&tlv_with_len, tal_bytelen(hops[i].rawtlv));
430-
towire_u8_array(&tlv_with_len,
431-
hops[i].rawtlv, tal_bytelen(hops[i].rawtlv));
432-
sphinx_add_hop(sphinx_path, &hops[i].id, take(tlv_with_len));
433-
}
426+
for (size_t i = 0; i < tal_count(hops); i++)
427+
sphinx_add_modern_hop(sphinx_path, &hops[i].id, hops[i].rawtlv);
428+
434429
/* BOLT-onion-message #4:
435430
* - SHOULD set `len` to 1366 or 32834.
436431
*/

0 commit comments

Comments
 (0)