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

Renamed vault_account CryptoType enum values and encrypted_private_key field into encrypted_suri

parent 4eafec2a
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
...@@ -274,7 +274,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -274,7 +274,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
root_derivation.address root_derivation.address
)))?; )))?;
if vault_account.crypto_type == CryptoType::Ed25519Seed { if vault_account.crypto_type == CryptoType::G1v1Seed {
println!( println!(
"Only \"{}\" and \"{}\" format are supported for derivations", "Only \"{}\" and \"{}\" format are supported for derivations",
Into::<&str>::into(SecretFormat::Substrate), Into::<&str>::into(SecretFormat::Substrate),
...@@ -487,10 +487,10 @@ fn parse_prefix_and_derivation_path_from_string( ...@@ -487,10 +487,10 @@ fn parse_prefix_and_derivation_path_from_string(
fn map_secret_format_to_crypto_type(secret_format: SecretFormat) -> CryptoType { fn map_secret_format_to_crypto_type(secret_format: SecretFormat) -> CryptoType {
match secret_format { match secret_format {
SecretFormat::Seed => vault_account::CryptoType::Sr25519Seed, SecretFormat::Seed => vault_account::CryptoType::EntropyKdfSeed,
SecretFormat::Substrate => vault_account::CryptoType::Sr25519Mnemonic, SecretFormat::Substrate => vault_account::CryptoType::Bip39Mnemonic,
SecretFormat::Predefined => vault_account::CryptoType::Sr25519Mnemonic, SecretFormat::Predefined => vault_account::CryptoType::Bip39Mnemonic,
SecretFormat::Cesium => vault_account::CryptoType::Ed25519Seed, SecretFormat::Cesium => vault_account::CryptoType::G1v1Seed,
} }
} }
...@@ -565,9 +565,9 @@ where ...@@ -565,9 +565,9 @@ where
)))?; )))?;
current_vault_format = match vault_account.crypto_type { current_vault_format = match vault_account.crypto_type {
CryptoType::Sr25519Mnemonic => Some(SecretFormat::Substrate.into()), CryptoType::Bip39Mnemonic => Some(SecretFormat::Substrate.into()),
CryptoType::Sr25519Seed => Some(SecretFormat::Seed.into()), CryptoType::EntropyKdfSeed => Some(SecretFormat::Seed.into()),
CryptoType::Ed25519Seed => Some(SecretFormat::Cesium.into()), CryptoType::G1v1Seed => Some(SecretFormat::Cesium.into()),
}; };
} }
...@@ -752,14 +752,14 @@ where ...@@ -752,14 +752,14 @@ where
))); )));
} }
let encrypted_private_key = let encrypted_suri =
encrypt(root_secret_suri.as_bytes(), password.to_string()).map_err(|e| anyhow!(e))?; encrypt(root_secret_suri.as_bytes(), password.to_string()).map_err(|e| anyhow!(e))?;
let _root_account = vault_account::create_vault_account( let _root_account = vault_account::create_vault_account(
db, db,
&root_address, &root_address,
map_secret_format_to_crypto_type(secret_format), map_secret_format_to_crypto_type(secret_format),
encrypted_private_key, encrypted_suri,
) )
.await?; .await?;
...@@ -914,7 +914,7 @@ pub fn retrieve_suri_from_vault_account( ...@@ -914,7 +914,7 @@ pub fn retrieve_suri_from_vault_account(
) -> Result<String, GcliError> { ) -> Result<String, GcliError> {
let password = inputs::prompt_password()?; let password = inputs::prompt_password()?;
let cypher = &vault_account.encrypted_private_key; let cypher = &vault_account.encrypted_suri;
let secret_vec = let secret_vec =
decrypt(cypher, password.clone()).map_err(|e| GcliError::Input(e.to_string()))?; decrypt(cypher, password.clone()).map_err(|e| GcliError::Input(e.to_string()))?;
let secret_suri = String::from_utf8(secret_vec).map_err(|e| anyhow!(e))?; let secret_suri = String::from_utf8(secret_vec).map_err(|e| anyhow!(e))?;
...@@ -924,10 +924,10 @@ pub fn retrieve_suri_from_vault_account( ...@@ -924,10 +924,10 @@ pub fn retrieve_suri_from_vault_account(
pub fn compute_keypair(crypto_type: CryptoType, secret_suri: &str) -> Result<KeyPair, GcliError> { pub fn compute_keypair(crypto_type: CryptoType, secret_suri: &str) -> Result<KeyPair, GcliError> {
let key_pair = match crypto_type { let key_pair = match crypto_type {
CryptoType::Sr25519Mnemonic | CryptoType::Sr25519Seed => { CryptoType::Bip39Mnemonic | CryptoType::EntropyKdfSeed => {
pair_from_sr25519_str(secret_suri)?.into() pair_from_sr25519_str(secret_suri)?.into()
} }
CryptoType::Ed25519Seed => pair_from_ed25519_str(secret_suri)?.into(), CryptoType::G1v1Seed => pair_from_ed25519_str(secret_suri)?.into(),
}; };
Ok(key_pair) Ok(key_pair)
} }
......
...@@ -17,7 +17,7 @@ pub struct Model { ...@@ -17,7 +17,7 @@ pub struct Model {
#[sea_orm(primary_key, auto_increment = false)] #[sea_orm(primary_key, auto_increment = false)]
pub address: String, pub address: String,
pub crypto_type: CryptoType, pub crypto_type: CryptoType,
pub encrypted_private_key: Vec<u8>, pub encrypted_suri: Vec<u8>,
} }
impl Display for Model { impl Display for Model {
...@@ -33,12 +33,12 @@ impl Display for Model { ...@@ -33,12 +33,12 @@ impl Display for Model {
rename_all = "PascalCase" rename_all = "PascalCase"
)] )]
pub enum CryptoType { pub enum CryptoType {
/// The secret key or BIP39 mnemonic /// The BIP39 mnemonic phrase (?12 words) (SR25519)
Sr25519Mnemonic, Bip39Mnemonic,
/// The 32B SR25519 seed without "0x" prefix /// The 32B hexadecimal seed with "0x" prefix (64+2 characters when unencrypted) (SR25519)
Sr25519Seed, EntropyKdfSeed,
/// The 32B ED25519 seed without "0x" prefix (for cesium) /// The 32B hexadecimal seed with "0x" prefix for cesium v1 (64+2 characters when unencrypted) (ED25519)
Ed25519Seed, G1v1Seed,
} }
#[derive(Copy, Clone, Debug, EnumIter)] #[derive(Copy, Clone, Debug, EnumIter)]
...@@ -68,7 +68,7 @@ pub async fn create_vault_account<C>( ...@@ -68,7 +68,7 @@ pub async fn create_vault_account<C>(
db: &C, db: &C,
address: &str, address: &str,
crypto_type: CryptoType, crypto_type: CryptoType,
encrypted_private_key: Vec<u8>, encrypted_suri: Vec<u8>,
) -> Result<Model, GcliError> ) -> Result<Model, GcliError>
where where
C: ConnectionTrait, C: ConnectionTrait,
...@@ -83,7 +83,7 @@ where ...@@ -83,7 +83,7 @@ where
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 ?")?;
if overwrite_key { if overwrite_key {
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_suri = Set(encrypted_suri);
let vault_account = vault_account.update(db).await?; let vault_account = vault_account.update(db).await?;
println!("Updated vault account {vault_account}"); println!("Updated vault account {vault_account}");
...@@ -96,7 +96,7 @@ where ...@@ -96,7 +96,7 @@ where
let vault_account = ActiveModel { let vault_account = ActiveModel {
address: Set(address.to_owned()), address: Set(address.to_owned()),
crypto_type: Set(crypto_type), crypto_type: Set(crypto_type),
encrypted_private_key: Set(encrypted_private_key), encrypted_suri: Set(encrypted_suri),
}; };
let vault_account = vault_account.insert(db).await?; let vault_account = vault_account.insert(db).await?;
println!("Created vault account {}", vault_account); println!("Created vault account {}", vault_account);
......
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