-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathdns_payment_instructions.rs
More file actions
107 lines (93 loc) · 3.54 KB
/
dns_payment_instructions.rs
File metadata and controls
107 lines (93 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use bdk_wallet::bitcoin::{Address, Amount, Network};
use bitcoin_payment_instructions::{
FixedAmountPaymentInstructions, ParseError, PaymentInstructions, PaymentMethod,
PaymentMethodType, amount, dns_resolver::DNSHrnResolver, hrn_resolution::HrnResolver,
};
use core::{net::SocketAddr, str::FromStr};
async fn parse_dns_instructions(
hrn: &str,
resolver: &impl HrnResolver,
network: Network,
) -> Result<PaymentInstructions, ParseError> {
let instructions = PaymentInstructions::parse(hrn, network, resolver, true).await?;
Ok(instructions)
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct Payment {
pub address: Address,
pub amount: Amount,
pub min_amount: Option<Amount>,
pub max_amount: Option<Amount>,
pub dnssec_proof: Option<Vec<u8>>,
}
fn process_fixed_instructions(
instructions: &FixedAmountPaymentInstructions,
) -> Result<Payment, ParseError> {
// 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(_)))
.map(|pm| pm)
.unwrap()
else {
return Err(ParseError::InvalidInstructions(
"Unsupported payment method",
));
};
let Some(onchain_amount) = instructions.onchain_payment_amount() else {
return Err(ParseError::InvalidInstructions(
"On chain amount should be specified",
));
};
// We need this conversion since Amount from instructions is different from Amount from bitcoin
let onchain_amount = Amount::from_sat(onchain_amount.milli_sats());
Ok(Payment {
address: addr.clone(),
amount: onchain_amount,
min_amount: None,
max_amount: None,
dnssec_proof: instructions.bip_353_dnssec_proof().clone(),
})
}
pub async fn resolve_dns_recipient(
hrn: &str,
amount: Amount,
network: Network,
) -> Result<Payment, ParseError> {
let resolver = DNSHrnResolver(SocketAddr::from_str("8.8.8.8:53").expect("Should not fail."));
let payment_instructions = parse_dns_instructions(hrn, &resolver, network).await?;
match payment_instructions {
PaymentInstructions::ConfigurableAmount(instructions) => {
// Look for on chain payment method as it's the only one we can support
if instructions
.methods()
.find(|method| matches!(method.method_type(), PaymentMethodType::OnChain))
.is_none()
{
return Err(ParseError::InvalidInstructions(
"Unsupported payment method",
));
}
let min_amount = instructions
.min_amt()
.map(|amnt| Amount::from_sat(amnt.sats_rounding_up()));
let max_amount = instructions
.max_amt()
.map(|amnt| Amount::from_sat(amnt.sats_rounding_up()));
let fixed_instructions = instructions
.set_amount(
amount::Amount::from_sats(amount.to_sat()).unwrap(),
&resolver,
)
.await
.map_err(|s| ParseError::InvalidInstructions(s))?;
let mut instructions = process_fixed_instructions(&fixed_instructions)?;
instructions.min_amount = min_amount;
instructions.max_amount = max_amount;
Ok(instructions)
}
PaymentInstructions::FixedAmount(instructions) => process_fixed_instructions(&instructions),
}
}