Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions ldk-server-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ use ldk_server_client::error::LdkServerErrorCode::{
use ldk_server_client::ldk_server_protos::api::{
Bolt11ReceiveRequest, Bolt11ReceiveResponse, Bolt11SendRequest, Bolt11SendResponse,
Bolt12ReceiveRequest, Bolt12ReceiveResponse, Bolt12SendRequest, Bolt12SendResponse,
CloseChannelRequest, CloseChannelResponse, ForceCloseChannelRequest, ForceCloseChannelResponse,
GetBalancesRequest, GetBalancesResponse, GetNodeInfoRequest, GetNodeInfoResponse,
GetPaymentDetailsRequest, GetPaymentDetailsResponse, ListChannelsRequest, ListChannelsResponse,
ListForwardedPaymentsRequest, ListPaymentsRequest, OnchainReceiveRequest,
OnchainReceiveResponse, OnchainSendRequest, OnchainSendResponse, OpenChannelRequest,
OpenChannelResponse, SpliceInRequest, SpliceInResponse, SpliceOutRequest, SpliceOutResponse,
UpdateChannelConfigRequest, UpdateChannelConfigResponse,
CloseChannelRequest, CloseChannelResponse, ConnectPeerRequest, ConnectPeerResponse,
ForceCloseChannelRequest, ForceCloseChannelResponse, GetBalancesRequest, GetBalancesResponse,
GetNodeInfoRequest, GetNodeInfoResponse, GetPaymentDetailsRequest, GetPaymentDetailsResponse,
ListChannelsRequest, ListChannelsResponse, ListForwardedPaymentsRequest, ListPaymentsRequest,
OnchainReceiveRequest, OnchainReceiveResponse, OnchainSendRequest, OnchainSendResponse,
OpenChannelRequest, OpenChannelResponse, SpliceInRequest, SpliceInResponse, SpliceOutRequest,
SpliceOutResponse, UpdateChannelConfigRequest, UpdateChannelConfigResponse,
};
use ldk_server_client::ldk_server_protos::types::{
bolt11_invoice_description, Bolt11InvoiceDescription, ChannelConfig, PageToken,
Expand Down Expand Up @@ -336,6 +336,22 @@ enum Commands {
)]
cltv_expiry_delta: Option<u32>,
},
#[command(about = "Connect to a peer on the Lightning Network without opening a channel")]
ConnectPeer {
#[arg(short, long, help = "The hex-encoded public key of the node to connect to")]
node_pubkey: String,
#[arg(
short,
long,
help = "Address to connect to remote peer (IPv4:port, IPv6:port, OnionV3:port, or hostname:port)"
)]
address: String,
#[arg(
long,
help = "Whether to persist the connection for automatic reconnection on restart (default: true)"
)]
persist: Option<bool>,
},
#[command(about = "Generate shell completions for the CLI")]
Completions {
#[arg(
Expand Down Expand Up @@ -673,6 +689,11 @@ async fn main() {
.await,
);
},
Commands::ConnectPeer { node_pubkey, address, persist } => {
handle_response_result::<_, ConnectPeerResponse>(
client.connect_peer(ConnectPeerRequest { node_pubkey, address, persist }).await,
);
},
Commands::Completions { .. } => unreachable!("Handled above"),
}
}
Expand Down
33 changes: 21 additions & 12 deletions ldk-server-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ use bitcoin_hashes::{sha256, Hash, HashEngine};
use ldk_server_protos::api::{
Bolt11ReceiveRequest, Bolt11ReceiveResponse, Bolt11SendRequest, Bolt11SendResponse,
Bolt12ReceiveRequest, Bolt12ReceiveResponse, Bolt12SendRequest, Bolt12SendResponse,
CloseChannelRequest, CloseChannelResponse, ForceCloseChannelRequest, ForceCloseChannelResponse,
GetBalancesRequest, GetBalancesResponse, GetNodeInfoRequest, GetNodeInfoResponse,
GetPaymentDetailsRequest, GetPaymentDetailsResponse, ListChannelsRequest, ListChannelsResponse,
ListForwardedPaymentsRequest, ListForwardedPaymentsResponse, ListPaymentsRequest,
ListPaymentsResponse, OnchainReceiveRequest, OnchainReceiveResponse, OnchainSendRequest,
OnchainSendResponse, OpenChannelRequest, OpenChannelResponse, SpliceInRequest,
SpliceInResponse, SpliceOutRequest, SpliceOutResponse, UpdateChannelConfigRequest,
UpdateChannelConfigResponse,
CloseChannelRequest, CloseChannelResponse, ConnectPeerRequest, ConnectPeerResponse,
ForceCloseChannelRequest, ForceCloseChannelResponse, GetBalancesRequest, GetBalancesResponse,
GetNodeInfoRequest, GetNodeInfoResponse, GetPaymentDetailsRequest, GetPaymentDetailsResponse,
ListChannelsRequest, ListChannelsResponse, ListForwardedPaymentsRequest,
ListForwardedPaymentsResponse, ListPaymentsRequest, ListPaymentsResponse,
OnchainReceiveRequest, OnchainReceiveResponse, OnchainSendRequest, OnchainSendResponse,
OpenChannelRequest, OpenChannelResponse, SpliceInRequest, SpliceInResponse, SpliceOutRequest,
SpliceOutResponse, UpdateChannelConfigRequest, UpdateChannelConfigResponse,
};
use ldk_server_protos::endpoints::{
BOLT11_RECEIVE_PATH, BOLT11_SEND_PATH, BOLT12_RECEIVE_PATH, BOLT12_SEND_PATH,
CLOSE_CHANNEL_PATH, FORCE_CLOSE_CHANNEL_PATH, GET_BALANCES_PATH, GET_NODE_INFO_PATH,
GET_PAYMENT_DETAILS_PATH, LIST_CHANNELS_PATH, LIST_FORWARDED_PAYMENTS_PATH, LIST_PAYMENTS_PATH,
ONCHAIN_RECEIVE_PATH, ONCHAIN_SEND_PATH, OPEN_CHANNEL_PATH, SPLICE_IN_PATH, SPLICE_OUT_PATH,
UPDATE_CHANNEL_CONFIG_PATH,
CLOSE_CHANNEL_PATH, CONNECT_PEER_PATH, FORCE_CLOSE_CHANNEL_PATH, GET_BALANCES_PATH,
GET_NODE_INFO_PATH, GET_PAYMENT_DETAILS_PATH, LIST_CHANNELS_PATH, LIST_FORWARDED_PAYMENTS_PATH,
LIST_PAYMENTS_PATH, ONCHAIN_RECEIVE_PATH, ONCHAIN_SEND_PATH, OPEN_CHANNEL_PATH, SPLICE_IN_PATH,
SPLICE_OUT_PATH, UPDATE_CHANNEL_CONFIG_PATH,
};
use ldk_server_protos::error::{ErrorCode, ErrorResponse};
use prost::Message;
Expand Down Expand Up @@ -252,6 +252,15 @@ impl LdkServerClient {
self.post_request(&request, &url).await
}

/// Connect to a peer on the Lightning Network.
/// For API contract/usage, refer to docs for [`ConnectPeerRequest`] and [`ConnectPeerResponse`].
pub async fn connect_peer(
&self, request: ConnectPeerRequest,
) -> Result<ConnectPeerResponse, LdkServerError> {
let url = format!("https://{}/{CONNECT_PEER_PATH}", self.base_url);
self.post_request(&request, &url).await
}

async fn post_request<Rq: Message, Rs: Message + Default>(
&self, request: &Rq, url: &str,
) -> Result<Rs, LdkServerError> {
Expand Down
26 changes: 26 additions & 0 deletions ldk-server-protos/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,29 @@ pub struct GetBalancesResponse {
pub pending_balances_from_channel_closures:
::prost::alloc::vec::Vec<super::types::PendingSweepBalance>,
}
/// Connect to a peer on the Lightning Network.
/// See more: <https://docs.rs/ldk-node/latest/ldk_node/struct.Node.html#method.connect>
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ConnectPeerRequest {
/// The hex-encoded public key of the node to connect to.
#[prost(string, tag = "1")]
pub node_pubkey: ::prost::alloc::string::String,
/// An address which can be used to connect to a remote peer.
/// It can be of type IPv4:port, IPv6:port, OnionV3:port or hostname:port
#[prost(string, tag = "2")]
pub address: ::prost::alloc::string::String,
/// Whether to persist the peer connection, i.e., whether the peer will be re-connected on
/// restart. Defaults to true.
#[prost(bool, optional, tag = "3")]
pub persist: ::core::option::Option<bool>,
}
/// The response `content` for the `ConnectPeer` API, when HttpStatusCode is OK (200).
/// When HttpStatusCode is not OK (non-200), the response `content` contains a serialized `ErrorResponse`.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ConnectPeerResponse {}
1 change: 1 addition & 0 deletions ldk-server-protos/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ pub const LIST_PAYMENTS_PATH: &str = "ListPayments";
pub const LIST_FORWARDED_PAYMENTS_PATH: &str = "ListForwardedPayments";
pub const UPDATE_CHANNEL_CONFIG_PATH: &str = "UpdateChannelConfig";
pub const GET_PAYMENT_DETAILS_PATH: &str = "GetPaymentDetails";
pub const CONNECT_PEER_PATH: &str = "ConnectPeer";
30 changes: 30 additions & 0 deletions ldk-server/src/api/connect_peer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

use crate::api::error::LdkServerError;
use crate::service::Context;
use ldk_node::bitcoin::secp256k1::PublicKey;
use ldk_node::lightning::ln::msgs::SocketAddress;
use ldk_server_protos::api::{ConnectPeerRequest, ConnectPeerResponse};
use std::str::FromStr;

pub(crate) fn handle_connect_peer(
context: Context, request: ConnectPeerRequest,
) -> Result<ConnectPeerResponse, LdkServerError> {
let node_id = PublicKey::from_str(&request.node_pubkey)
.map_err(|_| ldk_node::NodeError::InvalidPublicKey)?;
let address = SocketAddress::from_str(&request.address)
.map_err(|_| ldk_node::NodeError::InvalidSocketAddress)?;

let persist = request.persist.unwrap_or(true);

context.node.connect(node_id, address, persist)?;

Ok(ConnectPeerResponse {})
}
1 change: 1 addition & 0 deletions ldk-server/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(crate) mod bolt11_send;
pub(crate) mod bolt12_receive;
pub(crate) mod bolt12_send;
pub(crate) mod close_channel;
pub(crate) mod connect_peer;
pub(crate) mod error;
pub(crate) mod get_balances;
pub(crate) mod get_node_info;
Expand Down
12 changes: 8 additions & 4 deletions ldk-server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use ldk_node::bitcoin::hashes::{sha256, Hash, HashEngine};
use ldk_node::Node;
use ldk_server_protos::endpoints::{
BOLT11_RECEIVE_PATH, BOLT11_SEND_PATH, BOLT12_RECEIVE_PATH, BOLT12_SEND_PATH,
CLOSE_CHANNEL_PATH, FORCE_CLOSE_CHANNEL_PATH, GET_BALANCES_PATH, GET_NODE_INFO_PATH,
GET_PAYMENT_DETAILS_PATH, LIST_CHANNELS_PATH, LIST_FORWARDED_PAYMENTS_PATH, LIST_PAYMENTS_PATH,
ONCHAIN_RECEIVE_PATH, ONCHAIN_SEND_PATH, OPEN_CHANNEL_PATH, SPLICE_IN_PATH, SPLICE_OUT_PATH,
UPDATE_CHANNEL_CONFIG_PATH,
CLOSE_CHANNEL_PATH, CONNECT_PEER_PATH, FORCE_CLOSE_CHANNEL_PATH, GET_BALANCES_PATH,
GET_NODE_INFO_PATH, GET_PAYMENT_DETAILS_PATH, LIST_CHANNELS_PATH, LIST_FORWARDED_PAYMENTS_PATH,
LIST_PAYMENTS_PATH, ONCHAIN_RECEIVE_PATH, ONCHAIN_SEND_PATH, OPEN_CHANNEL_PATH, SPLICE_IN_PATH,
SPLICE_OUT_PATH, UPDATE_CHANNEL_CONFIG_PATH,
};
use prost::Message;

Expand All @@ -32,6 +32,7 @@ use crate::api::bolt11_send::handle_bolt11_send_request;
use crate::api::bolt12_receive::handle_bolt12_receive_request;
use crate::api::bolt12_send::handle_bolt12_send_request;
use crate::api::close_channel::{handle_close_channel_request, handle_force_close_channel_request};
use crate::api::connect_peer::handle_connect_peer;
use crate::api::error::LdkServerError;
use crate::api::error::LdkServerErrorCode::{AuthError, InvalidRequestError};
use crate::api::get_balances::handle_get_balances_request;
Expand Down Expand Up @@ -292,6 +293,9 @@ impl Service<Request<Incoming>> for NodeService {
api_key,
handle_list_forwarded_payments_request,
)),
CONNECT_PEER_PATH => {
Box::pin(handle_request(context, req, auth_params, api_key, handle_connect_peer))
},
path => {
let error = format!("Unknown request: {}", path).into_bytes();
Box::pin(async {
Expand Down