Skip to content

Commit

Permalink
[gh-2022] get account index from keys in keystore.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Jul 7, 2024
1 parent 276fb81 commit b5366f2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 1 addition & 2 deletions crates/rooch-key/src/keystore/account_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ pub trait AccountKeystore {

fn generate_and_add_new_key(
&mut self,
account_index: u32,
password: Option<String>,
) -> Result<GeneratedKeyPair, anyhow::Error> {
// load mnemonic phrase from keystore
let mnemonic = self.get_mnemonic(password.clone())?;
let account_index = mnemonic.mnemonic_data.addresses.len() as u32;
let derivation_path = generate_derivation_path(account_index)?;

let result = generate_new_key_pair(
Some(mnemonic.mnemonic_phrase),
derivation_path,
Expand Down
5 changes: 4 additions & 1 deletion crates/rooch-key/src/keystore/memory_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,11 @@ impl InMemKeystore {
pub fn new_insecure_for_tests(initial_key_number: usize) -> Self {
let mut keystore = BaseKeyStore::new();
keystore.init_keystore(None, None, None).unwrap();
let account_index = keystore.addresses().len() as u32;
for _ in 0..initial_key_number {
keystore.generate_and_add_new_key(None).unwrap();
keystore
.generate_and_add_new_key(account_index, None)
.unwrap();
}

Self { keystore }
Expand Down
5 changes: 4 additions & 1 deletion crates/rooch/src/commands/account/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ impl CommandAction<Option<RoochAddressView>> for CreateCommand {
async fn execute(self) -> RoochResult<Option<RoochAddressView>> {
let mut context = self.context_options.build_require_password()?;
let password = context.get_password();
let result = context.keystore.generate_and_add_new_key(password)?;
let account_index = context.keystore.addresses().len() as u32;
let result = context
.keystore
.generate_and_add_new_key(account_index, password)?;

if self.json {
Ok(Some(result.address.into()))
Expand Down

0 comments on commit b5366f2

Please sign in to comment.