Skip to content

Commit 438f2a7

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 438f2a7

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

src/cli.rs

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,49 @@ 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+
if peer_manager.peer_by_node_id(&pubkey).is_none() {
98+
println!("ERROR: Peer address not provided and peer is not connected");
99+
continue;
100+
}
101+
} else {
102+
let (pubkey, peer_addr) =
103+
match parse_peer_info(peer_pubkey_and_ip_addr.to_string()) {
104+
Ok(info) => info,
105+
Err(e) => {
106+
println!("{:?}", e.into_inner().unwrap());
107+
continue;
108+
},
109+
};
110+
111+
if tokio::runtime::Handle::current()
112+
.block_on(connect_peer_if_necessary(
113+
pubkey,
114+
peer_addr,
115+
peer_manager.clone(),
116+
))
117+
.is_err()
118+
{
119+
continue;
92120
};
121+
}
93122

94123
let chan_amt_sat: Result<u64, _> = channel_value_sat.unwrap().parse();
95124
if chan_amt_sat.is_err() {
96125
println!("ERROR: channel amount must be a number");
97126
continue;
98127
}
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-
111128
let (mut announce_channel, mut with_anchors) = (false, false);
112129
while let Some(word) = words.next() {
113130
match word {
@@ -131,11 +148,14 @@ pub(crate) fn poll_for_user_input(
131148
)
132149
.is_ok()
133150
{
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-
);
151+
if peer_addr_str.is_some() {
152+
let peer_data_path =
153+
format!("{}/channel_peer_data", ldk_data_dir.clone());
154+
let _ = disk::persist_channel_peer(
155+
Path::new(&peer_data_path),
156+
peer_pubkey_and_ip_addr,
157+
);
158+
}
139159
}
140160
},
141161
"sendpayment" => {
@@ -498,7 +518,7 @@ fn help() {
498518
println!(" help\tShows a list of commands.");
499519
println!(" quit\tClose the application.");
500520
println!("\n Channels:");
501-
println!(" openchannel pubkey@host:port <amt_satoshis> [--public] [--with-anchors]");
521+
println!(" openchannel pubkey@[host:port] <amt_satoshis> [--public] [--with-anchors]");
502522
println!(" closechannel <channel_id> <peer_pubkey>");
503523
println!(" forceclosechannel <channel_id> <peer_pubkey>");
504524
println!(" listchannels");

0 commit comments

Comments
 (0)