From 83ef7890023bb3e6a015e1e22cd16e4c44977a2f Mon Sep 17 00:00:00 2001 From: jonesmarvin8 <83104039+jonesmarvin8@users.noreply.github.com> Date: Wed, 18 Mar 2026 18:44:07 -0400 Subject: [PATCH] lint issues --- integration_tests/src/config.rs | 303 +++++++++--------- .../key_management/key_tree/keys_private.rs | 16 +- .../key_management/key_tree/keys_public.rs | 66 ++-- .../src/key_management/secret_holders.rs | 2 + 4 files changed, 203 insertions(+), 184 deletions(-) diff --git a/integration_tests/src/config.rs b/integration_tests/src/config.rs index c4f2256e..5bcf59b9 100644 --- a/integration_tests/src/config.rs +++ b/integration_tests/src/config.rs @@ -1,6 +1,6 @@ use std::{net::SocketAddr, path::PathBuf, time::Duration}; -use anyhow::{Context, Result}; +use anyhow::{Context as _, Result}; use bytesize::ByteSize; use common::block::{AccountInitialData, CommitmentsInitialData}; use indexer_service::{BackoffConfig, ChannelId, ClientConfig, IndexerConfig}; @@ -13,6 +13,157 @@ use wallet::config::{ InitialAccountData, InitialAccountDataPrivate, InitialAccountDataPublic, WalletConfig, }; +/// Sequencer config options available for custom changes in integration tests. +#[derive(Debug, Clone, Copy)] +pub struct SequencerPartialConfig { + pub max_num_tx_in_block: usize, + pub max_block_size: ByteSize, + pub mempool_max_size: usize, + pub block_create_timeout: Duration, +} + +impl Default for SequencerPartialConfig { + fn default() -> Self { + Self { + max_num_tx_in_block: 20, + max_block_size: ByteSize::mib(1), + mempool_max_size: 10_000, + block_create_timeout: Duration::from_secs(10), + } + } +} + +pub struct InitialData { + pub public_accounts: Vec<(PrivateKey, u128)>, + pub private_accounts: Vec<(KeyChain, Account)>, +} + +impl InitialData { + #[must_use] + pub fn with_two_public_and_two_private_initialized_accounts() -> Self { + let mut public_alice_private_key = PrivateKey::new_os_random(); + let mut public_alice_public_key = + PublicKey::new_from_private_key(&public_alice_private_key); + let mut public_alice_account_id = AccountId::from(&public_alice_public_key); + + let mut public_bob_private_key = PrivateKey::new_os_random(); + let mut public_bob_public_key = PublicKey::new_from_private_key(&public_bob_private_key); + let mut public_bob_account_id = AccountId::from(&public_bob_public_key); + + // Ensure consistent ordering + if public_alice_account_id > public_bob_account_id { + std::mem::swap(&mut public_alice_private_key, &mut public_bob_private_key); + std::mem::swap(&mut public_alice_public_key, &mut public_bob_public_key); + std::mem::swap(&mut public_alice_account_id, &mut public_bob_account_id); + } + + let mut private_charlie_key_chain = KeyChain::new_os_random(); + let mut private_charlie_account_id = + AccountId::from(&private_charlie_key_chain.nullifier_public_key); + + let mut private_david_key_chain = KeyChain::new_os_random(); + let mut private_david_account_id = + AccountId::from(&private_david_key_chain.nullifier_public_key); + + // Ensure consistent ordering + if private_charlie_account_id > private_david_account_id { + std::mem::swap(&mut private_charlie_key_chain, &mut private_david_key_chain); + std::mem::swap( + &mut private_charlie_account_id, + &mut private_david_account_id, + ); + } + + Self { + public_accounts: vec![ + (public_alice_private_key, 10_000), + (public_bob_private_key, 20_000), + ], + private_accounts: vec![ + ( + private_charlie_key_chain, + Account { + balance: 10_000, + data: Data::default(), + program_owner: DEFAULT_PROGRAM_ID, + nonce: 0_u128.into(), + }, + ), + ( + private_david_key_chain, + Account { + balance: 20_000, + data: Data::default(), + program_owner: DEFAULT_PROGRAM_ID, + nonce: 0_u128.into(), + }, + ), + ], + } + } + + fn sequencer_initial_accounts(&self) -> Vec { + self.public_accounts + .iter() + .map(|(priv_key, balance)| { + let pub_key = PublicKey::new_from_private_key(priv_key); + let account_id = AccountId::from(&pub_key); + AccountInitialData { + account_id, + balance: *balance, + } + }) + .collect() + } + + fn sequencer_initial_commitments(&self) -> Vec { + self.private_accounts + .iter() + .map(|(key_chain, account)| CommitmentsInitialData { + npk: key_chain.nullifier_public_key.clone(), + account: account.clone(), + }) + .collect() + } + + fn wallet_initial_accounts(&self) -> Vec { + self.public_accounts + .iter() + .map(|(priv_key, _)| { + let pub_key = PublicKey::new_from_private_key(priv_key); + let account_id = AccountId::from(&pub_key); + InitialAccountData::Public(InitialAccountDataPublic { + account_id, + pub_sign_key: priv_key.clone(), + }) + }) + .chain(self.private_accounts.iter().map(|(key_chain, account)| { + let account_id = AccountId::from(&key_chain.nullifier_public_key); + InitialAccountData::Private(Box::new(InitialAccountDataPrivate { + account_id, + account: account.clone(), + key_chain: key_chain.clone(), + })) + })) + .collect() + } +} + +#[derive(Debug, Clone, Copy)] +pub enum UrlProtocol { + Http, + Ws, +} + +impl std::fmt::Display for UrlProtocol { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Http => write!(f, "http"), + Self::Ws => write!(f, "ws"), + } + } +} + pub fn indexer_config( bedrock_addr: SocketAddr, home: PathBuf, @@ -37,25 +188,6 @@ pub fn indexer_config( }) } -/// Sequencer config options available for custom changes in integration tests. -pub struct SequencerPartialConfig { - pub max_num_tx_in_block: usize, - pub max_block_size: ByteSize, - pub mempool_max_size: usize, - pub block_create_timeout: Duration, -} - -impl Default for SequencerPartialConfig { - fn default() -> Self { - Self { - max_num_tx_in_block: 20, - max_block_size: ByteSize::mib(1), - mempool_max_size: 10_000, - block_create_timeout: Duration::from_secs(10), - } - } -} - pub fn sequencer_config( partial: SequencerPartialConfig, home: PathBuf, @@ -116,135 +248,6 @@ pub fn wallet_config( }) } -pub struct InitialData { - pub public_accounts: Vec<(PrivateKey, u128)>, - pub private_accounts: Vec<(KeyChain, Account)>, -} - -impl InitialData { - pub fn with_two_public_and_two_private_initialized_accounts() -> Self { - let mut public_alice_private_key = PrivateKey::new_os_random(); - let mut public_alice_public_key = - PublicKey::new_from_private_key(&public_alice_private_key); - let mut public_alice_account_id = AccountId::from(&public_alice_public_key); - - let mut public_bob_private_key = PrivateKey::new_os_random(); - let mut public_bob_public_key = PublicKey::new_from_private_key(&public_bob_private_key); - let mut public_bob_account_id = AccountId::from(&public_bob_public_key); - - // Ensure consistent ordering - if public_alice_account_id > public_bob_account_id { - std::mem::swap(&mut public_alice_private_key, &mut public_bob_private_key); - std::mem::swap(&mut public_alice_public_key, &mut public_bob_public_key); - std::mem::swap(&mut public_alice_account_id, &mut public_bob_account_id); - } - - let mut private_charlie_key_chain = KeyChain::new_os_random(); - let mut private_charlie_account_id = - AccountId::from(&private_charlie_key_chain.nullifier_public_key); - - let mut private_david_key_chain = KeyChain::new_os_random(); - let mut private_david_account_id = - AccountId::from(&private_david_key_chain.nullifier_public_key); - - // Ensure consistent ordering - if private_charlie_account_id > private_david_account_id { - std::mem::swap(&mut private_charlie_key_chain, &mut private_david_key_chain); - std::mem::swap( - &mut private_charlie_account_id, - &mut private_david_account_id, - ); - } - - Self { - public_accounts: vec![ - (public_alice_private_key, 10_000), - (public_bob_private_key, 20_000), - ], - private_accounts: vec![ - ( - private_charlie_key_chain, - Account { - balance: 10_000, - data: Data::default(), - program_owner: DEFAULT_PROGRAM_ID, - nonce: 0, - }, - ), - ( - private_david_key_chain, - Account { - balance: 20_000, - data: Data::default(), - program_owner: DEFAULT_PROGRAM_ID, - nonce: 0, - }, - ), - ], - } - } - - fn sequencer_initial_accounts(&self) -> Vec { - self.public_accounts - .iter() - .map(|(priv_key, balance)| { - let pub_key = PublicKey::new_from_private_key(priv_key); - let account_id = AccountId::from(&pub_key); - AccountInitialData { - account_id, - balance: *balance, - } - }) - .collect() - } - - fn sequencer_initial_commitments(&self) -> Vec { - self.private_accounts - .iter() - .map(|(key_chain, account)| CommitmentsInitialData { - npk: key_chain.nullifier_public_key.clone(), - account: account.clone(), - }) - .collect() - } - - fn wallet_initial_accounts(&self) -> Vec { - self.public_accounts - .iter() - .map(|(priv_key, _)| { - let pub_key = PublicKey::new_from_private_key(priv_key); - let account_id = AccountId::from(&pub_key); - InitialAccountData::Public(InitialAccountDataPublic { - account_id, - pub_sign_key: priv_key.clone(), - }) - }) - .chain(self.private_accounts.iter().map(|(key_chain, account)| { - let account_id = AccountId::from(&key_chain.nullifier_public_key); - InitialAccountData::Private(InitialAccountDataPrivate { - account_id, - account: account.clone(), - key_chain: key_chain.clone(), - }) - })) - .collect() - } -} - -pub enum UrlProtocol { - Http, - Ws, -} - -impl std::fmt::Display for UrlProtocol { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - UrlProtocol::Http => write!(f, "http"), - UrlProtocol::Ws => write!(f, "ws"), - } - } -} - pub fn addr_to_url(protocol: UrlProtocol, addr: SocketAddr) -> Result { // Convert 0.0.0.0 to 127.0.0.1 for client connections // When binding to port 0, the server binds to 0.0.0.0: @@ -259,7 +262,7 @@ pub fn addr_to_url(protocol: UrlProtocol, addr: SocketAddr) -> Result { } fn bedrock_channel_id() -> ChannelId { - let channel_id: [u8; 32] = [0u8, 1] + let channel_id: [u8; 32] = [0_u8, 1] .repeat(16) .try_into() .unwrap_or_else(|_| unreachable!()); diff --git a/key_protocol/src/key_management/key_tree/keys_private.rs b/key_protocol/src/key_management/key_tree/keys_private.rs index 9b2bf70f..071d596c 100644 --- a/key_protocol/src/key_management/key_tree/keys_private.rs +++ b/key_protocol/src/key_management/key_tree/keys_private.rs @@ -1,4 +1,4 @@ -use k256::{Scalar, elliptic_curve::PrimeField}; +use k256::{Scalar, elliptic_curve::PrimeField as _}; use nssa_core::{NullifierPublicKey, encryption::ViewingPublicKey}; use serde::{Deserialize, Serialize}; @@ -12,7 +12,7 @@ use crate::key_management::{ pub struct ChildKeysPrivate { pub value: (KeyChain, nssa::Account), pub ccc: [u8; 32], - /// Can be [`None`] if root + /// Can be [`None`] if root. pub cci: Option, } @@ -54,6 +54,7 @@ impl KeyNode for ChildKeysPrivate { } fn nth_child(&self, cci: u32) -> Self { + #[expect(clippy::arithmetic_side_effects, reason = "TODO: fix later")] let parent_pt = Scalar::from_repr(self.value.0.private_key_holder.nullifier_secret_key.into()) .expect("Key generated as scalar, must be valid representation") @@ -63,6 +64,7 @@ impl KeyNode for ChildKeysPrivate { input.extend_from_slice(b"LEE_seed_priv"); input.extend_from_slice(&parent_pt.to_bytes()); + #[expect(clippy::big_endian_bytes, reason = "BIP-032 uses big endian")] input.extend_from_slice(&cci.to_be_bytes()); let hash_value = hmac_sha512::HMAC::mac(input, self.ccc); @@ -113,12 +115,20 @@ impl KeyNode for ChildKeysPrivate { } } +#[expect( + clippy::single_char_lifetime_names, + reason = "TODO add meaningful name" +)] impl<'a> From<&'a ChildKeysPrivate> for &'a (KeyChain, nssa::Account) { fn from(value: &'a ChildKeysPrivate) -> Self { &value.value } } +#[expect( + clippy::single_char_lifetime_names, + reason = "TODO add meaningful name" +)] impl<'a> From<&'a mut ChildKeysPrivate> for &'a mut (KeyChain, nssa::Account) { fn from(value: &'a mut ChildKeysPrivate) -> Self { &mut value.value @@ -190,7 +200,7 @@ mod tests { ]; let root_node = ChildKeysPrivate::root(seed); - let child_node = ChildKeysPrivate::nth_child(&root_node, 42u32); + let child_node = ChildKeysPrivate::nth_child(&root_node, 42_u32); let expected_ccc: [u8; 32] = [ 27, 73, 133, 213, 214, 63, 217, 184, 164, 17, 172, 140, 223, 95, 255, 157, 11, 0, 58, diff --git a/key_protocol/src/key_management/key_tree/keys_public.rs b/key_protocol/src/key_management/key_tree/keys_public.rs index 28814398..ec7a31f1 100644 --- a/key_protocol/src/key_management/key_tree/keys_public.rs +++ b/key_protocol/src/key_management/key_tree/keys_public.rs @@ -8,36 +8,32 @@ pub struct ChildKeysPublic { pub csk: nssa::PrivateKey, pub cpk: nssa::PublicKey, pub ccc: [u8; 32], - /// Can be [`None`] if root + /// Can be [`None`] if root. pub cci: Option, } impl ChildKeysPublic { + #[expect(clippy::big_endian_bytes, reason = "BIP-032 uses big endian")] fn compute_hash_value(&self, cci: u32) -> [u8; 64] { let mut hash_input = vec![]; - match ((2u32).pow(31)).cmp(&cci) { - // Non-harden - std::cmp::Ordering::Greater => { - // BIP-032 compatibility requires 1-byte header from the public_key; - // Not stored in `self.cpk.value()` - let sk = secp256k1::SecretKey::from_byte_array(*self.csk.value()) - .expect("32 bytes, within curve order"); - let pk = secp256k1::PublicKey::from_secret_key(&secp256k1::Secp256k1::new(), &sk); - hash_input.extend_from_slice(&secp256k1::PublicKey::serialize(&pk)); - hash_input.extend_from_slice(&cci.to_be_bytes()); - - hmac_sha512::HMAC::mac(hash_input, self.ccc) - } - // Harden - _ => { - hash_input.extend_from_slice(&[0u8]); - hash_input.extend_from_slice(self.csk.value()); - hash_input.extend_from_slice(&cci.to_be_bytes()); - - hmac_sha512::HMAC::mac(hash_input, self.ccc) - } + if ((2_u32).pow(31)).cmp(&cci) == std::cmp::Ordering::Greater { + // Non-harden. + // BIP-032 compatibility requires 1-byte header from the public_key; + // Not stored in `self.cpk.value()`. + let sk = secp256k1::SecretKey::from_byte_array(*self.csk.value()) + .expect("32 bytes, within curve order"); + let pk = secp256k1::PublicKey::from_secret_key(&secp256k1::Secp256k1::new(), &sk); + hash_input.extend_from_slice(&secp256k1::PublicKey::serialize(&pk)); + } else { + // Harden. + hash_input.extend_from_slice(&[0_u8]); + hash_input.extend_from_slice(self.csk.value()); } + + hash_input.extend_from_slice(&cci.to_be_bytes()); + + hmac_sha512::HMAC::mac(hash_input, self.ccc) } } @@ -67,16 +63,20 @@ impl KeyNode for ChildKeysPublic { ) .unwrap(); - let csk = nssa::PrivateKey::try_new( - csk.add_tweak(&Scalar::from_be_bytes(*self.csk.value()).unwrap()) + let csk = nssa::PrivateKey::try_new({ + #[expect(clippy::big_endian_bytes, reason = "BIP-032 uses big endian")] + let scalar = Scalar::from_be_bytes(*self.csk.value()).unwrap(); + + csk.add_tweak(&scalar) .expect("Expect a valid Scalar") - .secret_bytes(), - ) + .secret_bytes() + }) .unwrap(); - if secp256k1::constants::CURVE_ORDER < *csk.value() { - panic!("Secret key cannot exceed curve order"); - } + assert!( + secp256k1::constants::CURVE_ORDER >= *csk.value(), + "Secret key cannot exceed curve order" + ); let ccc = *hash_value .last_chunk::<32>() @@ -105,6 +105,10 @@ impl KeyNode for ChildKeysPublic { } } +#[expect( + clippy::single_char_lifetime_names, + reason = "TODO add meaningful name" +)] impl<'a> From<&'a ChildKeysPublic> for &'a nssa::PrivateKey { fn from(value: &'a ChildKeysPublic) -> Self { &value.csk @@ -158,7 +162,7 @@ mod tests { 187, 148, 92, 44, 253, 210, 37, ]; let root_keys = ChildKeysPublic::root(seed); - let cci = (2u32).pow(31) + 13; + let cci = (2_u32).pow(31) + 13; let child_keys = ChildKeysPublic::nth_child(&root_keys, cci); let expected_ccc = [ @@ -226,7 +230,7 @@ mod tests { 187, 148, 92, 44, 253, 210, 37, ]; let root_keys = ChildKeysPublic::root(seed); - let cci = (2u32).pow(31); //equivant to 0, thus non-harden. + let cci = (2_u32).pow(31); //equivant to 0, thus non-harden. let child_keys = ChildKeysPublic::nth_child(&root_keys, cci); let expected_ccc = [ diff --git a/key_protocol/src/key_management/secret_holders.rs b/key_protocol/src/key_management/secret_holders.rs index b504f0df..9643abe0 100644 --- a/key_protocol/src/key_management/secret_holders.rs +++ b/key_protocol/src/key_management/secret_holders.rs @@ -79,6 +79,7 @@ impl SeedHolder { impl SecretSpendingKey { #[must_use] + #[expect(clippy::big_endian_bytes, reason = "BIP-032 uses big endian")] pub fn generate_nullifier_secret_key(&self, index: Option) -> NullifierSecretKey { const PREFIX: &[u8; 8] = b"LEE/keys"; const SUFFIX_1: &[u8; 1] = &[1]; @@ -100,6 +101,7 @@ impl SecretSpendingKey { } #[must_use] + #[expect(clippy::big_endian_bytes, reason = "BIP-032 uses big endian")] pub fn generate_viewing_secret_key(&self, index: Option) -> ViewingSecretKey { const PREFIX: &[u8; 8] = b"LEE/keys"; const SUFFIX_1: &[u8; 1] = &[2];