Skip to content

Commit 13dc0dd

Browse files
authored
Merge pull request #20 from TheBlueMatt/main
Replace reqwest with bitreq
2 parents be8a172 + c312f14 commit 13dc0dd

File tree

5 files changed

+55
-25
lines changed

5 files changed

+55
-25
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ jobs:
9797
cd fuzz
9898
cargo update -p syn --precise "2.0.106" --verbose
9999
cargo update -p quote --precise "1.0.41" --verbose
100+
cargo update -p proc-macro2 --precise "1.0.103" --verbose
100101
RUSTFLAGS="--cfg=fuzzing --cfg=secp256k1_fuzz --cfg=hashes_fuzz" cargo test --verbose --color always
101102
cargo clean
102103
- name: Run fuzzers

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bitcoin-payment-instructions"
3-
version = "0.6.0"
3+
version = "0.7.0"
44
authors = ["Matt Corallo", "Ben Carman"]
55
license = "MIT OR Apache-2.0"
66
repository = "https://github.com/rust-bitcoin/bitcoin-payment-instructions"
@@ -14,7 +14,8 @@ features = ["std", "http"]
1414
rustdoc-args = ["--cfg", "docsrs"]
1515

1616
[features]
17-
http = ["reqwest", "std", "serde", "serde_json"]
17+
http = ["bitreq", "std", "serde", "serde_json"]
18+
http_proxied = [ "http", "bitreq/proxy" ]
1819
std = ["dnssec-prover", "getrandom"]
1920
default = ["std"]
2021

@@ -24,7 +25,7 @@ lightning = { version = "0.2.0", default-features = false, features = ["dnssec"]
2425
bitcoin = { version = "0.32", default-features = false }
2526
getrandom = { version = "0.3", default-features = false, optional = true }
2627
dnssec-prover = { version = "0.6", default-features = false, optional = true, features = ["validation", "std", "tokio"] }
27-
reqwest = { version = "0.11", default-features = false, optional = true, features = ["rustls-tls-webpki-roots", "json"] }
28+
bitreq = { version = "0.2", default-features = false, optional = true, features = [ "async-https" ] }
2829
serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] }
2930
serde_json = { version = "1.0", default-features = false, optional = true, features = ["alloc"] }
3031

ci/check-lint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ RUSTC_MINOR_VERSION=$(rustc --version | awk '{ split($2,a,"."); print a[2] }')
66

77
# Starting with version 1.39.0, the `tokio` crate has an MSRV of rustc 1.70.0
88
[ "$RUSTC_MINOR_VERSION" -lt 70 ] && cargo update -p tokio --precise "1.38.1" --verbose
9-
[ "$RUSTC_MINOR_VERSION" -lt 70 ] && cargo update -p tokio-util --precise "0.7.10" --verbose
109

1110
# syn 2.0.107 requires rustc 1.68.0
1211
[ "$RUSTC_MINOR_VERSION" -lt 68 ] && cargo update -p syn --precise "2.0.106" --verbose
1312
# quote 1.0.42 requires rustc 1.68.0
1413
[ "$RUSTC_MINOR_VERSION" -lt 68 ] && cargo update -p quote --precise "1.0.41" --verbose
14+
# Starting with version 1.0.104, the `proc-macro2` crate has an MSRV of rustc 1.68
15+
[ "$RUSTC_MINOR_VERSION" -lt 68 ] && cargo update -p proc-macro2 --precise "1.0.103" --verbose
1516

1617
RUSTFLAGS='-D warnings' cargo clippy -- \
1718
`# We use this for sat groupings` \

ci/ci-tests.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,32 @@ RUSTC_MINOR_VERSION=$(rustc --version | awk '{ split($2,a,"."); print a[2] }')
55

66
# Starting with version 1.39.0, the `tokio` crate has an MSRV of rustc 1.70.0
77
[ "$RUSTC_MINOR_VERSION" -lt 70 ] && cargo update -p tokio --precise "1.38.1" --verbose
8-
[ "$RUSTC_MINOR_VERSION" -lt 70 ] && cargo update -p tokio-util --precise "0.7.10" --verbose
98

109
# syn 2.0.107 requires rustc 1.68.0
1110
[ "$RUSTC_MINOR_VERSION" -lt 68 ] && cargo update -p syn --precise "2.0.106" --verbose
1211
# quote 1.0.42 requires rustc 1.68.0
1312
[ "$RUSTC_MINOR_VERSION" -lt 68 ] && cargo update -p quote --precise "1.0.41" --verbose
13+
# Starting with version 1.0.104, the `proc-macro2` crate has an MSRV of rustc 1.68
14+
[ "$RUSTC_MINOR_VERSION" -lt 68 ] && cargo update -p proc-macro2 --precise "1.0.103" --verbose
15+
16+
# Starting with version 2.0.107, the `syn` crate has an MSRV of rustc 1.68
17+
[ "$RUSTC_MINOR_VERSION" -lt 68 ] && cargo update -p syn --precise "2.0.106" --verbose
18+
19+
# Starting with version 1.0.42, the `quote` crate has an MSRV of rustc 1.68
20+
[ "$RUSTC_MINOR_VERSION" -lt 68 ] && cargo update -p quote --precise "1.0.41" --verbose
21+
22+
# Starting with version 1.0.104, the `proc-macro2` crate has an MSRV of rustc 1.68
23+
[ "$RUSTC_MINOR_VERSION" -lt 68 ] && cargo update -p proc-macro2 --precise "1.0.103" --verbose
1424

1525
export RUST_BACKTRACE=1
1626

1727
cargo check --verbose --color always
1828
cargo check --release --verbose --color always
1929
cargo test --no-default-features
2030
[ "$RUSTC_MINOR_VERSION" -gt 81 ] && cargo test --features http
21-
# One std test syncs much of the lightning network graph, so --release is a must
22-
# At least until https://github.com/lightningdevkit/rust-lightning/pull/3687 gets backported
23-
export RUSTFLAGS="-C debug-assertions=on"
31+
[ "$RUSTC_MINOR_VERSION" -gt 81 ] && cargo test --features http_proxied
2432
cargo test --features std --release
2533
[ "$RUSTC_MINOR_VERSION" -gt 81 ] && cargo doc --document-private-items --no-default-features
2634
[ "$RUSTC_MINOR_VERSION" -gt 81 ] && cargo doc --document-private-items --features http,std
35+
[ "$RUSTC_MINOR_VERSION" -gt 81 ] && cargo doc --document-private-items --features http_proxied,std
2736
exit 0

src/http_resolver.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A [`HrnResolver`] which uses `reqwest` and `dns.google` (8.8.8.8) to resolve Human Readable
1+
//! A [`HrnResolver`] which uses `bitreq` and `dns.google` (8.8.8.8) to resolve Human Readable
22
//! Names into bitcoin payment instructions.
33
44
use std::boxed::Box;
@@ -23,31 +23,36 @@ use crate::hrn_resolution::{
2323

2424
const DOH_ENDPOINT: &'static str = "https://dns.google/dns-query?dns=";
2525

26-
/// An [`HrnResolver`] which uses `reqwest` and `dns.google` (8.8.8.8) to resolve Human Readable
26+
/// An [`HrnResolver`] which uses `bitreq` and `dns.google` (8.8.8.8) to resolve Human Readable
2727
/// Names into bitcoin payment instructions.
2828
///
2929
/// Note that using this may reveal our IP address to the recipient and information about who we're
3030
/// paying to Google (via `dns.google`).
3131
#[derive(Debug, Clone)]
3232
pub struct HTTPHrnResolver {
33-
client: reqwest::Client,
33+
#[cfg(feature = "http_proxied")]
34+
proxy: Option<bitreq::Proxy>,
3435
}
3536

3637
impl HTTPHrnResolver {
37-
/// Create a new `HTTPHrnResolver` with a default `reqwest::Client`.
38+
/// Create a new `HTTPHrnResolver`
3839
pub fn new() -> Self {
39-
HTTPHrnResolver::default()
40+
HTTPHrnResolver {
41+
#[cfg(feature = "http_proxied")]
42+
proxy: None,
43+
}
4044
}
4145

42-
/// Create a new `HTTPHrnResolver` with a custom `reqwest::Client`.
43-
pub fn with_client(client: reqwest::Client) -> Self {
44-
HTTPHrnResolver { client }
46+
/// Create a new `HTTPHrnResolver` which makes all requests via the given proxy.
47+
#[cfg(feature = "http_proxied")]
48+
pub fn new_proxied(proxy: bitreq::Proxy) -> Self {
49+
HTTPHrnResolver { proxy: Some(proxy) }
4550
}
4651
}
4752

4853
impl Default for HTTPHrnResolver {
4954
fn default() -> Self {
50-
HTTPHrnResolver { client: reqwest::Client::new() }
55+
HTTPHrnResolver::new()
5156
}
5257
}
5358

@@ -120,6 +125,19 @@ struct LNURLCallbackResponse {
120125
const DNS_ERR: &'static str = "DNS Request to dns.google failed";
121126

122127
impl HTTPHrnResolver {
128+
async fn http_get(&self, url: &str, accept_dns_message: bool) -> Result<Vec<u8>, ()> {
129+
let mut req = bitreq::get(url);
130+
if accept_dns_message {
131+
req = req.with_header("accept", "application/dns-message");
132+
}
133+
#[cfg(feature = "http_proxied")]
134+
if let Some(proxy) = &self.proxy {
135+
req = req.with_proxy(proxy.clone())
136+
}
137+
let resp = req.send_async().await.map_err(|_| ())?;
138+
Ok(resp.into_bytes())
139+
}
140+
123141
async fn resolve_dns(&self, hrn: &HumanReadableName) -> Result<HrnResolution, &'static str> {
124142
let dns_name =
125143
Name::try_from(format!("{}.user._bitcoin-payment.{}.", hrn.user(), hrn.domain()))
@@ -129,10 +147,7 @@ impl HTTPHrnResolver {
129147

130148
while let Some(query) = pending_queries.pop() {
131149
let request_url = query_to_url(query);
132-
let req =
133-
self.client.get(request_url).header("accept", "application/dns-message").build();
134-
let resp = self.client.execute(req.map_err(|_| DNS_ERR)?).await.map_err(|_| DNS_ERR)?;
135-
let body = resp.bytes().await.map_err(|_| DNS_ERR)?;
150+
let body = self.http_get(&request_url, true).await.map_err(|_| DNS_ERR)?;
136151

137152
let mut answer = QueryBuf::new_zeroed(0);
138153
answer.extend_from_slice(&body[..]);
@@ -156,8 +171,9 @@ impl HTTPHrnResolver {
156171

157172
async fn resolve_lnurl_impl(&self, lnurl_url: &str) -> Result<HrnResolution, &'static str> {
158173
let err = "Failed to fetch LN-Address initial well-known endpoint";
159-
let init_result = self.client.get(lnurl_url).send().await.map_err(|_| err)?;
160-
let init: LNURLInitResponse = init_result.json().await.map_err(|_| err)?;
174+
let resp = self.http_get(lnurl_url, false).await.map_err(|_| err)?;
175+
let resp_json = String::from_utf8(resp).map_err(|_| err)?;
176+
let init: LNURLInitResponse = serde_json::from_str(&resp_json).map_err(|_| err)?;
161177

162178
if init.tag != "payRequest" {
163179
return Err("LNURL initial init_response had an incorrect tag value");
@@ -218,8 +234,10 @@ impl HrnResolver for HTTPHrnResolver {
218234
} else {
219235
write!(&mut callback, "?amount={}", amt.milli_sats()).expect("Write to String");
220236
}
221-
let http_response = self.client.get(callback).send().await.map_err(|_| err)?;
222-
let response: LNURLCallbackResponse = http_response.json().await.map_err(|_| err)?;
237+
let resp = self.http_get(&callback, false).await.map_err(|_| err)?;
238+
let resp_json = String::from_utf8(resp).map_err(|_| err)?;
239+
let response: LNURLCallbackResponse =
240+
serde_json::from_str(&resp_json).map_err(|_| err)?;
223241

224242
if !response.routes.is_empty() {
225243
return Err("LNURL callback response contained a non-empty routes array");

0 commit comments

Comments
 (0)