Skip to content

Commit 303a7df

Browse files
refactor: extract swap/src/monero/wallet.rs to new monero-wallet crate
Closes #798
1 parent da217d2 commit 303a7df

File tree

8 files changed

+128
-42
lines changed

8 files changed

+128
-42
lines changed

Cargo.lock

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ members = [
99
"monero-rpc-pool",
1010
"monero-sys",
1111
"monero-tests",
12+
"monero-wallet",
1213
"src-tauri",
1314
"swap",
1415
"swap-asb",

monero-wallet/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "monero-wallet"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
# Error handling
8+
anyhow = { workspace = true }
9+
10+
# Other stuff
11+
monero = { workspace = true }
12+
monero-sys = { path = "../monero-sys" }
13+
swap-core = { path = "../swap-core" }
14+
throttle = { path = "../throttle" }
15+
time = "0.3"
16+
uuid = { workspace = true }
17+
18+
# Tokio
19+
tokio = { workspace = true, features = ["process", "fs", "net", "parking_lot", "rt"] }
20+
21+
# Tracing
22+
tracing = { workspace = true }
23+
tracing-appender = "0.2"
24+
tracing-subscriber = { workspace = true }
25+
26+
# monero-oxide
27+
monero-oxide-rpc = { git = "https://github.com/monero-oxide/monero-oxide.git", package = "monero-rpc" }
28+
monero-simple-request-rpc = { git = "https://github.com/monero-oxide/monero-oxide.git" }
Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,31 @@ use throttle::{throttle, Throttle};
1616
use tokio::sync::RwLock;
1717
use uuid::Uuid;
1818

19-
use crate::cli::api::{
20-
request::{GetMoneroBalanceResponse, GetMoneroHistoryResponse, GetMoneroSyncProgressResponse},
21-
tauri_bindings::{MoneroWalletUpdate, TauriEmitter, TauriEvent, TauriHandle},
22-
};
23-
24-
use super::{BlockHeight, TxHash, WatchRequest};
19+
use swap_core::monero::primitives::{Amount, BlockHeight, PrivateViewKey, TxHash, WatchRequest};
20+
21+
pub type TauriHandle = Arc<Box<dyn MoneroTauriHandle>>;
22+
pub trait MoneroTauriHandle: Send + Sync {
23+
/// tauri_handle.emit_unified_event(TauriEvent::MoneroWalletUpdate(
24+
/// MoneroWalletUpdate::BalanceChange(GetMoneroBalanceResponse {
25+
/// total_balance, unlocked_balance,
26+
/// })
27+
/// ));
28+
fn balance_change(&self, total_balance: Amount, unlocked_balance: Amount);
29+
30+
/// tauri_handle.emit_unified_event(TauriEvent::MoneroWalletUpdate(
31+
/// MoneroWalletUpdate::HistoryUpdate(
32+
/// GetMoneroHistoryResponse { transactions }
33+
/// ),
34+
/// ));
35+
fn history_update(&self, transactions: Vec<monero_sys::TransactionInfo>);
36+
37+
/// tauri_handle.emit_unified_event(TauriEvent::MoneroWalletUpdate(
38+
/// MoneroWalletUpdate::SyncProgress(GetMoneroSyncProgressResponse {
39+
/// current_block, target_block, progress_percentage,
40+
/// }),
41+
/// ));
42+
fn sync_progress(&self, current_block: u64, target_block: u64, progress_percentage: f32);
43+
}
2544

2645
/// Entrance point to the Monero blockchain.
2746
/// You can use this struct to open specific wallets and monitor the blockchain.
@@ -84,13 +103,7 @@ impl TauriWalletListener {
84103
}
85104
};
86105

87-
let response = GetMoneroBalanceResponse {
88-
total_balance: total_balance.into(),
89-
unlocked_balance: unlocked_balance.into(),
90-
};
91-
tauri.emit_unified_event(TauriEvent::MoneroWalletUpdate(
92-
MoneroWalletUpdate::BalanceChange(response),
93-
));
106+
tauri.balance_change(total_balance.into(), unlocked_balance.into());
94107
});
95108
}
96109
};
@@ -111,11 +124,8 @@ impl TauriWalletListener {
111124
return;
112125
}
113126
};
114-
let response = GetMoneroHistoryResponse { transactions };
115127

116-
tauri.emit_unified_event(TauriEvent::MoneroWalletUpdate(
117-
MoneroWalletUpdate::HistoryUpdate(response),
118-
));
128+
tauri.history_update(transactions);
119129
});
120130
}
121131
};
@@ -138,16 +148,11 @@ impl TauriWalletListener {
138148
};
139149

140150
let progress_percentage = sync_progress.percentage();
141-
142-
let response = GetMoneroSyncProgressResponse {
143-
current_block: sync_progress.current_block,
144-
target_block: sync_progress.target_block,
145-
progress_percentage: progress_percentage,
146-
};
147-
148-
tauri.emit_unified_event(TauriEvent::MoneroWalletUpdate(
149-
MoneroWalletUpdate::SyncProgress(response),
150-
));
151+
tauri.sync_progress(
152+
sync_progress.current_block,
153+
sync_progress.target_block,
154+
progress_percentage,
155+
);
151156
});
152157
}
153158
};
@@ -223,15 +228,17 @@ impl Wallets {
223228
///
224229
/// The main wallet will be kept alive and synced, other wallets are
225230
/// opened and closed on demand.
226-
pub async fn new(
231+
pub async fn new<T: Into<TauriHandle>>(
227232
wallet_dir: PathBuf,
228233
main_wallet_name: String,
229234
daemon: Daemon,
230235
network: Network,
231236
regtest: bool,
232-
tauri_handle: Option<TauriHandle>,
237+
tauri_handle: Option<T>,
233238
wallet_database: Option<Arc<monero_sys::Database>>,
234239
) -> Result<Self> {
240+
let tauri_handle = tauri_handle.map(|th| th.into());
241+
235242
let main_wallet = Wallet::open_or_create(
236243
wallet_dir.join(&main_wallet_name).display().to_string(),
237244
daemon.clone(),
@@ -290,15 +297,16 @@ impl Wallets {
290297

291298
/// Create a new `Wallets` instance with an existing wallet as the main wallet.
292299
/// This is used when we want to use a user-selected wallet instead of creating a new one.
293-
pub async fn new_with_existing_wallet(
300+
pub async fn new_with_existing_wallet<T: Into<TauriHandle>>(
294301
wallet_dir: PathBuf,
295302
daemon: Daemon,
296303
network: Network,
297304
regtest: bool,
298-
tauri_handle: Option<TauriHandle>,
305+
tauri_handle: Option<T>,
299306
existing_wallet: Wallet,
300307
wallet_database: Option<Arc<monero_sys::Database>>,
301308
) -> Result<Self> {
309+
let tauri_handle = tauri_handle.map(|th| th.into());
302310
// TODO: This code is duplicated in [`Wallets::new`]. Unify it.
303311

304312
if regtest {
@@ -367,7 +375,7 @@ impl Wallets {
367375
&self,
368376
swap_id: Uuid,
369377
spend_key: monero::PrivateKey,
370-
view_key: super::PrivateViewKey,
378+
view_key: PrivateViewKey,
371379
tx_lock_id: TxHash,
372380
) -> Result<Arc<Wallet>> {
373381
// Derive wallet address from the keys

swap/Cargo.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ bdk = { workspace = true }
1717
bdk_wallet = { workspace = true }
1818
bitcoin = { workspace = true }
1919

20-
# Bitcoin Wallet
20+
# Wallets
2121
bitcoin-wallet = { path = "../bitcoin-wallet" }
22+
monero-wallet = { path = "../monero-wallet" }
2223

2324
# Tor
2425
arti-client = { workspace = true, features = ["static-sqlite", "tokio", "rustls", "onion-service-service"] }
@@ -71,7 +72,6 @@ swap-p2p = { path = "../swap-p2p" }
7172
swap-proptest = { path = "../swap-proptest" }
7273
swap-serde = { path = "../swap-serde" }
7374
tauri = { version = "2.0", features = ["config-json5"], optional = true, default-features = false }
74-
throttle = { path = "../throttle" }
7575
time = "0.3"
7676
url = { workspace = true }
7777
uuid = { workspace = true, features = ["serde"] }
@@ -97,10 +97,6 @@ serde_with = { version = "1", features = ["macros"] }
9797
strum = { workspace = true, features = ["derive"] }
9898
typeshare = { workspace = true }
9999

100-
# monero-oxide
101-
monero-oxide-rpc = { git = "https://github.com/monero-oxide/monero-oxide.git", package = "monero-rpc" }
102-
monero-simple-request-rpc = { git = "https://github.com/monero-oxide/monero-oxide.git" }
103-
104100
# Database
105101
sqlx = { version = "0.8", features = ["sqlite", "runtime-tokio-rustls"] }
106102

swap/src/cli/api/tauri_bindings.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,41 @@ impl bitcoin_wallet::BitcoinTauriBackgroundTask
505505
}
506506
}
507507

508+
struct MoneroTauriHandle(TauriHandle);
509+
510+
impl Into<monero_wallet::TauriHandle> for TauriHandle {
511+
fn into(self) -> monero_wallet::TauriHandle {
512+
Arc::new(Box::new(MoneroTauriHandle(self)))
513+
}
514+
}
515+
516+
impl monero_wallet::MoneroTauriHandle for MoneroTauriHandle {
517+
fn balance_change(&self, total_balance: monero::Amount, unlocked_balance: monero::Amount) {
518+
self.0.emit_unified_event(TauriEvent::MoneroWalletUpdate(
519+
MoneroWalletUpdate::BalanceChange(GetMoneroBalanceResponse {
520+
total_balance,
521+
unlocked_balance,
522+
}),
523+
))
524+
}
525+
526+
fn history_update(&self, transactions: Vec<monero_sys::TransactionInfo>) {
527+
self.0.emit_unified_event(TauriEvent::MoneroWalletUpdate(
528+
MoneroWalletUpdate::HistoryUpdate(GetMoneroHistoryResponse { transactions }),
529+
))
530+
}
531+
532+
fn sync_progress(&self, current_block: u64, target_block: u64, progress_percentage: f32) {
533+
self.0.emit_unified_event(TauriEvent::MoneroWalletUpdate(
534+
MoneroWalletUpdate::SyncProgress(GetMoneroSyncProgressResponse {
535+
current_block,
536+
target_block,
537+
progress_percentage,
538+
}),
539+
))
540+
}
541+
}
542+
508543
impl Display for ApprovalRequest {
509544
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
510545
match self.request {

swap/src/common/tracing_util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ mod crates {
223223
pub const OUR_CRATES: &[&str] = &[
224224
// Library crates
225225
"bitcoin_wallet",
226+
"monero_wallet",
226227
"swap_p2p",
227228
"swap_env",
228229
"swap_core",

swap/src/monero.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub mod wallet;
1+
pub use monero_wallet as wallet;
22
pub mod wallet_rpc;
33

44
pub use ::monero::network::Network;

0 commit comments

Comments
 (0)