From 67609d50809c7d5933db504e2b6811699ee743ea Mon Sep 17 00:00:00 2001 From: Boris Oncev Date: Thu, 27 Feb 2025 10:36:42 +0100 Subject: [PATCH] fix more comments --- Cargo.lock | 42 ++++++++++++------------ node-gui/src/main_window/mod.rs | 2 ++ node-gui/src/widgets/create_hw_wallet.rs | 2 +- node-gui/src/widgets/wallet_mnemonic.rs | 13 +++++--- wallet/src/signer/trezor_signer/mod.rs | 6 ++-- wallet/src/signer/trezor_signer/tests.rs | 35 +++++++++++++++----- wallet/wallet-cli-commands/src/lib.rs | 10 +++--- wallet/wallet-controller/src/helpers.rs | 8 +++-- wallet/wallet-controller/src/lib.rs | 10 +++--- wallet/wallet-rpc-lib/src/rpc/types.rs | 14 +++----- 10 files changed, 81 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aad95674a..404488bfd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -726,9 +726,9 @@ checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "base64ct" -version = "1.7.0" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c103cbbedac994e292597ab79342dbd5b306a362045095db54917d92a9fdfd92" +checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" [[package]] name = "bb8" @@ -4300,9 +4300,9 @@ checksum = "2a385b1be4e5c3e362ad2ffa73c392e53f031eaa5b7d648e64cd87f27f6063d7" [[package]] name = "litemap" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" +checksum = "23fb14cb19457329c82206317a5663005a4d404783dc74f4252769b0d5f42856" [[package]] name = "lmdb-mintlayer" @@ -4419,9 +4419,9 @@ dependencies = [ [[package]] name = "lyon_path" -version = "1.0.6" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e0b8aec2f58586f6eef237985b9a9b7cb3a3aff4417c575075cf95bf925252e" +checksum = "0047f508cd7a85ad6bad9518f68cce7b1bf6b943fb71f6da0ee3bc1e8cb75f25" dependencies = [ "lyon_geom", "num-traits", @@ -6131,9 +6131,9 @@ dependencies = [ [[package]] name = "psl" -version = "2.1.87" +version = "2.1.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "826daf1dbfd289c8b192254afed4820293eb9992f1875f96468ea1d1c4cf3c09" +checksum = "f1e2f9b127fd1f6bcdd222a75c05d6c0c2d7ac326a5316a5537ed9185fa380b6" dependencies = [ "psl-types", ] @@ -6242,7 +6242,7 @@ checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" dependencies = [ "rand_chacha 0.9.0", "rand_core 0.9.2", - "zerocopy 0.8.20", + "zerocopy 0.8.21", ] [[package]] @@ -6300,7 +6300,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a509b1a2ffbe92afab0e55c8fd99dea1c280e8171bd2d88682bb20bc41cbc2c" dependencies = [ "getrandom 0.3.1", - "zerocopy 0.8.20", + "zerocopy 0.8.21", ] [[package]] @@ -8825,9 +8825,9 @@ dependencies = [ [[package]] name = "uuid" -version = "1.15.0" +version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd8dcafa1ca14750d8d7a05aa05988c17aab20886e1f3ae33a40223c58d92ef7" +checksum = "e0f540e3240398cce6128b64ba83fdbdd86129c16a3aa1a3a252efd66eb3d587" [[package]] name = "valuable" @@ -10361,11 +10361,11 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.8.20" +version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dde3bb8c68a8f3f1ed4ac9221aad6b10cece3e60a8e2ea54a6a2dec806d0084c" +checksum = "dcf01143b2dd5d134f11f545cf9f1431b13b749695cb33bcce051e7568f99478" dependencies = [ - "zerocopy-derive 0.8.20", + "zerocopy-derive 0.8.21", ] [[package]] @@ -10381,9 +10381,9 @@ dependencies = [ [[package]] name = "zerocopy-derive" -version = "0.8.20" +version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eea57037071898bf96a6da35fd626f4f27e9cee3ead2a6c703cf09d472b2e700" +checksum = "712c8386f4f4299382c9abee219bee7084f78fb939d88b6840fcc1320d5f6da2" dependencies = [ "proc-macro2", "quote", @@ -10392,18 +10392,18 @@ dependencies = [ [[package]] name = "zerofrom" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff3ee08c995dee1859d998dea82f7374f2826091dd9cd47def953cae446cd2e" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" dependencies = [ "zerofrom-derive", ] [[package]] name = "zerofrom-derive" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" dependencies = [ "proc-macro2", "quote", diff --git a/node-gui/src/main_window/mod.rs b/node-gui/src/main_window/mod.rs index 2104df266..35920fe8e 100644 --- a/node-gui/src/main_window/mod.rs +++ b/node-gui/src/main_window/mod.rs @@ -424,6 +424,8 @@ impl MainWindow { BackendEvent::OpenWallet(Err(error)) | BackendEvent::ImportWallet(Err(error)) => { self.show_error(error.to_string()); + self.file_dialog_active = false; + self.active_dialog = ActiveDialog::None; Task::none() } diff --git a/node-gui/src/widgets/create_hw_wallet.rs b/node-gui/src/widgets/create_hw_wallet.rs index 4e02b5d46..8884ff217 100644 --- a/node-gui/src/widgets/create_hw_wallet.rs +++ b/node-gui/src/widgets/create_hw_wallet.rs @@ -87,9 +87,9 @@ impl Component for CreateHwWalletDialog card.foot(container(text("Loading...")).center_x(Length::Fill)) } else { card.foot(container(button).center_x(Length::Fill)) + .on_close(ImportEvent::Cancel) } .max_width(600.0) - .on_close(ImportEvent::Cancel) .into() } } diff --git a/node-gui/src/widgets/wallet_mnemonic.rs b/node-gui/src/widgets/wallet_mnemonic.rs index ed73ce7f9..9ad3360ef 100644 --- a/node-gui/src/widgets/wallet_mnemonic.rs +++ b/node-gui/src/widgets/wallet_mnemonic.rs @@ -130,11 +130,14 @@ impl Component for WalletMnemonicDialog } .center_x(Length::Fill); - Card::new(Text::new(action_text), body) - .foot(footer) - .max_width(600.0) - .on_close(ImportEvent::Cancel) - .into() + let card = Card::new(Text::new(action_text), body).foot(footer).max_width(600.0); + + if state.importing { + card + } else { + card.on_close(ImportEvent::Cancel) + } + .into() } } diff --git a/wallet/src/signer/trezor_signer/mod.rs b/wallet/src/signer/trezor_signer/mod.rs index aff7853db..4459d5777 100644 --- a/wallet/src/signer/trezor_signer/mod.rs +++ b/wallet/src/signer/trezor_signer/mod.rs @@ -377,8 +377,7 @@ impl Signer for TrezorSigner { sighash, )?; - let sighash_type = SigHashType::try_from(SigHashType::ALL) - .expect("Should not fail"); + let sighash_type = SigHashType::all(); let sig = add_secret_if_needed(StandardInputSignature::new( sighash_type, current_signatures.encode(), @@ -405,8 +404,7 @@ impl Signer for TrezorSigner { }, None => match (destination, new_signatures.get(i)) { (Some(destination), Some(sig)) => { - let sighash_type = - SigHashType::try_from(SigHashType::ALL).expect("Should not fail"); + let sighash_type = SigHashType::all(); let sighash = signature_hash(sighash_type, ptx.tx(), &inputs_utxo_refs, i)?; let (sig, status) = self.make_signature( sig, diff --git a/wallet/src/signer/trezor_signer/tests.rs b/wallet/src/signer/trezor_signer/tests.rs index 84ce0064a..b75cbb234 100644 --- a/wallet/src/signer/trezor_signer/tests.rs +++ b/wallet/src/signer/trezor_signer/tests.rs @@ -85,9 +85,7 @@ fn sign_message(#[case] seed: Seed) { let pk_destination = Destination::PublicKey(pk); for destination in [pkh_destination, pk_destination] { - let mut devices = find_devices(false); - assert!(!devices.is_empty()); - let client = devices.pop().unwrap().connect().unwrap(); + let client = find_test_device(); let mut signer = TrezorSigner::new(chain_config.clone(), Arc::new(Mutex::new(client))); let message = vec![rng.gen::(), rng.gen::(), rng.gen::()]; @@ -127,9 +125,7 @@ fn sign_transaction_intent(#[case] seed: Seed) { .unwrap(); let mut account = Account::new(config.clone(), &mut db_tx, key_chain, None).unwrap(); - let mut devices = find_devices(false); - assert!(!devices.is_empty()); - let client = devices.pop().unwrap().connect().unwrap(); + let client = find_test_device(); let mut signer = TrezorSigner::new(config.clone(), Arc::new(Mutex::new(client))); @@ -493,9 +489,7 @@ fn sign_transaction(#[case] seed: Seed) { )); let ptx = req.into_partially_signed_tx(additional_info).unwrap(); - let mut devices = find_devices(false); - assert!(!devices.is_empty()); - let client = devices.pop().unwrap().connect().unwrap(); + let client = find_test_device(); let mut signer = TrezorSigner::new(chain_config.clone(), Arc::new(Mutex::new(client))); let (ptx, _, _) = signer.sign_tx(ptx, account.key_chain(), &db_tx).unwrap(); @@ -521,3 +515,26 @@ fn sign_transaction(#[case] seed: Seed) { .unwrap(); } } + +fn find_test_device() -> Trezor { + let use_real_device = std::env::var_os("TEST_REAL_DEVICE").is_some(); + + let mut devices = find_devices(false) + .into_iter() + .filter(|device| device.model == Model::Trezor || device.model == Model::TrezorEmulator) + .collect_vec(); + + if use_real_device { + // Try to find the first real device + if let Some(idx) = devices.iter().position(|d| d.model == Model::Trezor) { + return devices.swap_remove(idx).connect().unwrap(); + } + } + + devices + .into_iter() + .find(|d| d.model == Model::TrezorEmulator) + .unwrap() + .connect() + .unwrap() +} diff --git a/wallet/wallet-cli-commands/src/lib.rs b/wallet/wallet-cli-commands/src/lib.rs index 07f15c86b..bb8355f5f 100644 --- a/wallet/wallet-cli-commands/src/lib.rs +++ b/wallet/wallet-cli-commands/src/lib.rs @@ -68,8 +68,9 @@ pub enum WalletManagementCommand { passphrase: Option, /// Create a wallet using a connected hardware wallet. Only the public keys will be kept in - /// the software wallet. Cannot specify a mnemonic or passphrase here, input them on the - /// hardware wallet instead when initializing the device. + /// the software wallet. Cannot specify a mnemonic or passphrase here, + /// the former must have been entered on the hardware during the device setup + /// and the latter will have to be entered every time the device is connected to the host machine. #[arg(long, conflicts_with_all(["mnemonic", "passphrase", "whether_to_store_seed_phrase"]))] hardware_wallet: Option, }, @@ -95,8 +96,9 @@ pub enum WalletManagementCommand { passphrase: Option, /// Create a wallet using a connected hardware wallet. Only the public keys will be kept in - /// the software wallet. Cannot specify a mnemonic or passphrase here, input them on the - /// hardware wallet instead when initializing the device. + /// the software wallet. Cannot specify a mnemonic or passphrase here, + /// the former must have been entered on the hardware during the device setup + /// and the latter will have to be entered every time the device is connected to the host machine. #[arg(long, conflicts_with_all(["passphrase", "mnemonic", "whether_to_store_seed_phrase"]))] hardware_wallet: Option, }, diff --git a/wallet/wallet-controller/src/helpers.rs b/wallet/wallet-controller/src/helpers.rs index 97b33bd89..3878379a6 100644 --- a/wallet/wallet-controller/src/helpers.rs +++ b/wallet/wallet-controller/src/helpers.rs @@ -154,7 +154,7 @@ where } } -pub async fn fetch_utxo_extra_info( +pub async fn fetch_utxo_extra_info_for_hw_wallet( rpc_client: &T, utxo: TxOutput, ) -> Result<(TxOutput, TxAdditionalInfo), ControllerError> @@ -226,6 +226,7 @@ pub async fn into_balances( Ok(Balances::new(coins, tasks.try_collect().await?)) } +// TODO: optimize RPC calls to the Node pub async fn tx_to_partially_signed_tx( rpc_client: &T, wallet: &RuntimeWallet, @@ -252,7 +253,7 @@ pub async fn tx_to_partially_signed_tx( let tasks: FuturesOrdered<_> = tx .outputs() .iter() - .map(|out| fetch_utxo_extra_info(rpc_client, out.clone())) + .map(|out| fetch_utxo_extra_info_for_hw_wallet(rpc_client, out.clone())) .collect(); let additional_infos = tasks .try_collect::>() @@ -280,7 +281,8 @@ async fn into_utxo_and_destination( Ok(match tx_inp { TxInput::Utxo(outpoint) => { let (utxo, dest) = fetch_utxo_and_destination(rpc_client, outpoint, wallet).await?; - let (utxo, additional_infos) = fetch_utxo_extra_info(rpc_client, utxo).await?; + let (utxo, additional_infos) = + fetch_utxo_extra_info_for_hw_wallet(rpc_client, utxo).await?; (Some(utxo), additional_infos, Some(dest)) } TxInput::Account(acc_outpoint) => { diff --git a/wallet/wallet-controller/src/lib.rs b/wallet/wallet-controller/src/lib.rs index ea0582137..51fddc667 100644 --- a/wallet/wallet-controller/src/lib.rs +++ b/wallet/wallet-controller/src/lib.rs @@ -31,7 +31,7 @@ use chainstate::tx_verifier::{ self, error::ScriptError, input_check::signature_only_check::SignatureOnlyVerifiable, }; use futures::{never::Never, stream::FuturesOrdered, TryStreamExt}; -use helpers::{fetch_token_info, fetch_utxo, fetch_utxo_extra_info, into_balances}; +use helpers::{fetch_token_info, fetch_utxo, fetch_utxo_extra_info_for_hw_wallet, into_balances}; use node_comm::rpc_client::ColdWalletClient; use runtime_wallet::RuntimeWallet; use std::{ @@ -1053,7 +1053,7 @@ where .map_err(ControllerError::WalletError)?; let (input_utxos, additional_infos) = - self.fetch_utxos_extra_info(input_utxos).await?.into_iter().fold( + self.fetch_utxos_extra_info_for_hw_wallet(input_utxos).await?.into_iter().fold( (Vec::new(), TxAdditionalInfo::new()), |(mut input_utxos, additional_info), (x, y)| { input_utxos.push(x); @@ -1062,7 +1062,7 @@ where ); let additional_infos = self - .fetch_utxos_extra_info(tx.outputs().to_vec()) + .fetch_utxos_extra_info_for_hw_wallet(tx.outputs().to_vec()) .await? .into_iter() .fold(additional_infos, |acc, (_, info)| acc.join(info)); @@ -1155,13 +1155,13 @@ where Ok(input_utxos) } - async fn fetch_utxos_extra_info( + async fn fetch_utxos_extra_info_for_hw_wallet( &self, inputs: Vec, ) -> Result, ControllerError> { let tasks: FuturesOrdered<_> = inputs .into_iter() - .map(|input| fetch_utxo_extra_info(&self.rpc_client, input)) + .map(|input| fetch_utxo_extra_info_for_hw_wallet(&self.rpc_client, input)) .collect(); tasks.try_collect().await } diff --git a/wallet/wallet-rpc-lib/src/rpc/types.rs b/wallet/wallet-rpc-lib/src/rpc/types.rs index 8a0087fa2..0df912969 100644 --- a/wallet/wallet-rpc-lib/src/rpc/types.rs +++ b/wallet/wallet-rpc-lib/src/rpc/types.rs @@ -36,7 +36,6 @@ use crypto::{ vrf::VRFPublicKey, }; use rpc::description::HasValueHint; -#[cfg(feature = "trezor")] use utils::ensure; use wallet::account::PoolData; @@ -102,9 +101,6 @@ pub enum RpcError { #[error("Cannot specify a mnemonic or passphrase when creating a hardware wallet")] HardwareWalletWithMnemonicOrPassphrase, - #[error("Invalid hardware wallet selection")] - InvalidHardwareWallet, - #[error("Invalid ip address")] InvalidIpAddress, @@ -822,18 +818,18 @@ impl HardwareWalletType { passphrase, store_seed_phrase, }), - #[cfg(feature = "trezor")] - Some(HardwareWalletType::Trezor) => { + Some(hw_type) => { ensure!( mnemonic.is_none() && passphrase.is_none() && store_seed_phrase == StoreSeedPhrase::DoNotStore, RpcError::HardwareWalletWithMnemonicOrPassphrase ); - Ok(WalletTypeArgs::Trezor) + match hw_type { + #[cfg(feature = "trezor")] + HardwareWalletType::Trezor => Ok(WalletTypeArgs::Trezor), + } } - #[cfg(not(feature = "trezor"))] - Some(_) => Err(RpcError::::InvalidHardwareWallet), } } }