def test_restore_keystore_from_mnemonic(tmpdir, mocker): # Setup spy = mocker.spy(Mnemonic, 'generate') # Decrypt post-generation keystore = Keystore.generate(INSECURE_DEVELOPMENT_PASSWORD, keystore_dir=tmpdir) keystore.unlock(password=INSECURE_DEVELOPMENT_PASSWORD) mnemonic = Mnemonic(_MNEMONIC_LANGUAGE) words = spy.spy_return secret = bytes(mnemonic.to_entropy(words)) keystore_path = keystore.keystore_path # remove local and disk references, simulating a # lost keystore or forgotten password. del keystore os.unlink(keystore_path) # prove the keystore is lost or missing assert not keystore_path.exists() with pytest.raises(Keystore.NotFound): _keystore = Keystore(keystore_path=keystore_path) # Restore with user-supplied words and a new password keystore = Keystore.restore(words=words, password='******') keystore.unlock(password='******') assert keystore._Keystore__secret == secret
def recover_keystore(emitter) -> None: emitter.message('This procedure will recover your nucypher keystore from mnemonic seed words. ' 'You will need to provide the entire mnemonic (space seperated) in the correct ' 'order and choose a new password.', color='cyan') click.confirm('Do you want to continue', abort=True) __words = click.prompt("Enter nucypher keystore seed words") word_count = len(__words.split()) if word_count != _WORD_COUNT: emitter.message(f'Invalid mnemonic - Number of words must be {str(_WORD_COUNT)}, but only got {word_count}') __password = get_nucypher_password(emitter=emitter, confirm=True) keystore = Keystore.restore(words=__words, password=__password) emitter.message(f'Recovered nucypher keystore {keystore.id} to \n {keystore.keystore_path}', color='green')