Skip to content

Commit 5eab8db

Browse files
committed
json-rpc: add rpc to provide bound alt-addr
1 parent c5fec48 commit 5eab8db

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
"$schema": "../rpc-schema-draft.json",
3+
"type": "object",
4+
"additionalProperties": false,
5+
"rpc": "alt-addr",
6+
"title": "Command to set or clear alternative addresses for node connection.",
7+
"description": [
8+
"The **alt-addr** RPC command allows a node to set or clear alternate addresses for incoming connections.",
9+
"These addresses can be used by peers to connect to the node, offering flexibility in network configurations."
10+
],
11+
"request": {
12+
"required": [
13+
"node_id",
14+
"alt_addrs"
15+
],
16+
"properties": {
17+
"node_id": {
18+
"type": "pubkey",
19+
"description": [
20+
"The public key of the node for which the alternate addresses are being set."
21+
]
22+
},
23+
"alt_addrs": {
24+
"type": "array",
25+
"items": {
26+
"type": "string",
27+
"description": [
28+
"A list of alternate addresses in the format 'ip:port'.",
29+
"An empty string can be used to clear existing alternate addresses."
30+
]
31+
}
32+
}
33+
}
34+
},
35+
"response": {
36+
"required": [],
37+
"properties": {}
38+
},
39+
"errors": [
40+
"On failure, one of the following error codes may be returned:",
41+
"",
42+
"- -32602: Error in given parameters (e.g., invalid address format or node ID)."
43+
],
44+
"author": [
45+
"Max Ranti <<[email protected]>> wrote the initial version of this man page."
46+
],
47+
"resources": [
48+
"Main web site: <https://github.com/ElementsProject/lightning>"
49+
],
50+
"examples": [
51+
{
52+
"request": {
53+
"id": "example:alt-addr#1",
54+
"method": "alt-addr",
55+
"params": {
56+
"node_id": "0290bb147b0ea569938db873d5899048bd211ad034e57b47824b348756f66e8bca",
57+
"alt_addrs": [
58+
"127.0.0.21:9735"
59+
]
60+
}
61+
},
62+
"response": {}
63+
},
64+
{
65+
"request": {
66+
"id": "example:alt-addr#2",
67+
"method": "alt-addr",
68+
"params": {
69+
"node_id": "0290bb147b0ea569938db873d5899048bd211ad034e57b47824b348756f66e8bca",
70+
"alt_addrs": [
71+
"127.0.0.21:9735",
72+
"192.168.1.1:9736"
73+
]
74+
}
75+
},
76+
"response": {}
77+
},
78+
{
79+
"request": {
80+
"id": "example:alt-addr#3",
81+
"method": "alt-addr",
82+
"params": {
83+
"node_id": "0290bb147b0ea569938db873d5899048bd211ad034e57b47824b348756f66e8bca",
84+
"alt_addrs": [
85+
""
86+
]
87+
}
88+
},
89+
"response": {}
90+
}
91+
]
92+
}

lightningd/channel_control.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,3 +2462,63 @@ static const struct json_command dev_quiesce_command = {
24622462
.dev_only = true,
24632463
};
24642464
AUTODATA(json_command, &dev_quiesce_command);
2465+
2466+
static struct command_result *json_alt_addr(struct command *cmd,
2467+
const char *buffer,
2468+
const jsmntok_t *obj UNNEEDED,
2469+
const jsmntok_t *params)
2470+
{
2471+
struct node_id *p_id;
2472+
struct peer *peer;
2473+
struct channel *channel;
2474+
const jsmntok_t *alt_addrs_tok;
2475+
bool more_than_one;
2476+
2477+
if (!param_check(cmd, buffer, params,
2478+
p_req("node_id", param_node_id, &p_id),
2479+
p_req("alt_addrs", param_array, &alt_addrs_tok),
2480+
NULL))
2481+
return command_param_failed();
2482+
2483+
peer = peer_by_id(cmd->ld, p_id);
2484+
if (!peer) {
2485+
return command_fail(cmd, JSONRPC2_INVALID_REQUEST,
2486+
"No such peer: %s",
2487+
fmt_node_id(cmd, p_id));
2488+
}
2489+
2490+
channel = peer_any_channel(peer, channel_state_can_add_htlc, &more_than_one);
2491+
if (!channel || !channel->owner)
2492+
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
2493+
/* This is a dev command: fix the api if you need this! */
2494+
if (more_than_one)
2495+
return command_fail(cmd, LIGHTNINGD, "More than one channel");
2496+
2497+
if (command_check_only(cmd))
2498+
return command_check_done(cmd);
2499+
2500+
char *addr_list = tal_fmt(tmpctx, "%s", json_strdup(tmpctx, buffer, json_get_arr(alt_addrs_tok, 0)));
2501+
2502+
for (size_t i = 1; i < alt_addrs_tok->size; i++) {
2503+
const jsmntok_t *addr_tok = json_get_arr(alt_addrs_tok, i);
2504+
const char *my_alt_addr = json_strdup(tmpctx, buffer, addr_tok);
2505+
2506+
/* Format the rpc array into comma-seperated string for the db's */
2507+
addr_list = tal_fmt(tmpctx, "%s,%s", addr_list, my_alt_addr);
2508+
}
2509+
2510+
u8 *msg = towire_channeld_peer_alt_addr(peer, (u8 *)addr_list);
2511+
subd_send_msg(channel->owner, take(msg));
2512+
2513+
wallet_add_alt_addr(cmd->ld->wallet->db, p_id, addr_list, true);
2514+
process_and_update_whitelist(cmd->ld, p_id, (u8 *)addr_list);
2515+
2516+
return command_success(cmd, json_stream_success(cmd));
2517+
}
2518+
2519+
static const struct json_command alt_addr_command = {
2520+
"alt-addr",
2521+
json_alt_addr,
2522+
.dev_only = true,
2523+
};
2524+
AUTODATA(json_command, &alt_addr_command);

0 commit comments

Comments
 (0)