From 98c96e67916de18828eade2a1a80f8abe546f667 Mon Sep 17 00:00:00 2001 From: Daniel Olano Date: Fri, 14 Jun 2024 21:20:14 +0200 Subject: [PATCH] Address clippy lints for libwallet (#61) --- .github/workflows/libwallet-pr.yml | 16 ++++++++++++++++ libwallet/Cargo.toml | 1 + libwallet/justfile | 1 - libwallet/src/account.rs | 8 ++------ libwallet/src/key_pair.rs | 26 +++++++++++++++----------- libwallet/src/lib.rs | 11 ++--------- libwallet/src/substrate_ext.rs | 2 -- libwallet/src/util.rs | 12 ++++++------ libwallet/src/vault.rs | 20 +++++++++----------- libwallet/src/vault/simple.rs | 12 ++++-------- 10 files changed, 55 insertions(+), 54 deletions(-) create mode 100644 .github/workflows/libwallet-pr.yml diff --git a/.github/workflows/libwallet-pr.yml b/.github/workflows/libwallet-pr.yml new file mode 100644 index 0000000..8744a85 --- /dev/null +++ b/.github/workflows/libwallet-pr.yml @@ -0,0 +1,16 @@ +on: + pull_request: + paths: ['libwallet/**'] + +name: Libwallet + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: extractions/setup-just@v2 + - name: Check + run: just -f libwallet/justfile check + - name: Lint + run: just -f libwallet/justfile lint diff --git a/libwallet/Cargo.toml b/libwallet/Cargo.toml index da3cc32..2ddc0ef 100644 --- a/libwallet/Cargo.toml +++ b/libwallet/Cargo.toml @@ -35,6 +35,7 @@ serde_json = {version = "1.0", default-features = false, features = ["alloc"]} dirs = "4.0" [features] +default = ["substrate"] # default = ["std", "substrate", "vault_simple", "mnemonic", "rand", "vault_pass", "vault_os", "util_pin"] rand = ["rand_core", "schnorrkel?/getrandom"] sr25519 = ["dep:schnorrkel"] diff --git a/libwallet/justfile b/libwallet/justfile index b49c6a7..11b1cec 100644 --- a/libwallet/justfile +++ b/libwallet/justfile @@ -3,7 +3,6 @@ default: check: cargo check - lint: cargo clippy -- -D warnings diff --git a/libwallet/src/account.rs b/libwallet/src/account.rs index f6d3d9a..1abee29 100644 --- a/libwallet/src/account.rs +++ b/libwallet/src/account.rs @@ -1,9 +1,5 @@ -use core::fmt::{Debug, Display}; +use crate::{Public, Signer}; -use crate::{ - Public, Signer, -}; - -pub trait Account: Signer + Display { +pub trait Account: Signer + core::fmt::Display { fn public(&self) -> impl Public; } diff --git a/libwallet/src/key_pair.rs b/libwallet/src/key_pair.rs index c52c919..879ce58 100644 --- a/libwallet/src/key_pair.rs +++ b/libwallet/src/key_pair.rs @@ -27,13 +27,19 @@ impl Signature for Bytes {} /// Something that can sign messages pub trait Signer { type Signature: Signature; - async fn sign_msg(&self, data: impl AsRef<[u8]>) -> Result; - async fn verify(&self, msg: impl AsRef<[u8]>, sig: impl AsRef<[u8]>) -> bool; + fn sign_msg( + &self, + data: impl AsRef<[u8]>, + ) -> impl core::future::Future>; + + fn verify( + &self, + msg: impl AsRef<[u8]>, + sig: impl AsRef<[u8]>, + ) -> impl core::future::Future; } /// Wrappers to represent any supported key pair. pub mod any { - use crate::Signer; - use super::{Public, Signature}; use core::fmt; @@ -227,7 +233,7 @@ pub mod sr25519 { super::derive::parse_substrate_junctions(path) .fold(self.secret.clone(), |key, (part, hard)| { if hard { - key.hard_derive_mini_secret_key(Some(ChainCode(part)), &[]) + key.hard_derive_mini_secret_key(Some(ChainCode(part)), []) .0 .expand(ExpansionMode::Ed25519) } else { @@ -323,8 +329,6 @@ pub mod sr25519 { } mod derive { - use core::convert::identity; - use super::Bytes; /// Something to derive key pairs form @@ -342,8 +346,8 @@ mod derive { pub(super) fn parse_substrate_junctions( path: &str, ) -> impl Iterator + '_ { - path.split_inclusive("/") - .flat_map(|s| if s == "/" { "" } else { s }.split("/")) // "//Alice//Bob" -> ["","","Alice","","","Bob"] + path.split_inclusive('/') + .flat_map(|s| if s == "/" { "" } else { s }.split('/')) // "//Alice//Bob" -> ["","","Alice","","","Bob"] .scan(0u8, |j, part| { Some(if part.is_empty() { *j += 1; @@ -354,7 +358,7 @@ mod derive { Some((encoded_junction(part), hard)) }) }) - .filter_map(identity) + .flatten() } fn encoded_junction(part: &str) -> Junction { @@ -364,7 +368,7 @@ mod derive { } else { let len = part.len().min(JUNCTION_LEN - 1); code[0] = (len as u8) << 2; - code[1..len + 1].copy_from_slice(&part[..len].as_bytes()); + code[1..len + 1].copy_from_slice(part[..len].as_bytes()); } code } diff --git a/libwallet/src/lib.rs b/libwallet/src/lib.rs index 54a38a1..2add378 100644 --- a/libwallet/src/lib.rs +++ b/libwallet/src/lib.rs @@ -16,8 +16,7 @@ mod substrate_ext; pub use account::Account; use arrayvec::ArrayVec; -use core::{cell::RefCell, convert::TryInto, fmt}; -use key_pair::any::AnySignature; +use core::{convert::TryInto, fmt}; #[cfg(feature = "mnemonic")] use mnemonic; @@ -28,7 +27,6 @@ pub use mnemonic::{Language, Mnemonic}; pub use vault::Vault; pub mod vault; - const MSG_MAX_SIZE: usize = u8::MAX as usize; type Message = ArrayVec; @@ -95,11 +93,7 @@ where ) -> Result<(), Error> { if self.is_locked() { let vault = &mut self.vault; - - let signer = vault - .unlock(account, cred) - .await - .map_err(|e| Error::Vault(e))?; + let signer = vault.unlock(account, cred).await.map_err(Error::Vault)?; if self.default_account.is_none() { self.default_account = Some(0); @@ -292,4 +286,3 @@ impl From for Error { Error::InvalidPhrase } } - diff --git a/libwallet/src/substrate_ext.rs b/libwallet/src/substrate_ext.rs index 64bd570..fe38d23 100644 --- a/libwallet/src/substrate_ext.rs +++ b/libwallet/src/substrate_ext.rs @@ -1,7 +1,5 @@ use crate::Network; -trait SubstrateExt {} - impl From<&str> for Network { fn from(s: &str) -> Self { // TODO use registry diff --git a/libwallet/src/util.rs b/libwallet/src/util.rs index 6d7b931..3c37640 100644 --- a/libwallet/src/util.rs +++ b/libwallet/src/util.rs @@ -1,5 +1,4 @@ use core::{iter, ops}; -use crate::Pair; #[cfg(feature = "rand")] pub fn random_bytes(rng: &mut R) -> [u8; S] @@ -38,11 +37,8 @@ macro_rules! seed_from_entropy { }; } -use arrayvec::ArrayString; pub(crate) use seed_from_entropy; -use crate::{any::{self, AnySignature}, Network, Public }; - impl Pin { const LEN: usize = 4; @@ -63,7 +59,11 @@ impl Pin { let mut seed = [0; S]; // using same hashing strategy as Substrate to have some compatibility // when pin is 0(no pin) we produce the same addresses - let len = self.eq(&0).then_some(salt.len() - 2).unwrap_or(salt.len()); + let len = if self.eq(&0) { + salt.len() - 2 + } else { + salt.len() + }; pbkdf2::>(data, &salt[..len], 2048, &mut seed); seed } @@ -81,7 +81,7 @@ impl<'a> From<&'a str> for Pin { .map(|c| c.to_digit(16).unwrap_or(0)) .enumerate() .fold(0, |pin, (i, d)| { - pin | ((d as u16) << (Pin::LEN - 1 - i) * Pin::LEN) + pin | ((d as u16) << ((Pin::LEN - 1 - i) * Pin::LEN)) })) } } diff --git a/libwallet/src/vault.rs b/libwallet/src/vault.rs index e320904..a45d0aa 100644 --- a/libwallet/src/vault.rs +++ b/libwallet/src/vault.rs @@ -20,19 +20,18 @@ pub trait Vault { type Id; type Account: Account; - async fn unlock( + fn unlock( &mut self, account: Self::Id, cred: impl Into, - ) -> Result; + ) -> impl core::future::Future>; } mod utils { const MAX_PATH_LEN: usize = 16; use arrayvec::ArrayString; - use crate::{account::Account, any::AnySignature, any, Derive, Network, Pair, Public}; - + use crate::{account::Account, any, any::AnySignature, Derive, Network, Pair, Public}; /// The root account is a container of the key pairs stored in the vault and cannot be /// used to sign messages directly, we always derive new key pairs from it to create @@ -82,7 +81,7 @@ mod utils { path: ArrayString, name: ArrayString<{ MAX_PATH_LEN - 2 }>, } - + impl Account for AccountSigner { fn public(&self) -> impl Public { self.pair.as_ref().expect("account unlocked").public() @@ -91,13 +90,13 @@ mod utils { impl AccountSigner { pub(crate) fn new<'a>(name: impl Into>) -> Self { - let n = name.into().unwrap_or_else(|| "default"); + let n = name.into().unwrap_or("default"); let mut path = ArrayString::from("//").unwrap(); - path.push_str(&n); + path.push_str(n); AccountSigner { pair: None, network: Network::default(), - name: ArrayString::from(&n).expect("short name"), + name: ArrayString::from(n).expect("short name"), path, } } @@ -133,12 +132,11 @@ mod utils { type Signature = AnySignature; async fn sign_msg(&self, msg: impl AsRef<[u8]>) -> Result { - Ok(self - .pair + self.pair .as_ref() .expect("account unlocked") .sign_msg(msg) - .await?) + .await } async fn verify(&self, msg: impl AsRef<[u8]>, sig: impl AsRef<[u8]>) -> bool { diff --git a/libwallet/src/vault/simple.rs b/libwallet/src/vault/simple.rs index ae003e5..6c8a74f 100644 --- a/libwallet/src/vault/simple.rs +++ b/libwallet/src/vault/simple.rs @@ -1,19 +1,15 @@ -use core::convert::TryInto; -use core::marker::PhantomData; - -use arrayvec::ArrayVec; - use crate::util::{seed_from_entropy, Pin}; use crate::{ vault::utils::{AccountSigner, RootAccount}, - Derive, Vault, + Vault, }; +use core::marker::PhantomData; /// A vault that holds secrets in memory pub struct Simple { locked: Option<[u8; 32]>, unlocked: Option<[u8; 32]>, - _phantom: PhantomData + _phantom: PhantomData, } impl Simple { @@ -37,7 +33,7 @@ impl Simple { Simple { locked: Some(crate::util::random_bytes::<_, 32>(rng)), unlocked: None, - _phantom: Default::default() + _phantom: Default::default(), } }