Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for paying to BOLT12 #549

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading