Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli/alias] Add alias to keytool list #15168

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/sui-keys/src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, anyhow::Error>;

/// Check if an alias exists by its name
fn alias_exists(&self, alias: &str) -> bool {
self.alias_names().contains(&alias)
Expand Down Expand Up @@ -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<String, anyhow::Error> {
match self.aliases.get(address) {
Some(alias) => Ok(alias.alias.clone()),
Expand Down Expand Up @@ -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<String, anyhow::Error> {
match self.aliases.get(address) {
Some(alias) => Ok(alias.alias.clone()),
Expand Down
16 changes: 16 additions & 0 deletions crates/sui-keys/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
15 changes: 11 additions & 4 deletions crates/sui/src/keytool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ pub struct DecodedMultiSigOutput {
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Key {
alias: Option<String>,
sui_address: SuiAddress,
public_base64_key: String,
key_scheme: String,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -551,9 +553,12 @@ impl KeyToolCommand {
let keys = keystore
.keys()
.into_iter()
.map(Key::from)
.collect::<Vec<_>>();

.map(|pk| {
let mut key = Key::from(pk);
key.alias = keystore.get_alias_by_address(&key.sui_address).ok();
key
})
.collect();
CommandOutput::List(keys)
}

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -1046,7 +1052,8 @@ impl From<&SuiKeyPair> for Key {
impl From<PublicKey> for Key {
fn from(key: PublicKey) -> Self {
Key {
sui_address: Into::<SuiAddress>::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,
Expand Down
Loading