def validate_value(self, value): if not is_list_like(value): self.invalidate_value( value, msg="must be list-like such as array or tuple", ) for item in value: self.item_encoder.validate_value(item)
def validate_abi_value(abi_type, value): """ Helper function for validating a value against the expected abi_type Note: abi_type 'bytes' must either be python3 'bytes' object or '' """ if is_array_type(abi_type) and is_list_like(value): # validate length specified_length = length_of_array_type(abi_type) if specified_length is not None: if specified_length < 1: raise TypeError( "Invalid abi-type: {abi_type}. Length of fixed sized arrays" "must be greater than 0." .format(abi_type=abi_type) ) if specified_length != len(value): raise TypeError( "The following array length does not the length specified" "by the abi-type, {abi_type}: {value}" .format(abi_type=abi_type, value=value) ) # validate sub_types sub_type = sub_type_of_array_type(abi_type) for v in value: validate_abi_value(sub_type, v) return elif is_bool_type(abi_type) and is_boolean(value): return elif is_uint_type(abi_type) and is_integer(value) and value >= 0: return elif is_int_type(abi_type) and is_integer(value): return elif is_address_type(abi_type): validate_address(value) return elif is_bytes_type(abi_type): if is_bytes(value): return elif is_string(value): if is_0x_prefixed(value): return else: raise TypeError( "ABI values of abi-type 'bytes' must be either" "a python3 'bytes' object or an '0x' prefixed string." ) elif is_string_type(abi_type) and is_string(value): return raise TypeError( "The following abi value is not a '{abi_type}': {value}" .format(abi_type=abi_type, value=value) )
def _friendly_json_encode(self, obj, cls=None): try: encoded = json.dumps(obj, cls=cls) return encoded except TypeError as full_exception: if hasattr(obj, 'items'): item_errors = '; '.join(self._json_mapping_errors(obj)) raise TypeError("dict had unencodable value at keys: {{{}}}".format(item_errors)) elif is_list_like(obj): element_errors = '; '.join(self._json_list_errors(obj)) raise TypeError("list had unencodable value at index: [{}]".format(element_errors)) else: raise full_exception
def _align_abi_input(arg_abi, arg): """ Aligns the values of any mapping at any level of nesting in ``arg`` according to the layout of the corresponding abi spec. """ tuple_parts = get_tuple_type_str_parts(arg_abi['type']) if tuple_parts is None: # Arg is non-tuple. Just return value. return arg tuple_prefix, tuple_dims = tuple_parts if tuple_dims is None: # Arg is non-list tuple. Each sub arg in `arg` will be aligned # according to its corresponding abi. sub_abis = arg_abi['components'] else: # Arg is list tuple. A non-list version of its abi will be used to # align each element in `arg`. new_abi = copy.copy(arg_abi) new_abi['type'] = tuple_prefix sub_abis = itertools.repeat(new_abi) if isinstance(arg, abc.Mapping): # Arg is mapping. Align values according to abi order. aligned_arg = tuple(arg[abi['name']] for abi in sub_abis) else: aligned_arg = arg if not is_list_like(aligned_arg): raise TypeError( 'Expected non-string sequence for "{}" component type: got {}'. format( arg_abi['type'], aligned_arg, ), ) return type(aligned_arg)( _align_abi_input(sub_abi, sub_arg) for sub_abi, sub_arg in zip(sub_abis, aligned_arg))
def validate_abi(abi): """ Helper function for validating an ABI """ if not is_list_like(abi): raise ValueError("'abi' is not a list") if not all(is_dict(e) for e in abi): raise ValueError("'abi' is not a list of dictionaries") functions = filter_by_type('function', abi) selectors = groupby( compose(encode_hex, function_abi_to_4byte_selector), functions ) duplicates = valfilter(lambda funcs: len(funcs) > 1, selectors) if duplicates: raise ValueError( 'Abi contains functions with colliding selectors. ' 'Functions {0}'.format(_prepare_selector_collision_msg(duplicates)) )
def validate_value(self, value): if not is_list_like(value): self.invalidate_value( value, msg="must be list-like object such as array or tuple", ) if len(value) != len(self.encoders): self.invalidate_value( value, exc=ValueOutOfBounds, msg="value has {} items when {} were expected".format( len(value), len(self.encoders), ), ) for item, encoder in zip(value, self.encoders): try: encoder.validate_value(item) except AttributeError: encoder(item)
def is_array_of_dicts(value): if not is_list_like(value): return False return all((is_dict(item) for item in value))
def is_array_of_strings(value): if not is_list_like(value): return False return all((is_string(item) for item in value))