From b5366f2a220ff865910aeda19427318eace61a04 Mon Sep 17 00:00:00 2001
From: Feliciss <10203-feliciss@users.noreply.0xacab.org>
Date: Sun, 7 Jul 2024 13:39:58 +0900
Subject: [PATCH] [gh-2022] get account index from keys in keystore.

---
 crates/rooch-key/src/keystore/account_keystore.rs    | 3 +--
 crates/rooch-key/src/keystore/memory_keystore.rs     | 5 ++++-
 crates/rooch/src/commands/account/commands/create.rs | 5 ++++-
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/crates/rooch-key/src/keystore/account_keystore.rs b/crates/rooch-key/src/keystore/account_keystore.rs
index 89d0727996..53806e3c6b 100644
--- a/crates/rooch-key/src/keystore/account_keystore.rs
+++ b/crates/rooch-key/src/keystore/account_keystore.rs
@@ -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,
diff --git a/crates/rooch-key/src/keystore/memory_keystore.rs b/crates/rooch-key/src/keystore/memory_keystore.rs
index 1ed4c6ac1c..541eb1c8d9 100644
--- a/crates/rooch-key/src/keystore/memory_keystore.rs
+++ b/crates/rooch-key/src/keystore/memory_keystore.rs
@@ -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 }
diff --git a/crates/rooch/src/commands/account/commands/create.rs b/crates/rooch/src/commands/account/commands/create.rs
index 6df7b3a169..7e18eb70a1 100644
--- a/crates/rooch/src/commands/account/commands/create.rs
+++ b/crates/rooch/src/commands/account/commands/create.rs
@@ -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()))