From 34ce46b5069b97817de516d44d5367d76e030cbf Mon Sep 17 00:00:00 2001 From: DanGould Date: Fri, 18 Aug 2023 14:29:37 -0400 Subject: [PATCH] Abstract construct_payjoin_uri --- payjoin-cli/src/app.rs | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/payjoin-cli/src/app.rs b/payjoin-cli/src/app.rs index 55691cb4..b867d3ce 100644 --- a/payjoin-cli/src/app.rs +++ b/payjoin-cli/src/app.rs @@ -131,23 +131,7 @@ impl App { } pub fn receive_payjoin(self, amount_arg: &str) -> Result<()> { - use payjoin::Uri; - - let pj_receiver_address = self.bitcoind.get_new_address(None, None)?.assume_checked(); - let amount = Amount::from_sat(amount_arg.parse()?); - let pj_uri_string = format!( - "{}?amount={}&pj={}", - pj_receiver_address.to_qr_uri(), - amount.to_btc(), - self.config.pj_endpoint - ); - let pj_uri = Uri::from_str(&pj_uri_string) - .map_err(|e| anyhow!("Constructed a bad URI string from args: {}", e))?; - let _pj_uri = pj_uri - .assume_checked() - .check_pj_supported() - .map_err(|e| anyhow!("Constructed URI does not support payjoin: {}", e))?; - + let pj_uri_string = self.construct_payjoin_uri(amount_arg)?; println!( "Listening at {}. Configured to accept payjoin at BIP 21 Payjoin Uri:", self.config.pj_host @@ -180,6 +164,25 @@ impl App { Ok(()) } + fn construct_payjoin_uri(&self, amount_arg: &str) -> Result { + let pj_receiver_address = self.bitcoind.get_new_address(None, None)?.assume_checked(); + let amount = Amount::from_sat(amount_arg.parse()?); + let pj_uri_string = format!( + "{}?amount={}&pj={}", + pj_receiver_address.to_qr_uri(), + amount.to_btc(), + self.config.pj_endpoint + ); + let pj_uri = payjoin::Uri::from_str(&pj_uri_string) + .map_err(|e| anyhow!("Constructed a bad URI string from args: {}", e))?; + let _pj_uri = pj_uri + .assume_checked() + .check_pj_supported() + .map_err(|e| anyhow!("Constructed URI does not support payjoin: {}", e))?; + + Ok(pj_uri_string) + } + fn handle_web_request(&self, req: &Request) -> Response { log::debug!("Received request: {:?}", req); match (req.method(), req.url().as_ref()) {