diff --git a/crates/sui-keys/src/keystore.rs b/crates/sui-keys/src/keystore.rs index b92332721fecd..6ed25919d455f 100644 --- a/crates/sui-keys/src/keystore.rs +++ b/crates/sui-keys/src/keystore.rs @@ -47,7 +47,7 @@ pub trait AccountKeystore: Send + Sync { fn addresses(&self) -> Vec { self.keys().iter().map(|k| k.into()).collect() } - + fn addresses_with_alias(&self) -> Vec<(&SuiAddress, &Alias)>; fn aliases(&self) -> Vec<&Alias>; fn alias_names(&self) -> Vec<&str> { self.aliases() @@ -198,6 +198,10 @@ impl AccountKeystore for FileBasedKeystore { self.aliases.values().collect() } + fn addresses_with_alias(&self) -> Vec<(&SuiAddress, &Alias)> { + self.aliases.iter().collect::>() + } + fn keys(&self) -> Vec { self.keys.values().map(|key| key.public()).collect() } @@ -281,8 +285,8 @@ impl FileBasedKeystore { aliases .into_iter() .map(|alias| { - let key = SuiKeyPair::decode_base64(&alias.public_key_base64); - key.map(|k| (Into::::into(&k.public()), alias)) + let key = PublicKey::decode_base64(&alias.public_key_base64); + key.map(|k| (Into::::into(&k), alias)) }) .collect::, _>>() .map_err(|e| { @@ -301,6 +305,7 @@ impl FileBasedKeystore { .zip(names) .map(|((sui_address, skp), alias)| { let public_key_base64 = EncodeDecodeBase64::encode_base64(&skp.public()); + ( *sui_address, Alias { @@ -435,6 +440,10 @@ impl AccountKeystore for InMemKeystore { self.aliases.values().collect() } + fn addresses_with_alias(&self) -> Vec<(&SuiAddress, &Alias)> { + self.aliases.iter().collect::>() + } + fn keys(&self) -> Vec { self.keys.values().map(|key| key.public()).collect() } diff --git a/crates/sui-keys/tests/tests.rs b/crates/sui-keys/tests/tests.rs index dc5c2961f615e..daf4111f3822f 100644 --- a/crates/sui-keys/tests/tests.rs +++ b/crates/sui-keys/tests/tests.rs @@ -55,6 +55,26 @@ fn create_alias_keystore_file_test() { assert!(keystore_path.exists()); } +#[test] +fn check_reading_aliases_file_correctly() { + // when reading the alias file containing alias + public key base 64, + // make sure the addresses are correctly converted back from pk + + let temp_dir = TempDir::new().unwrap(); + let mut keystore_path = temp_dir.path().join("sui.keystore"); + let keystore_path_keep = temp_dir.path().join("sui.keystore"); + let mut keystore = Keystore::from(FileBasedKeystore::new(&keystore_path).unwrap()); + let kp = keystore + .generate_and_add_new_key(SignatureScheme::ED25519, None, None, None) + .unwrap(); + keystore_path.set_extension("aliases"); + assert!(keystore_path.exists()); + + let new_keystore = Keystore::from(FileBasedKeystore::new(&keystore_path_keep).unwrap()); + let addresses = new_keystore.addresses_with_alias(); + assert_eq!(kp.0, *addresses.get(0).unwrap().0) +} + #[test] fn create_alias_if_not_exists_test() { let temp_dir = TempDir::new().unwrap();