def run_action_account_create(): passphrase = single_question_prompt({ "type": "password", "message": Messages.input_passphrase }) Account.create(passphrase) return main_prompt()
def _run_unlock(self, **kw): passphrase = kw.get("passphrase") keystore_file_path = kw.get("keystore_file_path") account = Account(keystore_file_path) if account.check_passphrase(passphrase): set_passphrase(passphrase) self._send_redirect(kw.get("return_to")) else: self._send_error_message("Incorrect passphrase, try again.")
def load(cls, file_path: Path): file_name, _ = os.path.splitext(os.path.basename(file_path)) _, _, settings_name = file_name.split("-") try: settings = load_settings(settings_name) except FileNotFoundError as exc: raise ValueError( f"There are no Wizard settings {settings_name} for Raiden configuration {file_path}" ) with file_path.open() as config_file: data = toml.load(config_file) keystore_file_path = Account.find_keystore_file_path( to_canonical_address(data["address"]), Path(data["keystore-path"])) if keystore_file_path is None: raise ValueError( f"{data['keystore-path']} does not contain the account file for config {file_path}" ) return cls( account_filename=keystore_file_path, ethereum_client_rpc_endpoint=data["eth-rpc-endpoint"], settings=settings, routing_mode=data["routing-mode"], enable_monitoring=data["enable-monitoring"], _initial_funding_txhash=data.get("_initial_funding_txhash"), )
def prompt_account_selection(validate_passphrase=True): account = single_question_prompt({ "type": "list", "message": Messages.input_account_select, "choices": [{ "name": account.address, "value": account } for account in Account.get_user_accounts()], }) if validate_passphrase: validated = False while not validated: try: account.unlock( single_question_prompt({ "name": "passphrase", "type": "password", "message": Messages.input_account_verify_passphrase, })) validated = True except ValueError: pass return account
def _run_setup(self, **kw): account_file = kw.get("account_file") account = Account(account_file, passphrase=get_passphrase()) form = QuickSetupForm( meta=dict(network=self.installer_settings.network), endpoint=kw.get("endpoint")) if form.validate(): self._send_status_update( "Generating new wallet and configuration file for raiden") network = Network.get_by_name(self.installer_settings.network) infura_url_or_id = form.data["endpoint"].strip() ethereum_rpc_provider = Infura.make(network, infura_url_or_id) try: check_eth_node_responsivity(ethereum_rpc_provider.url) except ValueError as e: self._send_error_message(f"Ethereum node unavailable: {e}.") return conf_file = RaidenConfigurationFile( account.keystore_file_path, self.installer_settings, ethereum_rpc_provider.url, routing_mode=self.installer_settings.routing_mode, enable_monitoring=self.installer_settings.monitoring_enabled, ) conf_file.save() self._send_redirect( self.reverse_url("account", conf_file.file_name)) else: self._send_error_message( f"Failed to create account. Error: {form.errors}")
def run_action_account_list(): print("\nAvailable accounts:\n") for account in Account.get_user_accounts(): print("\t", account.keystore_file_path, account.address) print("\n") return main_prompt()
def __init__(self, account_filename: str, network: Network, ethereum_client_rpc_endpoint: str, **kw): if 'passphrase' in kw: self.account = Account(account_filename, passphrase=kw.get('passphrase')) else: self.account = Account(account_filename) self.account_filename = account_filename self.network = network self.settings = network_settings[network.name] self.ethereum_client_rpc_endpoint = ethereum_client_rpc_endpoint self.accept_disclaimer = kw.get("accept_disclaimer", True) self.enable_monitoring = kw.get("enable_monitoring", self.settings.monitoring_enabled) self.routing_mode = kw.get("routing_mode", self.settings.routing_mode) self.services_version = self.settings.services_version self._initial_funding_txhash = kw.get("_initial_funding_txhash")
def setUp(self): assert INFURA_PROJECT_ID self.account = Account.create(TESTING_KEYSTORE_FOLDER, "test_raiden_integration") self.network = Network.get_by_name(self.__class__.NETWORK_NAME) self.infura = Infura.make(self.network, INFURA_PROJECT_ID) self.w3 = make_web3_provider(self.infura.url, self.account)
def setUp(self): assert INFURA_PROJECT_ID Account.DEFAULT_KEYSTORE_FOLDER = Path(tempfile.gettempdir()) self.account = Account.create("test_raiden_integration") self.network = Network.get_by_name(self.__class__.NETWORK_NAME) self.infura = Infura.make(self.network, INFURA_PROJECT_ID) self.w3 = make_web3_provider(self.infura.url, self.account)
def load(cls, file_path: Path): file_name, _ = os.path.splitext(os.path.basename(file_path)) _, _, network_name = file_name.split("-") with file_path.open() as config_file: data = toml.load(config_file) passphrase = PassphraseFile(Path(data["password-file"])).retrieve() keystore_file_path = Account.find_keystore_file_path( data["address"], Path(data["keystore-path"]) ) account = Account(keystore_file_path, passphrase) return cls( account=account, ethereum_client_rpc_endpoint=data["eth-rpc-endpoint"], network=Network.get_by_name(network_name), routing_mode=data["routing-mode"], enable_monitoring=data["enable-monitoring"], )
def _run_create_wallet(self, **kw): form = PasswordForm(passphrase1=kw.get("passphrase1"), passphrase2=kw.get("passphrase2")) if form.validate(): self._send_status_update("Generating new wallet file for Raiden") passphrase = form.data["passphrase1"].strip() set_passphrase(passphrase) account = Account.create(find_keystore_folder_path(), passphrase) self._send_redirect( self.reverse_url("setup", account.keystore_file_path))
class LockedAccountTestCase(AccountBaseTestCase): def setUp(self): super().setUp() keystore_file_path = self.account.keystore_file_path self.locked_account = Account(keystore_file_path) def test_cannot_get_private_key_without_passphrase(self): with self.assertRaises(ValueError): self.locked_account.private_key def test_can_unlock_private_key(self): self.locked_account.unlock(self.passphrase) try: self.locked_account.private_key except ValueError: self.fail("should have unlocked private key") def test_cannot_unlock_with_wrong_password(self): with self.assertRaises(ValueError): self.locked_account.unlock("wrong" + self.passphrase)
def __init__(self, account_filename: Union[Path, str], settings: Settings, ethereum_client_rpc_endpoint: str, **kw): self.account = Account(account_filename) self.account_filename = account_filename self.settings = settings self.network = Network.get_by_name(self.settings.network) self.ethereum_client_rpc_endpoint = ethereum_client_rpc_endpoint self.accept_disclaimer = kw.get("accept_disclaimer", True) self.enable_monitoring = kw.get("enable_monitoring", self.settings.monitoring_enabled) self.routing_mode = kw.get("routing_mode", self.settings.routing_mode) self.services_version = self.settings.services_version self._initial_funding_txhash = kw.get("_initial_funding_txhash")
def setUp(self): RaidenConfigurationFile.FOLDER_PATH = TESTING_TEMP_FOLDER.joinpath( "config") self.account = Account.create(TESTING_KEYSTORE_FOLDER, passphrase="test_raiden_config") self.network = Network.get_by_name("goerli") self.settings = load_settings("demo_env") self.configuration_file = RaidenConfigurationFile( self.account.keystore_file_path, self.settings, "http://localhost:8545", )
def setUp(self): temp_folder_path = Path(tempfile.gettempdir()) RaidenConfigurationFile.FOLDER_PATH = temp_folder_path self.account = Account.create(passphrase="test_raiden_config") self.network = Network.get_by_name("goerli") self.ethereum_client_rpc_endpoint = "http://localhost:8545" self.configuration_file = RaidenConfigurationFile( account=self.account, network=self.network, ethereum_client_rpc_endpoint=self.ethereum_client_rpc_endpoint, ) passphrase_file = PassphraseFile(self.configuration_file.passphrase_file_path) passphrase_file.store(self.account.passphrase)
def load(cls, file_path: Path): file_name, _ = os.path.splitext(os.path.basename(file_path)) _, _, network_name = file_name.split("-") with file_path.open() as config_file: data = toml.load(config_file) keystore_file_path = Account.find_keystore_file_path( data["address"], Path(data["keystore-path"])) return cls( account_filename=keystore_file_path, ethereum_client_rpc_endpoint=data["eth-rpc-endpoint"], network=Network.get_by_name(network_name), routing_mode=data["routing-mode"], enable_monitoring=data["enable-monitoring"], _initial_funding_txhash=data.get("_initial_funding_txhash"), )
def main_prompt(): configuration_choices = [Messages.action_configuration_setup] account_choices = [Messages.action_account_create] raiden_release_management_choices = [Messages.action_release_manager] if RaidenConfigurationFile.get_available_configurations(): configuration_choices.insert(0, Messages.action_launch_raiden) configuration_choices.append(Messages.action_configuration_list) if Account.get_user_accounts(): account_choices.append(Messages.action_account_list) account_choices.append(Messages.action_account_fund) account_choices.append(Messages.action_swap_kyber) available_choices = configuration_choices + account_choices + raiden_release_management_choices available_choices.append(Messages.action_quit) return { "type": "list", "message": "What would you like to do?", "choices": available_choices }
def test_cannot_find_keyfile_with_invalid_content(self): with self.account.keystore_file_path.open("w") as keyfile: json.dump(dict(invalid="keyfile"), keyfile) keystore_file_path = Account.find_keystore_file_path( self.account.address, TESTING_KEYSTORE_FOLDER) self.assertIsNone(keystore_file_path)
def test_finding_keystore_file_path(self): path = Account.find_keystore_file_path(self.account.address, TESTING_KEYSTORE_FOLDER) self.assertEqual(path, self.account.keystore_file_path)
def test_can_not_get_private_key_without_passphrase(self): empty_account = Account(Path("/invalid_folder")) with self.assertRaises(ValueError): empty_account.private_key
def test_has_no_content_when_keystore_file_does_not_exist(self): path = TESTING_KEYSTORE_FOLDER.joinpath("non", "existent", "path") account = Account(path) self.assertIsNone(account.content)
def test_cannot_find_keystore_in_non_existent_directory(self): path = TESTING_KEYSTORE_FOLDER.joinpath("non", "existent", "path") keystore_file_path = Account.find_keystore_file_path( self.account.address, path) self.assertIsNone(keystore_file_path)
def setUp(self): Account.DEFAULT_KEYSTORE_FOLDER = Path(tempfile.gettempdir()) self.account = Account.create(passphrase="test_password")
class RaidenConfigurationFile: FOLDER_PATH = XDG_DATA_HOME.joinpath("raiden") def __init__(self, account_filename: str, network: Network, ethereum_client_rpc_endpoint: str, **kw): if 'passphrase' in kw: self.account = Account(account_filename, passphrase=kw.get('passphrase')) else: self.account = Account(account_filename) self.account_filename = account_filename self.network = network self.settings = network_settings[network.name] self.ethereum_client_rpc_endpoint = ethereum_client_rpc_endpoint self.accept_disclaimer = kw.get("accept_disclaimer", True) self.enable_monitoring = kw.get("enable_monitoring", self.settings.monitoring_enabled) self.routing_mode = kw.get("routing_mode", self.settings.routing_mode) self.services_version = self.settings.services_version self._initial_funding_txhash = kw.get("_initial_funding_txhash") @property def path_finding_service_url(self): return f"https://pfs-{self.network.name}.services-{self.services_version}.raiden.network" @property def configuration_data(self): base_config = { "environment-type": self.environment_type, "keystore-path": str(self.account.__class__.find_keystore_folder_path()), "address": to_checksum_address(self.account.address), "network-id": self.network.name, "accept-disclaimer": self.accept_disclaimer, "eth-rpc-endpoint": self.ethereum_client_rpc_endpoint, "routing-mode": self.routing_mode, "enable-monitoring": self.enable_monitoring, "_initial_funding_txhash": self._initial_funding_txhash, } # If the config is for a demo-env we'll need to add/overwrite some settings if self.settings.client_release_channel == "demo_env": # noqa base_config.update({"matrix-server": self.settings.matrix_server}) # noqa base_config["routing-mode"] = "pfs" base_config[ "pathfinding-service-address"] = self.settings.pathfinding_service_address # noqa return base_config @property def environment_type(self): return "production" if self.network.name == "mainnet" else "development" @property def file_name(self): return f"config-{self.account.address}-{self.network.name}.toml" @property def path(self): return self.FOLDER_PATH.joinpath(self.file_name) @property def ethereum_balance(self): w3 = make_web3_provider(self.ethereum_client_rpc_endpoint, self.account) return self.account.get_ethereum_balance(w3) def save(self): self.FOLDER_PATH.mkdir(parents=True, exist_ok=True) with open(self.path, "w") as config_file: toml.dump(self.configuration_data, config_file) @classmethod def list_existing_files(cls) -> List[Path]: config_glob = str(cls.FOLDER_PATH.joinpath("config-*.toml")) return [Path(file_path) for file_path in glob.glob(config_glob)] @classmethod def get_available_configurations(cls): configurations = [] for config_file_path in cls.list_existing_files(): try: configurations.append(cls.load(config_file_path)) except (ValueError, KeyError) as exc: log.warn( f"Failed to load {config_file_path} as configuration file: {exc}" ) return configurations @classmethod def load(cls, file_path: Path): file_name, _ = os.path.splitext(os.path.basename(file_path)) _, _, network_name = file_name.split("-") with file_path.open() as config_file: data = toml.load(config_file) keystore_file_path = Account.find_keystore_file_path( data["address"], Path(data["keystore-path"])) return cls( account_filename=keystore_file_path, ethereum_client_rpc_endpoint=data["eth-rpc-endpoint"], network=Network.get_by_name(network_name), routing_mode=data["routing-mode"], enable_monitoring=data["enable-monitoring"], _initial_funding_txhash=data.get("_initial_funding_txhash"), ) @classmethod def get_by_filename(cls, file_name): file_path = cls.FOLDER_PATH.joinpath(file_name) if not file_path.exists(): raise ValueError( f"{file_path} is not a valid configuration file path") return cls.load(file_path) @classmethod def get_ethereum_rpc_endpoints(cls): endpoints = [] config_glob = glob.glob(cls.FOLDER_PATH.joinpath("*.toml")) for config_file_path in config_glob: with open(config_file_path) as config_file: data = toml.load(config_file) endpoints.append( EthereumRPCProvider.make_from_url( data["eth-rpc-endpoint"])) return endpoints
def test_cannot_find_keyfile_with_non_json_content(self): with self.account.keystore_file_path.open("w") as keyfile: keyfile.write("This is no JSON") keystore_file_path = Account.find_keystore_file_path( self.account.address, TESTING_KEYSTORE_FOLDER) self.assertIsNone(keystore_file_path)
def test_cannot_find_keyfile_without_read_permission(self): self.account.keystore_file_path.chmod(0) keystore_file_path = Account.find_keystore_file_path( self.account.address, TESTING_KEYSTORE_FOLDER) self.assertIsNone(keystore_file_path)
def setUp(self): self.passphrase = "test_password" self.account = Account.create(TESTING_KEYSTORE_FOLDER, self.passphrase)
def test_cannot_find_non_existent_keyfile(self): self.account.keystore_file_path.unlink() keystore_file_path = Account.find_keystore_file_path( self.account.address, TESTING_KEYSTORE_FOLDER) self.assertIsNone(keystore_file_path)
def setUp(self): super().setUp() keystore_file_path = self.account.keystore_file_path self.locked_account = Account(keystore_file_path)
def setUp(self): self.account = Account.create(TESTING_KEYSTORE_FOLDER)