Skip to content

Commit 67609d5

Browse files
committed
fix more comments
1 parent fa2eeb0 commit 67609d5

File tree

10 files changed

+81
-61
lines changed

10 files changed

+81
-61
lines changed

Cargo.lock

+21-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node-gui/src/main_window/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ impl MainWindow {
424424

425425
BackendEvent::OpenWallet(Err(error)) | BackendEvent::ImportWallet(Err(error)) => {
426426
self.show_error(error.to_string());
427+
self.file_dialog_active = false;
428+
self.active_dialog = ActiveDialog::None;
427429
Task::none()
428430
}
429431

node-gui/src/widgets/create_hw_wallet.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ impl<Message> Component<Message, Theme, iced::Renderer> for CreateHwWalletDialog
8787
card.foot(container(text("Loading...")).center_x(Length::Fill))
8888
} else {
8989
card.foot(container(button).center_x(Length::Fill))
90+
.on_close(ImportEvent::Cancel)
9091
}
9192
.max_width(600.0)
92-
.on_close(ImportEvent::Cancel)
9393
.into()
9494
}
9595
}

node-gui/src/widgets/wallet_mnemonic.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,14 @@ impl<Message> Component<Message, Theme, iced::Renderer> for WalletMnemonicDialog
130130
}
131131
.center_x(Length::Fill);
132132

133-
Card::new(Text::new(action_text), body)
134-
.foot(footer)
135-
.max_width(600.0)
136-
.on_close(ImportEvent::Cancel)
137-
.into()
133+
let card = Card::new(Text::new(action_text), body).foot(footer).max_width(600.0);
134+
135+
if state.importing {
136+
card
137+
} else {
138+
card.on_close(ImportEvent::Cancel)
139+
}
140+
.into()
138141
}
139142
}
140143

wallet/src/signer/trezor_signer/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,7 @@ impl Signer for TrezorSigner {
377377
sighash,
378378
)?;
379379

380-
let sighash_type = SigHashType::try_from(SigHashType::ALL)
381-
.expect("Should not fail");
380+
let sighash_type = SigHashType::all();
382381
let sig = add_secret_if_needed(StandardInputSignature::new(
383382
sighash_type,
384383
current_signatures.encode(),
@@ -405,8 +404,7 @@ impl Signer for TrezorSigner {
405404
},
406405
None => match (destination, new_signatures.get(i)) {
407406
(Some(destination), Some(sig)) => {
408-
let sighash_type =
409-
SigHashType::try_from(SigHashType::ALL).expect("Should not fail");
407+
let sighash_type = SigHashType::all();
410408
let sighash = signature_hash(sighash_type, ptx.tx(), &inputs_utxo_refs, i)?;
411409
let (sig, status) = self.make_signature(
412410
sig,

wallet/src/signer/trezor_signer/tests.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ fn sign_message(#[case] seed: Seed) {
8585
let pk_destination = Destination::PublicKey(pk);
8686

8787
for destination in [pkh_destination, pk_destination] {
88-
let mut devices = find_devices(false);
89-
assert!(!devices.is_empty());
90-
let client = devices.pop().unwrap().connect().unwrap();
88+
let client = find_test_device();
9189

9290
let mut signer = TrezorSigner::new(chain_config.clone(), Arc::new(Mutex::new(client)));
9391
let message = vec![rng.gen::<u8>(), rng.gen::<u8>(), rng.gen::<u8>()];
@@ -127,9 +125,7 @@ fn sign_transaction_intent(#[case] seed: Seed) {
127125
.unwrap();
128126
let mut account = Account::new(config.clone(), &mut db_tx, key_chain, None).unwrap();
129127

130-
let mut devices = find_devices(false);
131-
assert!(!devices.is_empty());
132-
let client = devices.pop().unwrap().connect().unwrap();
128+
let client = find_test_device();
133129

134130
let mut signer = TrezorSigner::new(config.clone(), Arc::new(Mutex::new(client)));
135131

@@ -493,9 +489,7 @@ fn sign_transaction(#[case] seed: Seed) {
493489
));
494490
let ptx = req.into_partially_signed_tx(additional_info).unwrap();
495491

496-
let mut devices = find_devices(false);
497-
assert!(!devices.is_empty());
498-
let client = devices.pop().unwrap().connect().unwrap();
492+
let client = find_test_device();
499493

500494
let mut signer = TrezorSigner::new(chain_config.clone(), Arc::new(Mutex::new(client)));
501495
let (ptx, _, _) = signer.sign_tx(ptx, account.key_chain(), &db_tx).unwrap();
@@ -521,3 +515,26 @@ fn sign_transaction(#[case] seed: Seed) {
521515
.unwrap();
522516
}
523517
}
518+
519+
fn find_test_device() -> Trezor {
520+
let use_real_device = std::env::var_os("TEST_REAL_DEVICE").is_some();
521+
522+
let mut devices = find_devices(false)
523+
.into_iter()
524+
.filter(|device| device.model == Model::Trezor || device.model == Model::TrezorEmulator)
525+
.collect_vec();
526+
527+
if use_real_device {
528+
// Try to find the first real device
529+
if let Some(idx) = devices.iter().position(|d| d.model == Model::Trezor) {
530+
return devices.swap_remove(idx).connect().unwrap();
531+
}
532+
}
533+
534+
devices
535+
.into_iter()
536+
.find(|d| d.model == Model::TrezorEmulator)
537+
.unwrap()
538+
.connect()
539+
.unwrap()
540+
}

wallet/wallet-cli-commands/src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ pub enum WalletManagementCommand {
6868
passphrase: Option<String>,
6969

7070
/// Create a wallet using a connected hardware wallet. Only the public keys will be kept in
71-
/// the software wallet. Cannot specify a mnemonic or passphrase here, input them on the
72-
/// hardware wallet instead when initializing the device.
71+
/// the software wallet. Cannot specify a mnemonic or passphrase here,
72+
/// the former must have been entered on the hardware during the device setup
73+
/// and the latter will have to be entered every time the device is connected to the host machine.
7374
#[arg(long, conflicts_with_all(["mnemonic", "passphrase", "whether_to_store_seed_phrase"]))]
7475
hardware_wallet: Option<CliHardwareWalletType>,
7576
},
@@ -95,8 +96,9 @@ pub enum WalletManagementCommand {
9596
passphrase: Option<String>,
9697

9798
/// Create a wallet using a connected hardware wallet. Only the public keys will be kept in
98-
/// the software wallet. Cannot specify a mnemonic or passphrase here, input them on the
99-
/// hardware wallet instead when initializing the device.
99+
/// the software wallet. Cannot specify a mnemonic or passphrase here,
100+
/// the former must have been entered on the hardware during the device setup
101+
/// and the latter will have to be entered every time the device is connected to the host machine.
100102
#[arg(long, conflicts_with_all(["passphrase", "mnemonic", "whether_to_store_seed_phrase"]))]
101103
hardware_wallet: Option<CliHardwareWalletType>,
102104
},

wallet/wallet-controller/src/helpers.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ where
154154
}
155155
}
156156

157-
pub async fn fetch_utxo_extra_info<T>(
157+
pub async fn fetch_utxo_extra_info_for_hw_wallet<T>(
158158
rpc_client: &T,
159159
utxo: TxOutput,
160160
) -> Result<(TxOutput, TxAdditionalInfo), ControllerError<T>>
@@ -226,6 +226,7 @@ pub async fn into_balances<T: NodeInterface>(
226226
Ok(Balances::new(coins, tasks.try_collect().await?))
227227
}
228228

229+
// TODO: optimize RPC calls to the Node
229230
pub async fn tx_to_partially_signed_tx<T: NodeInterface, B: storage::Backend>(
230231
rpc_client: &T,
231232
wallet: &RuntimeWallet<B>,
@@ -252,7 +253,7 @@ pub async fn tx_to_partially_signed_tx<T: NodeInterface, B: storage::Backend>(
252253
let tasks: FuturesOrdered<_> = tx
253254
.outputs()
254255
.iter()
255-
.map(|out| fetch_utxo_extra_info(rpc_client, out.clone()))
256+
.map(|out| fetch_utxo_extra_info_for_hw_wallet(rpc_client, out.clone()))
256257
.collect();
257258
let additional_infos = tasks
258259
.try_collect::<Vec<_>>()
@@ -280,7 +281,8 @@ async fn into_utxo_and_destination<T: NodeInterface, B: storage::Backend>(
280281
Ok(match tx_inp {
281282
TxInput::Utxo(outpoint) => {
282283
let (utxo, dest) = fetch_utxo_and_destination(rpc_client, outpoint, wallet).await?;
283-
let (utxo, additional_infos) = fetch_utxo_extra_info(rpc_client, utxo).await?;
284+
let (utxo, additional_infos) =
285+
fetch_utxo_extra_info_for_hw_wallet(rpc_client, utxo).await?;
284286
(Some(utxo), additional_infos, Some(dest))
285287
}
286288
TxInput::Account(acc_outpoint) => {

wallet/wallet-controller/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use chainstate::tx_verifier::{
3131
self, error::ScriptError, input_check::signature_only_check::SignatureOnlyVerifiable,
3232
};
3333
use futures::{never::Never, stream::FuturesOrdered, TryStreamExt};
34-
use helpers::{fetch_token_info, fetch_utxo, fetch_utxo_extra_info, into_balances};
34+
use helpers::{fetch_token_info, fetch_utxo, fetch_utxo_extra_info_for_hw_wallet, into_balances};
3535
use node_comm::rpc_client::ColdWalletClient;
3636
use runtime_wallet::RuntimeWallet;
3737
use std::{
@@ -1053,7 +1053,7 @@ where
10531053
.map_err(ControllerError::WalletError)?;
10541054

10551055
let (input_utxos, additional_infos) =
1056-
self.fetch_utxos_extra_info(input_utxos).await?.into_iter().fold(
1056+
self.fetch_utxos_extra_info_for_hw_wallet(input_utxos).await?.into_iter().fold(
10571057
(Vec::new(), TxAdditionalInfo::new()),
10581058
|(mut input_utxos, additional_info), (x, y)| {
10591059
input_utxos.push(x);
@@ -1062,7 +1062,7 @@ where
10621062
);
10631063

10641064
let additional_infos = self
1065-
.fetch_utxos_extra_info(tx.outputs().to_vec())
1065+
.fetch_utxos_extra_info_for_hw_wallet(tx.outputs().to_vec())
10661066
.await?
10671067
.into_iter()
10681068
.fold(additional_infos, |acc, (_, info)| acc.join(info));
@@ -1155,13 +1155,13 @@ where
11551155
Ok(input_utxos)
11561156
}
11571157

1158-
async fn fetch_utxos_extra_info(
1158+
async fn fetch_utxos_extra_info_for_hw_wallet(
11591159
&self,
11601160
inputs: Vec<TxOutput>,
11611161
) -> Result<Vec<(TxOutput, TxAdditionalInfo)>, ControllerError<T>> {
11621162
let tasks: FuturesOrdered<_> = inputs
11631163
.into_iter()
1164-
.map(|input| fetch_utxo_extra_info(&self.rpc_client, input))
1164+
.map(|input| fetch_utxo_extra_info_for_hw_wallet(&self.rpc_client, input))
11651165
.collect();
11661166
tasks.try_collect().await
11671167
}

wallet/wallet-rpc-lib/src/rpc/types.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use crypto::{
3636
vrf::VRFPublicKey,
3737
};
3838
use rpc::description::HasValueHint;
39-
#[cfg(feature = "trezor")]
4039
use utils::ensure;
4140
use wallet::account::PoolData;
4241

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

105-
#[error("Invalid hardware wallet selection")]
106-
InvalidHardwareWallet,
107-
108104
#[error("Invalid ip address")]
109105
InvalidIpAddress,
110106

@@ -822,18 +818,18 @@ impl HardwareWalletType {
822818
passphrase,
823819
store_seed_phrase,
824820
}),
825-
#[cfg(feature = "trezor")]
826-
Some(HardwareWalletType::Trezor) => {
821+
Some(hw_type) => {
827822
ensure!(
828823
mnemonic.is_none()
829824
&& passphrase.is_none()
830825
&& store_seed_phrase == StoreSeedPhrase::DoNotStore,
831826
RpcError::HardwareWalletWithMnemonicOrPassphrase
832827
);
833-
Ok(WalletTypeArgs::Trezor)
828+
match hw_type {
829+
#[cfg(feature = "trezor")]
830+
HardwareWalletType::Trezor => Ok(WalletTypeArgs::Trezor),
831+
}
834832
}
835-
#[cfg(not(feature = "trezor"))]
836-
Some(_) => Err(RpcError::<N>::InvalidHardwareWallet),
837833
}
838834
}
839835
}

0 commit comments

Comments
 (0)