diff --git a/Cargo.lock b/Cargo.lock index d984ac41..1e72e8a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -204,6 +204,7 @@ dependencies = [ "bdk_redb", "bdk_sp", "bdk_wallet", + "bitcoin-payment-instructions", "claims", "clap", "clap_complete", @@ -489,6 +490,19 @@ dependencies = [ "toml 0.5.11", ] +[[package]] +name = "bitcoin-payment-instructions" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c300c948b2ff78c965ea3a613372352125448a22f1acf49e95e3878149824091" +dependencies = [ + "bitcoin", + "dnssec-prover", + "getrandom 0.3.4", + "lightning", + "lightning-invoice", +] + [[package]] name = "bitcoin-units" version = "0.1.2" @@ -958,6 +972,16 @@ dependencies = [ "syn", ] +[[package]] +name = "dnssec-prover" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec4f825369fc7134da70ca4040fddc8e03b80a46d249ae38d9c1c39b7b4476bf" +dependencies = [ + "bitcoin_hashes 0.14.1", + "tokio", +] + [[package]] name = "dunce" version = "1.0.5" @@ -1236,6 +1260,12 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "hashbrown" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" + [[package]] name = "hashbrown" version = "0.14.5" @@ -1669,6 +1699,12 @@ version = "0.2.180" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" + [[package]] name = "libredox" version = "0.1.12" @@ -1690,6 +1726,54 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "lightning" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45c421e68d603b989c8f59a95a1860f1f5de90a6305ed5bcb6d9b35a3a11f450" +dependencies = [ + "bech32", + "bitcoin", + "dnssec-prover", + "hashbrown 0.13.2", + "libm", + "lightning-invoice", + "lightning-macros", + "lightning-types", + "possiblyrandom", +] + +[[package]] +name = "lightning-invoice" +version = "0.34.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d83bd798e04ab9eecc8bbef1fa17d3808859bcdc0406bd16c55d51c8834444" +dependencies = [ + "bech32", + "bitcoin", + "lightning-types", +] + +[[package]] +name = "lightning-macros" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4c717494cdc2c8bb85bee7113031248f5f6c64f8802b33c1c9e2d98e594aa71" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "lightning-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c211dfcff95ca308247da8b1e0e81604bc9e568239967cd2c34572558511e869" +dependencies = [ + "bitcoin", +] + [[package]] name = "linux-raw-sys" version = "0.11.0" @@ -1980,6 +2064,15 @@ dependencies = [ "portable-atomic", ] +[[package]] +name = "possiblyrandom" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c564dbf654befd49035528299f1208a40508f6e07efb11c163444e304e4484f" +dependencies = [ + "getrandom 0.2.17", +] + [[package]] name = "potential_utf" version = "0.1.4" diff --git a/Cargo.toml b/Cargo.toml index 46036621..c2c15818 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ payjoin = { version = "=1.0.0-rc.1", features = ["v1", "v2", "io", "_test-utils" reqwest = { version = "0.13.2", default-features = false, optional = true } url = { version = "2.5.8", optional = true } bdk_bip322 = { version = "0.1.0", optional = true } +bitcoin-payment-instructions = { version = "0.7.0", optional = true} [features] default = ["repl", "sqlite"] @@ -55,7 +56,8 @@ redb = ["bdk_redb"] cbf = ["bdk_kyoto", "_payjoin-dependencies"] electrum = ["bdk_electrum", "_payjoin-dependencies"] esplora = ["bdk_esplora", "_payjoin-dependencies"] -rpc = ["bdk_bitcoind_rpc", "_payjoin-dependencies"] +rpc = ["bdk_bitcoind_rpc", "_payjoin-dependencies"] +dns_payment = ["bitcoin-payment-instructions"] # Internal features _payjoin-dependencies = ["payjoin", "reqwest", "url"] diff --git a/src/commands.rs b/src/commands.rs index 46404dcf..c1ee182a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -23,6 +23,8 @@ use bdk_wallet::bitcoin::{ use clap::{Args, Parser, Subcommand, ValueEnum, value_parser}; use clap_complete::Shell; +#[cfg(feature = "dns_payment")] +use crate::utils::parse_dns_recipient; #[cfg(any(feature = "electrum", feature = "esplora", feature = "rpc"))] use crate::utils::parse_proxy_auth; use crate::utils::{parse_address, parse_outpoint, parse_recipient}; @@ -198,6 +200,16 @@ pub enum CliSubCommand { #[arg(long = "spend_key")] spend: bdk_sp::bitcoin::secp256k1::PublicKey, }, + + #[cfg(feature = "dns_payment")] + /// Resolves BIP-353 DNS payment instructions for a human-readable name. + ResolveDnsRecipient { + /// Human-readable name (e.g. user@domain.com) + hrn: String, + /// DNS resolver address + #[arg(long, default_value = "8.8.8.8")] + resolver: String, + }, } /// Wallet operation subcommands. @@ -368,8 +380,16 @@ pub enum OfflineWalletSubCommand { /// Adds a recipient to the transaction. // Clap Doesn't support complex vector parsing https://github.com/clap-rs/clap/issues/1704. // Address and amount parsing is done at run time in handler function. - #[arg(env = "ADDRESS:SAT", long = "to", required = true, value_parser = parse_recipient)] + #[arg(env = "ADDRESS:SAT", long = "to", value_parser = parse_recipient)] recipients: Vec<(ScriptBuf, u64)>, + #[cfg(feature = "dns_payment")] + /// Adds DNS recipients to the transaction + #[arg(long = "to_dns", value_parser = parse_dns_recipient)] + dns_recipients: Vec<(String, u64)>, + #[cfg(feature = "dns_payment")] + /// Custom resolver DNS IP to be used for resolution. + #[arg(long = "dns_resolver", default_value = "8.8.8.8")] + dns_resolver: String, /// Sends all the funds (or all the selected utxos). Requires only one recipient with value 0. #[arg(long = "send_all", short = 'a')] send_all: bool, diff --git a/src/dns_payment_instructions.rs b/src/dns_payment_instructions.rs new file mode 100644 index 00000000..b219ddc6 --- /dev/null +++ b/src/dns_payment_instructions.rs @@ -0,0 +1,208 @@ +use bdk_wallet::bitcoin::{Address, Amount, Network}; +use bitcoin_payment_instructions::{ + FixedAmountPaymentInstructions, ParseError, PaymentInstructions, PaymentMethod, + PaymentMethodType, amount, dns_resolver::DNSHrnResolver, +}; +use core::{net::SocketAddr, str::FromStr}; +use tokio::time::timeout; + +use crate::error::BDKCliError as Error; + +#[derive(Debug)] +pub struct ResolvedPaymentInfo { + pub hrn: String, + pub payment_methods: Vec, + pub description: Option, + pub min_amount: Option, + pub max_amount: Option, + pub notes: String, +} + +impl ResolvedPaymentInfo { + pub fn display(&self) -> Result { + let methods: Vec = self + .payment_methods + .iter() + .map(|pm| match pm { + PaymentMethod::LightningBolt11(bolt11) => { + format!("Bolt 11 invoice ({})", bolt11) + } + PaymentMethod::LightningBolt12(offer) => format!("Bolt 12 invoice ({})", offer), + PaymentMethod::OnChain(address) => format!("On chain ({})", address), + PaymentMethod::Cashu(csh) => format!("Cashu payment ({})", csh), + }) + .collect(); + + Ok(serde_json::to_string_pretty(&serde_json::json!({ + "hrn": self.hrn, + "payment_methods": methods, + "description": self.description, + "min_amount": self.min_amount, + "max_amount": self.max_amount, + "notes": self.notes + }))?) + } +} + +pub(crate) async fn parse_dns_instructions( + hrn: &str, + network: Network, + dns_resolver: &str, +) -> Result<(DNSHrnResolver, PaymentInstructions), ParseError> { + let ip_address = if dns_resolver.contains(':') { + dns_resolver + } else { + &format!("{dns_resolver}:53") + }; + + let sock_addr = SocketAddr::from_str(ip_address).map_err(|_| { + ParseError::HrnResolutionError("Unable to create socket from provided address") + })?; + let resolver = DNSHrnResolver(sock_addr); + let instructions = timeout( + std::time::Duration::from_secs(30), + PaymentInstructions::parse(hrn, network, &resolver, true), + ) + .await + .map_err(|_| ParseError::HrnResolutionError("Resolution request timed out"))??; + Ok((resolver, instructions)) +} + +fn get_onchain_info( + instructions: &FixedAmountPaymentInstructions, +) -> Result<(Address, Amount), Error> { + // Look for on chain payment method as it's the only one we can support + let PaymentMethod::OnChain(addr) = instructions + .methods() + .iter() + .find(|ix| matches!(ix, PaymentMethod::OnChain(_))) + .ok_or(Error::Generic( + "Missing Onchain payment method option.".to_string(), + ))? + else { + return Err(Error::Generic("Unsupported payment method".to_string())); + }; + + let Some(onchain_amount) = instructions.onchain_payment_amount() else { + return Err(Error::Generic( + "On chain amount should be specified".to_string(), + )); + }; + + // We need this conversion since Amount from instructions is different from Amount from bitcoin + Ok((addr.clone(), Amount::from_sat(onchain_amount.milli_sats()))) +} + +pub async fn process_instructions( + amount_to_send: Amount, + payment_instructions: &PaymentInstructions, + resolver: DNSHrnResolver, +) -> Result<(Address, Amount), Error> { + match payment_instructions { + PaymentInstructions::ConfigurableAmount(instructions) => { + // Look for on chain payment method as it's the only one we can support + if !instructions + .methods() + .any(|method| matches!(method.method_type(), PaymentMethodType::OnChain)) + { + return Err(Error::Generic("Unsupported payment method".to_string())); + } + + let min_amount = instructions + .min_amt() + .map(|amnt| Amount::from_sat(amnt.milli_sats())); + + let max_amount = instructions + .max_amt() + .map(|amnt| Amount::from_sat(amnt.milli_sats())); + + if min_amount.is_some_and(|min| amount_to_send < min) { + return Err(Error::Generic( + format!( + "Amount to send should be greater than min {}", + min_amount.unwrap() + ) + .to_string(), + )); + } + + if max_amount.is_some_and(|max| amount_to_send > max) { + return Err(Error::Generic( + format!( + "Amount to send should be lower than max {}", + max_amount.unwrap() + ) + .to_string(), + )); + } + + let fixed_instructions = instructions + .clone() + .set_amount( + amount::Amount::from_sats(amount_to_send.to_sat()).unwrap(), + &resolver, + ) + .await + .map_err(|err| { + Error::Generic(format!("Error occured while parsing instructions {err}")) + })?; + + let onchain_details = get_onchain_info(&fixed_instructions)?; + + Ok((onchain_details.0.clone(), onchain_details.1)) + } + + PaymentInstructions::FixedAmount(instructions) => Ok(get_onchain_info(instructions)?), + } +} + +/// Resolves the dns payment instructions found at the specified Human Readable Name +pub async fn resolve_dns_recipient( + hrn: &str, + network: Network, + dns_resolver: &str, +) -> Result { + let (resolver, instructions) = parse_dns_instructions(hrn, network, dns_resolver).await?; + + match instructions { + PaymentInstructions::ConfigurableAmount(ix) => { + let description = ix.recipient_description().map(|s| s.to_string()); + let min_amount = ix.min_amt().map(|amnt| Amount::from_sat(amnt.milli_sats())); + let max_amount = ix.max_amt().map(|amnt| Amount::from_sat(amnt.milli_sats())); + + // Let's set a dummy amount to resolve the payment methods accepted. + let fixed_instructions = ix + .set_amount(amount::Amount::ZERO, &resolver) + .await + .map_err(ParseError::InvalidInstructions)?; + + let payment = ResolvedPaymentInfo { + min_amount, + max_amount, + payment_methods: fixed_instructions.methods().into(), + description, + hrn: hrn.to_string(), + notes: "This is configurable payment instructions. You must send an amount between min_amount and max_amount if set.".to_string(), + }; + + Ok(payment) + } + + PaymentInstructions::FixedAmount(ix) => { + let max_amount = ix + .max_amount() + .map(|amnt| Amount::from_sat(amnt.milli_sats())); + + let payment = ResolvedPaymentInfo { + min_amount: None, + max_amount, + payment_methods: ix.methods().into(), + description: ix.recipient_description().map(|s| s.to_string()), + hrn: hrn.to_string(), + notes: "This is a fixed payment instructions. You must send exactly the amount specified in max_amount.".to_string(), + }; + + Ok(payment) + } + } +} diff --git a/src/handlers.rs b/src/handlers.rs index a8ae778b..9944c28a 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -12,6 +12,10 @@ use crate::commands::OfflineWalletSubCommand::*; use crate::commands::*; use crate::config::{WalletConfig, WalletConfigInner}; +#[cfg(feature = "dns_payment")] +use crate::dns_payment_instructions::{ + parse_dns_instructions, process_instructions, resolve_dns_recipient, +}; use crate::error::BDKCliError as Error; #[cfg(any(feature = "sqlite", feature = "redb"))] use crate::persister::Persister; @@ -120,7 +124,7 @@ const NUMS_UNSPENDABLE_KEY_HEX: &str = /// Execute an offline wallet sub-command /// /// Offline wallet sub-commands are described in [`OfflineWalletSubCommand`]. -pub fn handle_offline_wallet_subcommand( +pub async fn handle_offline_wallet_subcommand( wallet: &mut Wallet, wallet_opts: &WalletOpts, cli_opts: &CliOpts, @@ -533,7 +537,14 @@ pub fn handle_offline_wallet_subcommand( } } CreateTx { + #[cfg(feature = "dns_payment")] + mut recipients, + #[cfg(not(feature = "dns_payment"))] recipients, + #[cfg(feature = "dns_payment")] + dns_recipients, + #[cfg(feature = "dns_payment")] + dns_resolver, send_all, enable_rbf, offline_signer, @@ -547,10 +558,29 @@ pub fn handle_offline_wallet_subcommand( } => { let mut tx_builder = wallet.build_tx(); + #[cfg(feature = "dns_payment")] + for recipient in dns_recipients { + log::info!("Resolving DNS instructions for recipient {}", recipient.0); + let amount = Amount::from_sat(recipient.1); + let (resolver, instructions) = + parse_dns_instructions(&recipient.0, cli_opts.network, &dns_resolver) + .await + .map_err(|e| Error::Generic(format!("Parsing error occured {e:#?}")))?; + let payment = process_instructions(amount, &instructions, resolver).await?; + + recipients.push((payment.0.into(), payment.1.to_sat())); + } + + if recipients.is_empty() { + return Err(Error::Generic( + "Either --to or --to_dns parameters must be specified".to_string(), + )); + } + if send_all { tx_builder.drain_wallet().drain_to(recipients[0].0.clone()); } else { - let recipients = recipients + let recipients: Vec<_> = recipients .into_iter() .map(|(script, amount)| (script, Amount::from_sat(amount))) .collect(); @@ -1348,6 +1378,19 @@ pub(crate) fn handle_compile_subcommand( } } +#[cfg(feature = "dns_payment")] +pub(crate) async fn handle_resolve_dns_recipient_command( + hrn: &str, + resolver: &str, + network: Network, +) -> Result { + let resolved = resolve_dns_recipient(hrn, network, resolver) + .await + .map_err(|e| Error::Generic(format!("{:?}", e)))?; + + resolved.display() +} + /// Handle wallets command to show all saved wallet configurations pub fn handle_wallets_subcommand(datadir: &Path, pretty: bool) -> Result { let load_config = WalletConfig::load(datadir)?; @@ -1589,7 +1632,8 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result { &wallet_opts, &cli_opts, offline_subcommand.clone(), - )?; + ) + .await?; wallet.persist(&mut persister)?; result }; @@ -1601,7 +1645,8 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result { &wallet_opts, &cli_opts, offline_subcommand.clone(), - )? + ) + .await? }; Ok(result) } @@ -1727,6 +1772,13 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result { Ok("".to_string()) } + + #[cfg(feature = "dns_payment")] + CliSubCommand::ResolveDnsRecipient { hrn, resolver } => { + let res = + handle_resolve_dns_recipient_command(&hrn, &resolver, cli_opts.network).await?; + Ok(res) + } }; result } @@ -1767,6 +1819,7 @@ async fn respond( } => { let value = handle_offline_wallet_subcommand(wallet, wallet_opts, cli_opts, offline_subcommand) + .await .map_err(|e| e.to_string())?; Some(value) } diff --git a/src/main.rs b/src/main.rs index 90d701b0..6b7745c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,9 @@ mod payjoin; mod persister; mod utils; +#[cfg(feature = "dns_payment")] +mod dns_payment_instructions; + use bdk_wallet::bitcoin::Network; use log::{debug, error, warn}; diff --git a/src/utils.rs b/src/utils.rs index 34baf867..d78abb55 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -96,6 +96,17 @@ pub(crate) fn parse_sp_code_value_pairs(s: &str) -> Result<(SilentPaymentCode, u Ok((key, value)) } +#[cfg(feature = "dns_payment")] +/// Parse dns recipients in the form "test@me.com:10000" from cli input +pub(crate) fn parse_dns_recipient(s: &str) -> Result<(String, u64), String> { + let parts: Vec<_> = s.split(':').collect(); + if parts.len() != 2 { + return Err("Invalid format".to_string()); + } + let sending_amount = u64::from_str(parts[1]).map_err(|e| e.to_string())?; + Ok((parts[0].to_string(), sending_amount)) +} + #[cfg(any(feature = "electrum", feature = "esplora", feature = "rpc"))] /// Parse the proxy (Socket:Port) argument from the cli input. pub(crate) fn parse_proxy_auth(s: &str) -> Result<(String, String), Error> {