Apply suggestions from code review 1

Co-authored-by: Daniil Polyakov <arjentix@gmail.com>
This commit is contained in:
Pravdyvy 2025-11-26 07:07:58 +02:00 committed by GitHub
parent e3d095464c
commit 463942df80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 45 additions and 71 deletions

View File

@ -56,7 +56,7 @@ fn make_private_account_input_from_str(addr: &str) -> String {
pub async fn pre_test(
home_dir: PathBuf,
) -> Result<(ServerHandle, JoinHandle<Result<()>>, TempDir)> {
wallet::execute_setup("test_pass".to_string()).await?;
wallet::execute_setup("test_pass".to_owned()).await?;
let home_dir_sequencer = home_dir.join("sequencer");

View File

@ -17,25 +17,15 @@ impl FromStr for ChainIndex {
type Err = ChainIndexError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.starts_with("/") {
return Err(ChainIndexError::NoRootFound);
if !s.starts_with('/') {
return Err(ChainIndexError:NoRootFound);
}
if s == "/" {
return Ok(ChainIndex(vec![]));
}
let uprooted_substring = s.strip_prefix("/").unwrap();
let splitted_chain: Vec<&str> = uprooted_substring.split("/").collect();
let mut res = vec![];
for split_ch in splitted_chain {
let cci = split_ch.parse()?;
res.push(cci);
}
Ok(Self(res))
s
.split("/")
.map(u32::from_str)
.collect()
.map_err(Into::into)
}
}
@ -68,7 +58,7 @@ impl ChainIndex {
ChainIndex(chain)
}
pub fn n_th_child(&self, child_id: u32) -> ChainIndex {
pub fn nth_child(&self, child_id: u32) -> ChainIndex {
let mut chain = self.0.clone();
chain.push(child_id);

View File

@ -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<u32>,
}
@ -49,7 +49,7 @@ impl KeyNode for ChildKeysPrivate {
}
}
fn n_th_child(&self, cci: u32) -> Self {
fn nth_child(&self, cci: u32) -> Self {
let parent_pt = Scalar::from_repr(
self.value
.0
@ -109,8 +109,8 @@ impl KeyNode for ChildKeysPrivate {
&self.ccc
}
fn child_index(&self) -> &Option<u32> {
&self.cci
fn child_index(&self) -> Option<u32> {
self.cci
}
fn address(&self) -> nssa::Address {

View File

@ -7,7 +7,7 @@ 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<u32>,
}
@ -27,7 +27,7 @@ impl KeyNode for ChildKeysPublic {
}
}
fn n_th_child(&self, cci: u32) -> Self {
fn nth_child(&self, cci: u32) -> Self {
let mut hash_input = vec![];
hash_input.extend_from_slice(self.csk.value());
hash_input.extend_from_slice(&cci.to_le_bytes());
@ -50,8 +50,8 @@ impl KeyNode for ChildKeysPublic {
&self.ccc
}
fn child_index(&self) -> &Option<u32> {
&self.cci
fn child_index(&self) -> Option<u32> {
self.cci
}
fn address(&self) -> nssa::Address {

View File

@ -31,11 +31,8 @@ impl<Node: KeyNode> KeyTree<Node> {
let root_keys = Node::root(seed_fit);
let address = root_keys.address();
let mut key_map = BTreeMap::new();
let mut addr_map = HashMap::new();
key_map.insert(ChainIndex::root(), root_keys);
addr_map.insert(address, ChainIndex::root());
let key_map = BTreeMap::from_iter([(ChainIndex::root(), root_keys)]);
let addr_map = HashMap::from_iter([(address, ChainIndex::root())]);
Self { key_map, addr_map }
}
@ -60,7 +57,8 @@ impl<Node: KeyNode> KeyTree<Node> {
let leftmost_child = parent_id.n_th_child(u32::MIN);
if !self.key_map.contains_key(&leftmost_child) {
Some(0)
return Some(0)
}
} else {
let mut right = u32::MAX - 1;
let mut left_border = u32::MIN;
@ -93,11 +91,7 @@ impl<Node: KeyNode> KeyTree<Node> {
}
pub fn generate_new_node(&mut self, parent_cci: ChainIndex) -> Option<nssa::Address> {
if !self.key_map.contains_key(&parent_cci) {
return None;
}
let father_keys = self.key_map.get(&parent_cci).unwrap();
let father_keys = self.key_map.get(&parent_cci)?;
let next_child_id = self.find_next_last_child_of_id(&parent_cci).unwrap();
let next_cci = parent_cci.n_th_child(next_child_id);

View File

@ -1,7 +1,7 @@
pub trait KeyNode {
fn root(seed: [u8; 64]) -> Self;
fn n_th_child(&self, cci: u32) -> Self;
fn nth_child(&self, cci: u32) -> Self;
fn chain_code(&self) -> &[u8; 32];

View File

@ -41,8 +41,8 @@ impl KeyChain {
}
pub fn new_mnemonic(passphrase: String) -> Self {
//Currently dropping SeedHolder at the end of initialization.
//Now entirely sure if we need it in the future.
// Currently dropping SeedHolder at the end of initialization.
// Not entirely sure if we need it in the future.
let seed_holder = SeedHolder::new_mnemonic(passphrase);
let secret_spending_key = seed_holder.produce_top_secret_key_holder();

View File

@ -45,7 +45,7 @@ impl SeedHolder {
}
pub fn new_mnemonic(passphrase: String) -> Self {
//Enthropy bytes must be deterministic as well
// Enthropy bytes must be deterministic as well
let enthopy_bytes: [u8; 32] = [0; 32];
let mnemonic = Mnemonic::from_entropy(&enthopy_bytes).unwrap();

View File

@ -14,9 +14,9 @@ pub type PublicKey = AffinePoint;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NSSAUserData {
///Default public accounts
/// Default public accounts
pub default_pub_account_signing_keys: HashMap<nssa::Address, nssa::PrivateKey>,
///Default private accounts
/// Default private accounts
pub default_user_private_accounts:
HashMap<nssa::Address, (KeyChain, nssa_core::account::Account)>,
/// Tree of public keys

View File

@ -126,17 +126,13 @@ impl WalletChainStore {
addr: nssa::Address,
account: nssa_core::account::Account,
) {
println!("inserting at address {}, this account {:?}", addr, account);
println!("inserting at address {addr}, this account {account:?}");
if self
.user_data
.default_user_private_accounts
.contains_key(&addr)
{
self.user_data
let entry = self.user_data
.default_user_private_accounts
.entry(addr)
.and_modify(|data| data.1 = account);
if matches!(entry, Entry::Vacant(_)) {
} else {
self.user_data
.private_key_tree
@ -270,27 +266,21 @@ mod tests {
}
fn create_sample_persistent_accounts() -> Vec<PersistentAccountData> {
let mut accs = vec![];
let public_data = ChildKeysPublic::root([42; 64]);
accs.push(PersistentAccountData::Public(PersistentAccountDataPublic {
address: public_data.address(),
chain_index: ChainIndex::root(),
data: public_data,
}));
let private_data = ChildKeysPrivate::root([47; 64]);
accs.push(PersistentAccountData::Private(
PersistentAccountDataPrivate {
vec![
PersistentAccountData::Public(PersistentAccountDataPublic {
address: public_data.address(),
chain_index: ChainIndex::root(),
data: public_data,
}),
PersistentAccountData::Private(PersistentAccountDataPrivate {
address: private_data.address(),
chain_index: ChainIndex::root(),
data: private_data,
},
));
accs
})
]
}
#[test]

View File

@ -249,7 +249,7 @@ pub enum Command {
Config(ConfigSubcommand),
}
///Represents CLI command for a wallet with setup included
/// Represents CLI command for a wallet with setup included
#[derive(Debug, Subcommand, Clone)]
#[clap(about)]
pub enum OverCommand {
@ -410,12 +410,12 @@ pub async fn parse_block_range(
}
}
for (_, keys_node) in wallet_core
for keys_node in wallet_core
.storage
.user_data
.private_key_tree
.key_map
.iter()
.values()
{
let acc_addr = keys_node.address();
let key_chain = &keys_node.value.0;