bdk icon indicating copy to clipboard operation
bdk copied to clipboard

Explain better how to use https with `use-esplora-reqwest`

Open w0xlt opened this issue 2 years ago • 5 comments

#526 adds sample code to connect to Esplora and has the following configuration in Cargo.toml.

[[example]]
name = "esplora_backend"
path = "examples/esplora_backend.rs"
required-features = ["use-esplora-ureq"]

The cargo run --features="use-esplora-ureq" --example esplora_backend command works fine. But if the below change is made:

 [[example]]
 name = "esplora_backend"
 path = "examples/esplora_backend.rs"
-required-features = ["use-esplora-ureq"]
+required-features = ["use-esplora-reqwest"]

The cargo run --features="use-esplora-reqwest" --example esplora_backend command will fail.

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Esplora(Reqwest(reqwest::Error { kind: Request, url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("blockstream.info")), port: None, path: "/testnet/api/scripthash/2a69c29645663a231f39008866d65f855038570c1664987845f60dcad9f99253/txs", query: None, fragment: None }, source: hyper::Error(Connect, "invalid URL, scheme is not http") }))', examples/esplora_backend.rs:42:54

w0xlt avatar Jul 26 '22 04:07 w0xlt

The error says scheme is not http. That is bizarre, because your esplora_url is using an HTTP scheme. But when I changed your url's scheme to HTTPS, I still get the same error. So regardless of whether your url is HTTP or HTTPS, the client seems to always be doing an HTTPS call. There is a related hyper issue that suggested using an HTTPS connector. Since reqwest uses hyper as its HTTP client, I figured out that the issue might be coming from the way reqwest is creating the HTTPS connection. From the reqwest documentation it is suppose to add the default-tls feature by default but we set default-features = false in cargo.toml. To solve the issue, I think we should just add default-tls to the features of reqwest in cargo.toml. After adding default-tls it works on my end.

vladimirfomene avatar Jul 26 '22 09:07 vladimirfomene

The reason why it fails is that blockstream.info redirects all the traffic to https. So reqwest will first make an http request (which will succeed), then try to follow the redirect and fail because the next url is https.

$ curl -v http://blockstream.info/
*   Trying 35.201.74.156:80...
* Connected to blockstream.info (35.201.74.156) port 80 (#0)
> GET / HTTP/1.1
> Host: blockstream.info
> User-Agent: curl/7.79.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Referrer-Policy: no-referrer
< Location: https://blockstream.info:443/
< Content-Length: 226
< Date: Tue, 26 Jul 2022 10:07:13 GMT
< 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://blockstream.info:443/">here</A>.
</BODY></HTML>
* Connection #0 to host blockstream.info left intact

afilini avatar Jul 26 '22 10:07 afilini

We disabled tls by default because it adds extra code and not everybody needs it, but if we were to include it, it would be hard to disable it from the outside. Basically, it's easier to make this opt-in than opt-out.

See the PR for the discussion: https://github.com/bitcoindevkit/bdk/pull/495

You can re-enable it as @vladimirfomene said (but this forces you to add reqwest to your cargo toml) or more easily you can enable reqwest-default-tls on the bdk dependency.

afilini avatar Jul 26 '22 10:07 afilini

I'm tagging this as a docs enhancement, I agree it should be explained better for the user but I don't think there's any bug with our code here.

afilini avatar Jul 26 '22 10:07 afilini

Added a documentation in the example of https://github.com/bitcoindevkit/bdk/pull/526.

w0xlt avatar Jul 27 '22 03:07 w0xlt

Closing this issue as it has been solved by https://github.com/bitcoindevkit/bdk/pull/526.

w0xlt avatar Jan 24 '23 03:01 w0xlt