Skip to content

Commit

Permalink
Abstract construct_payjoin_uri
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Aug 19, 2023
1 parent 051c17a commit 34ce46b
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions payjoin-cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -180,6 +164,25 @@ impl App {
Ok(())
}

fn construct_payjoin_uri(&self, amount_arg: &str) -> Result<String> {
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()) {
Expand Down

0 comments on commit 34ce46b

Please sign in to comment.