Skip to content

Commit

Permalink
Use native-tls for better close_notify handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Apr 26, 2024
1 parent cf2544a commit b90d07a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ panic = "abort"
[dependencies]
serde = { version = "1.0.0", features = ["derive"] }
serde_json = "1.0.0"
ureq = {version = "2.8.0", features = ["json"]}
ureq = { version = "2.9.7", features = ["json", "native-tls"] }
bip39 = "2.0.0"
electrum-client = "0.19.0"
bitcoin = {version = "0.31.1", features = ["rand", "base64", "rand-std"]}
elements = { version = "0.24.0", features = ["serde"] }
lightning-invoice = "0.26.0"
native-tls = "0.2.11"
tungstenite = { version = "0.21.0", features = ["native-tls-vendored"] }
url = "2.5.0"
log = "^0.4"
Expand Down
21 changes: 19 additions & 2 deletions src/swaps/boltz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
//! assert!((output_amount - base_fees) == response.onchain_amount?);

//! ```
use std::str::FromStr;
use std::sync::Arc;

use crate::error::Error;
use crate::network::Chain;
use bitcoin::absolute::LockTime;
Expand All @@ -51,7 +54,7 @@ use crate::util::secrets::Preimage;
use serde::Serializer;
use serde::{Deserialize, Serialize};
use serde_json;
use std::str::FromStr;
use ureq::{AgentBuilder, TlsConnector};

pub const BOLTZ_TESTNET_URL: &str = "https://testnet.boltz.exchange/api";
pub const BOLTZ_MAINNET_URL: &str = "https://api.boltz.exchange";
Expand Down Expand Up @@ -82,7 +85,21 @@ impl BoltzApiClient {
/// Make a Post request. Returns the Response
fn post(&self, end_point: &str, data: Value) -> Result<String, Error> {
let url = format!("{}/{}", self.base_url, end_point);
Ok(ureq::post(&url).send_json(data)?.into_string()?)

let response = match native_tls::TlsConnector::new() {
// If native_tls is available, use that for TLS
// It has better handling of close_notify, which avoids some POST call failures
// See https://github.com/SatoshiPortal/boltz-rust/issues/39
Ok(tls_connector) => AgentBuilder::new()
.tls_connector(Arc::new(tls_connector))
.build()
.request("POST", &url)
.send_json(data)?
.into_string()?,
// If native_tls is not available, fallback to the default (rustls)
Err(_) => ureq::post(&url).send_json(data)?.into_string()?,
};
Ok(response)
}
/// In order to create a swap, one first has to know which pairs are supported and what kind of rates, limits and fees are applied when creating a new swap.
/// The following call returns this information.
Expand Down

0 comments on commit b90d07a

Please sign in to comment.