-
Notifications
You must be signed in to change notification settings - Fork 28
Implementation of bip-0353 #246
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
Open
IgnacioPorte
wants to merge
6
commits into
lndk-org:master
Choose a base branch
from
IgnacioPorte:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f129b38
feat: implement bip-0353 with self resolve.
IgnacioPorte 1a56d82
feat: add specific BIP-353 errors for DNS resolution failures
IgnacioPorte c746a64
test: add BIP-353 integration test for pay_offer with name resolution
IgnacioPorte 07693ea
refactor: move pay offer with name to a separate endpoint
IgnacioPorte 3623a11
refactor: inject DNS resolver into OfferHandler
IgnacioPorte 9848db5
chore: add timeout and improve URI parsing in DNS resolver
IgnacioPorte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| use crate::OfferError; | ||
| use bitcoin_payment_instructions::hrn_resolution::{HrnResolution, HrnResolver, HumanReadableName}; | ||
| use bitcoin_payment_instructions::http_resolver::HTTPHrnResolver; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
| use url::Url; | ||
|
|
||
| const DEFAULT_DNS_TIMEOUT_SECS: u64 = 20; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct LndkDNSResolverMessageHandler { | ||
| resolver: Arc<dyn HrnResolver + Send + Sync>, | ||
| } | ||
|
|
||
| impl Default for LndkDNSResolverMessageHandler { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl LndkDNSResolverMessageHandler { | ||
| pub fn new() -> Self { | ||
| let client = reqwest::Client::builder() | ||
| .timeout(Duration::from_secs(DEFAULT_DNS_TIMEOUT_SECS)) | ||
| .build() | ||
| .expect("Failed to build HTTP client for DNS resolution"); | ||
|
|
||
| Self::with_resolver(HTTPHrnResolver::with_client(client)) | ||
| } | ||
|
|
||
| pub fn with_resolver<R: HrnResolver + Send + Sync + 'static>(resolver: R) -> Self { | ||
| Self { | ||
| resolver: Arc::new(resolver), | ||
| } | ||
| } | ||
|
|
||
| pub async fn resolver_hrn_to_offer(&self, name_str: &str) -> Result<String, OfferError> { | ||
| let resolved_uri = self.resolve_locally(name_str.to_string()).await?; | ||
| self.extract_offer_from_uri(&resolved_uri) | ||
| } | ||
|
|
||
| pub fn extract_offer_from_uri(&self, uri: &str) -> Result<String, OfferError> { | ||
| let url = Url::parse(uri) | ||
| .map_err(|_| OfferError::ResolveUriError("Invalid URI format".to_string()))?; | ||
|
|
||
| for (key, value) in url.query_pairs() { | ||
| if key.eq_ignore_ascii_case("lno") { | ||
| return Ok(value.into_owned()); | ||
| } | ||
| } | ||
|
|
||
| Err(OfferError::ResolveUriError( | ||
| "URI does not contain 'lno' parameter with BOLT12 offer".to_string(), | ||
| )) | ||
| } | ||
|
|
||
| pub async fn resolve_locally(&self, name: String) -> Result<String, OfferError> { | ||
| let hrn_parsed = HumanReadableName::from_encoded(&name) | ||
| .map_err(|_| OfferError::ParseHrnFailure(name.clone()))?; | ||
|
|
||
| let resolution = self | ||
| .resolver | ||
| .resolve_hrn(&hrn_parsed) | ||
|
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. Does it have a time out?
Contributor
Author
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. No, now I added |
||
| .await | ||
| .map_err(|e| OfferError::HrnResolutionFailure(format!("{}: {}", name, e)))?; | ||
|
|
||
| let uri = match resolution { | ||
| HrnResolution::DNSSEC { result, .. } => result, | ||
| HrnResolution::LNURLPay { .. } => { | ||
| return Err(OfferError::ResolveUriError( | ||
| "LNURL resolution not supported in this flow".to_string(), | ||
| )) | ||
| } | ||
| }; | ||
|
|
||
| Ok(uri) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_extract_offer_from_simple_uri() { | ||
| let handler = LndkDNSResolverMessageHandler::new(); | ||
| let uri = "bitcoin:?lno=lno1qgsqvgnwgcg35z"; | ||
| let result = handler.extract_offer_from_uri(uri); | ||
| assert_eq!(result.unwrap(), "lno1qgsqvgnwgcg35z"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_extract_offer_with_percent_encoding() { | ||
| let handler = LndkDNSResolverMessageHandler::new(); | ||
| let uri = "bitcoin:?lno=lno1%20test%3Dvalue"; | ||
| let result = handler.extract_offer_from_uri(uri); | ||
| assert_eq!(result.unwrap(), "lno1 test=value"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_extract_offer_missing_param() { | ||
| let handler = LndkDNSResolverMessageHandler::new(); | ||
| let uri = "bitcoin:?amount=50&label=test"; | ||
| let result = handler.extract_offer_from_uri(uri); | ||
| assert!(result.is_err()); | ||
| assert!(result | ||
| .unwrap_err() | ||
| .to_string() | ||
| .contains("does not contain 'lno' parameter")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| mod clock; | ||
| pub mod dns_resolver; | ||
| mod grpc; | ||
| #[allow(dead_code)] | ||
| pub mod lnd; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.