From 947bcb61927a9342f65eb68d13a642d475dae297 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Wed, 24 Feb 2021 18:00:07 +1100 Subject: [PATCH] ASB reloads the default wallet after generate_from_keys atomically --- swap/src/bin/asb.rs | 6 ++++- swap/src/bin/swap_cli.rs | 6 ++++- swap/src/monero.rs | 10 ++++++++ swap/src/monero/wallet.rs | 42 +++++++++++++++++++++++++++++---- swap/src/protocol/alice/swap.rs | 4 ++-- swap/tests/testutils/mod.rs | 1 + 6 files changed, 61 insertions(+), 8 deletions(-) diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index 38cc88da..d63246fa 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -156,7 +156,11 @@ async fn init_wallets( bitcoin_balance ); - let monero_wallet = monero::Wallet::new(config.monero.wallet_rpc_url.clone(), MONERO_NETWORK); + let monero_wallet = monero::Wallet::new( + config.monero.wallet_rpc_url.clone(), + MONERO_NETWORK, + DEFAULT_WALLET_NAME.to_string(), + ); // Setup the Monero wallet let open_wallet_response = monero_wallet.open_wallet(DEFAULT_WALLET_NAME).await; diff --git a/swap/src/bin/swap_cli.rs b/swap/src/bin/swap_cli.rs index f17d224e..2efffb3f 100644 --- a/swap/src/bin/swap_cli.rs +++ b/swap/src/bin/swap_cli.rs @@ -289,7 +289,11 @@ async fn init_wallets( bitcoin_balance ); - let monero_wallet = monero::Wallet::new(config.monero.wallet_rpc_url.clone(), monero_network); + let monero_wallet = monero::Wallet::new( + config.monero.wallet_rpc_url.clone(), + monero_network, + MONERO_BLOCKCHAIN_MONITORING_WALLET_NAME.to_string(), + ); // Setup the temporary Monero wallet necessary for monitoring the blockchain let open_monitoring_wallet_response = monero_wallet diff --git a/swap/src/monero.rs b/swap/src/monero.rs index ccaffa3b..e17e137b 100644 --- a/swap/src/monero.rs +++ b/swap/src/monero.rs @@ -220,6 +220,16 @@ pub trait CreateWalletForOutput { ) -> Result<()>; } +#[async_trait] +pub trait CreateWalletForOutputThenLoadDefaultWallet { + async fn create_and_load_wallet_for_output_then_load_default_wallet( + &self, + private_spend_key: PrivateKey, + private_view_key: PrivateViewKey, + restore_height: BlockHeight, + ) -> Result<()>; +} + #[async_trait] pub trait OpenWallet { async fn open_wallet(&self, file_name: &str) -> Result<()>; diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 1c38de31..08fbb9c8 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -1,7 +1,7 @@ use crate::monero::{ - Amount, CreateWallet, CreateWalletForOutput, GetAddress, InsufficientFunds, OpenWallet, - PrivateViewKey, PublicViewKey, Refresh, Transfer, TransferProof, TxHash, WalletBlockHeight, - WatchForTransfer, + Amount, CreateWallet, CreateWalletForOutput, CreateWalletForOutputThenLoadDefaultWallet, + GetAddress, InsufficientFunds, OpenWallet, PrivateViewKey, PublicViewKey, Refresh, Transfer, + TransferProof, TxHash, WalletBlockHeight, WatchForTransfer, }; use ::monero::{Address, Network, PrivateKey, PublicKey}; use anyhow::Result; @@ -25,13 +25,15 @@ use url::Url; pub struct Wallet { pub inner: Mutex, pub network: Network, + pub default_wallet_name: String, } impl Wallet { - pub fn new(url: Url, network: Network) -> Self { + pub fn new(url: Url, network: Network, default_wallet_name: String) -> Self { Self { inner: Mutex::new(wallet::Client::new(url)), network, + default_wallet_name, } } @@ -103,6 +105,38 @@ impl CreateWalletForOutput for Wallet { } } +#[async_trait] +impl CreateWalletForOutputThenLoadDefaultWallet for Wallet { + async fn create_and_load_wallet_for_output_then_load_default_wallet( + &self, + private_spend_key: PrivateKey, + private_view_key: PrivateViewKey, + restore_height: BlockHeight, + ) -> Result<()> { + let public_spend_key = PublicKey::from_private_key(&private_spend_key); + let public_view_key = PublicKey::from_private_key(&private_view_key.into()); + + let address = Address::standard(self.network, public_spend_key, public_view_key); + + let wallet = self.inner.lock().await; + + let _ = wallet + .generate_from_keys( + &address.to_string(), + &private_spend_key.to_string(), + &PrivateKey::from(private_view_key).to_string(), + restore_height.height, + ) + .await?; + + let _ = wallet + .open_wallet(self.default_wallet_name.as_str()) + .await?; + + Ok(()) + } +} + #[async_trait] impl OpenWallet for Wallet { async fn open_wallet(&self, file_name: &str) -> Result<()> { diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index 0f480dfd..e233c1e7 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -10,7 +10,7 @@ use crate::{ database::Database, execution_params::ExecutionParams, monero, - monero::{CreateWalletForOutput, WalletBlockHeight}, + monero::{CreateWalletForOutputThenLoadDefaultWallet, WalletBlockHeight}, monero_ext::ScalarExt, protocol::{ alice, @@ -402,7 +402,7 @@ async fn run_until_internal( let view_key = state3.v; monero_wallet - .create_and_load_wallet_for_output( + .create_and_load_wallet_for_output_then_load_default_wallet( spend_key, view_key, monero_wallet_restore_blockheight, diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 0d4fc750..8688083a 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -592,6 +592,7 @@ async fn init_test_wallets( let xmr_wallet = swap::monero::Wallet { inner: Mutex::new(monero.wallet(name).unwrap().client()), network: monero::Network::default(), + default_wallet_name: "irrelevant_for_tests".to_string(), }; let electrum_rpc_url = {