Skip to content
92 changes: 92 additions & 0 deletions doc/schemas/lightning-alt-addr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"$schema": "../rpc-schema-draft.json",
"type": "object",
"additionalProperties": false,
"rpc": "alt-addr",
"title": "Command to set or clear alternative addresses for node connection.",
"description": [
"The **alt-addr** RPC command allows a node to set or clear alternate addresses for incoming connections.",
"These addresses can be used by peers to connect to the node, offering flexibility in network configurations."
],
"request": {
"required": [
"node_id",
"alt_addrs"
],
"properties": {
"node_id": {
"type": "pubkey",
"description": [
"The public key of the node for which the alternate addresses are being set."
]
},
"alt_addrs": {
"type": "array",
"items": {
"type": "string",
"description": [
"A list of alternate addresses in the format 'ip:port'.",
"An empty string can be used to clear existing alternate addresses."
]
}
}
}
},
"response": {
"required": [],
"properties": {}
},
"errors": [
"On failure, one of the following error codes may be returned:",
"",
"- -32602: Error in given parameters (e.g., invalid address format or node ID)."
],
"author": [
"Max Ranti <<[email protected]>> wrote the initial version of this man page."
],
"resources": [
"Main web site: <https://github.com/ElementsProject/lightning>"
],
"examples": [
{
"request": {
"id": "example:alt-addr#1",
"method": "alt-addr",
"params": {
"node_id": "0290bb147b0ea569938db873d5899048bd211ad034e57b47824b348756f66e8bca",
"alt_addrs": [
"127.0.0.21:9735"
]
}
},
"response": {}
},
{
"request": {
"id": "example:alt-addr#2",
"method": "alt-addr",
"params": {
"node_id": "0290bb147b0ea569938db873d5899048bd211ad034e57b47824b348756f66e8bca",
"alt_addrs": [
"127.0.0.21:9735",
"192.168.1.1:9736"
]
}
},
"response": {}
},
{
"request": {
"id": "example:alt-addr#3",
"method": "alt-addr",
"params": {
"node_id": "0290bb147b0ea569938db873d5899048bd211ad034e57b47824b348756f66e8bca",
"alt_addrs": [
""
]
}
},
"response": {}
}
]
}
60 changes: 60 additions & 0 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -2462,3 +2462,63 @@ static const struct json_command dev_quiesce_command = {
.dev_only = true,
};
AUTODATA(json_command, &dev_quiesce_command);

static struct command_result *json_alt_addr(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct node_id *p_id;
struct peer *peer;
struct channel *channel;
const jsmntok_t *alt_addrs_tok;
bool more_than_one;

if (!param_check(cmd, buffer, params,
p_req("node_id", param_node_id, &p_id),
p_req("alt_addrs", param_array, &alt_addrs_tok),
NULL))
return command_param_failed();

peer = peer_by_id(cmd->ld, p_id);
if (!peer) {
return command_fail(cmd, JSONRPC2_INVALID_REQUEST,
"No such peer: %s",
fmt_node_id(cmd, p_id));
}

channel = peer_any_channel(peer, channel_state_can_add_htlc, &more_than_one);
if (!channel || !channel->owner)
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
/* This is a dev command: fix the api if you need this! */
if (more_than_one)
return command_fail(cmd, LIGHTNINGD, "More than one channel");

Comment on lines +2493 to +2496
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's not a dev command!

Plus, you can totally whitelist ids which are not peers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you say so! I just couldn't understand how to whitelist ids which are not peers.

if (command_check_only(cmd))
return command_check_done(cmd);

char *addr_list = tal_fmt(tmpctx, "%s", json_strdup(tmpctx, buffer, json_get_arr(alt_addrs_tok, 0)));

for (size_t i = 1; i < alt_addrs_tok->size; i++) {
const jsmntok_t *addr_tok = json_get_arr(alt_addrs_tok, i);
const char *my_alt_addr = json_strdup(tmpctx, buffer, addr_tok);

/* Format the rpc array into comma-seperated string for the db's */
addr_list = tal_fmt(tmpctx, "%s,%s", addr_list, my_alt_addr);
}

u8 *msg = towire_channeld_peer_alt_addr(peer, (u8 *)addr_list);
subd_send_msg(channel->owner, take(msg));

wallet_add_alt_addr(cmd->ld->wallet->db, p_id, addr_list, true);
process_and_update_whitelist(cmd->ld, p_id, (u8 *)addr_list);

return command_success(cmd, json_stream_success(cmd));
}

static const struct json_command alt_addr_command = {
"alt-addr",
json_alt_addr,
.dev_only = true,
};
AUTODATA(json_command, &alt_addr_command);