common: sphinx_path_new to take explicit len.

Useful if associated_data is not a tal pointer (xpay wants this).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-11-17 16:05:06 +10:30
parent 80357911fb
commit ef475db478
10 changed files with 33 additions and 15 deletions

View File

@ -58,10 +58,18 @@ struct sphinx_path {
struct pubkey *rendezvous_id;
};
struct sphinx_path *sphinx_path_new(const tal_t *ctx, const u8 *associated_data)
struct sphinx_path *sphinx_path_new(const tal_t *ctx,
const u8 *associated_data,
size_t associated_data_len)
{
struct sphinx_path *sp = tal(ctx, struct sphinx_path);
sp->associated_data = tal_dup_talarr(sp, u8, associated_data);
if (associated_data) {
sp->associated_data
= tal_dup_arr(sp, u8, associated_data, associated_data_len, 0);
} else {
assert(associated_data_len == 0);
sp->associated_data = NULL;
}
sp->session_key = NULL;
sp->rendezvous_id = NULL;
sp->hops = tal_arr(sp, struct sphinx_hop, 0);
@ -70,9 +78,10 @@ struct sphinx_path *sphinx_path_new(const tal_t *ctx, const u8 *associated_data)
struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx,
const u8 *associated_data,
size_t associated_data_len,
const struct secret *session_key)
{
struct sphinx_path *sp = sphinx_path_new(ctx, associated_data);
struct sphinx_path *sp = sphinx_path_new(ctx, associated_data, associated_data_len);
sp->session_key = tal_dup(sp, struct secret, session_key);
return sp;
}

View File

@ -182,7 +182,8 @@ u8 *unwrap_onionreply(const tal_t *ctx,
* passed to `create_onionpacket` to generate the packet.
*/
struct sphinx_path *sphinx_path_new(const tal_t *ctx,
const u8 *associated_data);
const u8 *associated_data,
size_t associated_data_len);
/**
* Create a new empty sphinx_path with a given `session_key`.
@ -192,6 +193,7 @@ struct sphinx_path *sphinx_path_new(const tal_t *ctx,
*/
struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx,
const u8 *associated_data,
size_t associated_data_len,
const struct secret *session_key);
/**

View File

@ -235,7 +235,7 @@ int main(int argc, char *argv[])
pubkey_from_privkey(&path_key[DAVE], &path_key_pub[DAVE]);
/* Create an onion which encodes this. */
sphinx_path = sphinx_path_new(tmpctx, NULL);
sphinx_path = sphinx_path_new(tmpctx, NULL, 0);
for (size_t i = 0; i < 4; i++) {
struct tlv_onionmsg_tlv *payload
= tlv_onionmsg_tlv_new(tmpctx);

View File

@ -306,7 +306,7 @@ int main(int argc, char *argv[])
json_strfield("unknown_tag_1", "68656c6c6f");
/* Create the onionmessage */
sphinx_path = sphinx_path_new(tmpctx, NULL);
sphinx_path = sphinx_path_new(tmpctx, NULL, 0);
for (size_t i = 0; i < ARRAY_SIZE(erd); i++) {
struct tlv_onionmsg_tlv *tlv = tlv_onionmsg_tlv_new(tmpctx);
u8 *onionmsg_tlv;

View File

@ -161,7 +161,8 @@ int main(int argc, char *argv[])
generate_tok = json_get_member(json, toks, "generate");
json_to_secret(json, json_get_member(json, generate_tok, "session_key"), &session_key);
assoc_data = json_tok_bin_from_hex(tmpctx, json, json_get_member(json, generate_tok, "associated_data"));
sp = sphinx_path_new_with_key(tmpctx, assoc_data, &session_key);
sp = sphinx_path_new_with_key(tmpctx, assoc_data, tal_bytelen(assoc_data),
&session_key);
json_for_each_arr(i, t, json_get_member(json, generate_tok, "hops")) {
struct pubkey k;
const u8 *cursor;

View File

@ -171,7 +171,8 @@ int main(int argc, char *argv[])
}
/* Now, create onion! */
sp = sphinx_path_new_with_key(tmpctx, associated_data, &session_key);
sp = sphinx_path_new_with_key(tmpctx, associated_data, tal_bytelen(associated_data),
&session_key);
for (i = 0; i < tal_count(ids); i++)
sphinx_add_hop_has_length(sp, &ids[i], onionhops[i]);

View File

@ -36,7 +36,8 @@ static void do_generate(int argc, char **argv,
memset(&session_key, 'A', sizeof(struct secret));
sp = sphinx_path_new_with_key(ctx, assocdata, &session_key);
sp = sphinx_path_new_with_key(ctx, assocdata, tal_bytelen(assocdata),
&session_key);
sphinx_path_set_rendezvous(sp, rvnode_id);
for (int i = 0; i < num_hops; i++) {
@ -218,7 +219,8 @@ static void runtest(const char *filename)
associated_data = json_tok_bin_from_hex(ctx, buffer, associated_data_tok);
session_key_raw = json_tok_bin_from_hex(ctx, buffer, session_key_tok);
memcpy(&session_key, session_key_raw, sizeof(session_key));
path = sphinx_path_new_with_key(ctx, associated_data, &session_key);
path = sphinx_path_new_with_key(ctx, associated_data, tal_bytelen(associated_data),
&session_key);
/* Unpack the hops and build up the path */
hopstok = json_get_member(buffer, gentok, "hops");

View File

@ -236,7 +236,7 @@ static struct command_result *json_injectonionmessage(struct command *cmd,
"experimental-onion-messages not enabled");
/* Create an onion which encodes this. */
sphinx_path = sphinx_path_new(cmd, NULL);
sphinx_path = sphinx_path_new(cmd, NULL, 0);
for (size_t i = 0; i < tal_count(hops); i++)
sphinx_add_hop(sphinx_path, &hops[i].node, hops[i].tlv);

View File

@ -1196,7 +1196,7 @@ send_payment(struct lightningd *ld,
and use bitcoind's block height, even if we're behind in processing */
base_expiry = get_network_blockheight(ld->topology) + 1;
path = sphinx_path_new(tmpctx, rhash->u.u8);
path = sphinx_path_new(tmpctx, rhash->u.u8, sizeof(rhash->u.u8));
/* Extract IDs for each hop: create_onionpacket wants array. */
for (i = 0; i < n_hops; i++)
ids[i] = route[i].node_id;
@ -2340,9 +2340,10 @@ static struct command_result *json_createonion(struct command *cmd,
}
if (session_key == NULL)
sp = sphinx_path_new(cmd, assocdata);
sp = sphinx_path_new(cmd, assocdata, tal_bytelen(assocdata));
else
sp = sphinx_path_new_with_key(cmd, assocdata, session_key);
sp = sphinx_path_new_with_key(cmd, assocdata, tal_bytelen(assocdata),
session_key);
for (size_t i=0; i<tal_count(hops); i++) {
if (!sphinx_add_hop_has_length(sp, &hops[i].pubkey, hops[i].raw_payload))

View File

@ -962,11 +962,13 @@ void sphinx_add_v0_hop(struct sphinx_path *path UNNEEDED, const struct pubkey *p
{ fprintf(stderr, "sphinx_add_v0_hop called!\n"); abort(); }
/* Generated stub for sphinx_path_new */
struct sphinx_path *sphinx_path_new(const tal_t *ctx UNNEEDED,
const u8 *associated_data UNNEEDED)
const u8 *associated_data UNNEEDED,
size_t associated_data_len UNNEEDED)
{ fprintf(stderr, "sphinx_path_new called!\n"); abort(); }
/* Generated stub for sphinx_path_new_with_key */
struct sphinx_path *sphinx_path_new_with_key(const tal_t *ctx UNNEEDED,
const u8 *associated_data UNNEEDED,
size_t associated_data_len UNNEEDED,
const struct secret *session_key UNNEEDED)
{ fprintf(stderr, "sphinx_path_new_with_key called!\n"); abort(); }
/* Generated stub for sphinx_path_payloads_size */