def encode_intended_validator(validator_address: Union[Address, str], primitive: bytes = None, *, hexstr: str = None, text: str = None) -> SignableMessage: """ Encode a message using the "intended validator" approach (ie~ version 0) defined in EIP-191_. Supply the message as exactly one of these three arguments: bytes as a primitive, a hex string, or a unicode string. .. WARNING:: Note that this code has not gone through an external audit. Also, watch for updates to the format, as the EIP is still in DRAFT. :param validator_address: which on-chain contract is capable of validating this message, provided as a checksummed address or in native bytes. :param primitive: the binary message to be signed :type primitive: bytes or int :param str hexstr: the message encoded as hex :param str text: the message as a series of unicode characters (a normal Py3 str) :returns: The EIP-191 encoded message, ready for signing .. _EIP-191: https://eips.ethereum.org/EIPS/eip-191 """ if not is_valid_address(validator_address): raise ValidationError( f"Cannot encode message with 'Validator Address': {validator_address}. " "It must be a checksum address, or an address converted to bytes.") message_bytes = to_bytes(primitive, hexstr=hexstr, text=text) return SignableMessage( b'\x00', # version 0, as defined in EIP-191 to_canonical_address(validator_address), message_bytes, )
def is_empty_or_checksum_address(val): if val in VALID_EMPTY_ADDRESSES: return True else: return is_valid_address(val)