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 restore(cls, words: str, password: str, keystore_dir: Optional[Path] = None) -> 'Keystore': """Restore a keystore from seed words""" __mnemonic = Mnemonic(_MNEMONIC_LANGUAGE) __secret = bytes(__mnemonic.to_entropy(words)) path = Keystore.__save(secret=__secret, password=password, keystore_dir=keystore_dir) keystore = cls(keystore_path=path) return keystore
def test_decrypt_keystore(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)) assert keystore._Keystore__secret == secret # Decrypt from keystore file keystore_path = keystore.keystore_path del words del keystore keystore = Keystore(keystore_path=keystore_path) keystore.unlock(INSECURE_DEVELOPMENT_PASSWORD) assert keystore._Keystore__secret == secret
def generate( cls, password: str, keystore_dir: Optional[Path] = None, interactive: bool = True, ) -> Union['Keystore', Tuple['Keystore', str]]: """Generate a new nucypher keystore for use with characters""" mnemonic = Mnemonic(_MNEMONIC_LANGUAGE) __words = mnemonic.generate(strength=_ENTROPY_BITS) if interactive: cls._confirm_generate(__words) __secret = bytes(mnemonic.to_entropy(__words)) path = Keystore.__save(secret=__secret, password=password, keystore_dir=keystore_dir) keystore = cls(keystore_path=path) if interactive: return keystore return keystore, __words