MCPcopy Create free account
hub / github.com/ElementsProject/lightning / json_createonion

Function json_createonion

lightningd/pay.c:2404–2462  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2402AUTODATA(json_command, &delpay_command);
2403
2404static struct command_result *json_createonion(struct command *cmd,
2405 const char *buffer,
2406 const jsmntok_t *obj UNNEEDED,
2407 const jsmntok_t *params)
2408{
2409 struct json_stream *response;
2410 struct secret *session_key, *shared_secrets;
2411 struct sphinx_path *sp;
2412 u8 *assocdata, *serialized;
2413 u32 *packet_size;
2414 struct onionpacket *packet;
2415 struct sphinx_hop *hops;
2416
2417 if (!param_check(cmd, buffer, params,
2418 p_req("hops", param_hops_array, &hops),
2419 p_req("assocdata", param_bin_from_hex, &assocdata),
2420 p_opt("session_key", param_secret, &session_key),
2421 p_opt_def("onion_size", param_number, &packet_size, ROUTING_INFO_SIZE),
2422 NULL)) {
2423 return command_param_failed();
2424 }
2425
2426 if (session_key == NULL)
2427 sp = sphinx_path_new(cmd, assocdata, tal_bytelen(assocdata));
2428 else
2429 sp = sphinx_path_new_with_key(cmd, assocdata, tal_bytelen(assocdata),
2430 session_key);
2431
2432 for (size_t i=0; i<tal_count(hops); i++) {
2433 if (!sphinx_add_hop_has_length(sp, &hops[i].pubkey, hops[i].raw_payload))
2434 return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
2435 "hops[%zi] payload is not prefixed with length!",
2436 i);
2437 }
2438
2439 if (sphinx_path_payloads_size(sp) > *packet_size)
2440 return command_fail(
2441 cmd, JSONRPC2_INVALID_PARAMS,
2442 "Payloads exceed maximum onion packet size.");
2443
2444 packet = create_onionpacket(cmd, sp, *packet_size, &shared_secrets);
2445 if (!packet)
2446 return command_fail(cmd, LIGHTNINGD,
2447 "Could not create onion packet");
2448
2449 if (command_check_only(cmd))
2450 return command_check_done(cmd);
2451
2452 serialized = serialize_onionpacket(cmd, packet);
2453
2454 response = json_stream_success(cmd);
2455 json_add_hex(response, "onion", serialized, tal_bytelen(serialized));
2456 json_array_start(response, "shared_secrets");
2457 for (size_t i=0; i<tal_count(hops); i++) {
2458 json_add_secret(response, NULL, &shared_secrets[i]);
2459 }
2460 json_array_end(response);
2461 return command_success(cmd, response);

Callers

nothing calls this directly

Calls 15

param_checkFunction · 0.85
sphinx_path_newFunction · 0.85
tal_bytelenFunction · 0.85
sphinx_path_new_with_keyFunction · 0.85
create_onionpacketFunction · 0.85
serialize_onionpacketFunction · 0.85
json_add_hexFunction · 0.85
json_array_startFunction · 0.85
json_add_secretFunction · 0.85
json_array_endFunction · 0.85

Tested by

no test coverage detected