FuelLabs / forc-wallet

A forc plugin for managing Fuel wallets.
99 stars 55 forks source link

Add support for wallet import via private key #215

Open zees-dev opened 3 weeks ago

zees-dev commented 3 weeks ago

Context

Currently forc wallet import only supports importing wallet(s) via mnemonic. Private keys are commonly used to more easily transfer accounts across wallets; hence it should also be supported here.

Current behaviour of forc wallet import:

> forc wallet import

Please enter your mnemonic phrase:  

Proposed behaviour:

> forc wallet import

Please enter your mnemonic phrase or private key:

This solution retains the simplicity of the current flow while supporting importing accounts via both mnemonic and private-key - without introducing additional flags.

tusharnagar17 commented 3 weeks ago

Hi @zees-dev, Could you please assign this issue to me?

zees-dev commented 2 weeks ago

Hi @zees-dev, Could you please assign this issue to me?

This seems to be quite useful functionality; hence will be revisited in ~1-2 weeks for checking progress. Nevertheless thanks for picking this up! 👍

tusharnagar17 commented 1 week ago

Hi @zees-dev, I'm working on this functionality

While passing hardcoded private_key in eth_keystore::encrypt_key. It will save correctly without any error

 let hex_string = "bb01503b7133531412c7f0f7bc040d5c7ae2377dc772c2b020d010651f4a4f3a";
    let private_key_bytes = hex::decode(hex_string).expect("Invalid hex string");

    // Encrypt and write the wallet file.
    eth_keystore::encrypt_key(
        wallet_dir,
        &mut rand::thread_rng(),
        &private_key_bytes,
        password,
        Some(wallet_file_name),
    )
    .with_context(|| format!("failed to create keystore at {wallet_path:?}"))
    .map(|_| ())
}

but while accessing private-key or public-key

cargo run -- account 0 public-key

NOTE: this functionality working correctly with mnemonics:

Have any idea regarding this?

zees-dev commented 1 week ago

Hi @zees-dev, I'm working on this functionality

While passing hardcoded private_key in eth_keystore::encrypt_key. It will save correctly without any error

let hex_string = "bb01503b7133531412c7f0f7bc040d5c7ae2377dc772c2b020d010651f4a4f3a";
   let private_key_bytes = hex::decode(hex_string).expect("Invalid hex string");

   // Encrypt and write the wallet file.
   eth_keystore::encrypt_key(
       wallet_dir,
       &mut rand::thread_rng(),
       &private_key_bytes,
       password,
       Some(wallet_file_name),
   )
   .with_context(|| format!("failed to create keystore at {wallet_path:?}"))
   .map(|_| ())
}

but while accessing private-key or public-key

cargo run -- account 0 public-key
  • getting this error
error: invalid utf-8 sequence of 1 bytes from index 0

NOTE: this functionality working correctly with mnemonics:

Have any idea regarding this?

Unfortunately don't have an answer to this at the moment since this would most likely entail changes to other parts of the codebase.

When using mnemonic based account creation/import; a single mnemonic generates a master seed following BIP-39 - which can then be used to derive multiple child keys/accounts; this is the account index param you will see being used.

When using private key for importing; it's not designed to be used as a seed for generating other accounts/keys. A single private key generates a single account.

^ Due to this; the task is most likely not a good first issue and requires additional planning to retain a good UX.