Skip to content

Commit

Permalink
chore: sanitize Boltz API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jun 29, 2024
1 parent 912a4b1 commit 92223c2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/boltz/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub struct Client {

impl Client {
pub fn new(endpoint: String) -> Self {
Client { endpoint }
Client {
endpoint: crate::utils::string::trim_suffix(endpoint, '/'),
}
}

pub async fn send_raw_transaction(&self, hex: String) -> Result<String, Box<dyn Error>> {
Expand Down Expand Up @@ -65,6 +67,12 @@ mod client_test {

const ENDPOINT: &str = "https://api.testnet.boltz.exchange/v2";

#[test]
fn test_trim_suffix() {
assert_eq!(Client::new(ENDPOINT.to_string() + "/").endpoint, ENDPOINT);
assert_eq!(Client::new(ENDPOINT.to_string()).endpoint, ENDPOINT);
}

#[tokio::test]
async fn test_send_raw_transaction_error_handling() {
let client = Client::new(ENDPOINT.to_string());
Expand Down
8 changes: 1 addition & 7 deletions src/chain/esplora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ impl EsploraClient {
max_reqs_per_second: u64,
boltz_endpoint: String,
) -> Result<Self, Box<dyn Error>> {
// TODO: also do in boltz
let trimmed_endpoint = match endpoint.strip_suffix('/') {
Some(s) => s.to_string(),
None => endpoint,
};

let (tx_sender, tx_receiver) = crossbeam_channel::bounded::<Transaction>(1);
let (block_sender, block_receiver) = crossbeam_channel::unbounded::<Block>();

Expand Down Expand Up @@ -79,7 +73,7 @@ impl EsploraClient {
boltz_client,
poll_interval,
block_receiver,
endpoint: trimmed_endpoint,
endpoint: crate::utils::string::trim_suffix(endpoint, '/'),
})
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod boltz;
mod chain;
mod claimer;
mod db;
mod utils;

pub mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod string;
6 changes: 6 additions & 0 deletions src/utils/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub fn trim_suffix(string: String, suffix: char) -> String {
match string.strip_suffix(suffix) {
Some(s) => s.to_string(),
None => string,
}
}

0 comments on commit 92223c2

Please sign in to comment.