Skip to content

Commit

Permalink
[CLI] fix command sui keytool import using --alias is useless whe…
Browse files Browse the repository at this point in the history
…n inputting mnemonic phrase (#17935)

## Description 

[CLI] fix command `sui keytool import` using `--alias` is useless when
inputting mnemonic phrase

<img width="1067" alt="2x"
src="https://github.com/MystenLabs/sui/assets/123343203/b3357aa8-d7de-4a4b-885c-944d16c215d8">

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [x] CLI: Fixed a bug where the provided alias in the `sui keytool
import` command was not kept when importing a key via mnemonic.
- [ ] Rust SDK:

---------

Co-authored-by: stefan-mysten <135084671+stefan-mysten@users.noreply.github.com>
Co-authored-by: howtosui <jasonrzx@gmail.com>
  • Loading branch information
3 people committed May 28, 2024
1 parent 1782fb0 commit 80b8d95
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
3 changes: 2 additions & 1 deletion crates/sui-keys/src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ pub trait AccountKeystore: Send + Sync {
phrase: &str,
key_scheme: SignatureScheme,
derivation_path: Option<DerivationPath>,
alias: Option<String>,
) -> Result<SuiAddress, anyhow::Error> {
let mnemonic = Mnemonic::from_phrase(phrase, Language::English)
.map_err(|e| anyhow::anyhow!("Invalid mnemonic phrase: {:?}", e))?;
let seed = Seed::new(&mnemonic, "");
match derive_key_pair_from_path(seed.as_bytes(), derivation_path, &key_scheme) {
Ok((address, kp)) => {
self.add_key(None, kp)?;
self.add_key(alias, kp)?;
Ok(address)
}
Err(e) => Err(anyhow!("error getting keypair {:?}", e)),
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-keys/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn mnemonic_test() {
let keystore_path_2 = temp_dir.path().join("sui2.keystore");
let mut keystore2 = Keystore::from(FileBasedKeystore::new(&keystore_path_2).unwrap());
let imported_address = keystore2
.import_from_mnemonic(&phrase, SignatureScheme::ED25519, None)
.import_from_mnemonic(&phrase, SignatureScheme::ED25519, None, None)
.unwrap();
assert_eq!(scheme.flag(), Ed25519SuiSignature::SCHEME.flag());
assert_eq!(address, imported_address);
Expand All @@ -222,7 +222,7 @@ fn sui_wallet_address_mnemonic_test() -> Result<(), anyhow::Error> {
let mut keystore = Keystore::from(FileBasedKeystore::new(&keystore_path).unwrap());

keystore
.import_from_mnemonic(phrase, SignatureScheme::ED25519, None)
.import_from_mnemonic(phrase, SignatureScheme::ED25519, None, None)
.unwrap();

let pubkey = keystore.keys()[0].clone();
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-sdk/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn mnemonic_test() {
let keystore_path_2 = temp_dir.path().join("sui2.keystore");
let mut keystore2 = Keystore::from(FileBasedKeystore::new(&keystore_path_2).unwrap());
let imported_address = keystore2
.import_from_mnemonic(&phrase, SignatureScheme::ED25519, None)
.import_from_mnemonic(&phrase, SignatureScheme::ED25519, None, None)
.unwrap();
assert_eq!(scheme.flag(), Ed25519SuiSignature::SCHEME.flag());
assert_eq!(address, imported_address);
Expand Down
1 change: 1 addition & 0 deletions crates/sui/src/keytool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ impl KeyToolCommand {
&input_string,
key_scheme,
derivation_path,
alias,
)?;
let skp = keystore.get_key(&sui_address)?;
let key = Key::from(skp);
Expand Down

0 comments on commit 80b8d95

Please sign in to comment.