Skip to content
Snippets Groups Projects
Commit da66773d authored by Nicolas80's avatar Nicolas80
Browse files

* Small refactorings & rewordings

parent 90f5f2aa
No related branches found
No related tags found
1 merge request!41Adding db persistence for all SecretFormat of vault keys as well as supporting derivations
...@@ -177,7 +177,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -177,7 +177,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
let table = compute_vault_key_files_table(&vault_key_addresses).await?; let table = compute_vault_key_files_table(&vault_key_addresses).await?;
println!("available key files (needs to be migrated with command \"vault migrate\" in order to use them):"); println!("available key files (needs to be migrated with command `vault migrate` in order to use them):");
println!("{table}"); println!("{table}");
} }
Subcommand::Use { Subcommand::Use {
...@@ -190,7 +190,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -190,7 +190,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
data.cfg.address = data.cfg.address =
Some(AccountId::from_str(&derivation.address).expect("invalid address")); Some(AccountId::from_str(&derivation.address).expect("invalid address"));
println!("Using key {}", derivation); println!("Using: {}", derivation);
conf::save_config(&data); conf::save_config(&data);
} }
Subcommand::Generate => { Subcommand::Generate => {
...@@ -213,8 +213,8 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -213,8 +213,8 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
.await? .await?
{ {
println!( println!(
"Vault entry already exists for address:'{}'", "Vault entry already exists for that address: {}",
vault_data_for_import.address derivation
); );
let linked_derivations = vault_derivation::fetch_all_linked_derivations_in_order( let linked_derivations = vault_derivation::fetch_all_linked_derivations_in_order(
...@@ -222,7 +222,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -222,7 +222,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
&derivation.root_address.clone(), &derivation.root_address.clone(),
) )
.await?; .await?;
println!("Here are all the linked derivations already present in the vault:"); println!("Here are all the SS58 Addresses linked to it in the vault:");
let table = compute_vault_derivations_table( let table = compute_vault_derivations_table(
data.connection.as_ref().unwrap(), data.connection.as_ref().unwrap(),
...@@ -260,12 +260,12 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -260,12 +260,12 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
let root_derivation = retrieve_vault_derivation(&data, address_or_vault_name).await?; let root_derivation = retrieve_vault_derivation(&data, address_or_vault_name).await?;
if root_derivation.path.is_some() { if root_derivation.path.is_some() {
println!("Can only add derivation on a ROOT key."); println!("Can only add derivation on an <Account>");
println!( println!(
"The selected key with address:'{}' already has a ROOT with address:'{}'", "The selected address:'{}' already has an <Account> with address:'{}'",
root_derivation.address, root_derivation.root_address root_derivation.address, root_derivation.root_address
); );
println!("You can check for available ROOT keys with command 'vault list root'"); println!("You can check for available <Account> addresses with command 'vault list account'");
return Ok(()); return Ok(());
} }
...@@ -273,7 +273,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -273,7 +273,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
.one(data.connection.as_ref().unwrap()) .one(data.connection.as_ref().unwrap())
.await? .await?
.ok_or(GcliError::Input(format!( .ok_or(GcliError::Input(format!(
"Could not find (root) vault account for address:'{}'", "Could not find vault_account for address:'{}'",
root_derivation.address root_derivation.address
)))?; )))?;
...@@ -283,13 +283,15 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -283,13 +283,15 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
Into::<&str>::into(SecretFormat::Substrate), Into::<&str>::into(SecretFormat::Substrate),
Into::<&str>::into(SecretFormat::Seed) Into::<&str>::into(SecretFormat::Seed)
); );
println!("Use command `vault list root` to see available <Account> and their format"); println!(
"Use command `vault list account` to see available <Account> and their format"
);
return Ok(()); return Ok(());
} }
println!("Adding derivation to root key: {root_derivation}"); println!("Adding derivation to: {root_derivation}");
println!("Enter password to decrypt the root key"); println!("Enter password to decrypt the <Account> key");
let root_secret_suri = retrieve_suri_from_vault_account(&vault_account)?; let root_secret_suri = retrieve_suri_from_vault_account(&vault_account)?;
...@@ -321,7 +323,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -321,7 +323,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
root_address: Set(root_derivation.root_address.clone()), root_address: Set(root_derivation.root_address.clone()),
}; };
let derivation = derivation.insert(data.connection.as_ref().unwrap()).await?; let derivation = derivation.insert(data.connection.as_ref().unwrap()).await?;
println!("Created derivation {}", derivation); println!("Created: {}", derivation);
} }
Subcommand::Rename { address } => { Subcommand::Rename { address } => {
let derivation = vault_derivation::Entity::find_by_id(address.to_string()) let derivation = vault_derivation::Entity::find_by_id(address.to_string())
...@@ -341,7 +343,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -341,7 +343,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
derivation.name derivation.name
); );
println!("Enter new vault name for the key (leave empty to remove the name)"); println!("Enter new name for address (leave empty to remove the name)");
let name = inputs::prompt_vault_name()?; let name = inputs::prompt_vault_name()?;
let old_name = derivation.name.clone(); let old_name = derivation.name.clone();
...@@ -373,16 +375,16 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -373,16 +375,16 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
let table = let table =
compute_vault_derivations_table(&txn, &all_derivations_to_delete).await?; compute_vault_derivations_table(&txn, &all_derivations_to_delete).await?;
println!("All derivations linked to the root derivation:"); println!("All addresses linked to: {derivation}");
println!("{table}"); println!("{table}");
println!( println!(
"This root derivation has {} derivations in total", "This <Account> has {} addresses in total",
all_derivations_to_delete.len() all_derivations_to_delete.len()
); );
let confirmed = inputs::confirm_action( let confirmed = inputs::confirm_action(
"Are you sure you want to delete it along with the vault account (and saved key)?".to_string() "Are you sure you want to delete it along with the saved key ?".to_string(),
)?; )?;
if !confirmed { if !confirmed {
return Ok(()); return Ok(());
...@@ -390,7 +392,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -390,7 +392,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
for derivation_to_delete in all_derivations_to_delete { for derivation_to_delete in all_derivations_to_delete {
let delete_result = derivation_to_delete.delete(&txn).await?; let delete_result = derivation_to_delete.delete(&txn).await?;
println!("Deleted {} derivation", delete_result.rows_affected); println!("Deleted {} address", delete_result.rows_affected);
} }
let delete_result = vault_account::Entity::delete_by_id(&address_to_delete) let delete_result = vault_account::Entity::delete_by_id(&address_to_delete)
...@@ -399,7 +401,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -399,7 +401,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
println!("Deleted {} vault account", delete_result.rows_affected); println!("Deleted {} vault account", delete_result.rows_affected);
} else { } else {
let delete_result = derivation.delete(&txn).await?; let delete_result = derivation.delete(&txn).await?;
println!("Deleted {} derivation", delete_result.rows_affected); println!("Deleted {} address", delete_result.rows_affected);
} }
txn.commit().await?; txn.commit().await?;
...@@ -550,7 +552,7 @@ where ...@@ -550,7 +552,7 @@ where
for derivation in derivations_ordered { for derivation in derivations_ordered {
if derivation.root_address != current_root_address { if derivation.root_address != current_root_address {
// First entry should be a root derivation // First entry when changing root address should be an account ("root" derivation)
if derivation.path.is_some() { if derivation.path.is_some() {
return Err(GcliError::Input( return Err(GcliError::Input(
"Order of derivations parameter is wrong".to_string(), "Order of derivations parameter is wrong".to_string(),
...@@ -571,13 +573,6 @@ where ...@@ -571,13 +573,6 @@ where
CryptoType::Sr25519Seed => Some(SecretFormat::Seed.into()), CryptoType::Sr25519Seed => Some(SecretFormat::Seed.into()),
CryptoType::Ed25519Seed => Some(SecretFormat::Cesium.into()), CryptoType::Ed25519Seed => Some(SecretFormat::Cesium.into()),
}; };
} else {
// Validate that the RootAddress is the same as current_root_address
if derivation.root_address != current_root_address {
return Err(GcliError::Input(
"Order of derivations parameter is wrong".to_string(),
));
}
} }
let address = if derivation.path.is_none() { let address = if derivation.path.is_none() {
......
...@@ -77,7 +77,7 @@ where ...@@ -77,7 +77,7 @@ where
Ok(match vault_account { Ok(match vault_account {
Some(vault_account) => { Some(vault_account) => {
println!("Already existing (root) vault account {vault_account}"); println!("Already existing vault account {vault_account}");
let overwrite_key = let overwrite_key =
inputs::confirm_action("Do you want to overwrite with the new encrypted key ?")?; inputs::confirm_action("Do you want to overwrite with the new encrypted key ?")?;
...@@ -85,7 +85,7 @@ where ...@@ -85,7 +85,7 @@ where
let mut vault_account: ActiveModel = vault_account.into(); let mut vault_account: ActiveModel = vault_account.into();
vault_account.encrypted_private_key = Set(encrypted_private_key); vault_account.encrypted_private_key = Set(encrypted_private_key);
let vault_account = vault_account.update(db).await?; let vault_account = vault_account.update(db).await?;
println!("Updated (root) vault account {vault_account}"); println!("Updated vault account {vault_account}");
vault_account vault_account
} else { } else {
......
...@@ -191,7 +191,7 @@ pub fn pair_from_cesium(id: String, pwd: String) -> nacl::sign::Keypair { ...@@ -191,7 +191,7 @@ pub fn pair_from_cesium(id: String, pwd: String) -> nacl::sign::Keypair {
fn seed_from_cesium(id: &str, pwd: &str) -> [u8; 32] { fn seed_from_cesium(id: &str, pwd: &str) -> [u8; 32] {
let params = scrypt::Params::new(12u8, 16u32, 1u32, 32).unwrap(); let params = scrypt::Params::new(12u8, 16u32, 1u32, 32).unwrap();
let mut seed = [0u8; 32]; let mut seed = [0u8; 32];
scrypt::scrypt(&pwd.as_bytes(), &id.as_bytes(), &params, &mut seed).unwrap(); scrypt::scrypt(pwd.as_bytes(), id.as_bytes(), &params, &mut seed).unwrap();
seed seed
} }
...@@ -582,7 +582,8 @@ mod tests { ...@@ -582,7 +582,8 @@ mod tests {
let cesium_id = "test_cesium_id".to_string(); let cesium_id = "test_cesium_id".to_string();
let cesium_pwd = "test_cesium_pwd".to_string(); let cesium_pwd = "test_cesium_pwd".to_string();
let expected_cesium_v1_ss58_address: String = "5ET2jhgJFoNQUpgfdSkdwftK8DKWdqZ1FKm5GKWdPfMWhPr4".to_string(); let expected_cesium_v1_ss58_address: String =
"5ET2jhgJFoNQUpgfdSkdwftK8DKWdqZ1FKm5GKWdPfMWhPr4".to_string();
let cesium_keypair = pair_from_cesium(cesium_id, cesium_pwd); let cesium_keypair = pair_from_cesium(cesium_id, cesium_pwd);
...@@ -597,7 +598,10 @@ mod tests { ...@@ -597,7 +598,10 @@ mod tests {
let cesium_v1_address: AccountId = cesium_keypair.pkey.into(); let cesium_v1_address: AccountId = cesium_keypair.pkey.into();
println!("SS58 Address: '{}'", cesium_v1_address); println!("SS58 Address: '{}'", cesium_v1_address);
assert_eq!(expected_cesium_v1_ss58_address, cesium_v1_address.to_string()); assert_eq!(
expected_cesium_v1_ss58_address,
cesium_v1_address.to_string()
);
//ed25519 seed **seems** to be the first 32 bytes of the secret key from nacl keypair //ed25519 seed **seems** to be the first 32 bytes of the secret key from nacl keypair
let mut seed: [u8; 32] = [0; 32]; let mut seed: [u8; 32] = [0; 32];
...@@ -692,7 +696,8 @@ mod tests { ...@@ -692,7 +696,8 @@ mod tests {
let cesium_id = "test_cesium_id".to_string(); let cesium_id = "test_cesium_id".to_string();
let cesium_pwd = "test_cesium_pwd".to_string(); let cesium_pwd = "test_cesium_pwd".to_string();
let expected_cesium_v1_ss58_address: String = "5ET2jhgJFoNQUpgfdSkdwftK8DKWdqZ1FKm5GKWdPfMWhPr4".to_string(); let expected_cesium_v1_ss58_address: String =
"5ET2jhgJFoNQUpgfdSkdwftK8DKWdqZ1FKm5GKWdPfMWhPr4".to_string();
// retrieving seed using scrypt // retrieving seed using scrypt
let seed = seed_from_cesium(&cesium_id, &cesium_pwd); let seed = seed_from_cesium(&cesium_id, &cesium_pwd);
...@@ -712,7 +717,10 @@ mod tests { ...@@ -712,7 +717,10 @@ mod tests {
"ed25519 keypair from seed : public SS58 Address:'{}'", "ed25519 keypair from seed : public SS58 Address:'{}'",
ed25519_address_from_seed ed25519_address_from_seed
); );
assert_eq!(expected_cesium_v1_ss58_address, ed25519_address_from_seed.to_string()); assert_eq!(
expected_cesium_v1_ss58_address,
ed25519_address_from_seed.to_string()
);
let root_suri = "0x".to_string() + &hex::encode(seed); let root_suri = "0x".to_string() + &hex::encode(seed);
let ed25519_pair_from_suri = let ed25519_pair_from_suri =
...@@ -722,7 +730,10 @@ mod tests { ...@@ -722,7 +730,10 @@ mod tests {
"ed25519 keypair from suri : public SS58 Address:'{}'", "ed25519 keypair from suri : public SS58 Address:'{}'",
ed25519_address_from_suri ed25519_address_from_suri
); );
assert_eq!(expected_cesium_v1_ss58_address, ed25519_address_from_suri.to_string()); assert_eq!(
expected_cesium_v1_ss58_address,
ed25519_address_from_suri.to_string()
);
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment