Skip to content

Commit

Permalink
Add support for paying to BOLT12 (#549)
Browse files Browse the repository at this point in the history
Add support for paying to BOLT12
  • Loading branch information
ok300 authored Nov 19, 2024
1 parent 0750e2e commit fe1dc30
Show file tree
Hide file tree
Showing 33 changed files with 2,947 additions and 206 deletions.
153 changes: 143 additions & 10 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 27 additions & 18 deletions cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use std::thread;
use std::time::Duration;

use anyhow::Result;
use anyhow::{anyhow, Result};
use breez_sdk_liquid::prelude::*;
use clap::{arg, Parser};
use qrcode_rs::render::unicode;
Expand All @@ -21,10 +21,14 @@ use serde_json::to_string_pretty;
pub(crate) enum Command {
/// Send a payment directly or via a swap
SendPayment {
/// Invoice which has to be paid
/// Invoice which has to be paid (BOLT11)
#[arg(long)]
invoice: Option<String>,

/// BOLT12 offer. If specified, amount_sat must also be set.
#[arg(long)]
offer: Option<String>,

/// Either BIP21 URI or Liquid address we intend to pay to
#[arg(long)]
address: Option<String>,
Expand Down Expand Up @@ -290,12 +294,12 @@ pub(crate) async fn handle_command(
InputType::Bolt11 { invoice } => result.push_str(&build_qr_text(&invoice.bolt11)),
InputType::LiquidAddress { address } => {
result.push_str(&build_qr_text(&address.to_uri().map_err(|e| {
anyhow::anyhow!("Could not build BIP21 from address data: {e:?}")
anyhow!("Could not build BIP21 from address data: {e:?}")
})?))
}
InputType::BitcoinAddress { address } => {
result.push_str(&build_qr_text(&address.to_uri().map_err(|e| {
anyhow::anyhow!("Could not build BIP21 from address data: {e:?}")
anyhow!("Could not build BIP21 from address data: {e:?}")
})?))
}
_ => {}
Expand All @@ -312,25 +316,30 @@ pub(crate) async fn handle_command(
}
Command::SendPayment {
invoice,
offer,
address,
amount_sat,
drain,
delay,
} => {
let destination = match (invoice, address) {
(None, None) => {
return Err(anyhow::anyhow!(
"Must specify either an invoice or a direct/BIP21 address."
let destination = match (invoice, offer, address) {
(Some(invoice), None, None) => Ok(invoice),
(None, Some(offer), None) => match amount_sat {
Some(_) => Ok(offer),
None => Err(anyhow!(
"Must specify an amount for a BOLT12 offer."
))
}
(Some(invoice), None) => invoice,
(None, Some(address)) => address,
(Some(_), Some(_)) => {
return Err(anyhow::anyhow!(
},
(None, None, Some(address)) => Ok(address),
(Some(_), _, Some(_)) => {
Err(anyhow::anyhow!(
"Cannot specify both invoice and address at the same time."
))
}
};
_ => Err(anyhow!(
"Must specify either a BOLT11 invoice, a BOLT12 offer or a direct/BIP21 address."
))
}?;
let amount = match (amount_sat, drain.unwrap_or(false)) {
(Some(amount_sat), _) => Some(PayAmount::Receiver { amount_sat }),
(_, true) => Some(PayAmount::Drain),
Expand Down Expand Up @@ -492,7 +501,7 @@ pub(crate) async fn handle_command(
match maybe_payment {
Some(payment) => command_result!(payment),
None => {
return Err(anyhow::anyhow!("Payment not found."));
return Err(anyhow!("Payment not found."));
}
}
}
Expand Down Expand Up @@ -595,7 +604,7 @@ pub(crate) async fn handle_command(
.await?;
Ok(pay_res)
}
_ => Err(anyhow::anyhow!("Invalid input")),
_ => Err(anyhow!("Invalid input")),
}?;

command_result!(res)
Expand All @@ -619,7 +628,7 @@ pub(crate) async fn handle_command(
.await?;
Ok(withdraw_res)
}
_ => Err(anyhow::anyhow!("Invalid input")),
_ => Err(anyhow!("Invalid input")),
}?;

command_result!(res)
Expand All @@ -632,7 +641,7 @@ pub(crate) async fn handle_command(
let auth_res = sdk.lnurl_auth(ad).await?;
serde_json::to_string_pretty(&auth_res).map_err(|e| e.into())
}
_ => Err(anyhow::anyhow!("Unexpected result type")),
_ => Err(anyhow!("Unexpected result type")),
}?;

command_result!(res)
Expand Down
Loading

0 comments on commit fe1dc30

Please sign in to comment.