Skip to main content

bip353/
config.rs

1//! Configuration options for BIP-353 resolver
2
3use std::net::{SocketAddr, IpAddr, Ipv4Addr};
4use std::time::Duration;
5
6/// Configuration for BIP-353 resolver
7#[derive(Debug, Clone)]
8pub struct ResolverConfig {
9    /// The DNS resolver to use (IP and port)
10    pub dns_resolver: SocketAddr,
11    
12    /// Whether to enforce DNSSEC validation
13    pub enforce_dnssec: bool,
14    
15    /// Timeout for DNS queries in milliseconds
16    pub timeout_ms: u64,
17    
18    /// Whether to allow HTTP resolution fallback 
19    /// (for domains that don't support BIP-353 DNS but do support LN-Address)
20    pub allow_http_fallback: bool,
21    
22    /// Network to use for parsing payment instructions
23    pub network: bitcoin::Network,
24}
25
26impl Default for ResolverConfig {
27    fn default() -> Self {
28        Self {
29            // Google DNS with DNSSEC support
30            dns_resolver: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)), 53),
31            enforce_dnssec: true,
32            timeout_ms: 5000, // 5 second timeout
33            allow_http_fallback: true,
34            network: bitcoin::Network::Bitcoin,
35        }
36    }
37}
38
39impl ResolverConfig {
40    /// Create a configuration for testnet
41    pub fn testnet() -> Self {
42        Self {
43            network: bitcoin::Network::Testnet,
44            ..Default::default()
45        }
46    }
47    
48    /// Create a configuration for signet
49    pub fn signet() -> Self {
50        Self {
51            network: bitcoin::Network::Signet, 
52            ..Default::default()
53        }
54    }
55
56    /// Create a configuration for regtest
57    pub fn regtest() -> Self {
58        Self {
59            network: bitcoin::Network::Regtest,
60            ..Default::default()
61        }
62    }
63    
64    /// Set the DNS resolver
65    pub fn with_dns_resolver(mut self, resolver: SocketAddr) -> Self {
66        self.dns_resolver = resolver;
67        self
68    }
69    
70    /// Set the DNSSEC enforcement
71    pub fn with_dnssec(mut self, enforce: bool) -> Self {
72        self.enforce_dnssec = enforce;
73        self
74    }
75    
76    /// Set the timeout
77    pub fn with_timeout(mut self, timeout: Duration) -> Self {
78        self.timeout_ms = timeout.as_millis() as u64;
79        self
80    }
81    
82    /// Set whether to allow HTTP fallback
83    pub fn with_http_fallback(mut self, allow: bool) -> Self {
84        self.allow_http_fallback = allow;
85        self
86    }
87    
88    /// Set the network
89    pub fn with_network(mut self, network: bitcoin::Network) -> Self {
90        self.network = network;
91        self
92    }
93    
94    /// Get the timeout as a Duration
95    pub fn timeout(&self) -> Duration {
96        Duration::from_millis(self.timeout_ms)
97    }
98}