diff --git a/common/sphinx.c b/common/sphinx.c index 27cb5ff61..1eb4496ea 100644 --- a/common/sphinx.c +++ b/common/sphinx.c @@ -112,6 +112,19 @@ void sphinx_add_hop(struct sphinx_path *path, const struct pubkey *pubkey, tal_arr_expand(&path->hops, sp); } +void sphinx_add_modern_hop(struct sphinx_path *path, const struct pubkey *pubkey, + const u8 *payload TAKES) +{ + u8 *with_len = tal_arr(NULL, u8, 0); + size_t len = tal_bytelen(payload); + towire_bigsize(&with_len, len); + towire_u8_array(&with_len, payload, len); + if (taken(payload)) + tal_free(payload); + + sphinx_add_hop(path, pubkey, take(with_len)); +} + /* Small helper to append data to a buffer and update the position * into the buffer */ diff --git a/common/sphinx.h b/common/sphinx.h index dcce024c1..2d7c3727b 100644 --- a/common/sphinx.h +++ b/common/sphinx.h @@ -228,6 +228,13 @@ struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx, void sphinx_add_hop(struct sphinx_path *path, const struct pubkey *pubkey, const u8 *payload TAKES); +/** + * Prepend length to payload and add: for onionmessage, any size is OK, + * for HTLC onions tal_bytelen(payload) must be > 1. + */ +void sphinx_add_modern_hop(struct sphinx_path *path, const struct pubkey *pubkey, + const u8 *payload TAKES); + /** * Compute the size of the serialized payloads. */ diff --git a/common/test/run-sphinx-xor_cipher_stream.c b/common/test/run-sphinx-xor_cipher_stream.c index bcbea52e3..a742cc5ab 100644 --- a/common/test/run-sphinx-xor_cipher_stream.c +++ b/common/test/run-sphinx-xor_cipher_stream.c @@ -110,6 +110,9 @@ void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED) /* Generated stub for towire_amount_sat */ void towire_amount_sat(u8 **pptr UNNEEDED, const struct amount_sat sat UNNEEDED) { fprintf(stderr, "towire_amount_sat called!\n"); abort(); } +/* Generated stub for towire_bigsize */ +void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED) +{ fprintf(stderr, "towire_bigsize called!\n"); abort(); } /* Generated stub for towire_bool */ void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED) { fprintf(stderr, "towire_bool called!\n"); abort(); } diff --git a/lightningd/onion_message.c b/lightningd/onion_message.c index 048a42ff7..40794f1af 100644 --- a/lightningd/onion_message.c +++ b/lightningd/onion_message.c @@ -423,14 +423,9 @@ static struct command_result *json_send_onion_message(struct command *cmd, /* Create an onion which encodes this. */ populate_tlvs(hops, reply_path); sphinx_path = sphinx_path_new(cmd, NULL); - for (size_t i = 0; i < tal_count(hops); i++) { - /* FIXME: Remove legacy, then length prefix can be removed! */ - u8 *tlv_with_len = tal_arr(NULL, u8, 0); - towire_bigsize(&tlv_with_len, tal_bytelen(hops[i].rawtlv)); - towire_u8_array(&tlv_with_len, - hops[i].rawtlv, tal_bytelen(hops[i].rawtlv)); - sphinx_add_hop(sphinx_path, &hops[i].id, take(tlv_with_len)); - } + for (size_t i = 0; i < tal_count(hops); i++) + sphinx_add_modern_hop(sphinx_path, &hops[i].id, hops[i].rawtlv); + /* BOLT-onion-message #4: * - SHOULD set `len` to 1366 or 32834. */