Mnemonic key recovery (#1579)

## Issue Addressed

N/A

## Proposed Changes

Add a  `lighthouse am wallet recover` command that recreates a wallet from a mnemonic but no validator keys.  Add a `lighthouse am validator recover` command which would directly create keys from a mnemonic for a given index and count.

## Additional Info


Co-authored-by: Paul Hauner <paul@paulhauner.com>
This commit is contained in:
realbigsean
2020-09-08 12:17:51 +00:00
parent 00cdc4bb35
commit 9cf8f45192
11 changed files with 443 additions and 36 deletions

View File

@@ -6,6 +6,6 @@ pub mod json_wallet;
pub use bip39;
pub use validator_path::{KeyType, ValidatorPath, COIN_TYPE, PURPOSE};
pub use wallet::{
recover_validator_secret, DerivedKey, Error, KeystoreError, PlainText, Uuid,
ValidatorKeystores, Wallet, WalletBuilder,
recover_validator_secret, recover_validator_secret_from_mnemonic, DerivedKey, Error,
KeystoreError, PlainText, Uuid, ValidatorKeystores, Wallet, WalletBuilder,
};

View File

@@ -285,3 +285,19 @@ pub fn recover_validator_secret(
Ok((destination.secret().to_vec().into(), path))
}
/// Returns `(secret, path)` for the `key_type` for the validator at `index`.
///
/// This function should only be used for key recovery since it can easily lead to key duplication.
pub fn recover_validator_secret_from_mnemonic(
secret: &[u8],
index: u32,
key_type: KeyType,
) -> Result<(PlainText, ValidatorPath), Error> {
let path = ValidatorPath::new(index, key_type);
let master = DerivedKey::from_seed(secret).map_err(|()| Error::EmptyPassword)?;
let destination = path.iter_nodes().fold(master, |dk, i| dk.child(*i));
Ok((destination.secret().to_vec().into(), path))
}