-
Notifications
You must be signed in to change notification settings - Fork 125
Add support for resolving BIP 353 Human-Readable Names #630
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
base: main
Are you sure you want to change the base?
Changes from all commits
047b68f
b11b169
e5ab051
0209b89
0b9291f
5d4036d
d3eeb32
12424fc
1303b12
ee68b32
571ba08
8d9ff9a
c96a78e
7d3ae68
b154398
7c5a4b9
310fdc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ use std::collections::HashMap; | |
| use std::convert::TryInto; | ||
| use std::default::Default; | ||
| use std::path::PathBuf; | ||
| use std::sync::{Arc, Mutex, Once, RwLock}; | ||
| use std::sync::{Arc, Mutex, Once, RwLock, Weak}; | ||
| use std::time::SystemTime; | ||
| use std::{fmt, fs}; | ||
|
|
||
|
|
@@ -19,12 +19,14 @@ use bitcoin::bip32::{ChildNumber, Xpriv}; | |
| use bitcoin::key::Secp256k1; | ||
| use bitcoin::secp256k1::PublicKey; | ||
| use bitcoin::{BlockHash, Network}; | ||
| use bitcoin_payment_instructions::dns_resolver::DNSHrnResolver; | ||
| use bitcoin_payment_instructions::onion_message_resolver::LDKOnionMessageDNSSECHrnResolver; | ||
| use lightning::chain::{chainmonitor, BestBlock}; | ||
| use lightning::ln::channelmanager::{self, ChainParameters, ChannelManagerReadArgs}; | ||
| use lightning::ln::msgs::{RoutingMessageHandler, SocketAddress}; | ||
| use lightning::ln::peer_handler::{IgnoringMessageHandler, MessageHandler}; | ||
| use lightning::log_trace; | ||
| use lightning::onion_message::dns_resolution::DNSResolverMessageHandler; | ||
| use lightning::routing::gossip::NodeAlias; | ||
| use lightning::routing::router::DefaultRouter; | ||
| use lightning::routing::scoring::{ | ||
|
|
@@ -39,13 +41,14 @@ use lightning::util::persist::{ | |
| }; | ||
| use lightning::util::ser::ReadableArgs; | ||
| use lightning::util::sweep::OutputSweeper; | ||
| use lightning_dns_resolver::OMDomainResolver; | ||
| use lightning_persister::fs_store::FilesystemStore; | ||
| use vss_client::headers::VssHeaderProvider; | ||
|
|
||
| use crate::chain::ChainSource; | ||
| use crate::config::{ | ||
| default_user_config, may_announce_channel, AnnounceError, AsyncPaymentsRole, | ||
| BitcoindRestClientConfig, Config, ElectrumSyncConfig, EsploraSyncConfig, | ||
| BitcoindRestClientConfig, Config, ElectrumSyncConfig, EsploraSyncConfig, HRNResolverConfig, | ||
| DEFAULT_ESPLORA_SERVER_URL, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL, | ||
| }; | ||
| use crate::connection::ConnectionManager; | ||
|
|
@@ -76,8 +79,8 @@ use crate::runtime::{Runtime, RuntimeSpawner}; | |
| use crate::tx_broadcaster::TransactionBroadcaster; | ||
| use crate::types::{ | ||
| AsyncPersister, ChainMonitor, ChannelManager, DynStore, DynStoreWrapper, GossipSync, Graph, | ||
| KeysManager, MessageRouter, OnionMessenger, PaymentStore, PeerManager, PendingPaymentStore, | ||
| Persister, SyncAndAsyncKVStore, | ||
| HRNResolver, KeysManager, MessageRouter, OnionMessenger, PaymentStore, PeerManager, | ||
| PendingPaymentStore, Persister, SyncAndAsyncKVStore, | ||
| }; | ||
| use crate::wallet::persist::KVStoreWalletPersister; | ||
| use crate::wallet::Wallet; | ||
|
|
@@ -189,6 +192,10 @@ pub enum BuildError { | |
| NetworkMismatch, | ||
| /// The role of the node in an asynchronous payments context is not compatible with the current configuration. | ||
| AsyncPaymentsConfigMismatch, | ||
| /// An attempt to setup a DNS Resolver failed. | ||
| DNSResolverSetupFailed, | ||
| /// Failed to set up the peer manager. | ||
| PeerManagerSetupFailed, | ||
| } | ||
|
|
||
| impl fmt::Display for BuildError { | ||
|
|
@@ -221,6 +228,12 @@ impl fmt::Display for BuildError { | |
| "The async payments role is not compatible with the current configuration." | ||
| ) | ||
| }, | ||
| Self::DNSResolverSetupFailed => { | ||
| write!(f, "An attempt to setup a DNS resolver has failed.") | ||
| }, | ||
| Self::PeerManagerSetupFailed => { | ||
| write!(f, "Failed to set up the peer manager.") | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1517,7 +1530,79 @@ fn build_with_store_internal( | |
| })?; | ||
| } | ||
|
|
||
| let hrn_resolver = Arc::new(LDKOnionMessageDNSSECHrnResolver::new(Arc::clone(&network_graph))); | ||
| let peer_manager_hook: Arc<Mutex<Option<Weak<PeerManager>>>> = Arc::new(Mutex::new(None)); | ||
| let mut hrn_resolver_out = None; | ||
|
|
||
| let om_resolver = match &config.hrn_config { | ||
| None => Arc::new(IgnoringMessageHandler {}), | ||
| Some(hrn_config) => { | ||
| let runtime_handle = runtime.handle(); | ||
|
|
||
| let client_resolver: Arc<dyn DNSResolverMessageHandler + Send + Sync> = | ||
| match &hrn_config.client_resolution_config { | ||
| HRNResolverConfig::Blip32Onion => { | ||
| let hrn_res = Arc::new(LDKOnionMessageDNSSECHrnResolver::new(Arc::clone( | ||
| &network_graph, | ||
| ))); | ||
| hrn_resolver_out = Some(Arc::new(HRNResolver::Onion(Arc::clone(&hrn_res)))); | ||
|
|
||
| let pm_hook_clone = Arc::clone(&peer_manager_hook); | ||
| hrn_res.register_post_queue_action(Box::new(move || { | ||
| if let Ok(guard) = pm_hook_clone.lock() { | ||
| if let Some(weak_pm) = &*guard { | ||
| if let Some(pm) = weak_pm.upgrade() { | ||
| pm.process_events(); | ||
| } | ||
| } | ||
| } | ||
| })); | ||
|
|
||
| hrn_res as Arc<dyn DNSResolverMessageHandler + Send + Sync> | ||
| }, | ||
| HRNResolverConfig::LocalDns { dns_server_address } => { | ||
| let addr = dns_server_address | ||
| .parse() | ||
| .map_err(|_| BuildError::DNSResolverSetupFailed)?; | ||
| let hrn_res = Arc::new(DNSHrnResolver(addr)); | ||
| hrn_resolver_out = Some(Arc::new(HRNResolver::Local(hrn_res))); | ||
| let resolver = | ||
| Arc::new(OMDomainResolver::<IgnoringMessageHandler>::with_runtime( | ||
| addr, | ||
| None, | ||
| Some(runtime_handle.clone()), | ||
| )); | ||
| resolver.set_runtime(runtime_handle.clone()); | ||
| resolver as Arc<dyn DNSResolverMessageHandler + Send + Sync> | ||
| }, | ||
| }; | ||
|
|
||
| let should_act_as_service = if hrn_config.disable_hrn_resolution_service { | ||
| false | ||
| } else { | ||
| may_announce_channel(&config).is_ok() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to log and error out here, rather than silently overriding the user's configuration. |
||
| }; | ||
|
|
||
| if should_act_as_service { | ||
| if let HRNResolverConfig::LocalDns { dns_server_address } = | ||
| &hrn_config.client_resolution_config | ||
| { | ||
| let service_dns_addr = dns_server_address | ||
| .parse() | ||
| .map_err(|_| BuildError::DNSResolverSetupFailed)?; | ||
| Arc::new(OMDomainResolver::with_runtime( | ||
| service_dns_addr, | ||
| Some(client_resolver), | ||
| Some(runtime_handle.clone()), | ||
| )) | ||
| } else { | ||
| log_error!(logger, "To act as an HRN resolution service, the DNS resolver must be configured to use a DNS server."); | ||
| return Err(BuildError::DNSResolverSetupFailed); | ||
| } | ||
| } else { | ||
| client_resolver | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| // Initialize the PeerManager | ||
| let onion_messenger: Arc<OnionMessenger> = | ||
|
|
@@ -1530,7 +1615,7 @@ fn build_with_store_internal( | |
| message_router, | ||
| Arc::clone(&channel_manager), | ||
| Arc::clone(&channel_manager), | ||
| Arc::clone(&hrn_resolver), | ||
| Arc::clone(&om_resolver), | ||
| IgnoringMessageHandler {}, | ||
| )) | ||
| } else { | ||
|
|
@@ -1542,7 +1627,7 @@ fn build_with_store_internal( | |
| message_router, | ||
| Arc::clone(&channel_manager), | ||
| Arc::clone(&channel_manager), | ||
| Arc::clone(&hrn_resolver), | ||
| Arc::clone(&om_resolver), | ||
| IgnoringMessageHandler {}, | ||
| )) | ||
| }; | ||
|
|
@@ -1663,7 +1748,7 @@ fn build_with_store_internal( | |
| BuildError::InvalidSystemTime | ||
| })?; | ||
|
|
||
| let peer_manager = Arc::new(PeerManager::new( | ||
| let peer_manager: Arc<PeerManager> = Arc::new(PeerManager::new( | ||
| msg_handler, | ||
| cur_time.as_secs().try_into().map_err(|e| { | ||
| log_error!(logger, "Failed to get current time: {}", e); | ||
|
|
@@ -1674,12 +1759,11 @@ fn build_with_store_internal( | |
| Arc::clone(&keys_manager), | ||
| )); | ||
|
|
||
| let peer_manager_clone = Arc::downgrade(&peer_manager); | ||
| hrn_resolver.register_post_queue_action(Box::new(move || { | ||
| if let Some(upgraded_pointer) = peer_manager_clone.upgrade() { | ||
| upgraded_pointer.process_events(); | ||
| } | ||
| })); | ||
| if let Ok(mut guard) = peer_manager_hook.lock() { | ||
| *guard = Some(Arc::downgrade(&peer_manager)); | ||
| } else { | ||
| return Err(BuildError::PeerManagerSetupFailed); | ||
| } | ||
|
|
||
| liquidity_source.as_ref().map(|l| l.set_peer_manager(Arc::downgrade(&peer_manager))); | ||
|
|
||
|
|
@@ -1786,7 +1870,7 @@ fn build_with_store_internal( | |
| node_metrics, | ||
| om_mailbox, | ||
| async_payments_role, | ||
| hrn_resolver, | ||
| hrn_resolver: hrn_resolver_out, | ||
| #[cfg(cycle_tests)] | ||
| _leak_checker, | ||
| }) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,7 +127,8 @@ pub(crate) const HRN_RESOLUTION_TIMEOUT_SECS: u64 = 5; | |
| /// | `probing_liquidity_limit_multiplier` | 3 | | ||
| /// | `log_level` | Debug | | ||
| /// | `anchor_channels_config` | Some(..) | | ||
| /// | `route_parameters` | None | | ||
| /// | `route_parameters` | None | | ||
| /// | `hrn_config` | Some(..) | | ||
| /// | ||
| /// See [`AnchorChannelsConfig`] and [`RouteParametersConfig`] for more information regarding their | ||
| /// respective default values. | ||
|
|
@@ -192,6 +193,10 @@ pub struct Config { | |
| /// **Note:** If unset, default parameters will be used, and you will be able to override the | ||
| /// parameters on a per-payment basis in the corresponding method calls. | ||
| pub route_parameters: Option<RouteParametersConfig>, | ||
| /// Configuration options for Human-Readable Names ([BIP 353]). | ||
| /// | ||
| /// [BIP 353]: https://github.com/bitcoin/bips/blob/master/bip-0353.mediawiki | ||
| pub hrn_config: Option<HumanReadableNamesConfig>, | ||
| } | ||
|
|
||
| impl Default for Config { | ||
|
|
@@ -206,6 +211,43 @@ impl Default for Config { | |
| anchor_channels_config: Some(AnchorChannelsConfig::default()), | ||
| route_parameters: None, | ||
| node_alias: None, | ||
| hrn_config: Some(HumanReadableNamesConfig::default()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Configuration options for how our node resolves Human-Readable Names (HRNs) when acting as a client. | ||
| #[derive(Debug, Clone)] | ||
| pub enum HRNResolverConfig { | ||
| /// Use bLIP-32 to ask other nodes to resolve names for us. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please link to the bLIP. |
||
| Blip32Onion, | ||
| /// Resolve names locally using a specific DNS server. | ||
| LocalDns { | ||
| /// The IP and port of the DNS server (e.g., "8.8.8.8:53"). | ||
| dns_server_address: String, | ||
| }, | ||
| } | ||
|
|
||
| /// Configuration options for Human-Readable Names ([BIP 353]). | ||
| /// | ||
| /// [BIP 353]: https://github.com/bitcoin/bips/blob/master/bip-0353.mediawiki | ||
| #[derive(Debug, Clone)] | ||
| pub struct HumanReadableNamesConfig { | ||
| /// This sets how our node resolves names when we want to send a payment. | ||
| pub client_resolution_config: HRNResolverConfig, | ||
| /// If set, this allows others to use our node for HRN resolutions ([bLIP-32]). | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to document the additional requirements, i.e., explain that even if you keep this enabled, you still need to run an announceable node etc. |
||
| /// | ||
| /// [bLIP-32]: https://github.com/lightning/blips/blob/master/blip-0032.md | ||
| pub disable_hrn_resolution_service: bool, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make this an It seems semantically it might make more sense to have this as a field on Though, maybe we should rename |
||
| } | ||
|
|
||
| impl Default for HumanReadableNamesConfig { | ||
| fn default() -> Self { | ||
| HumanReadableNamesConfig { | ||
| client_resolution_config: HRNResolverConfig::LocalDns { | ||
| dns_server_address: "8.8.8.8:53".to_string(), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document the default choices in the |
||
| }, | ||
| disable_hrn_resolution_service: false, | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to introduce a feature just to enable/disable some tests. If they need a flag, please make it a
cfg(hrn_tests)as features are meant to be user-facing.