Skip to content

Commit 3d3bcc8

Browse files
committed
openchannel: allow creating channels with connected peers without an address
openchannel now supports both `pubkey@host:port` and `pubkey` as arguments. If only `pubkey` is provided, a channel is created only if the peer is already connected; otherwise, an error is returned.
1 parent 3676248 commit 3d3bcc8

File tree

1 file changed

+52
-24
lines changed

1 file changed

+52
-24
lines changed

src/cli.rs

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,56 @@ pub(crate) fn poll_for_user_input(
8282
continue;
8383
}
8484
let peer_pubkey_and_ip_addr = peer_pubkey_and_ip_addr.unwrap();
85-
let (pubkey, peer_addr) =
86-
match parse_peer_info(peer_pubkey_and_ip_addr.to_string()) {
87-
Ok(info) => info,
88-
Err(e) => {
89-
println!("{:?}", e.into_inner().unwrap());
90-
continue;
91-
},
85+
86+
let mut pubkey_and_addr = peer_pubkey_and_ip_addr.split("@");
87+
let pubkey = pubkey_and_addr.next();
88+
let peer_addr_str = pubkey_and_addr.next();
89+
let pubkey = hex_utils::to_compressed_pubkey(pubkey.unwrap());
90+
if pubkey.is_none() {
91+
println!("ERROR: unable to parse given pubkey for node");
92+
continue;
93+
}
94+
let pubkey = pubkey.unwrap();
95+
96+
if peer_addr_str.is_none() {
97+
let mut peer_connected = false;
98+
for peer_details in peer_manager.list_peers() {
99+
if peer_details.counterparty_node_id == pubkey {
100+
peer_connected = true;
101+
break;
102+
}
103+
}
104+
if peer_connected == false {
105+
println!("ERROR: Peer address not provided");
106+
continue;
107+
}
108+
} else {
109+
let (pubkey, peer_addr) =
110+
match parse_peer_info(peer_pubkey_and_ip_addr.to_string()) {
111+
Ok(info) => info,
112+
Err(e) => {
113+
println!("{:?}", e.into_inner().unwrap());
114+
continue;
115+
},
116+
};
117+
118+
if tokio::runtime::Handle::current()
119+
.block_on(connect_peer_if_necessary(
120+
pubkey,
121+
peer_addr,
122+
peer_manager.clone(),
123+
))
124+
.is_err()
125+
{
126+
continue;
92127
};
128+
}
93129

94130
let chan_amt_sat: Result<u64, _> = channel_value_sat.unwrap().parse();
95131
if chan_amt_sat.is_err() {
96132
println!("ERROR: channel amount must be a number");
97133
continue;
98134
}
99-
100-
if tokio::runtime::Handle::current()
101-
.block_on(connect_peer_if_necessary(
102-
pubkey,
103-
peer_addr,
104-
peer_manager.clone(),
105-
))
106-
.is_err()
107-
{
108-
continue;
109-
};
110-
111135
let (mut announce_channel, mut with_anchors) = (false, false);
112136
while let Some(word) = words.next() {
113137
match word {
@@ -131,11 +155,14 @@ pub(crate) fn poll_for_user_input(
131155
)
132156
.is_ok()
133157
{
134-
let peer_data_path = format!("{}/channel_peer_data", ldk_data_dir.clone());
135-
let _ = disk::persist_channel_peer(
136-
Path::new(&peer_data_path),
137-
peer_pubkey_and_ip_addr,
138-
);
158+
if peer_addr_str.is_some() {
159+
let peer_data_path =
160+
format!("{}/channel_peer_data", ldk_data_dir.clone());
161+
let _ = disk::persist_channel_peer(
162+
Path::new(&peer_data_path),
163+
peer_pubkey_and_ip_addr,
164+
);
165+
}
139166
}
140167
},
141168
"sendpayment" => {
@@ -499,6 +526,7 @@ fn help() {
499526
println!(" quit\tClose the application.");
500527
println!("\n Channels:");
501528
println!(" openchannel pubkey@host:port <amt_satoshis> [--public] [--with-anchors]");
529+
println!(" openchannel pubkey <amt_satoshis> [--public] [--with-anchors]");
502530
println!(" closechannel <channel_id> <peer_pubkey>");
503531
println!(" forceclosechannel <channel_id> <peer_pubkey>");
504532
println!(" listchannels");

0 commit comments

Comments
 (0)