def validate(self): """ Ensure the condition is valid according the local rules. Checks the condition against the local bitmask (supported condition types) and the local maximum fulfillment size. Returns: bool: Whether the condition is valid according to local rules. """ # Get class for type ID, throws on error TypeRegistry.get_class_from_type_id(self.type_id) # Bitmask can have at most 32 bits with current implementation if self.bitmask > Condition.MAX_SAFE_BITMASK: raise ValueError('Bitmask too large to be safely represented') # Assert all requested features are supported by this implementation if self.bitmask & ~Condition.SUPPORTED_BITMASK: raise ValueError('Condition requested unsupported feature suites') # Assert the requested fulfillment size is supported by this implementation if self.max_fulfillment_length > Condition.MAX_FULFILLMENT_LENGTH: raise ValueError( 'Condition requested too large of a max fulfillment size') return True
def validate(self): """ Ensure the condition is valid according the local rules. Checks the condition against the local bitmask (supported condition types) and the local maximum fulfillment size. Returns: bool: Whether the condition is valid according to local rules. """ # Get class for type ID, throws on error TypeRegistry.get_class_from_type_id(self.type_id) # Bitmask can have at most 32 bits with current implementation if self.bitmask > Condition.MAX_SAFE_BITMASK: raise ValueError('Bitmask too large to be safely represented') # Assert all requested features are supported by this implementation if self.bitmask & ~Condition.SUPPORTED_BITMASK: raise ValueError('Condition requested unsupported feature suites') # Assert the requested fulfillment size is supported by this implementation if self.max_fulfillment_length > Condition.MAX_FULFILLMENT_LENGTH: raise ValueError('Condition requested too large of a max fulfillment size') return True
def from_uri(serialized_fulfillment): """ Create a Fulfillment object from a URI. This method will parse a fulfillment URI and construct a corresponding Fulfillment object. Args: serialized_fulfillment (str): URI representing the fulfillment Return: Fulfillment: Resulting object """ if isinstance(serialized_fulfillment, Fulfillment): return serialized_fulfillment elif not isinstance(serialized_fulfillment, str): raise TypeError('Serialized fulfillment must be a string') pieces = serialized_fulfillment.split(':') if not pieces[0] == 'cf': raise ValueError('Serialized fulfillment must start with "cf:"') if not re.match(Fulfillment.REGEX, serialized_fulfillment): raise ValueError('Invalid fulfillment format') # try: type_id = int(pieces[1], 16) payload = base64.urlsafe_b64decode(base64_add_padding(pieces[2])) cls = TypeRegistry.get_class_from_type_id(type_id) fulfillment = cls() fulfillment.parse_payload(Reader.from_source(payload), len(payload)) # except Exception as e: # raise ParsingError(str(e)) return fulfillment
def from_dict(data): cls_type = data['type_id'] cls = TypeRegistry.get_class_from_type_id(cls_type) fulfillment = cls() fulfillment.parse_dict(data) return fulfillment
def from_binary(reader): """ Create a Fulfillment object from a binary blob. This method will parse a stream of binary data and construct a corresponding Fulfillment object. Args: reader (Reader): Binary stream implementing the Reader interface Returns: Fulfillment: Resulting object """ reader = Reader.from_source(reader) cls_type = reader.read_uint16() cls = TypeRegistry.get_class_from_type_id(cls_type) fulfillment = cls() payload_length = reader.read_length_prefix() fulfillment.parse_payload(reader, payload_length) return fulfillment