cfx_address.utils module

cfx_address.utils.validate_hex_address(value: Any) Literal[True]

Checks if the given string of text type is an address in hexadecimal encoded form.

Parameters:

hex_address (Any) – value to validate

Raises:

InvalidHexAddress – raised if not valid

Return Literal[True]:

returns True if valid

cfx_address.utils.validate_network_id(network_id: Any) Literal[True]

Checks if the given value is a positive integer.

Parameters:

hex_address (Any) – value to validate

Raises:

InvalidHexAddress – raised if not valid

Return Literal[True]:

returns True if valid

cfx_address.utils.eth_eoa_address_to_cfx_hex(eoa_address: str) HexAddress

Convert an ethereum EOA address to valid cfx hex address.

In conflux, only addresses starting with 0x1 are valid user-type addresses. This function convert ethereum EOA address to the corresponding form in conflux.

Parameters:

address (str) – ethereum address

Raises:

InvalidHexAddress – the argument is not a valid hex address

Return HexAddress:

corresponding hex address in conflux, starting with ‘0x1’

Examplse:

>>> eth_eoa_address_to_cfx_hex("0xd43d2a93e97245E290feE74276a1EF8D275bE646")
'0x143d2a93e97245e290fee74276a1ef8d275be646'
cfx_address.utils.validate_base32(value: Any) Literal[True]

Validate if a value is a valid string-typed base32_address, raises an exception if not

Parameters:

value (str) – value to validate

Raises:

InvalidBase32Address – raises an exception if the address is not a valid base32 address

Return Literal[True]:

returns True only if address is valid

cfx_address.utils.is_valid_base32(value: Any) bool

Whether a value is a valid string-typed base32 address

Return bool:

True if valid, else False

cfx_address.utils.validate_address_agaist_network_id(address: str, network_id: int | None, accept_hex: bool | None = False) Literal[True]
Validate address in specific network context. Error will be raised only if:
  1. address validity checking:

    address is a base32 address or hex / base32 address if accept_hex

  2. network id validity checking:

    the network id of the address should be same as network_id, this step will be skipped if address is hex address or network_id is None

Parameters:
  • address (str) – address to validate

  • network_id (Union[int, None]) – if is None, then network id checking will be skipped

  • accept_hex (Optional[bool]) – whether a hex address is accepted, if. Defaults to False

Raises:
  • AddressNotMatch – hex address is received when accept_hex is not True

  • Base32AddressNotMatch – the network id of address does not equal to network_id

Return Literal[True]:

returns True if no exceptions are raised

>>> from cfx_address.utils import validate_address_agaist_network_id
>>> address = Base32Address.zero_address(1)
>>> validate_address_agaist_network_id(address, 1)
True
>>> validate_address_agaist_network_id(address.hex_address, 1, True)
True
>>> validate_address_agaist_network_id(address, None)
True
>>> validate_address_agaist_network_id(address.hex_address, None)
Traceback (most recent call last):
    ...
cfx_utils.exceptions.AddressNotMatch: hex address is not accepted
>>> validate_address_agaist_network_id(address, 1029)
Traceback (most recent call last):
    ...
cfx_utils.exceptions.Base32AddressNotMatch: expects address of network id 1029, receives address of network id 1
cfx_address.utils.public_key_to_cfx_hex(public_key: str | bytes) HexAddress

return the corresponding hex address of a public key in conflux: “0x1” + keccak(pk).hex()[-39]

Parameters:

public_key (Union[str, bytes]) – str or bytes representation of public key

Return HexAddress:

Hex representation of the correspondign hex address

>>> public_key_to_cfx_hex("0xdacdaeba8e391e7649d3ac4b5329ca0e202d38facd928d88b5f729b89a497e43cc4ad3816fcfdb241497b3b43862afb4c899bc284bf60feca4ee66ff868d1feb")
'0x152d251c36aec31072b90a85b95bf9435b07edb8'