async def _one_password_config( self, # type: HummingbotApplication ): """ Special handler function to handle one password unlocking all secure conf variable and wallets - let a user creates a new password if there is no existing encrypted_files or key_files. - verify the entered password is valid by trying to unlock files. """ encrypted_files = list_encrypted_file_paths() wallets = list_wallets() password_valid = False err_msg = "Invalid password, please try again." if not encrypted_files and not wallets: password = await self.app.prompt( prompt="Enter your new password >>> ", is_password=True) re_password = await self.app.prompt( prompt="Please reenter your password >>> ", is_password=True) if password == re_password: password_valid = True else: err_msg = "Passwords entered do not match, please try again." else: password = await self.app.prompt(prompt="Enter your password >>> ", is_password=True) if encrypted_files: try: decrypt_file(encrypted_files[0], password) password_valid = True except ValueError as err: if str(err) != "MAC mismatch": raise err else: for wallet in wallets: try: unlock_wallet(public_key=wallet, password=password) password_valid = True break except ValueError as err: if str(err) != "MAC mismatch": raise err if password_valid: return password else: self._notify(err_msg) return await self._one_password_config()
def login(cls, password): encrypted_files = list_encrypted_file_paths() wallets = list_wallets() if encrypted_files: try: decrypt_file(encrypted_files[0], password) except ValueError as err: if str(err) == "MAC mismatch": return False raise err elif wallets: try: unlock_wallet(wallets[0], password) except ValueError as err: if str(err) == "MAC mismatch": return False raise err Security.password = password coro = AsyncCallScheduler.shared_instance().call_async(cls.decrypt_all, timeout_seconds=30) safe_ensure_future(coro) return True
def _list_all_encrypted(self, encrypted_files, password): self._notify("\nDecrypting configs...") for file_path in encrypted_files: key = get_encrypted_key_name_from_file(file_path) try: decrypted = decrypt_file(file_path, password) self._notify(f"{key}: {decrypted}") except ValueError as err: if str(err) == "MAC mismatch": self._notify( f"{key}: Error: This config was encrypted with a different password." ) else: raise err self._notify("All decryption done.")
def decrypt_file(cls, file_path): key_name = secure_config_key(file_path) cls._secure_configs[key_name] = decrypt_file(file_path, Security.password)