-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename gorc keys {cosmos,eth} import to recover; add gorc keys eth im…
…port (#82)
- Loading branch information
Showing
9 changed files
with
150 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use super::show::ShowEthKeyCmd; | ||
use crate::application::APP; | ||
use abscissa_core::{Application, Command, Options, Runnable}; | ||
use k256::pkcs8::ToPrivateKey; | ||
use signatory::FsKeyStore; | ||
use std::path; | ||
|
||
#[derive(Command, Debug, Default, Options)] | ||
pub struct RecoverEthKeyCmd { | ||
#[options(free, help = "recover [name] (bip39-mnemonic)")] | ||
pub args: Vec<String>, | ||
|
||
#[options(help = "overwrite existing key")] | ||
pub overwrite: bool, | ||
|
||
#[options(help = "show private key")] | ||
show_private_key: bool, | ||
} | ||
|
||
// Entry point for `gorc keys eth recover [name] (bip39-mnemonic)` | ||
// - [name] required; key name | ||
// - (bip39-mnemonic) optional; when absent the user will be prompted to enter it | ||
impl Runnable for RecoverEthKeyCmd { | ||
fn run(&self) { | ||
let config = APP.config(); | ||
let keystore = path::Path::new(&config.keystore); | ||
let keystore = | ||
FsKeyStore::create_or_open(keystore).expect("Could not open keystore"); | ||
|
||
let name = self.args.get(0).expect("name is required"); | ||
let name = name.parse().expect("Could not parse name"); | ||
if let Ok(_info) = keystore.info(&name) { | ||
if !self.overwrite { | ||
eprintln!("Key already exists, exiting."); | ||
return; | ||
} | ||
} | ||
|
||
let mnemonic = match self.args.get(1) { | ||
Some(mnemonic) => mnemonic.clone(), | ||
None => rpassword::read_password_from_tty(Some( | ||
"> Enter your bip39-mnemonic:\n", | ||
)) | ||
.expect("Could not read mnemonic"), | ||
}; | ||
|
||
let mnemonic = bip32::Mnemonic::new(mnemonic.trim(), Default::default()) | ||
.expect("Could not parse mnemonic"); | ||
|
||
let seed = mnemonic.to_seed(""); | ||
|
||
let path = config.ethereum.key_derivation_path.trim(); | ||
let path = path | ||
.parse::<bip32::DerivationPath>() | ||
.expect("Could not parse derivation path"); | ||
|
||
let key = bip32::XPrv::derive_from_path(seed, &path).expect("Could not derive key"); | ||
let key = k256::SecretKey::from(key.private_key()); | ||
let key = key | ||
.to_pkcs8_der() | ||
.expect("Could not PKCS8 encod private key"); | ||
|
||
keystore.store(&name, &key).expect("Could not store key"); | ||
|
||
let show_cmd = ShowEthKeyCmd { | ||
args: vec![name.to_string()], | ||
show_private_key: self.show_private_key, | ||
show_name: false, | ||
}; | ||
show_cmd.run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters