Skip to content

Commit

Permalink
fix more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Feb 28, 2025
1 parent fa2eeb0 commit 67609d5
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 61 deletions.
42 changes: 21 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions node-gui/src/main_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion node-gui/src/widgets/create_hw_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ impl<Message> Component<Message, Theme, iced::Renderer> 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()
}
}
Expand Down
13 changes: 8 additions & 5 deletions node-gui/src/widgets/wallet_mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,14 @@ impl<Message> Component<Message, Theme, iced::Renderer> 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()
}
}

Expand Down
6 changes: 2 additions & 4 deletions wallet/src/signer/trezor_signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
Expand Down
35 changes: 26 additions & 9 deletions wallet/src/signer/trezor_signer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>(), rng.gen::<u8>(), rng.gen::<u8>()];
Expand Down Expand Up @@ -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)));

Expand Down Expand Up @@ -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();
Expand All @@ -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()
}
10 changes: 6 additions & 4 deletions wallet/wallet-cli-commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ pub enum WalletManagementCommand {
passphrase: Option<String>,

/// 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<CliHardwareWalletType>,
},
Expand All @@ -95,8 +96,9 @@ pub enum WalletManagementCommand {
passphrase: Option<String>,

/// 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<CliHardwareWalletType>,
},
Expand Down
8 changes: 5 additions & 3 deletions wallet/wallet-controller/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ where
}
}

pub async fn fetch_utxo_extra_info<T>(
pub async fn fetch_utxo_extra_info_for_hw_wallet<T>(
rpc_client: &T,
utxo: TxOutput,
) -> Result<(TxOutput, TxAdditionalInfo), ControllerError<T>>
Expand Down Expand Up @@ -226,6 +226,7 @@ pub async fn into_balances<T: NodeInterface>(
Ok(Balances::new(coins, tasks.try_collect().await?))
}

// TODO: optimize RPC calls to the Node
pub async fn tx_to_partially_signed_tx<T: NodeInterface, B: storage::Backend>(
rpc_client: &T,
wallet: &RuntimeWallet<B>,
Expand All @@ -252,7 +253,7 @@ pub async fn tx_to_partially_signed_tx<T: NodeInterface, B: storage::Backend>(
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::<Vec<_>>()
Expand Down Expand Up @@ -280,7 +281,8 @@ async fn into_utxo_and_destination<T: NodeInterface, B: storage::Backend>(
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) => {
Expand Down
10 changes: 5 additions & 5 deletions wallet/wallet-controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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<TxOutput>,
) -> Result<Vec<(TxOutput, TxAdditionalInfo)>, ControllerError<T>> {
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
}
Expand Down
14 changes: 5 additions & 9 deletions wallet/wallet-rpc-lib/src/rpc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use crypto::{
vrf::VRFPublicKey,
};
use rpc::description::HasValueHint;
#[cfg(feature = "trezor")]
use utils::ensure;
use wallet::account::PoolData;

Expand Down Expand Up @@ -102,9 +101,6 @@ pub enum RpcError<N: NodeInterface> {
#[error("Cannot specify a mnemonic or passphrase when creating a hardware wallet")]
HardwareWalletWithMnemonicOrPassphrase,

#[error("Invalid hardware wallet selection")]
InvalidHardwareWallet,

#[error("Invalid ip address")]
InvalidIpAddress,

Expand Down Expand Up @@ -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::<N>::InvalidHardwareWallet),
}
}
}
Expand Down

0 comments on commit 67609d5

Please sign in to comment.