-
Notifications
You must be signed in to change notification settings - Fork 7
Allow for custom HTTPHrnResolvers #7
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
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -29,7 +29,28 @@ const DOH_ENDPOINT: &'static str = "https://dns.google/dns-query?dns="; | |
/// | ||
/// Note that using this may reveal our IP address to the recipient and information about who we're | ||
/// paying to Google (via `dns.google`). | ||
pub struct HTTPHrnResolver; | ||
#[derive(Debug, Clone)] | ||
pub struct HTTPHrnResolver { | ||
client: reqwest::Client, | ||
} | ||
|
||
impl HTTPHrnResolver { | ||
/// Create a new `HTTPHrnResolver` with a default `reqwest::Client`. | ||
pub fn new() -> Self { | ||
HTTPHrnResolver::default() | ||
} | ||
|
||
/// Create a new `HTTPHrnResolver` with a custom `reqwest::Client`. | ||
pub fn with_client(client: reqwest::Client) -> Self { | ||
HTTPHrnResolver { client } | ||
} | ||
} | ||
|
||
impl Default for HTTPHrnResolver { | ||
fn default() -> Self { | ||
HTTPHrnResolver { client: reqwest::Client::new() } | ||
} | ||
} | ||
|
||
const B64_CHAR: [u8; 64] = [ | ||
b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P', | ||
|
@@ -107,11 +128,10 @@ impl HTTPHrnResolver { | |
let mut pending_queries = vec![initial_query]; | ||
|
||
while let Some(query) = pending_queries.pop() { | ||
let client = reqwest::Client::new(); | ||
|
||
let request_url = query_to_url(query); | ||
let req = client.get(request_url).header("accept", "application/dns-message").build(); | ||
let resp = client.execute(req.map_err(|_| DNS_ERR)?).await.map_err(|_| DNS_ERR)?; | ||
let req = | ||
self.client.get(request_url).header("accept", "application/dns-message").build(); | ||
let resp = self.client.execute(req.map_err(|_| DNS_ERR)?).await.map_err(|_| DNS_ERR)?; | ||
let body = resp.bytes().await.map_err(|_| DNS_ERR)?; | ||
|
||
let mut answer = QueryBuf::new_zeroed(0); | ||
|
@@ -136,8 +156,15 @@ impl HTTPHrnResolver { | |
|
||
async fn resolve_lnurl_impl(&self, lnurl_url: &str) -> Result<HrnResolution, &'static str> { | ||
let err = "Failed to fetch LN-Address initial well-known endpoint"; | ||
let init: LNURLInitResponse = | ||
reqwest::get(lnurl_url).await.map_err(|_| err)?.json().await.map_err(|_| err)?; | ||
let init: LNURLInitResponse = self | ||
.client | ||
.get(lnurl_url) | ||
.send() | ||
.await | ||
.map_err(|_| err)? | ||
.json() | ||
.await | ||
.map_err(|_| err)?; | ||
|
||
if init.tag != "payRequest" { | ||
return Err("LNURL initial init_response had an incorrect tag value"); | ||
|
@@ -198,8 +225,15 @@ impl HrnResolver for HTTPHrnResolver { | |
} else { | ||
write!(&mut callback, "?amount={}", amt.milli_sats()).expect("Write to String"); | ||
} | ||
let callback_response: LNURLCallbackResponse = | ||
reqwest::get(callback).await.map_err(|_| err)?.json().await.map_err(|_| err)?; | ||
let callback_response: LNURLCallbackResponse = self | ||
.client | ||
.get(callback) | ||
.send() | ||
.await | ||
.map_err(|_| err)? | ||
.json() | ||
.await | ||
.map_err(|_| err)?; | ||
|
||
if !callback_response.routes.is_empty() { | ||
return Err("LNURL callback response contained a non-empty routes array"); | ||
|
@@ -257,7 +291,7 @@ mod tests { | |
|
||
#[tokio::test] | ||
async fn test_dns_via_http_hrn_resolver() { | ||
let resolver = HTTPHrnResolver; | ||
let resolver = HTTPHrnResolver::default(); | ||
let instructions = PaymentInstructions::parse( | ||
"[email protected]", | ||
bitcoin::Network::Bitcoin, | ||
|
@@ -303,10 +337,11 @@ mod tests { | |
|
||
#[tokio::test] | ||
async fn test_http_hrn_resolver() { | ||
let resolver = HTTPHrnResolver::default(); | ||
let instructions = PaymentInstructions::parse( | ||
"[email protected]", | ||
bitcoin::Network::Bitcoin, | ||
&HTTPHrnResolver, | ||
&resolver, | ||
true, | ||
) | ||
.await | ||
|
@@ -323,7 +358,7 @@ mod tests { | |
assert_eq!(hrn.user(), "lnurltest"); | ||
assert_eq!(hrn.domain(), "bitcoin.ninja"); | ||
|
||
instr.set_amount(Amount::from_sats(100_000).unwrap(), &HTTPHrnResolver).await.unwrap() | ||
instr.set_amount(Amount::from_sats(100_000).unwrap(), &resolver).await.unwrap() | ||
} else { | ||
panic!(); | ||
}; | ||
|
@@ -348,11 +383,12 @@ mod tests { | |
|
||
#[tokio::test] | ||
async fn test_http_lnurl_resolver() { | ||
let resolver = HTTPHrnResolver::default(); | ||
let instructions = PaymentInstructions::parse( | ||
// lnurl encoding for [email protected] | ||
"lnurl1dp68gurn8ghj7cnfw33k76tw9ehxjmn2vyhjuam9d3kz66mwdamkutmvde6hymrs9akxuatjd36x2um5ahcq39", | ||
Network::Bitcoin, | ||
&HTTPHrnResolver, | ||
&resolver, | ||
true, | ||
) | ||
.await | ||
|
@@ -365,7 +401,7 @@ mod tests { | |
assert_eq!(instr.pop_callback(), None); | ||
assert!(instr.bip_353_dnssec_proof().is_none()); | ||
|
||
instr.set_amount(Amount::from_sats(100_000).unwrap(), &HTTPHrnResolver).await.unwrap() | ||
instr.set_amount(Amount::from_sats(100_000).unwrap(), &resolver).await.unwrap() | ||
} else { | ||
panic!(); | ||
}; | ||
|
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
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.
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.
lol might want to split the json into a separate line so its not so insane.