def set_registry(self, address: Address) -> None: """ Sets the current registry used in ``web3.pm`` functions that read/write to an on-chain registry. This method accepts checksummed/canonical addresses or ENS names. Addresses must point to an on-chain instance of an ERC1319 registry implementation. To use an ENS domain as the address, make sure a valid ENS instance set as ``web3.ens``. * Parameters: * ``address``: Address of on-chain Registry. """ if is_canonical_address(address) or is_checksum_address(address): self.registry = SimpleRegistry(address, self.web3) elif is_ens_name(address): self._validate_set_ens() addr_lookup = self.web3.ens.address(address) if not addr_lookup: raise NameNotFound( "No address found after ENS lookup for name: {0}.".format( address)) self.registry = SimpleRegistry(addr_lookup, self.web3) else: raise PMError( "Expected a canonical/checksummed address or ENS name for the address, " "instead received {0}.".format(type(address)))
def normalize_address(ens: ENS, address: ChecksumAddress) -> ChecksumAddress: if address: if is_ens_name(address): validate_name_has_address(ens, address) else: validate_address(address) return address
def normalize_address(ens, address): if address: if is_ens_name(address): validate_name_has_address(ens, address) else: validate_address(address) return address
def set_registry(self, address: Address) -> None: """ Sets the current registry used in ``web3.pm`` functions that read/write to an on-chain registry. This method accepts checksummed/canonical addresses or ENS names. Addresses must point to an instance of the Vyper Reference Registry implementation. If you want to use a different registry implementation with ``web3.pm``, manually set the ``web3.pm.registry`` attribute to any subclass of ``ERCRegistry``. To use an ENS domain as the address, make sure a valid ENS instance set as ``web3.ens``. * Parameters: * ``address``: Address of on-chain Vyper Reference Registry. """ if is_canonical_address(address) or is_checksum_address(address): canonical_address = to_canonical_address(address) self.registry = VyperReferenceRegistry(canonical_address, self.web3) elif is_ens_name(address): self._validate_set_ens() addr_lookup = self.web3.ens.address(address) if not addr_lookup: raise NameNotFound( "No address found after ENS lookup for name: {0}.".format( address)) self.registry = VyperReferenceRegistry( to_canonical_address(addr_lookup), self.web3) else: raise PMError( "Expected a canonical/checksummed address or ENS name for the address, " "instead received {0}.".format(type(address)))
def is_encodable(_type, value): if not isinstance(_type, str): raise ValueError("is_encodable only accepts type strings") base, sub, arrlist = process_type(_type) if arrlist: if not is_list_like(value): return False if arrlist[-1] and len(value) != arrlist[-1][0]: return False sub_type = (base, sub, arrlist[:-1]) return all(is_encodable(collapse_type(*sub_type), sub_value) for sub_value in value) elif base == 'address' and is_ens_name(value): # ENS names can be used anywhere an address is needed # Web3.py will resolve the name to an address before encoding it return True elif base == 'bytes' and isinstance(value, str): # Hex-encoded bytes values can be used anywhere a bytes value is needed if is_hex(value) and len(value) % 2 == 0: # Require hex-encoding of full bytes (even length) bytes_val = to_bytes(hexstr=value) return eth_abi_is_encodable(_type, bytes_val) else: return False elif base == 'string' and isinstance(value, bytes): # bytes that were encoded with utf-8 can be used anywhere a string is needed try: string_val = to_text(value) except UnicodeDecodeError: return False else: return eth_abi_is_encodable(_type, string_val) else: return eth_abi_is_encodable(_type, value)
def normalize_address(ens, address): if address: if is_ens_name(address): validate_name_has_address(ens, address) else: validate_address(address) return address
def abi_ens_resolver(w3, type_str, val): if type_str == 'address' and is_ens_name(val): if w3 is None: raise InvalidAddress("Could not look up name %r because no web3" " connection available" % (val)) elif w3.ens is None: raise InvalidAddress("Could not look up name %r because ENS is" " set to None" % (val)) elif int(w3.net.version) != 1 and not isinstance(w3.ens, StaticENS): raise InvalidAddress("Could not look up name %r because web3 is" " not connected to mainnet" % (val)) else: return type_str, validate_name_has_address(w3.ens, val) else: return type_str, val
def abi_ens_resolver(w3: "Web3", type_str: TypeStr, val: Any) -> Tuple[TypeStr, Any]: if type_str == 'address' and is_ens_name(val): if w3 is None: raise InvalidAddress( f"Could not look up name {val!r} because no web3" " connection available") elif w3.ens is None: raise InvalidAddress( f"Could not look up name {val!r} because ENS is" " set to None") elif int(w3.net.version) != 1 and not isinstance(w3.ens, StaticENS): raise InvalidAddress( f"Could not look up name {val!r} because web3 is" " not connected to mainnet") else: return type_str, validate_name_has_address(w3.ens, val) else: return type_str, val
def abi_ens_resolver(w3, abi_type, val): if abi_type == 'address' and is_ens_name(val): if w3 is None: raise InvalidAddress( "Could not look up name %r because no web3" " connection available" % (val) ) elif w3.ens is None: raise InvalidAddress( "Could not look up name %r because ENS is" " set to None" % (val) ) elif int(w3.net.version) is not 1 and not isinstance(w3.ens, StaticENS): raise InvalidAddress( "Could not look up name %r because web3 is" " not connected to mainnet" % (val) ) else: return (abi_type, validate_name_has_address(w3.ens, val)) else: return (abi_type, val)
def is_encodable(_type, value): if not isinstance(_type, str): raise ValueError("is_encodable only accepts type strings") base, sub, arrlist = process_type(_type) if arrlist: if not is_list_like(value): return False if arrlist[-1] and len(value) != arrlist[-1][0]: return False sub_type = (base, sub, arrlist[:-1]) return all( is_encodable(collapse_type(*sub_type), sub_value) for sub_value in value) elif base == 'address' and is_ens_name(value): # ENS names can be used anywhere an address is needed # Web3.py will resolve the name to an address before encoding it return True elif base == 'bytes' and isinstance(value, str): # Hex-encoded bytes values can be used anywhere a bytes value is needed if is_hex(value) and len(value) % 2 == 0: # Require hex-encoding of full bytes (even length) bytes_val = to_bytes(hexstr=value) return eth_abi_is_encodable(_type, bytes_val) else: return False elif base == 'string' and isinstance(value, bytes): # bytes that were encoded with utf-8 can be used anywhere a string is needed try: string_val = to_text(value) except UnicodeDecodeError: return False else: return eth_abi_is_encodable(_type, string_val) else: return eth_abi_is_encodable(_type, value)
def validate_value(cls, value): if is_ens_name(value): return super().validate_value(value)
def validate_value(cls, value: Any) -> None: if is_ens_name(value): return super().validate_value(value)