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]);