mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-01-05 23:03:06 +00:00
Apply suggestions from code review 1
Co-authored-by: Daniil Polyakov <arjentix@gmail.com>
This commit is contained in:
parent
e3d095464c
commit
463942df80
@ -56,7 +56,7 @@ fn make_private_account_input_from_str(addr: &str) -> String {
|
|||||||
pub async fn pre_test(
|
pub async fn pre_test(
|
||||||
home_dir: PathBuf,
|
home_dir: PathBuf,
|
||||||
) -> Result<(ServerHandle, JoinHandle<Result<()>>, TempDir)> {
|
) -> 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");
|
let home_dir_sequencer = home_dir.join("sequencer");
|
||||||
|
|
||||||
|
|||||||
@ -17,25 +17,15 @@ impl FromStr for ChainIndex {
|
|||||||
type Err = ChainIndexError;
|
type Err = ChainIndexError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
if !s.starts_with("/") {
|
if !s.starts_with('/') {
|
||||||
return Err(ChainIndexError::NoRootFound);
|
return Err(ChainIndexError:NoRootFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
if s == "/" {
|
s
|
||||||
return Ok(ChainIndex(vec![]));
|
.split("/")
|
||||||
}
|
.map(u32::from_str)
|
||||||
|
.collect()
|
||||||
let uprooted_substring = s.strip_prefix("/").unwrap();
|
.map_err(Into::into)
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,7 +58,7 @@ impl ChainIndex {
|
|||||||
ChainIndex(chain)
|
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();
|
let mut chain = self.0.clone();
|
||||||
chain.push(child_id);
|
chain.push(child_id);
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,7 @@ use crate::key_management::{
|
|||||||
pub struct ChildKeysPrivate {
|
pub struct ChildKeysPrivate {
|
||||||
pub value: (KeyChain, nssa::Account),
|
pub value: (KeyChain, nssa::Account),
|
||||||
pub ccc: [u8; 32],
|
pub ccc: [u8; 32],
|
||||||
///Can be None if root
|
/// Can be [`None`] if root
|
||||||
pub cci: Option<u32>,
|
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(
|
let parent_pt = Scalar::from_repr(
|
||||||
self.value
|
self.value
|
||||||
.0
|
.0
|
||||||
@ -109,8 +109,8 @@ impl KeyNode for ChildKeysPrivate {
|
|||||||
&self.ccc
|
&self.ccc
|
||||||
}
|
}
|
||||||
|
|
||||||
fn child_index(&self) -> &Option<u32> {
|
fn child_index(&self) -> Option<u32> {
|
||||||
&self.cci
|
self.cci
|
||||||
}
|
}
|
||||||
|
|
||||||
fn address(&self) -> nssa::Address {
|
fn address(&self) -> nssa::Address {
|
||||||
|
|||||||
@ -7,7 +7,7 @@ pub struct ChildKeysPublic {
|
|||||||
pub csk: nssa::PrivateKey,
|
pub csk: nssa::PrivateKey,
|
||||||
pub cpk: nssa::PublicKey,
|
pub cpk: nssa::PublicKey,
|
||||||
pub ccc: [u8; 32],
|
pub ccc: [u8; 32],
|
||||||
///Can be None if root
|
/// Can be [`None`] if root
|
||||||
pub cci: Option<u32>,
|
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![];
|
let mut hash_input = vec![];
|
||||||
hash_input.extend_from_slice(self.csk.value());
|
hash_input.extend_from_slice(self.csk.value());
|
||||||
hash_input.extend_from_slice(&cci.to_le_bytes());
|
hash_input.extend_from_slice(&cci.to_le_bytes());
|
||||||
@ -50,8 +50,8 @@ impl KeyNode for ChildKeysPublic {
|
|||||||
&self.ccc
|
&self.ccc
|
||||||
}
|
}
|
||||||
|
|
||||||
fn child_index(&self) -> &Option<u32> {
|
fn child_index(&self) -> Option<u32> {
|
||||||
&self.cci
|
self.cci
|
||||||
}
|
}
|
||||||
|
|
||||||
fn address(&self) -> nssa::Address {
|
fn address(&self) -> nssa::Address {
|
||||||
|
|||||||
@ -31,11 +31,8 @@ impl<Node: KeyNode> KeyTree<Node> {
|
|||||||
let root_keys = Node::root(seed_fit);
|
let root_keys = Node::root(seed_fit);
|
||||||
let address = root_keys.address();
|
let address = root_keys.address();
|
||||||
|
|
||||||
let mut key_map = BTreeMap::new();
|
let key_map = BTreeMap::from_iter([(ChainIndex::root(), root_keys)]);
|
||||||
let mut addr_map = HashMap::new();
|
let addr_map = HashMap::from_iter([(address, ChainIndex::root())]);
|
||||||
|
|
||||||
key_map.insert(ChainIndex::root(), root_keys);
|
|
||||||
addr_map.insert(address, ChainIndex::root());
|
|
||||||
|
|
||||||
Self { key_map, addr_map }
|
Self { key_map, addr_map }
|
||||||
}
|
}
|
||||||
@ -60,7 +57,8 @@ impl<Node: KeyNode> KeyTree<Node> {
|
|||||||
let leftmost_child = parent_id.n_th_child(u32::MIN);
|
let leftmost_child = parent_id.n_th_child(u32::MIN);
|
||||||
|
|
||||||
if !self.key_map.contains_key(&leftmost_child) {
|
if !self.key_map.contains_key(&leftmost_child) {
|
||||||
Some(0)
|
return Some(0)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut right = u32::MAX - 1;
|
let mut right = u32::MAX - 1;
|
||||||
let mut left_border = u32::MIN;
|
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> {
|
pub fn generate_new_node(&mut self, parent_cci: ChainIndex) -> Option<nssa::Address> {
|
||||||
if !self.key_map.contains_key(&parent_cci) {
|
let father_keys = self.key_map.get(&parent_cci)?;
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let father_keys = self.key_map.get(&parent_cci).unwrap();
|
|
||||||
let next_child_id = self.find_next_last_child_of_id(&parent_cci).unwrap();
|
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);
|
let next_cci = parent_cci.n_th_child(next_child_id);
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
pub trait KeyNode {
|
pub trait KeyNode {
|
||||||
fn root(seed: [u8; 64]) -> Self;
|
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];
|
fn chain_code(&self) -> &[u8; 32];
|
||||||
|
|
||||||
|
|||||||
@ -41,8 +41,8 @@ impl KeyChain {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_mnemonic(passphrase: String) -> Self {
|
pub fn new_mnemonic(passphrase: String) -> Self {
|
||||||
//Currently dropping SeedHolder at the end of initialization.
|
// Currently dropping SeedHolder at the end of initialization.
|
||||||
//Now entirely sure if we need it in the future.
|
// Not entirely sure if we need it in the future.
|
||||||
let seed_holder = SeedHolder::new_mnemonic(passphrase);
|
let seed_holder = SeedHolder::new_mnemonic(passphrase);
|
||||||
let secret_spending_key = seed_holder.produce_top_secret_key_holder();
|
let secret_spending_key = seed_holder.produce_top_secret_key_holder();
|
||||||
|
|
||||||
|
|||||||
@ -45,7 +45,7 @@ impl SeedHolder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_mnemonic(passphrase: String) -> Self {
|
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 enthopy_bytes: [u8; 32] = [0; 32];
|
||||||
|
|
||||||
let mnemonic = Mnemonic::from_entropy(&enthopy_bytes).unwrap();
|
let mnemonic = Mnemonic::from_entropy(&enthopy_bytes).unwrap();
|
||||||
|
|||||||
@ -14,9 +14,9 @@ pub type PublicKey = AffinePoint;
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub struct NSSAUserData {
|
pub struct NSSAUserData {
|
||||||
///Default public accounts
|
/// Default public accounts
|
||||||
pub default_pub_account_signing_keys: HashMap<nssa::Address, nssa::PrivateKey>,
|
pub default_pub_account_signing_keys: HashMap<nssa::Address, nssa::PrivateKey>,
|
||||||
///Default private accounts
|
/// Default private accounts
|
||||||
pub default_user_private_accounts:
|
pub default_user_private_accounts:
|
||||||
HashMap<nssa::Address, (KeyChain, nssa_core::account::Account)>,
|
HashMap<nssa::Address, (KeyChain, nssa_core::account::Account)>,
|
||||||
/// Tree of public keys
|
/// Tree of public keys
|
||||||
|
|||||||
@ -126,17 +126,13 @@ impl WalletChainStore {
|
|||||||
addr: nssa::Address,
|
addr: nssa::Address,
|
||||||
account: nssa_core::account::Account,
|
account: nssa_core::account::Account,
|
||||||
) {
|
) {
|
||||||
println!("inserting at address {}, this account {:?}", addr, account);
|
println!("inserting at address {addr}, this account {account:?}");
|
||||||
|
|
||||||
if self
|
let entry = self.user_data
|
||||||
.user_data
|
|
||||||
.default_user_private_accounts
|
|
||||||
.contains_key(&addr)
|
|
||||||
{
|
|
||||||
self.user_data
|
|
||||||
.default_user_private_accounts
|
.default_user_private_accounts
|
||||||
.entry(addr)
|
.entry(addr)
|
||||||
.and_modify(|data| data.1 = account);
|
.and_modify(|data| data.1 = account);
|
||||||
|
if matches!(entry, Entry::Vacant(_)) {
|
||||||
} else {
|
} else {
|
||||||
self.user_data
|
self.user_data
|
||||||
.private_key_tree
|
.private_key_tree
|
||||||
@ -270,27 +266,21 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn create_sample_persistent_accounts() -> Vec<PersistentAccountData> {
|
fn create_sample_persistent_accounts() -> Vec<PersistentAccountData> {
|
||||||
let mut accs = vec![];
|
|
||||||
|
|
||||||
let public_data = ChildKeysPublic::root([42; 64]);
|
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]);
|
let private_data = ChildKeysPrivate::root([47; 64]);
|
||||||
|
|
||||||
accs.push(PersistentAccountData::Private(
|
vec![
|
||||||
PersistentAccountDataPrivate {
|
PersistentAccountData::Public(PersistentAccountDataPublic {
|
||||||
|
address: public_data.address(),
|
||||||
|
chain_index: ChainIndex::root(),
|
||||||
|
data: public_data,
|
||||||
|
}),
|
||||||
|
PersistentAccountData::Private(PersistentAccountDataPrivate {
|
||||||
address: private_data.address(),
|
address: private_data.address(),
|
||||||
chain_index: ChainIndex::root(),
|
chain_index: ChainIndex::root(),
|
||||||
data: private_data,
|
data: private_data,
|
||||||
},
|
})
|
||||||
));
|
]
|
||||||
|
|
||||||
accs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@ -249,7 +249,7 @@ pub enum Command {
|
|||||||
Config(ConfigSubcommand),
|
Config(ConfigSubcommand),
|
||||||
}
|
}
|
||||||
|
|
||||||
///Represents CLI command for a wallet with setup included
|
/// Represents CLI command for a wallet with setup included
|
||||||
#[derive(Debug, Subcommand, Clone)]
|
#[derive(Debug, Subcommand, Clone)]
|
||||||
#[clap(about)]
|
#[clap(about)]
|
||||||
pub enum OverCommand {
|
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
|
.storage
|
||||||
.user_data
|
.user_data
|
||||||
.private_key_tree
|
.private_key_tree
|
||||||
.key_map
|
.key_map
|
||||||
.iter()
|
.values()
|
||||||
{
|
{
|
||||||
let acc_addr = keys_node.address();
|
let acc_addr = keys_node.address();
|
||||||
let key_chain = &keys_node.value.0;
|
let key_chain = &keys_node.value.0;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user