Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some clippy warnings #1641

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/src/wallet_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ pub async fn prompt_internal(
let prompt = if let Some(alias) = wallet.alias().await {
format!("Wallet \"{alias}\": ")
} else {
format!("Wallet: ")
String::from("Wallet: ")
};

if let Some(helper) = rl.helper_mut() {
Expand Down
3 changes: 1 addition & 2 deletions sdk/src/client/api/high_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ impl Client {

let mut total_already_spent = 0;
let mut selected_inputs = Vec::new();
for (_offset, output_wrapper) in basic_outputs
for output_wrapper in basic_outputs
.into_iter()
// Max inputs is 128
.take(INPUT_COUNT_MAX.into())
.enumerate()
{
// Break if we have enough funds and don't create dust for the remainder
if total_already_spent == amount || total_already_spent >= amount {
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/client/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl TryFromDto<PreparedTransactionDataDto> for PreparedTransactionData {
inputs_data: dto
.inputs_data
.into_iter()
.map(|i| InputSigningData::try_from(i))
.map(InputSigningData::try_from)
.collect::<crate::client::Result<Vec<InputSigningData>>>()
.map_err(|_| Error::InvalidField("input_data"))?,
remainder: match dto.remainder {
Expand Down Expand Up @@ -125,7 +125,7 @@ impl TryFromDto<SignedTransactionDataDto> for SignedTransactionData {
inputs_data: dto
.inputs_data
.into_iter()
.map(|i| InputSigningData::try_from(i))
.map(InputSigningData::try_from)
.collect::<crate::client::Result<Vec<InputSigningData>>>()
.map_err(|_| Error::InvalidField("inputs_data"))?,
})
Expand Down
4 changes: 1 addition & 3 deletions sdk/src/client/secret/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ impl SecretManage for MnemonicSecretManager {
.to_bytes();

// Hash the public key to get the address
let result = Blake2b256::digest(public_key).try_into().map_err(|_e| {
crate::client::Error::Blake2b256("hashing the public key while generating the address failed.")
})?;
let result = Blake2b256::digest(public_key).into();

crate::client::Result::Ok(Ed25519Address::new(result))
})
Expand Down
4 changes: 1 addition & 3 deletions sdk/src/client/secret/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ impl SecretManage for PrivateKeySecretManager {
let public_key = self.0.public_key().to_bytes();

// Hash the public key to get the address
let result = Blake2b256::digest(public_key).try_into().map_err(|_e| {
crate::client::Error::Blake2b256("hashing the public key while generating the address failed.")
})?;
let result = Blake2b256::digest(public_key).into();

crate::client::Result::Ok(vec![Ed25519Address::new(result)])
}
Expand Down
8 changes: 3 additions & 5 deletions sdk/src/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ pub fn bech32_to_hex(bech32: impl ConvertTo<Bech32Address>) -> Result<String> {

/// Transforms a hex encoded address to a bech32 encoded address
pub fn hex_to_bech32(hex: &str, bech32_hrp: impl ConvertTo<Hrp>) -> Result<Bech32Address> {
let address: Ed25519Address = hex.parse::<Ed25519Address>()?;
let address = hex.parse::<Ed25519Address>()?;

Ok(Address::Ed25519(address).try_to_bech32(bech32_hrp)?)
}

/// Transforms a prefix hex encoded public key to a bech32 encoded address
pub fn hex_public_key_to_bech32_address(hex: &str, bech32_hrp: impl ConvertTo<Hrp>) -> Result<Bech32Address> {
let public_key: [u8; Ed25519Address::LENGTH] = prefix_hex::decode(hex)?;
let address = Ed25519Address::new(Blake2b256::digest(public_key).into());

let address = Blake2b256::digest(public_key)
.try_into()
.map_err(|_e| Error::Blake2b256("hashing the public key failed."))?;
let address: Ed25519Address = Ed25519Address::new(address);
Ok(Address::Ed25519(address).try_to_bech32(bech32_hrp)?)
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/address/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ impl Ed25519Address {
}

/// Creates a new [`Ed25519Address`] from the bytes of a [`PublicKey`].
pub fn from_public_key_bytes(public_key_bytes: [u8; PublicKey::LENGTH]) -> Result<Self, Error> {
Ok(Self::new(Blake2b256::digest(public_key_bytes).try_into()?))
pub fn from_public_key_bytes(public_key_bytes: [u8; PublicKey::LENGTH]) -> Self {
Self::new(Blake2b256::digest(public_key_bytes).into())
}
}

Expand Down
19 changes: 10 additions & 9 deletions sdk/src/types/block/address/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ impl WeightedAddress {
}

fn verify_address<const VERIFY: bool>(address: &Address) -> Result<(), Error> {
if VERIFY {
if !matches!(
if VERIFY
&& !matches!(
address,
Address::Ed25519(_) | Address::Account(_) | Address::Nft(_) | Address::Anchor(_)
) {
return Err(Error::InvalidAddressKind(address.kind()));
}
)
{
Err(Error::InvalidAddressKind(address.kind()))
} else {
Ok(())
}
Ok(())
}

fn verify_weight<const VERIFY: bool>(weight: &u8) -> Result<(), Error> {
if VERIFY && *weight == 0 {
return Err(Error::InvalidAddressWeight(*weight));
Err(Error::InvalidAddressWeight(*weight))
} else {
Ok(())
}
Expand Down Expand Up @@ -123,15 +124,15 @@ impl MultiAddress {

fn verify_addresses<const VERIFY: bool>(addresses: &[WeightedAddress]) -> Result<(), Error> {
if VERIFY && !is_unique_sorted(addresses.iter().map(WeightedAddress::address)) {
return Err(Error::WeightedAddressesNotUniqueSorted);
Err(Error::WeightedAddressesNotUniqueSorted)
} else {
Ok(())
}
}

fn verify_threshold<const VERIFY: bool>(threshold: &u16) -> Result<(), Error> {
if VERIFY && *threshold == 0 {
return Err(Error::InvalidMultiAddressThreshold(*threshold));
Err(Error::InvalidMultiAddressThreshold(*threshold))
} else {
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/mana/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ mod test {
};
params
});
&*PARAMS
&PARAMS
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ pub(crate) mod dto {

impl AccountOutput {
#[allow(clippy::too_many_arguments)]
pub fn try_from_dtos<'a>(
pub fn try_from_dtos(
amount: OutputBuilderAmount,
mana: u64,
native_tokens: Option<Vec<NativeToken>>,
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ pub(crate) mod dto {

impl AnchorOutput {
#[allow(clippy::too_many_arguments)]
pub fn try_from_dtos<'a>(
pub fn try_from_dtos(
amount: OutputBuilderAmount,
mana: u64,
native_tokens: Option<Vec<NativeToken>>,
Expand Down Expand Up @@ -851,7 +851,7 @@ mod tests {
output.mana(),
Some(output.native_tokens().to_vec()),
output.anchor_id(),
output.state_index().into(),
output.state_index(),
output.state_metadata().to_owned().into(),
output.unlock_conditions().iter().map(Into::into).collect(),
Some(output.features().to_vec()),
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ pub(crate) mod dto {
}

impl BasicOutput {
pub fn try_from_dtos<'a>(
pub fn try_from_dtos(
amount: OutputBuilderAmount,
mana: u64,
native_tokens: Option<Vec<NativeToken>>,
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ pub(crate) mod dto {

impl DelegationOutput {
#[allow(clippy::too_many_arguments)]
pub fn try_from_dtos<'a>(
pub fn try_from_dtos(
amount: OutputBuilderAmount,
delegated_amount: u64,
delegation_id: &DelegationId,
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ pub(crate) mod dto {

impl FoundryOutput {
#[allow(clippy::too_many_arguments)]
pub fn try_from_dtos<'a>(
pub fn try_from_dtos(
amount: OutputBuilderAmount,
native_tokens: Option<Vec<NativeToken>>,
serial_number: u32,
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ pub(crate) mod dto {

impl NftOutput {
#[allow(clippy::too_many_arguments)]
pub fn try_from_dtos<'a>(
pub fn try_from_dtos(
amount: OutputBuilderAmount,
mana: u64,
native_tokens: Option<Vec<NativeToken>>,
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/unlock/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl MultiUnlock {

fn verify_unlocks<const VERIFY: bool>(unlocks: &[Unlock]) -> Result<(), Error> {
if VERIFY && unlocks.iter().any(Unlock::is_multi) {
return Err(Error::MultiUnlockRecursion);
Err(Error::MultiUnlockRecursion)
} else {
Ok(())
}
Expand Down
6 changes: 2 additions & 4 deletions sdk/src/wallet/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,8 @@ where
// Check if the db exists and if not, return an error if one parameter is missing, because otherwise the db
// would be created with an empty parameter which just leads to errors later
#[cfg(feature = "storage")]
if !storage_options.path.is_dir() {
if self.client_options.is_none() {
return Err(crate::wallet::Error::MissingParameter("client_options"));
}
if !storage_options.path.is_dir() && self.client_options.is_none() {
return Err(crate::wallet::Error::MissingParameter("client_options"));
}

#[cfg(all(feature = "rocksdb", feature = "storage"))]
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/wallet/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ where
if let Address::Ed25519(address) = bech32_address.inner() {
Ok(Bech32Address::new(
*bech32_address.hrp(),
ImplicitAccountCreationAddress::from(address.clone()),
ImplicitAccountCreationAddress::from(*address),
))
} else {
Err(Error::NonEd25519Address)
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/wallet/core/operations/stronghold_backup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Wallet {
});

if !ignore_backup_values {
(*wallet_data).bip_path = read_bip_path;
wallet_data.bip_path = read_bip_path;
}

if let Some(mut read_secret_manager) = read_secret_manager {
Expand Down Expand Up @@ -267,7 +267,7 @@ impl Wallet<StrongholdSecretManager> {
});

if !ignore_backup_values {
(*wallet_data).bip_path = read_bip_path;
wallet_data.bip_path = read_bip_path;
}

if let Some(mut read_secret_manager) = read_secret_manager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) async fn read_wallet_data_from_stronghold_snapshot<S: 'static + Secre
let restored_wallet_data = stronghold
.get::<WalletDataDto>(WALLET_DATA_KEY)
.await?
.map(|dto| WalletData::try_from_dto(dto))
.map(WalletData::try_from_dto)
.transpose()?;

Ok((client_options, restored_secret_manager, restored_wallet_data))
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/wallet/operations/syncing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ where
let new_outputs_data_inner = self.get_outputs(output_ids).await?;

let outputs_data_inner = self
.output_response_to_output_data(new_outputs_data_inner, &address_with_unspent_outputs)
.output_response_to_output_data(new_outputs_data_inner, address_with_unspent_outputs)
.await?;

outputs_data.extend(outputs_data_inner.clone());
Expand Down
12 changes: 5 additions & 7 deletions sdk/src/wallet/operations/syncing/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ where
.get(output_with_meta.metadata().transaction_id())
.map_or(false, |tx| !tx.incoming);

let chain = wallet_data.bip_path.map_or(None, |bip_path| {
let chain = wallet_data.bip_path.map(|bip_path| {
// BIP 44 (HD wallets) and 4218 is the registered index for IOTA https://github.com/satoshilabs/slips/blob/master/slip-0044.md
Some(
Bip44::new(bip_path.coin_type)
.with_account(bip_path.account)
.with_change(associated_address.internal as _)
.with_address_index(associated_address.key_index),
)
Bip44::new(bip_path.coin_type)
.with_account(bip_path.account)
.with_change(associated_address.internal as _)
.with_address_index(associated_address.key_index)
});

OutputData {
Expand Down
2 changes: 1 addition & 1 deletion sdk/tests/types/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use iota_sdk::types::block::{
address::AccountAddress,
output::{AccountOutput, Feature, FoundryId, MinimumOutputAmount, NativeToken, Output, SimpleTokenScheme, TokenId},
output::{AccountOutput, Feature, FoundryId, MinimumOutputAmount, NativeToken, SimpleTokenScheme, TokenId},
protocol::protocol_parameters,
rand::output::{
feature::{rand_issuer_feature, rand_metadata_feature, rand_sender_feature},
Expand Down
2 changes: 1 addition & 1 deletion sdk/tests/types/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use iota_sdk::types::block::{
output::{BasicOutput, Feature, FoundryId, MinimumOutputAmount, NativeToken, Output, SimpleTokenScheme, TokenId},
output::{BasicOutput, Feature, FoundryId, MinimumOutputAmount, NativeToken, SimpleTokenScheme, TokenId},
protocol::protocol_parameters,
rand::{
address::rand_account_address,
Expand Down
2 changes: 1 addition & 1 deletion sdk/tests/types/output/foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use iota_sdk::types::block::{
output::{
unlock_condition::ImmutableAccountAddressUnlockCondition, FoundryId, FoundryOutput, MinimumOutputAmount,
NativeToken, Output, SimpleTokenScheme, TokenId,
NativeToken, SimpleTokenScheme, TokenId,
},
protocol::protocol_parameters,
rand::{
Expand Down
2 changes: 1 addition & 1 deletion sdk/tests/types/output/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use iota_sdk::types::block::{
output::{FoundryId, MinimumOutputAmount, NativeToken, NftId, NftOutput, Output, SimpleTokenScheme, TokenId},
output::{FoundryId, MinimumOutputAmount, NativeToken, NftId, NftOutput, SimpleTokenScheme, TokenId},
protocol::protocol_parameters,
rand::{
address::rand_account_address,
Expand Down
Loading