Skip to content

Commit

Permalink
refactor: add tor to ContextBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
binarybaron committed Nov 20, 2024
1 parent 43c9dcf commit 03ff710
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ async fn initialize_context(
})
.with_json(false)
.with_debug(true)
.with_tor(true)
.with_tauri(tauri_handle.clone())
.build()
.await;
Expand Down
24 changes: 18 additions & 6 deletions swap/src/cli/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ pub struct ContextBuilder {
is_testnet: bool,
debug: bool,
json: bool,
tor: bool,
tauri_handle: Option<TauriHandle>,
}

Expand All @@ -225,6 +226,7 @@ impl ContextBuilder {
is_testnet: false,
debug: false,
json: false,
tor: false,
tauri_handle: None,
}
}
Expand Down Expand Up @@ -272,6 +274,12 @@ impl ContextBuilder {
self
}

/// Whether to initialize a Tor client (default false)
pub fn with_tor(mut self, tor: bool) -> Self {
self.tor = tor;
self
}

/// Takes the builder, initializes the context by initializing the wallets and other components and returns the Context.
pub async fn build(self) -> Result<Context> {
let data_dir = data::data_dir_from(self.data, self.is_testnet)?;
Expand Down Expand Up @@ -371,12 +379,16 @@ impl ContextBuilder {
TauriContextInitializationProgress::EstablishingTorCircuits,
));

let tor = init_tor_client(&data_dir)
.await
.inspect_err(|err| {
tracing::error!(%err, "Failed to establish Tor client");
})
.ok();
let tor = if self.tor {
init_tor_client(&data_dir)
.await
.inspect_err(|err| {
tracing::error!(%err, "Failed to establish Tor client");
})
.ok()
} else {
None
};

let context = Context {
db,
Expand Down
4 changes: 4 additions & 0 deletions swap/src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ where
.with_data_dir(data)
.with_debug(debug)
.with_json(json)
.with_tor(true)
.build()
.await?,
);
Expand Down Expand Up @@ -184,6 +185,7 @@ where
.with_data_dir(data)
.with_debug(debug)
.with_json(json)
.with_tor(true)
.build()
.await?,
);
Expand Down Expand Up @@ -229,6 +231,7 @@ where
.with_data_dir(data)
.with_debug(debug)
.with_json(json)
.with_tor(true)
.build()
.await?,
);
Expand Down Expand Up @@ -263,6 +266,7 @@ where
.with_data_dir(data)
.with_debug(debug)
.with_json(json)
.with_tor(true)
.build()
.await?,
);
Expand Down

0 comments on commit 03ff710

Please sign in to comment.