def test_get_account_in_keystore(): account_manager = AccountManager(test_keystore) assert account_manager.address_in_keystore('0d5a0e4fece4b84365b9b8dba6e6d41348c73645') assert account_manager.address_in_keystore('0x0d5a0e4fece4b84365b9b8dba6e6d41348c73645') assert account_manager.address_in_keystore('3593403033d18b82f7b4a0f18e1ed24623d23b20') assert account_manager.address_in_keystore('0x3593403033d18b82f7b4a0f18e1ed24623d23b20') assert not account_manager.address_in_keystore('a05934d3033d18b82f7b4adf18e1ed24e3d23b19')
def test_get_account_in_keystore(keystore_mock): account_manager = AccountManager(keystore_mock) assert account_manager.address_in_keystore( "0x0d5a0e4FECE4b84365b9B8DbA6e6D41348C73645") assert account_manager.address_in_keystore( "0x3593403033d18b82f7b4a0F18e1ED24623D23b20") assert not account_manager.address_in_keystore( "0xa05934d3033D18b82F7b4AdF18E1eD24E3D23b19") assert not account_manager.address_in_keystore(None)
def check_account(account_manager: AccountManager, address_hex: AddressHex) -> None: if not account_manager.address_in_keystore(address_hex): click.secho( f"Account '{address_hex}' could not be found on the system. Aborting ...", fg='red', ) sys.exit(1)
def test_get_account_in_keystore(keystore_mock): account_manager = AccountManager(keystore_mock) assert account_manager.address_in_keystore( "0d5a0e4fece4b84365b9b8dba6e6d41348c73645") assert account_manager.address_in_keystore( "0x0d5a0e4fece4b84365b9b8dba6e6d41348c73645") assert account_manager.address_in_keystore( "0x0D5A0E4fece4b84365b9b8dba6e6d41348c73645") assert account_manager.address_in_keystore( "3593403033d18b82f7b4a0f18e1ed24623d23b20") assert account_manager.address_in_keystore( "0x3593403033d18b82f7b4a0f18e1ed24623d23b20") assert not account_manager.address_in_keystore( "a05934d3033d18b82f7b4adf18e1ed24e3d23b19")
def prompt_account(address_hex, keystore_path, password_file): accmgr = AccountManager(keystore_path) if not accmgr.accounts: raise RuntimeError('No Ethereum accounts found in the user\'s system') if not accmgr.address_in_keystore(address_hex): # check if an address has been passed if address_hex is not None: print( "Account '{}' could not be found on the system. Aborting ...". format(address_hex)) sys.exit(1) addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - 0x{}'.format(idx, addr) for idx, addr in enumerate(addresses) ] should_prompt = True print('The following accounts were found in your machine:') print('') print('\n'.join(formatted_addresses)) print('') while should_prompt: idx = click.prompt('Select one of them by index to continue', type=int) if idx >= 0 and idx < len(addresses): should_prompt = False else: print("\nError: Provided index '{}' is out of bounds\n".format( idx)) address_hex = addresses[idx] password = None if password_file: password = password_file.read().splitlines()[0] if password: try: privatekey_bin = accmgr.get_privkey(address_hex, password) except ValueError: # ValueError exception raised if the password is incorrect print('Incorrect password for {} in file. Aborting ...'.format( address_hex)) sys.exit(1) else: unlock_tries = 3 while True: try: privatekey_bin = accmgr.get_privkey(address_hex) break except ValueError: # ValueError exception raised if the password is incorrect if unlock_tries == 0: print( 'Exhausted passphrase unlock attempts for {}. Aborting ...' .format(address_hex)) sys.exit(1) print( 'Incorrect passphrase to unlock the private key. {} tries remaining. ' 'Please try again or kill the process to quit. ' 'Usually Ctrl-c.'.format(unlock_tries)) unlock_tries -= 1 return address_hex, privatekey_bin
def check_account(account_manager: AccountManager, address_hex: Address) -> None: if not account_manager.address_in_keystore(to_checksum_address(address_hex)): raise RaidenError( f"Account '{address_hex}' could not be found on the system. Aborting ..." )
def app(address, keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, logging, logfile, max_unresponsive_time, send_ping_time): slogging.configure(logging, log_file=logfile) # config_file = args.config_file (listen_host, listen_port) = split_endpoint(listen_address) config = App.default_config.copy() config['host'] = listen_host config['port'] = listen_port config['max_unresponsive_time'] = max_unresponsive_time config['send_ping_time'] = send_ping_time accmgr = AccountManager(keystore_path) if not accmgr.accounts: raise RuntimeError('No Ethereum accounts found in the user\'s system') if not accmgr.address_in_keystore(address): addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - 0x{}'.format(idx, addr) for idx, addr in enumerate(addresses) ] should_prompt = True while should_prompt: idx = click.prompt( "The following accounts were found in your machine:\n\n{}" "\nSelect one of them by index to continue: ".format( "\n".join(formatted_addresses)), type=int) if idx >= 0 and idx < len(addresses): should_prompt = False else: print("\nError: Provided index '{}' is out of bounds\n".format( idx)) address = addresses[idx] privatekey = accmgr.get_privkey(address) config['privatekey_hex'] = encode_hex(privatekey) endpoint = eth_rpc_endpoint if eth_rpc_endpoint.startswith("http://"): endpoint = eth_rpc_endpoint[len("http://"):] rpc_port = 80 elif eth_rpc_endpoint.startswith("https://"): endpoint = eth_rpc_endpoint[len("https://"):] rpc_port = 443 if ':' not in endpoint: # no port was given in url rpc_host = endpoint else: rpc_host, rpc_port = split_endpoint(endpoint) blockchain_service = BlockChainService( privatekey, decode_hex(registry_contract_address), host=rpc_host, port=rpc_port, ) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address)) return App(config, blockchain_service, discovery)
def app( address, # pylint: disable=too-many-arguments,too-many-locals keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, socket, logging, logfile, max_unresponsive_time, send_ping_time, api_port, rpc, console): slogging.configure(logging, log_file=logfile) # config_file = args.config_file (listen_host, listen_port) = split_endpoint(listen_address) config = App.default_config.copy() config['host'] = listen_host config['port'] = listen_port config['max_unresponsive_time'] = max_unresponsive_time config['send_ping_time'] = send_ping_time config['console'] = console config['rpc'] = rpc config['api_port'] = api_port config['socket'] = socket accmgr = AccountManager(keystore_path) if not accmgr.accounts: raise RuntimeError('No Ethereum accounts found in the user\'s system') if not accmgr.address_in_keystore(address): addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - 0x{}'.format(idx, addr) for idx, addr in enumerate(addresses) ] should_prompt = True print('The following accounts were found in your machine:') print('') print('\n'.join(formatted_addresses)) print('') while should_prompt: idx = click.prompt('Select one of them by index to continue', type=int) if idx >= 0 and idx < len(addresses): should_prompt = False else: print("\nError: Provided index '{}' is out of bounds\n".format( idx)) address = addresses[idx] unlock_tries = 3 while True: try: privatekey_bin = accmgr.get_privkey(address) break except ValueError as e: # ValueError exception raised if the password is incorrect if unlock_tries == 0: print( 'Exhausted passphrase unlock attempts for {}. Aborting ...' .format(address)) sys.exit(1) print( 'Incorrect passphrase to unlock the private key. {} tries remaining. ' 'Please try again or kill the process to quit. ' 'Usually Ctrl-c.'.format(unlock_tries)) unlock_tries -= 1 privatekey_hex = privatekey_bin.encode('hex') config['privatekey_hex'] = privatekey_hex endpoint = eth_rpc_endpoint if eth_rpc_endpoint.startswith("http://"): endpoint = eth_rpc_endpoint[len("http://"):] rpc_port = 80 elif eth_rpc_endpoint.startswith("https://"): endpoint = eth_rpc_endpoint[len("https://"):] rpc_port = 443 if ':' not in endpoint: # no port was given in url rpc_host = endpoint else: rpc_host, rpc_port = split_endpoint(endpoint) # user may have provided registry and discovery contracts with leading 0x registry_contract_address = address_decoder(registry_contract_address) discovery_contract_address = address_decoder(discovery_contract_address) try: blockchain_service = BlockChainService( privatekey_bin, registry_contract_address, host=rpc_host, port=rpc_port, ) except ValueError as e: # ValueError exception raised if: # - The registry contract address doesn't have code, this might happen # if the connected geth process is not synced or if the wrong address # is provided (e.g. using the address from a smart contract deployed on # ropsten with a geth node connected to morden) print(e.message) sys.exit(1) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address)) # default database directory raiden_directory = os.path.join(os.path.expanduser('~'), '.raiden') if not os.path.exists(raiden_directory): os.makedirs(raiden_directory) database_path = os.path.join(raiden_directory, 'log.db') config['database_path'] = database_path return App(config, blockchain_service, discovery)
def app(address, keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, logging, logfile, max_unresponsive_time, send_ping_time): slogging.configure(logging, log_file=logfile) # config_file = args.config_file (listen_host, listen_port) = split_endpoint(listen_address) config = App.default_config.copy() config['host'] = listen_host config['port'] = listen_port config['max_unresponsive_time'] = max_unresponsive_time config['send_ping_time'] = send_ping_time accmgr = AccountManager(keystore_path) if not accmgr.accounts: raise RuntimeError('No Ethereum accounts found in the user\'s system') if not accmgr.address_in_keystore(address): addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - 0x{}'.format(idx, addr) for idx, addr in enumerate(addresses) ] should_prompt = True while should_prompt: idx = click.prompt( "The following accounts were found in your machine:\n\n{}" "\nSelect one of them by index to continue: ".format( "\n".join(formatted_addresses)), type=int ) if idx >= 0 and idx < len(addresses): should_prompt = False else: print("\nError: Provided index '{}' is out of bounds\n".format(idx)) address = addresses[idx] privatekey = accmgr.get_privkey(address) config['privatekey_hex'] = encode_hex(privatekey) endpoint = eth_rpc_endpoint if eth_rpc_endpoint.startswith("http://"): endpoint = eth_rpc_endpoint[len("http://"):] rpc_port = 80 elif eth_rpc_endpoint.startswith("https://"): endpoint = eth_rpc_endpoint[len("https://"):] rpc_port = 443 if ':' not in endpoint: # no port was given in url rpc_host = endpoint else: rpc_host, rpc_port = split_endpoint(endpoint) blockchain_service = BlockChainService( privatekey, decode_hex(registry_contract_address), host=rpc_host, port=rpc_port, ) discovery = ContractDiscovery( blockchain_service, decode_hex(discovery_contract_address) # FIXME: double encoding ) return App(config, blockchain_service, discovery)
def app( address, keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, rpccorsdomain, # pylint: disable=unused-argument mapped_socket, logging, logfile, max_unresponsive_time, send_ping_time, api_address, rpc, console, password_file, datadir): from raiden.app import App from raiden.network.rpc.client import BlockChainService # config_file = args.config_file (listen_host, listen_port) = split_endpoint(listen_address) (api_host, api_port) = split_endpoint(api_address) config = App.DEFAULT_CONFIG.copy() config['host'] = listen_host config['port'] = listen_port config['console'] = console config['rpc'] = rpc config['api_host'] = api_host config['api_port'] = api_port if mapped_socket: config['socket'] = mapped_socket.socket config['external_ip'] = mapped_socket.external_ip config['external_port'] = mapped_socket.external_port else: config['socket'] = None config['external_ip'] = listen_host config['external_port'] = listen_port retries = max_unresponsive_time / DEFAULT_NAT_KEEPALIVE_RETRIES config['protocol']['nat_keepalive_retries'] = retries config['protocol']['nat_keepalive_timeout'] = send_ping_time accmgr = AccountManager(keystore_path) if not accmgr.accounts: raise RuntimeError('No Ethereum accounts found in the user\'s system') if not accmgr.address_in_keystore(address): addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - 0x{}'.format(idx, addr) for idx, addr in enumerate(addresses) ] should_prompt = True print('The following accounts were found in your machine:') print('') print('\n'.join(formatted_addresses)) print('') while should_prompt: idx = click.prompt('Select one of them by index to continue', type=int) if idx >= 0 and idx < len(addresses): should_prompt = False else: print("\nError: Provided index '{}' is out of bounds\n".format( idx)) address = addresses[idx] password = None if password_file: password = password_file.read().splitlines()[0] if password: try: privatekey_bin = accmgr.get_privkey(address, password) except ValueError as e: # ValueError exception raised if the password is incorrect print('Incorrect password for {} in file. Aborting ...'.format( address)) sys.exit(1) else: unlock_tries = 3 while True: try: privatekey_bin = accmgr.get_privkey(address) break except ValueError as e: # ValueError exception raised if the password is incorrect if unlock_tries == 0: print( 'Exhausted passphrase unlock attempts for {}. Aborting ...' .format(address)) sys.exit(1) print( 'Incorrect passphrase to unlock the private key. {} tries remaining. ' 'Please try again or kill the process to quit. ' 'Usually Ctrl-c.'.format(unlock_tries)) unlock_tries -= 1 privatekey_hex = privatekey_bin.encode('hex') config['privatekey_hex'] = privatekey_hex endpoint = eth_rpc_endpoint # Fallback default port if only an IP address is given rpc_port = 8545 if eth_rpc_endpoint.startswith("http://"): endpoint = eth_rpc_endpoint[len("http://"):] rpc_port = 80 elif eth_rpc_endpoint.startswith("https://"): endpoint = eth_rpc_endpoint[len("https://"):] rpc_port = 443 if ':' not in endpoint: # no port was given in url rpc_host = endpoint else: rpc_host, rpc_port = split_endpoint(endpoint) # user may have provided registry and discovery contracts with leading 0x registry_contract_address = address_decoder(registry_contract_address) discovery_contract_address = address_decoder(discovery_contract_address) try: blockchain_service = BlockChainService( privatekey_bin, registry_contract_address, host=rpc_host, port=rpc_port, ) except ValueError as e: # ValueError exception raised if: # - The registry contract address doesn't have code, this might happen # if the connected geth process is not synced or if the wrong address # is provided (e.g. using the address from a smart contract deployed on # ropsten with a geth node connected to morden) print(e.message) sys.exit(1) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address)) if datadir is None: # default database directory raiden_directory = os.path.join(os.path.expanduser('~'), '.raiden') else: raiden_directory = datadir if not os.path.exists(raiden_directory): os.makedirs(raiden_directory) user_db_dir = os.path.join(raiden_directory, address[:6]) if not os.path.exists(user_db_dir): os.makedirs(user_db_dir) database_path = os.path.join(user_db_dir, 'log.db') config['database_path'] = database_path return App(config, blockchain_service, discovery)
def app(address, # pylint: disable=too-many-arguments,too-many-locals keystore_path, eth_rpc_endpoint, registry_contract_address, discovery_contract_address, listen_address, logging, logfile, max_unresponsive_time, send_ping_time): slogging.configure(logging, log_file=logfile) # config_file = args.config_file (listen_host, listen_port) = split_endpoint(listen_address) config = App.default_config.copy() config['host'] = listen_host config['port'] = listen_port config['max_unresponsive_time'] = max_unresponsive_time config['send_ping_time'] = send_ping_time accmgr = AccountManager(keystore_path) if not accmgr.accounts: raise RuntimeError('No Ethereum accounts found in the user\'s system') if not accmgr.address_in_keystore(address): addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - 0x{}'.format(idx, addr) for idx, addr in enumerate(addresses) ] should_prompt = True print('The following accounts were found in your machine:') print('') print('\n'.join(formatted_addresses)) print('') while should_prompt: idx = click.prompt('Select one of them by index to continue', type=int) if idx >= 0 and idx < len(addresses): should_prompt = False else: print("\nError: Provided index '{}' is out of bounds\n".format(idx)) address = addresses[idx] try: privatekey_bin = accmgr.get_privkey(address) except ValueError as e: # ValueError exception raised if the password is incorrect, print the # exception message and exit the process, the user may try again by # re-executing Raiden. print(e.message) sys.exit(1) privatekey_hex = privatekey_bin.encode('hex') config['privatekey_hex'] = privatekey_hex endpoint = eth_rpc_endpoint if eth_rpc_endpoint.startswith("http://"): endpoint = eth_rpc_endpoint[len("http://"):] rpc_port = 80 elif eth_rpc_endpoint.startswith("https://"): endpoint = eth_rpc_endpoint[len("https://"):] rpc_port = 443 if ':' not in endpoint: # no port was given in url rpc_host = endpoint else: rpc_host, rpc_port = split_endpoint(endpoint) # user may have provided registry and discovery contracts with leading 0x registry_contract_address = address_decoder(registry_contract_address) discovery_contract_address = address_decoder(discovery_contract_address) try: blockchain_service = BlockChainService( privatekey_bin, registry_contract_address, host=rpc_host, port=rpc_port, ) except ValueError as e: # ValueError exception raised if: # - The registry contract address doesn't have code, this might happen # if the connected geth process is not synced or if the wrong address # is provided (e.g. using the address from a smart contract deployed on # ropsten with a geth node connected to morden) print(e.message) sys.exit(1) discovery = ContractDiscovery( blockchain_service.node_address, blockchain_service.discovery(discovery_contract_address) ) return App(config, blockchain_service, discovery)
def prompt_account(address_hex, keystore_path, password_file): accmgr = AccountManager(keystore_path) if not accmgr.accounts: print( 'No Ethereum accounts found in the provided keystore directory {}. ' 'Please provide a directory containing valid ethereum account ' 'files.'.format(keystore_path), ) sys.exit(1) if not accmgr.address_in_keystore(address_hex): # check if an address has been passed if address_hex is not None: print("Account '{}' could not be found on the system. Aborting ...".format( address_hex)) sys.exit(1) addresses = list(accmgr.accounts.keys()) formatted_addresses = [ '[{:3d}] - {}'.format(idx, to_checksum_address(addr)) for idx, addr in enumerate(addresses) ] should_prompt = True print('The following accounts were found in your machine:') print('') print('\n'.join(formatted_addresses)) print('') while should_prompt: idx = click.prompt('Select one of them by index to continue', type=int) if 0 <= idx < len(addresses): should_prompt = False else: print('\nError: Provided index "{}" is out of bounds\n'.format(idx)) address_hex = addresses[idx] password = None if password_file: password = password_file.read() if password != '': password = password.splitlines()[0] if password is not None: try: privatekey_bin = accmgr.get_privkey(address_hex, password) except ValueError: # ValueError exception raised if the password is incorrect print('Incorrect password for {} in file. Aborting ...'.format(address_hex)) sys.exit(1) else: unlock_tries = 3 while True: try: privatekey_bin = accmgr.get_privkey(address_hex) break except ValueError: # ValueError exception raised if the password is incorrect if unlock_tries == 0: print( 'Exhausted passphrase unlock attempts for {}. Aborting ...' .format(address_hex), ) sys.exit(1) print( 'Incorrect passphrase to unlock the private key. {} tries remaining. ' 'Please try again or kill the process to quit. ' 'Usually Ctrl-c.'.format(unlock_tries), ) unlock_tries -= 1 return address_hex, privatekey_bin