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

Function json_createonion

lightningd/pay.c:2401–2459  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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