def _convert_to_classic_address(json: Dict[str, Any], field: str) -> None: """ Mutates JSON-like dictionary to convert the given field from an X-address (if applicable) to a classic address. """ if field in json and is_valid_xaddress(json[field]): json[field] = xaddress_to_classic_address(json[field])
async def get_account_info(address: str, client: Client) -> Response: """ Query the ledger for account info of given address. Args: address: the account to query. client: the network client used to make network calls. Returns: The account info for the address. Raises: XRPLRequestFailureException: if the rippled API call fails. """ if is_valid_xaddress(address): address, _, _ = xaddress_to_classic_address(address) response = await client.request_impl( AccountInfo( account=address, ledger_index="validated", )) if response.is_successful(): return response result = cast(Dict[str, Any], response.result) raise XRPLRequestFailureException(result)
def _validate_account_xaddress( json: Dict[str, Any], account_field: str, tag_field: str ) -> None: """ Mutates JSON-like dictionary so the X-Address in the account field is the classic address, and the tag is in the tag field. """ if is_valid_xaddress(json[account_field]): account, tag, _ = xaddress_to_classic_address(json[account_field]) json[account_field] = account if json[tag_field] and json[tag_field] != tag: raise XRPLException(f"{tag_field} value does not match X-Address tag") json[tag_field] = tag
def test_xaddress_to_classic_address(self): for test_case in test_cases: ( expected_classic_address, expected_tag, main_xaddress, test_xaddress, ) = test_case # test classic_address, tag, is_test = addresscodec.xaddress_to_classic_address( test_xaddress) self.assertEqual(classic_address, expected_classic_address) self.assertEqual(tag, expected_tag) self.assertTrue(is_test) # main classic_address, tag, is_test = addresscodec.xaddress_to_classic_address( main_xaddress) self.assertEqual(classic_address, expected_classic_address) self.assertEqual(tag, expected_tag) self.assertFalse(is_test)
async def get_latest_transaction(account: str, client: Client) -> Response: """ Fetches the most recent transaction on the ledger associated with an account. Args: account: the account to query. client: the network client used to communicate with a rippled node. Returns: The Response object containing the transaction info. Raises: XRPLRequestFailureException: if the transaction fails. """ # max == -1 means that it's the most recent validated ledger version if is_valid_xaddress(account): account, _, _ = xaddress_to_classic_address(account) response = await client.request_impl( AccountTx(account=account, ledger_index_max=-1, limit=1)) if not response.is_successful(): result = cast(Dict[str, Any], response.result) raise XRPLRequestFailureException(result) return response
async def get_account_transactions(address: str, client: Client) -> List[Dict[str, Any]]: """ Query the ledger for a list of transactions that involved a given account. Args: address: the account to query. client: the network client used to make network calls. Returns: The transaction history for the address. Raises: XRPLRequestFailureException: if the transaction fails. """ if is_valid_xaddress(address): address, _, _ = xaddress_to_classic_address(address) request = AccountTx(account=address) response = await client.request_impl(request) result = cast(Dict[str, Any], response.result) if not response.is_successful(): raise XRPLRequestFailureException(result) return cast(List[Dict[str, Any]], result["transactions"])
def _handle_xaddress(field: str, xaddress: str) -> Dict[str, Union[str, int]]: """Break down an X-Address into a classic address and a tag. Args: field: Name of field xaddress: X-Address corresponding to the field Returns: A dictionary representing the classic address and tag. Raises: XRPLBinaryCodecException: field-tag combo is invalid. """ (classic_address, tag, is_test_network) = xaddress_to_classic_address(xaddress) if field == _DESTINATION: tag_name = _DEST_TAG elif field == _ACCOUNT: tag_name = _SOURCE_TAG elif tag is not None: raise XRPLBinaryCodecException(f"{field} cannot have an associated tag") if tag is not None: return {field: classic_address, tag_name: tag} return {field: classic_address}
def from_value(cls: Type[AccountID], value: str) -> AccountID: """ Construct an AccountID from a hex string or a base58 r-Address. Args: value: The string to construct an AccountID from. Returns: The AccountID constructed from value. Raises: XRPLBinaryCodecException: If the supplied value is of the wrong type. """ if not isinstance(value, str): raise XRPLBinaryCodecException( "Invalid type to construct an AccountID: expected str," f" received {value.__class__.__name__}." ) if value == "": return cls() # hex-encoded case if _HEX_REGEX.fullmatch(value): return cls(bytes.fromhex(value)) # base58 case if is_valid_classic_address(value): return cls(decode_classic_address(value)) if is_valid_xaddress(value): classic_address, _, _ = xaddress_to_classic_address(value) return cls(decode_classic_address(classic_address)) raise XRPLBinaryCodecException( "Invalid value to construct an AccountID: expected valid classic address " f"or X-Address, received {value.__class__.__name__}." )