From 81bbeaa6128a122f56dc4747e7fb24cd6918891e Mon Sep 17 00:00:00 2001 From: Max Rantil Date: Mon, 22 Jul 2024 13:15:17 +0200 Subject: [PATCH] json-rpc: add rpc to provide bound alt-addr Signed-off-by: Max Rantil --- lightningd/channel_control.c | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 093ef5446647..5c4826445188 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -2361,3 +2361,56 @@ 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 char *our_alt_addr; + bool more_than_one; + + if (!param_check(cmd, buffer, params, + p_req("node_id", param_node_id, &p_id), + p_req("alt_addr", param_string, &our_alt_addr), + 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"); + + if (command_check_only(cmd)) + return command_check_done(cmd); + + u8 *msg = towire_channeld_peer_alt_addr(peer, (u8 *)our_alt_addr); + subd_send_msg(channel->owner, take(msg)); + + wallet_add_alt_addr(cmd->ld->wallet->db, p_id, our_alt_addr, true); + + /* FIXME(maxrantil), We need to add this to 'listnodes' command too! */ + + return command_success(cmd, json_stream_success(cmd)); +} + +static const struct json_command alt_addr_command = { + "alt-addr", + "developer", + json_alt_addr, + "Select an alternative private address for peer-to-peer connections", + .dev_only = true, +}; +AUTODATA(json_command, &alt_addr_command);