Skip to content

Commit 6c300c7

Browse files
authored
Merge pull request #139 from Psycho-Pirate/openchannel_parser
openchannel: allow creating channels with connected peers without an address
2 parents 664f6c4 + 438f2a7 commit 6c300c7

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

src/cli.rs

+45-25
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,49 @@ pub(crate) fn poll_for_user_input(
8585
continue;
8686
}
8787
let peer_pubkey_and_ip_addr = peer_pubkey_and_ip_addr.unwrap();
88-
let (pubkey, peer_addr) =
89-
match parse_peer_info(peer_pubkey_and_ip_addr.to_string()) {
90-
Ok(info) => info,
91-
Err(e) => {
92-
println!("{:?}", e.into_inner().unwrap());
93-
continue;
94-
},
88+
89+
let mut pubkey_and_addr = peer_pubkey_and_ip_addr.split("@");
90+
let pubkey = pubkey_and_addr.next();
91+
let peer_addr_str = pubkey_and_addr.next();
92+
let pubkey = hex_utils::to_compressed_pubkey(pubkey.unwrap());
93+
if pubkey.is_none() {
94+
println!("ERROR: unable to parse given pubkey for node");
95+
continue;
96+
}
97+
let pubkey = pubkey.unwrap();
98+
99+
if peer_addr_str.is_none() {
100+
if peer_manager.peer_by_node_id(&pubkey).is_none() {
101+
println!("ERROR: Peer address not provided and peer is not connected");
102+
continue;
103+
}
104+
} else {
105+
let (pubkey, peer_addr) =
106+
match parse_peer_info(peer_pubkey_and_ip_addr.to_string()) {
107+
Ok(info) => info,
108+
Err(e) => {
109+
println!("{:?}", e.into_inner().unwrap());
110+
continue;
111+
},
112+
};
113+
114+
if tokio::runtime::Handle::current()
115+
.block_on(connect_peer_if_necessary(
116+
pubkey,
117+
peer_addr,
118+
peer_manager.clone(),
119+
))
120+
.is_err()
121+
{
122+
continue;
95123
};
124+
}
96125

97126
let chan_amt_sat: Result<u64, _> = channel_value_sat.unwrap().parse();
98127
if chan_amt_sat.is_err() {
99128
println!("ERROR: channel amount must be a number");
100129
continue;
101130
}
102-
103-
if tokio::runtime::Handle::current()
104-
.block_on(connect_peer_if_necessary(
105-
pubkey,
106-
peer_addr,
107-
peer_manager.clone(),
108-
))
109-
.is_err()
110-
{
111-
continue;
112-
};
113-
114131
let (mut announce_channel, mut with_anchors) = (false, false);
115132
while let Some(word) = words.next() {
116133
match word {
@@ -134,11 +151,14 @@ pub(crate) fn poll_for_user_input(
134151
)
135152
.is_ok()
136153
{
137-
let peer_data_path = format!("{}/channel_peer_data", ldk_data_dir.clone());
138-
let _ = disk::persist_channel_peer(
139-
Path::new(&peer_data_path),
140-
peer_pubkey_and_ip_addr,
141-
);
154+
if peer_addr_str.is_some() {
155+
let peer_data_path =
156+
format!("{}/channel_peer_data", ldk_data_dir.clone());
157+
let _ = disk::persist_channel_peer(
158+
Path::new(&peer_data_path),
159+
peer_pubkey_and_ip_addr,
160+
);
161+
}
142162
}
143163
},
144164
"sendpayment" => {
@@ -565,7 +585,7 @@ fn help() {
565585
println!(" help\tShows a list of commands.");
566586
println!(" quit\tClose the application.");
567587
println!("\n Channels:");
568-
println!(" openchannel pubkey@host:port <amt_satoshis> [--public] [--with-anchors]");
588+
println!(" openchannel pubkey@[host:port] <amt_satoshis> [--public] [--with-anchors]");
569589
println!(" closechannel <channel_id> <peer_pubkey>");
570590
println!(" forceclosechannel <channel_id> <peer_pubkey>");
571591
println!(" listchannels");

0 commit comments

Comments
 (0)