common/sphinx: add helper to prepend length to payload.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2021-09-21 13:35:24 +09:30
parent 6aa520bb9b
commit f31f7b1eec
4 changed files with 26 additions and 8 deletions

View File

@@ -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
*/

View File

@@ -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.
*/

View File

@@ -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(); }

View File

@@ -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.
*/