|
| 1 | +use bdk_wallet::bitcoin::{Address, Amount, Network}; |
| 2 | +use bitcoin_payment_instructions::{ |
| 3 | + FixedAmountPaymentInstructions, ParseError, PaymentInstructions, PaymentMethod, |
| 4 | + PaymentMethodType, amount, dns_resolver::DNSHrnResolver, hrn_resolution::HrnResolver, |
| 5 | +}; |
| 6 | +use core::{net::SocketAddr, str::FromStr}; |
| 7 | + |
| 8 | +async fn parse_dns_instructions( |
| 9 | + hrn: &str, |
| 10 | + resolver: &impl HrnResolver, |
| 11 | + network: Network, |
| 12 | +) -> Result<PaymentInstructions, ParseError> { |
| 13 | + let instructions = PaymentInstructions::parse(hrn, network, resolver, true).await?; |
| 14 | + Ok(instructions) |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug)] |
| 18 | +pub struct Payment { |
| 19 | + pub address: Address, |
| 20 | + pub amount: Amount, |
| 21 | + pub dnssec_proof: Option<Vec<u8>>, |
| 22 | +} |
| 23 | + |
| 24 | +fn process_fixed_instructions( |
| 25 | + amount: Amount, |
| 26 | + instructions: &FixedAmountPaymentInstructions, |
| 27 | +) -> Result<Payment, ParseError> { |
| 28 | + // Look for on chain payment method as it's the only one we can support |
| 29 | + let PaymentMethod::OnChain(addr) = instructions |
| 30 | + .methods() |
| 31 | + .iter() |
| 32 | + .find(|ix| matches!(ix, PaymentMethod::OnChain(_))) |
| 33 | + .map(|pm| pm) |
| 34 | + .unwrap() |
| 35 | + else { |
| 36 | + return Err(ParseError::InvalidInstructions( |
| 37 | + "Unsupported payment method", |
| 38 | + )); |
| 39 | + }; |
| 40 | + |
| 41 | + let Some(onchain_amount) = instructions.onchain_payment_amount() else { |
| 42 | + return Err(ParseError::InvalidInstructions( |
| 43 | + "On chain amount should be specified", |
| 44 | + )); |
| 45 | + }; |
| 46 | + |
| 47 | + // We need this conversion since Amount from instructions is different from Amount from bitcoin |
| 48 | + let onchain_amount = Amount::from_sat(onchain_amount.sats_rounding_up()); |
| 49 | + |
| 50 | + if onchain_amount != amount { |
| 51 | + return Err(ParseError::InvalidInstructions( |
| 52 | + "Mismatched amount expected , got", |
| 53 | + )); |
| 54 | + } |
| 55 | + |
| 56 | + Ok(Payment { |
| 57 | + address: addr.clone(), |
| 58 | + amount: onchain_amount, |
| 59 | + dnssec_proof: instructions.bip_353_dnssec_proof().clone(), |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +pub async fn resolve_dns_recipient( |
| 65 | + hrn: &str, |
| 66 | + amount: Amount, |
| 67 | + network: Network, |
| 68 | +) -> Result<Payment, ParseError> { |
| 69 | + let resolver = DNSHrnResolver(SocketAddr::from_str("8.8.8.8:53").expect("Should not fail.")); |
| 70 | + let payment_instructions = parse_dns_instructions(hrn, &resolver, network).await?; |
| 71 | + |
| 72 | + match payment_instructions { |
| 73 | + PaymentInstructions::ConfigurableAmount(instructions) => { |
| 74 | + // Look for on chain payment method as it's the only one we can support |
| 75 | + if instructions |
| 76 | + .methods() |
| 77 | + .find(|method| matches!(method.method_type(), PaymentMethodType::OnChain)) |
| 78 | + .is_none() |
| 79 | + { |
| 80 | + return Err(ParseError::InvalidInstructions( |
| 81 | + "Unsupported payment method", |
| 82 | + )); |
| 83 | + } |
| 84 | + |
| 85 | + let min_amount = instructions.min_amt(); |
| 86 | + let max_amount = instructions.max_amt(); |
| 87 | + |
| 88 | + if min_amount.is_some() { |
| 89 | + let min_amount = min_amount |
| 90 | + .map(|a| Amount::from_sat(a.sats_rounding_up())) |
| 91 | + .unwrap(); |
| 92 | + if amount < min_amount { |
| 93 | + return Err(ParseError::InvalidInstructions( |
| 94 | + "Amount lesser than min amount", |
| 95 | + )); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if max_amount.is_some() { |
| 100 | + let max_amount = max_amount |
| 101 | + .map(|a| Amount::from_sat(a.sats_rounding_up())) |
| 102 | + .unwrap(); |
| 103 | + if amount > max_amount { |
| 104 | + return Err(ParseError::InvalidInstructions( |
| 105 | + "Amount greater than max amount", |
| 106 | + )); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + let fixed_instructions = instructions |
| 111 | + .set_amount( |
| 112 | + amount::Amount::from_sats(amount.to_sat()).unwrap(), |
| 113 | + &resolver, |
| 114 | + ) |
| 115 | + .await |
| 116 | + .map_err(|s| ParseError::InvalidInstructions(s))?; |
| 117 | + |
| 118 | + process_fixed_instructions(amount, &fixed_instructions) |
| 119 | + } |
| 120 | + |
| 121 | + PaymentInstructions::FixedAmount(instructions) => { |
| 122 | + process_fixed_instructions(amount, &instructions) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments