Skip to content

Commit 34ce46b

Browse files
committed
Abstract construct_payjoin_uri
1 parent 051c17a commit 34ce46b

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

payjoin-cli/src/app.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,7 @@ impl App {
131131
}
132132

133133
pub fn receive_payjoin(self, amount_arg: &str) -> Result<()> {
134-
use payjoin::Uri;
135-
136-
let pj_receiver_address = self.bitcoind.get_new_address(None, None)?.assume_checked();
137-
let amount = Amount::from_sat(amount_arg.parse()?);
138-
let pj_uri_string = format!(
139-
"{}?amount={}&pj={}",
140-
pj_receiver_address.to_qr_uri(),
141-
amount.to_btc(),
142-
self.config.pj_endpoint
143-
);
144-
let pj_uri = Uri::from_str(&pj_uri_string)
145-
.map_err(|e| anyhow!("Constructed a bad URI string from args: {}", e))?;
146-
let _pj_uri = pj_uri
147-
.assume_checked()
148-
.check_pj_supported()
149-
.map_err(|e| anyhow!("Constructed URI does not support payjoin: {}", e))?;
150-
134+
let pj_uri_string = self.construct_payjoin_uri(amount_arg)?;
151135
println!(
152136
"Listening at {}. Configured to accept payjoin at BIP 21 Payjoin Uri:",
153137
self.config.pj_host
@@ -180,6 +164,25 @@ impl App {
180164
Ok(())
181165
}
182166

167+
fn construct_payjoin_uri(&self, amount_arg: &str) -> Result<String> {
168+
let pj_receiver_address = self.bitcoind.get_new_address(None, None)?.assume_checked();
169+
let amount = Amount::from_sat(amount_arg.parse()?);
170+
let pj_uri_string = format!(
171+
"{}?amount={}&pj={}",
172+
pj_receiver_address.to_qr_uri(),
173+
amount.to_btc(),
174+
self.config.pj_endpoint
175+
);
176+
let pj_uri = payjoin::Uri::from_str(&pj_uri_string)
177+
.map_err(|e| anyhow!("Constructed a bad URI string from args: {}", e))?;
178+
let _pj_uri = pj_uri
179+
.assume_checked()
180+
.check_pj_supported()
181+
.map_err(|e| anyhow!("Constructed URI does not support payjoin: {}", e))?;
182+
183+
Ok(pj_uri_string)
184+
}
185+
183186
fn handle_web_request(&self, req: &Request) -> Response {
184187
log::debug!("Received request: {:?}", req);
185188
match (req.method(), req.url().as_ref()) {

0 commit comments

Comments
 (0)