2022-10-17 01:44:39 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "../../wire/fromwire.c"
|
|
|
|
#include "../../wire/tlvstream.c"
|
|
|
|
#include "../../wire/towire.c"
|
|
|
|
#include "../amount.c"
|
|
|
|
#include "../bigsize.c"
|
|
|
|
#include "../blindedpath.c"
|
|
|
|
#include "../blindedpay.c"
|
|
|
|
#include "../blinding.c"
|
|
|
|
#include "../features.c"
|
|
|
|
#include "../hmac.c"
|
|
|
|
#include "../json_parse.c"
|
|
|
|
#include "../json_parse_simple.c"
|
2022-11-09 01:30:10 +00:00
|
|
|
#include "../onion_encode.c"
|
2022-10-17 01:44:39 +01:00
|
|
|
#include "../sphinx.c"
|
|
|
|
#include "../type_to_string.c"
|
2023-05-22 01:51:44 +01:00
|
|
|
#include "../../wire/onion_wiregen.c"
|
2022-10-17 01:44:39 +01:00
|
|
|
#include <ccan/tal/grab_file/grab_file.h>
|
|
|
|
#include <ccan/tal/path/path.h>
|
|
|
|
#include <common/bolt12.h>
|
|
|
|
#include <common/channel_id.h>
|
|
|
|
#include <common/json_parse.h>
|
|
|
|
#include <common/json_stream.h>
|
|
|
|
#include <common/setup.h>
|
|
|
|
#include <common/wireaddr.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/* AUTOGENERATED MOCKS START */
|
|
|
|
/* Generated stub for mvt_tag_str */
|
|
|
|
const char *mvt_tag_str(enum mvt_tag tag UNNEEDED)
|
|
|
|
{ fprintf(stderr, "mvt_tag_str called!\n"); abort(); }
|
|
|
|
/* Generated stub for new_onionreply */
|
|
|
|
struct onionreply *new_onionreply(const tal_t *ctx UNNEEDED, const u8 *contents TAKES UNNEEDED)
|
|
|
|
{ fprintf(stderr, "new_onionreply called!\n"); abort(); }
|
|
|
|
/* Generated stub for node_id_from_hexstr */
|
|
|
|
bool node_id_from_hexstr(const char *str UNNEEDED, size_t slen UNNEEDED, struct node_id *id UNNEEDED)
|
|
|
|
{ fprintf(stderr, "node_id_from_hexstr called!\n"); abort(); }
|
|
|
|
/* Generated stub for pubkey_from_node_id */
|
|
|
|
bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED)
|
|
|
|
{ fprintf(stderr, "pubkey_from_node_id called!\n"); abort(); }
|
|
|
|
/* AUTOGENERATED MOCKS END */
|
|
|
|
|
2023-01-12 03:54:38 +00:00
|
|
|
#if 0
|
|
|
|
/* Updated each time, as we pretend to be Alice, Bob, Carol */
|
|
|
|
static struct secret mykey;
|
|
|
|
|
|
|
|
static void test_ecdh(const struct pubkey *point, struct secret *ss)
|
|
|
|
{
|
|
|
|
if (secp256k1_ecdh(secp256k1_ctx, ss->data, &point->pubkey,
|
|
|
|
mykey.data, NULL, NULL) != 1)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-10-17 01:44:39 +01:00
|
|
|
static bool json_to_tok(const char *buffer, const jsmntok_t *tok,
|
|
|
|
const jsmntok_t **tokp)
|
|
|
|
{
|
|
|
|
*tokp = tok;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *json;
|
|
|
|
size_t i;
|
|
|
|
jsmn_parser parser;
|
|
|
|
jsmntok_t toks[5000];
|
|
|
|
const jsmntok_t *t, *hops_tok;
|
|
|
|
struct blinded_path *bpath;
|
|
|
|
struct pubkey *ids;
|
|
|
|
u8 **onionhops, *associated_data, *onion, *expected_onion;
|
|
|
|
struct short_channel_id initscid;
|
|
|
|
struct sphinx_path *sp;
|
|
|
|
struct secret session_key, *path_secrets;
|
2023-01-12 03:54:38 +00:00
|
|
|
u32 final_cltv;
|
|
|
|
struct amount_msat initial_amount, final_amount;
|
|
|
|
u32 path_fee_base_msat, path_fee_proportional_millionths, path_cltv_delta;
|
2022-10-17 01:44:39 +01:00
|
|
|
|
|
|
|
common_setup(argv[0]);
|
|
|
|
|
|
|
|
if (argv[1])
|
|
|
|
json = grab_file(tmpctx, argv[1]);
|
|
|
|
else {
|
|
|
|
char *dir = getenv("BOLTDIR");
|
|
|
|
json = grab_file(tmpctx,
|
|
|
|
path_join(tmpctx,
|
|
|
|
dir ? dir : "../bolts",
|
2023-01-12 03:54:38 +00:00
|
|
|
"bolt04/blinded-payment-onion-test.json"));
|
2022-10-17 01:44:39 +01:00
|
|
|
if (!json) {
|
|
|
|
printf("test file not found, skipping\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jsmn_init(&parser);
|
|
|
|
if (jsmn_parse(&parser, json, strlen(json), toks, ARRAY_SIZE(toks)) < 0)
|
|
|
|
abort();
|
|
|
|
|
|
|
|
bpath = tal(tmpctx, struct blinded_path);
|
|
|
|
|
2023-01-12 03:54:38 +00:00
|
|
|
assert(json_scan(tmpctx, json, toks,
|
|
|
|
"{generate:{session_key:%,"
|
|
|
|
"associated_data:%,"
|
|
|
|
"final_amount_msat:%,"
|
|
|
|
"final_cltv:%,"
|
|
|
|
"blinded_payinfo:{fee_base_msat:%,fee_proportional_millionths:%,cltv_expiry_delta:%},"
|
|
|
|
"blinded_route:{introduction_node_id:%,blinding:%,hops:%}}}",
|
2022-10-17 01:44:39 +01:00
|
|
|
JSON_SCAN(json_to_secret, &session_key),
|
|
|
|
JSON_SCAN_TAL(tmpctx, json_tok_bin_from_hex, &associated_data),
|
2023-01-12 03:54:38 +00:00
|
|
|
JSON_SCAN(json_to_msat, &final_amount),
|
|
|
|
JSON_SCAN(json_to_u32, &final_cltv),
|
|
|
|
JSON_SCAN(json_to_u32, &path_fee_base_msat),
|
|
|
|
JSON_SCAN(json_to_u32, &path_fee_proportional_millionths),
|
|
|
|
JSON_SCAN(json_to_u32, &path_cltv_delta),
|
2022-10-17 01:44:39 +01:00
|
|
|
JSON_SCAN(json_to_pubkey, &bpath->first_node_id),
|
|
|
|
JSON_SCAN(json_to_pubkey, &bpath->blinding),
|
|
|
|
JSON_SCAN(json_to_tok, &hops_tok)) == NULL);
|
|
|
|
|
2022-10-17 01:44:39 +01:00
|
|
|
bpath->path = tal_arr(bpath, struct onionmsg_hop *, hops_tok->size);
|
2022-10-17 01:44:39 +01:00
|
|
|
json_for_each_arr(i, t, hops_tok) {
|
2022-10-17 01:44:39 +01:00
|
|
|
bpath->path[i] = tal(bpath->path, struct onionmsg_hop);
|
2022-10-17 01:44:39 +01:00
|
|
|
assert(json_scan(tmpctx, json, t, "{blinded_node_id:%,encrypted_data:%}",
|
2022-10-17 01:44:39 +01:00
|
|
|
JSON_SCAN(json_to_pubkey,
|
|
|
|
&bpath->path[i]->blinded_node_id),
|
2022-10-17 01:44:39 +01:00
|
|
|
JSON_SCAN_TAL(bpath->path[i],
|
|
|
|
json_tok_bin_from_hex,
|
|
|
|
&bpath->path[i]->encrypted_recipient_data)) == NULL);
|
|
|
|
}
|
|
|
|
|
2023-01-12 03:54:38 +00:00
|
|
|
assert(json_scan(tmpctx, json, toks, "{generate:{full_route:{hops:%}}}",
|
|
|
|
JSON_SCAN(json_to_tok, &hops_tok)) == NULL);
|
|
|
|
|
|
|
|
/* We have to read scid from first hop contents, since it's made up */
|
|
|
|
assert(json_scan(tmpctx, json, hops_tok + 1, "{tlvs:{outgoing_channel_id:%}}",
|
|
|
|
JSON_SCAN(json_to_short_channel_id, &initscid)) == NULL);
|
|
|
|
|
|
|
|
initial_amount = final_amount;
|
|
|
|
assert(amount_msat_add_fee(&initial_amount,
|
|
|
|
path_fee_base_msat, path_fee_proportional_millionths));
|
|
|
|
|
|
|
|
/* FIXME: Test vector actually claims total_amount_msat is 150000msat! */
|
|
|
|
struct amount_msat total_amount;
|
|
|
|
assert(amount_msat_add(&total_amount, final_amount, AMOUNT_MSAT(50000)));
|
|
|
|
onionhops = blinded_onion_hops(tmpctx, final_amount, final_cltv, total_amount, bpath);
|
2022-10-17 01:44:39 +01:00
|
|
|
|
2023-01-12 03:54:38 +00:00
|
|
|
/* Prepend Alice: poor thing doesn't speak blinding! (But doesn't charge fees!) */
|
2022-10-17 01:44:39 +01:00
|
|
|
tal_resize(&onionhops, tal_count(onionhops) + 1);
|
|
|
|
memmove(onionhops + 1, onionhops,
|
|
|
|
(tal_count(onionhops) - 1) * sizeof(*onionhops));
|
|
|
|
onionhops[0] = onion_nonfinal_hop(onionhops, &initscid,
|
2023-01-12 03:54:38 +00:00
|
|
|
initial_amount, final_cltv + path_cltv_delta);
|
2022-10-17 01:44:39 +01:00
|
|
|
|
|
|
|
ids = tal_arr(tmpctx, struct pubkey, hops_tok->size);
|
|
|
|
json_for_each_arr(i, t, hops_tok) {
|
|
|
|
u8 *payload;
|
|
|
|
assert(json_scan(tmpctx, json, t, "{payload:%,pubkey:%}",
|
|
|
|
JSON_SCAN_TAL(tmpctx,
|
|
|
|
json_tok_bin_from_hex,
|
|
|
|
&payload),
|
|
|
|
JSON_SCAN(json_to_pubkey, &ids[i])) == NULL);
|
|
|
|
assert(memeq(payload, tal_bytelen(payload),
|
|
|
|
onionhops[i], tal_bytelen(onionhops[i])));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, create onion! */
|
|
|
|
sp = sphinx_path_new_with_key(tmpctx, associated_data, &session_key);
|
|
|
|
for (i = 0; i < tal_count(ids); i++)
|
2023-01-12 03:54:38 +00:00
|
|
|
sphinx_add_hop_has_length(sp, &ids[i], onionhops[i]);
|
2022-10-17 01:44:39 +01:00
|
|
|
|
|
|
|
onion = serialize_onionpacket(tmpctx,
|
|
|
|
create_onionpacket(tmpctx, sp, ROUTING_INFO_SIZE,
|
|
|
|
&path_secrets));
|
|
|
|
assert(json_scan(tmpctx, json, toks, "{generate:{onion:%}}",
|
|
|
|
JSON_SCAN_TAL(tmpctx,
|
|
|
|
json_tok_bin_from_hex,
|
|
|
|
&expected_onion)) == NULL);
|
|
|
|
assert(memeq(expected_onion, tal_bytelen(expected_onion),
|
|
|
|
onion, tal_bytelen(onion)));
|
|
|
|
|
|
|
|
/* FIXME: unwrap and test! */
|
2023-01-12 03:54:38 +00:00
|
|
|
#if 0
|
|
|
|
struct onionpacket *op;
|
|
|
|
struct pubkey *blinding;
|
|
|
|
|
|
|
|
assert(json_scan(tmpctx, json, toks, "{decrypt:{hops:%}}",
|
|
|
|
JSON_SCAN(json_to_tok, &hops_tok)) == NULL);
|
|
|
|
op = parse_onionpacket(tmpctx, expected_onion, tal_bytelen(expected_onion), NULL);
|
|
|
|
blinding = NULL;
|
|
|
|
json_for_each_arr(i, t, hops_tok) {
|
|
|
|
struct route_step *rs;
|
|
|
|
struct secret ss;
|
|
|
|
const u8 *serialized;
|
|
|
|
|
|
|
|
assert(json_scan(tmpctx, json, t, "{node_privkey:%,onion:%}",
|
|
|
|
JSON_SCAN(json_to_secret, &mykey),
|
|
|
|
JSON_SCAN_TAL(tmpctx, json_tok_bin_from_hex, &expected_onion))
|
|
|
|
== NULL);
|
|
|
|
serialized = serialize_onionpacket(tmpctx, op);
|
|
|
|
assert(memeq(expected_onion, tal_bytelen(expected_onion),
|
|
|
|
serialized, tal_bytelen(serialized)));
|
|
|
|
|
|
|
|
if (blinding) {
|
|
|
|
assert(unblind_onion(blinding, test_ecdh,
|
|
|
|
&op->ephemeralkey, &ss));
|
|
|
|
} else {
|
|
|
|
test_ecdh(&op->ephemeralkey, &ss);
|
|
|
|
}
|
|
|
|
rs = process_onionpacket(tmpctx, op, &ss, associated_data,
|
|
|
|
tal_bytelen(associated_data), true);
|
|
|
|
assert(memeq(rs->raw_payload, tal_bytelen(rs->raw_payload),
|
|
|
|
onionhops[i], tal_bytelen(onionhops[i])));
|
|
|
|
if (rs->nextcase == ONION_FORWARD)
|
|
|
|
op = rs->next;
|
|
|
|
else
|
|
|
|
op = NULL;
|
|
|
|
blinding = tal(tmpctx, struct pubkey);
|
|
|
|
/* Alice doesn't have a blinding! */
|
|
|
|
if (json_scan(tmpctx, json, t, "{next_blinding:%}",
|
|
|
|
JSON_SCAN(json_to_pubkey, blinding)) != NULL)
|
|
|
|
blinding = NULL;
|
|
|
|
}
|
|
|
|
assert(!op);
|
|
|
|
#endif
|
2022-10-17 01:44:39 +01:00
|
|
|
|
|
|
|
out:
|
|
|
|
common_shutdown();
|
|
|
|
}
|