async def setup_wallet_node( port, consensus_constants: ConsensusConstants, local_bt, full_node_port=None, introducer_port=None, key_seed=None, starting_height=None, ): config = bt.config["wallet"] config["port"] = port config["rpc_port"] = port + 1000 if starting_height is not None: config["starting_height"] = starting_height config["initial_num_public_keys"] = 5 entropy = token_bytes(32) keychain = Keychain(entropy.hex(), True) if key_seed is None: key_seed = entropy keychain.add_private_key(bytes_to_mnemonic(key_seed), "") first_pk = keychain.get_first_public_key() assert first_pk is not None db_path_key_suffix = str(first_pk.get_fingerprint()) db_name = f"test-wallet-db-{port}-KEY.sqlite" db_path_replaced: str = db_name.replace("KEY", db_path_key_suffix) db_path = bt.root_path / db_path_replaced if db_path.exists(): db_path.unlink() config["database_path"] = str(db_name) config["testing"] = True config["introducer_peer"]["host"] = self_hostname if introducer_port is not None: config["introducer_peer"]["port"] = introducer_port config["peer_connect_interval"] = 10 else: config["introducer_peer"] = None if full_node_port is not None: config["full_node_peer"] = {} config["full_node_peer"]["host"] = self_hostname config["full_node_peer"]["port"] = full_node_port else: del config["full_node_peer"] kwargs = service_kwargs_for_wallet(local_bt.root_path, config, consensus_constants, keychain) kwargs.update( parse_cli_args=False, connect_to_daemon=False, ) service = Service(**kwargs) await service.start(new_wallet=True) yield service._node, service._node.server service.stop() await service.wait_closed() if db_path.exists(): db_path.unlink() keychain.delete_all_keys()
class KeychainProxy(DaemonProxy): """ KeychainProxy can act on behalf of a local or remote keychain. In the case of wrapping a local keychain, the proxy object simply forwards-along the calls to the underlying local keychain. In the remote case, calls are made to the daemon over the RPC interface, allowing the daemon to act as the keychain authority. """ def __init__( self, log: logging.Logger, uri: str = None, ssl_context: Optional[ssl.SSLContext] = None, local_keychain: Optional[Keychain] = None, user: str = None, testing: bool = False, ): self.log = log if local_keychain: self.keychain = local_keychain elif not supports_keyring_passphrase(): self.keychain = Keychain() # Proxy locally, don't use RPC else: self.keychain = None # type: ignore self.keychain_user = user self.keychain_testing = testing super().__init__(uri or "", ssl_context) def use_local_keychain(self) -> bool: """ Indicates whether the proxy forwards calls to a local keychain """ return self.keychain is not None def format_request(self, command: str, data: Dict[str, Any]) -> WsRpcMessage: """ Overrides DaemonProxy.format_request() to add keychain-specific RPC params """ if data is None: data = {} if self.keychain_user or self.keychain_testing: data["kc_user"] = self.keychain_user data["kc_testing"] = self.keychain_testing return super().format_request(command, data) async def get_response_for_request(self, request_name: str, data: Dict[str, Any]) -> Tuple[WsRpcMessage, bool]: request = self.format_request(request_name, data) response = await self._get(request) success = response["data"].get("success", False) return response, success def handle_error(self, response: WsRpcMessage): """ Common error handling for RPC responses """ error = response["data"].get("error", None) if error: error_details = response["data"].get("error_details", {}) if error == KEYCHAIN_ERR_LOCKED: raise KeyringIsLocked() elif error == KEYCHAIN_ERR_NO_KEYS: raise KeyringIsEmpty() elif error == KEYCHAIN_ERR_MALFORMED_REQUEST: message = error_details.get("message", "") raise MalformedKeychainRequest(message) else: err = f"{response['data'].get('command')} failed with error: {error}" self.log.error(f"{err}") raise Exception(f"{err}") async def add_private_key(self, mnemonic: str, passphrase: str) -> PrivateKey: """ Forwards to Keychain.add_private_key() """ key: PrivateKey if self.use_local_keychain(): key = self.keychain.add_private_key(mnemonic, passphrase) else: response, success = await self.get_response_for_request( "add_private_key", {"mnemonic": mnemonic, "passphrase": passphrase} ) if success: seed = mnemonic_to_seed(mnemonic, passphrase) key = AugSchemeMPL.key_gen(seed) else: error = response["data"].get("error", None) if error == KEYCHAIN_ERR_KEYERROR: error_details = response["data"].get("error_details", {}) word = error_details.get("word", "") raise KeyError(word) else: self.handle_error(response) return key async def check_keys(self, root_path): """ Forwards to init_funcs.check_keys() """ if self.use_local_keychain(): check_keys(root_path, self.keychain) else: response, success = await self.get_response_for_request("check_keys", {"root_path": str(root_path)}) if not success: self.handle_error(response) async def delete_all_keys(self): """ Forwards to Keychain.delete_all_keys() """ if self.use_local_keychain(): self.keychain.delete_all_keys() else: response, success = await self.get_response_for_request("delete_all_keys", {}) if not success: self.handle_error(response) async def delete_key_by_fingerprint(self, fingerprint: int): """ Forwards to Keychain.delete_key_by_fingerprint() """ if self.use_local_keychain(): self.keychain.delete_key_by_fingerprint(fingerprint) else: response, success = await self.get_response_for_request( "delete_key_by_fingerprint", {"fingerprint": fingerprint} ) if not success: self.handle_error(response) async def get_all_private_keys(self) -> List[Tuple[PrivateKey, bytes]]: """ Forwards to Keychain.get_all_private_keys() """ keys: List[Tuple[PrivateKey, bytes]] = [] if self.use_local_keychain(): keys = self.keychain.get_all_private_keys() else: response, success = await self.get_response_for_request("get_all_private_keys", {}) if success: private_keys = response["data"].get("private_keys", None) if private_keys is None: err = f"Missing private_keys in {response.get('command')} response" self.log.error(f"{err}") raise MalformedKeychainResponse(f"{err}") else: for key_dict in private_keys: pk = key_dict.get("pk", None) ent_str = key_dict.get("entropy", None) if pk is None or ent_str is None: err = f"Missing pk and/or ent in {response.get('command')} response" self.log.error(f"{err}") continue # We'll skip the incomplete key entry ent = bytes.fromhex(ent_str) mnemonic = bytes_to_mnemonic(ent) seed = mnemonic_to_seed(mnemonic, passphrase="") key = AugSchemeMPL.key_gen(seed) if bytes(key.get_g1()).hex() == pk: keys.append((key, ent)) else: err = "G1Elements don't match" self.log.error(f"{err}") else: self.handle_error(response) return keys async def get_first_private_key(self) -> Optional[PrivateKey]: """ Forwards to Keychain.get_first_private_key() """ key: Optional[PrivateKey] = None if self.use_local_keychain(): sk_ent = self.keychain.get_first_private_key() if sk_ent: key = sk_ent[0] else: response, success = await self.get_response_for_request("get_first_private_key", {}) if success: private_key = response["data"].get("private_key", None) if private_key is None: err = f"Missing private_key in {response.get('command')} response" self.log.error(f"{err}") raise MalformedKeychainResponse(f"{err}") else: pk = private_key.get("pk", None) ent_str = private_key.get("entropy", None) if pk is None or ent_str is None: err = f"Missing pk and/or ent in {response.get('command')} response" self.log.error(f"{err}") raise MalformedKeychainResponse(f"{err}") ent = bytes.fromhex(ent_str) mnemonic = bytes_to_mnemonic(ent) seed = mnemonic_to_seed(mnemonic, passphrase="") sk = AugSchemeMPL.key_gen(seed) if bytes(sk.get_g1()).hex() == pk: key = sk else: err = "G1Elements don't match" self.log.error(f"{err}") else: self.handle_error(response) return key async def get_key_for_fingerprint(self, fingerprint: Optional[int]) -> Optional[PrivateKey]: """ Locates and returns a private key matching the provided fingerprint """ key: Optional[PrivateKey] = None if self.use_local_keychain(): private_keys = self.keychain.get_all_private_keys() if len(private_keys) == 0: raise KeyringIsEmpty() else: if fingerprint is not None: for sk, _ in private_keys: if sk.get_g1().get_fingerprint() == fingerprint: key = sk break else: key = private_keys[0][0] else: response, success = await self.get_response_for_request( "get_key_for_fingerprint", {"fingerprint": fingerprint} ) if success: pk = response["data"].get("pk", None) ent = response["data"].get("entropy", None) if pk is None or ent is None: err = f"Missing pk and/or ent in {response.get('command')} response" self.log.error(f"{err}") raise MalformedKeychainResponse(f"{err}") else: mnemonic = bytes_to_mnemonic(bytes.fromhex(ent)) seed = mnemonic_to_seed(mnemonic, passphrase="") private_key = AugSchemeMPL.key_gen(seed) if bytes(private_key.get_g1()).hex() == pk: key = private_key else: err = "G1Elements don't match" self.log.error(f"{err}") else: self.handle_error(response) return key