diff --git a/crates/sui-keys/src/keystore.rs b/crates/sui-keys/src/keystore.rs index 930742ec8c02fd..14584adad9b3d0 100644 --- a/crates/sui-keys/src/keystore.rs +++ b/crates/sui-keys/src/keystore.rs @@ -55,8 +55,8 @@ pub trait AccountKeystore: Send + Sync { .map(|a| a.alias.as_str()) .collect() } + /// Get alias of address fn get_alias_by_address(&self, address: &SuiAddress) -> Result; - /// Check if an alias exists by its name fn alias_exists(&self, alias: &str) -> bool { self.alias_names().contains(&alias) @@ -225,7 +225,7 @@ impl AccountKeystore for FileBasedKeystore { } } - /// Get the alias if it exists, or return an error if it does not exist. + /// Get alias of address fn get_alias_by_address(&self, address: &SuiAddress) -> Result { match self.aliases.get(address) { Some(alias) => Ok(alias.alias.clone()), @@ -455,7 +455,7 @@ impl AccountKeystore for InMemKeystore { } } - /// Get an alias by its address + /// Get alias of address fn get_alias_by_address(&self, address: &SuiAddress) -> Result { match self.aliases.get(address) { Some(alias) => Ok(alias.alias.clone()), diff --git a/crates/sui-keys/tests/tests.rs b/crates/sui-keys/tests/tests.rs index daf4111f3822f0..815498712f652d 100644 --- a/crates/sui-keys/tests/tests.rs +++ b/crates/sui-keys/tests/tests.rs @@ -174,3 +174,19 @@ fn keystore_display_test() -> Result<(), anyhow::Error> { assert!(!keystore.to_string().contains("keys:")); Ok(()) } + +#[test] +fn get_alias_by_address_test() { + let temp_dir = TempDir::new().unwrap(); + let keystore_path = temp_dir.path().join("sui.keystore"); + let mut keystore = Keystore::from(FileBasedKeystore::new(&keystore_path).unwrap()); + let alias = "my_alias_test".to_string(); + let keypair = keystore + .generate_and_add_new_key(SignatureScheme::ED25519, Some(alias.clone()), None, None) + .unwrap(); + assert_eq!(alias, keystore.get_alias_by_address(&keypair.0).unwrap()); + + // Test getting an alias of an address that is not in keystore + let address = generate_new_key(SignatureScheme::ED25519, None, None).unwrap(); + assert!(keystore.get_alias_by_address(&address.0).is_err()) +} diff --git a/crates/sui/src/keytool.rs b/crates/sui/src/keytool.rs index b6de2c7c5fad41..156236fc6bab20 100644 --- a/crates/sui/src/keytool.rs +++ b/crates/sui/src/keytool.rs @@ -278,6 +278,7 @@ pub struct DecodedMultiSigOutput { #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct Key { + alias: Option, sui_address: SuiAddress, public_base64_key: String, key_scheme: String, @@ -487,6 +488,7 @@ impl KeyToolCommand { let file_name = format!("bls-{sui_address}.key"); write_authority_keypair_to_file(&kp, file_name)?; CommandOutput::Generate(Key { + alias: None, sui_address, public_base64_key: kp.public().encode_base64(), key_scheme: key_scheme.to_string(), @@ -551,9 +553,12 @@ impl KeyToolCommand { let keys = keystore .keys() .into_iter() - .map(Key::from) - .collect::>(); - + .map(|pk| { + let mut key = Key::from(pk); + key.alias = keystore.get_alias_by_address(&key.sui_address).ok(); + key + }) + .collect(); CommandOutput::List(keys) } @@ -667,6 +672,7 @@ impl KeyToolCommand { Ok(keypair) => { let public_base64_key = keypair.public().encode_base64(); CommandOutput::Show(Key { + alias: None, // alias does not get stored in key files sui_address: (keypair.public()).into(), public_base64_key, key_scheme: SignatureScheme::BLS12381.to_string(), @@ -1046,7 +1052,8 @@ impl From<&SuiKeyPair> for Key { impl From for Key { fn from(key: PublicKey) -> Self { Key { - sui_address: Into::::into(&key), + alias: None, // this is retrieved later + sui_address: SuiAddress::from(&key), public_base64_key: key.encode_base64(), key_scheme: key.scheme().to_string(), mnemonic: None,