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 e115f36
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
33 changes: 26 additions & 7 deletions crates/rooch-key/src/keystore/account_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,21 @@ pub trait AccountKeystore {
password: Option<String>,
) -> Result<GeneratedKeyPair, anyhow::Error> {
let derivation_path = generate_derivation_path(0)?;
let result =
generate_new_key_pair(mnemonic_phrase, derivation_path, word_length, password)?;
let result = generate_new_key_pair(
mnemonic_phrase,
derivation_path.clone(),
word_length,
password,
)?;
let new_address = result.address;
println!("addresses: {:?}", new_address);
println!("account_index: {:?}", 0);
println!("derivation_path: {:?}", derivation_path);
println!(
"private_key_encryption: {:?}",
result.key_pair_data.private_key_encryption
);
println!();
self.add_address_encryption_data(
new_address,
result.key_pair_data.private_key_encryption.clone(),
Expand All @@ -43,20 +55,27 @@ 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 mnemonic: MnemonicResult = self.get_mnemonic(password.clone())?;
let derivation_path = generate_derivation_path(account_index)?;

let result = generate_new_key_pair(
Some(mnemonic.mnemonic_phrase),
derivation_path,
Some(mnemonic.mnemonic_phrase.clone()),
derivation_path.clone(),
None,
password,
)?;
let new_address = result.address;
println!("addresses: {:?}", new_address);
println!("account_index: {:?}", account_index);
println!("derivation_path: {:?}", derivation_path);
println!(
"private_key_encryption: {:?}",
result.key_pair_data.private_key_encryption
);
println!();
self.add_address_encryption_data(
new_address,
result.key_pair_data.private_key_encryption.clone(),
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
3 changes: 2 additions & 1 deletion crates/rooch-rpc-client/src/wallet_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ impl WalletContext {

let client_config = client_config.persisted(&client_config_path);

let keystore_result = FileBasedKeystore::load(&client_config.keystore_path);
let keystore_result: std::result::Result<FileBasedKeystore, anyhow::Error> =
FileBasedKeystore::load(&client_config.keystore_path);
let keystore = match keystore_result {
Ok(file_keystore) => Keystore::File(file_keystore),
Err(error) => return Err(error),
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 e115f36

Please sign in to comment.