Beispiel #1
0
    def parse_json(self, data):
        """
        Generate fulfillment payload from a dict

        Args:
            data (dict): description of the fulfillment

        Returns:
            Fulfillment
        """
        self.script = urlsafe_b64decode(base64_add_padding(data['script']))
        self.data = urlsafe_b64decode(base64_add_padding(data['data']))
        self.keys = urlsafe_b64decode(base64_add_padding(data['keys']))
Beispiel #2
0
    def parse_json(self, data):
        """
        Generate fulfillment payload from a dict

        Args:
            data (dict): description of the fulfillment

        Returns:
            Fulfillment
        """
        self.public_key = urlsafe_b64decode(base64_add_padding(
            data['publicKey']))
        self.signature = urlsafe_b64decode(base64_add_padding(
            data['signature']))
Beispiel #3
0
    def parse_json(self, data):
        """
        Generate fulfillment payload from a dict

        Args:
            data (dict): description of the fulfillment

        Returns:
            Fulfillment
        """
        self.public_key = urlsafe_b64decode(
            base64_add_padding(data['publicKey']))
        self.signature = urlsafe_b64decode(
            base64_add_padding(data['signature']))
Beispiel #4
0
    def from_uri(serialized_condition):
        """
        Create a Condition object from a URI.

        This method will parse a condition URI and construct a corresponding Condition object.

        Args:
            serialized_condition (str): URI representing the condition

        Returns:
            Condition: Resulting object
        """
        if isinstance(serialized_condition, Condition):
            return serialized_condition
        elif not isinstance(serialized_condition, str):
            raise TypeError('Serialized condition must be a string')

        pieces = serialized_condition.split(':')
        if not pieces[0] == 'cc':
            raise ValueError('Serialized condition must start with "cc:"')

        if not re.match(CONDITION_REGEX_STRICT, serialized_condition):
            raise ValueError('Invalid condition format')

        condition = Condition()
        condition.type_id = int(pieces[1], 16)
        condition.bitmask = int(pieces[2], 16)
        condition.hash = base64.urlsafe_b64decode(base64_add_padding(
            pieces[3]))
        condition.max_fulfillment_length = int(pieces[4])

        return condition
    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_uri(serialized_condition):
        """
        Create a Condition object from a URI.

        This method will parse a condition URI and construct a corresponding Condition object.

        Args:
            serialized_condition (str): URI representing the condition

        Returns:
            Condition: Resulting object
        """
        if not isinstance(serialized_condition, str):
            raise TypeError('Serialized condition must be a string')

        pieces = serialized_condition.split(':')
        if not pieces[0] == 'cc':
            raise ValueError('Serialized condition must start with "cc:"')

        if not re.match(CONDITION_REGEX_STRICT, serialized_condition):
            raise ValueError('Invalid condition format')

        condition = Condition()
        condition.type_id = int(pieces[1], 16)
        condition.bitmask = int(pieces[2], 16)
        condition.hash = base64.urlsafe_b64decode(base64_add_padding(pieces[3]))
        condition.max_fulfillment_length = int(pieces[4])

        return condition
    def from_uri(serialized_condition):
        """
        Create a Condition object from a URI.

        This method will parse a condition URI and construct a corresponding Condition object.

        Args:
            serialized_condition (str): URI representing the condition

        Returns:
            Condition: Resulting object
        """
        # TODO consider removing.
        if isinstance(serialized_condition, Condition):
            return serialized_condition
        # TODO Use static typing instead (e.g.: with mypy).
        elif not isinstance(serialized_condition, str):
            raise TypeError('Serialized condition must be a string')

        pieces = serialized_condition.split(':')
        if pieces[0] != CONDITION_URI_SCHEME:
            raise PrefixError(
                'Serialized condition must start with "{}:"'.format(
                    CONDITION_URI_SCHEME
                )
            )

        regex_match = re.match(CONDITION_REGEX_STRICT, serialized_condition)
        if not regex_match:
            raise ParsingError('Invalid condition format')

        qs_dict = parse_qs(regex_match.group(2))

        try:
            fingerprint_type = qs_dict['fpt'][0]
        except (KeyError, IndexError):
            raise ParsingError(
                'Invalid condition format: "fpt" parameter or value missing.')

        condition_type = TypeRegistry.find_by_name(fingerprint_type)
        try:
            cost = qs_dict['cost'][0]
        except (KeyError, IndexError):
            raise ParsingError(
                'Invalid condition format: "cost" parameter or value missing.')

        if not re.match(INTEGER_REGEX, cost):
            raise ParsingError('No or invalid cost provided')

        fingerprint = regex_match.group(1)
        condition = Condition()
        condition.type_id = condition_type['type_id']
        condition._subtypes = set()
        if condition_type['class'].TYPE_CATEGORY == 'compound':
            condition._subtypes.update(qs_dict['subtypes'][0].split(','))
        condition.hash = base64.urlsafe_b64decode(
            base64_add_padding(fingerprint))
        condition.cost = int(cost)
        return condition
 def test_valid_condition_invalid_signature_ilp(self, vk_ilp, signature):
     vk = VerifyingKey(
         VerifyingKey.encode(
             base64_add_padding(vk_ilp[2]['b64'])))
     msg = base64.b64decode(signature['msg'])
     assert vk.verify(msg, signature['msg'], encoding='base64') is False
     assert vk.verify(msg, binascii.hexlify(base64.b64decode(signature['msg'])), encoding='hex') is False
     assert vk.verify(msg, base64.b64decode(signature['msg']), encoding=None) is False
    def parse_json(self, data):
        """
        Generate fulfillment payload from a dict

        Args:
            data (dict): description of the fulfillmjson
        Returns:
            Fulfillment
        """
        self.preimage = urlsafe_b64decode(base64_add_padding(data['preimage']))
Beispiel #10
0
    def parse_json(self, data):
        """
        Generate fulfillment payload from a dict

        Args:
            data (dict): description of the fulfillmjson
        Returns:
            Fulfillment
        """
        self.preimage = urlsafe_b64decode(base64_add_padding(data['preimage']))
 def test_sign_verify(self, sk_ilp, vk_ilp):
     message = 'Hello World!'
     sk = SigningKey(sk_ilp['b58'])
     vk = VerifyingKey(vk_ilp['b58'])
     assert vk.verify(message, sk.sign(message)) is True
     assert vk.verify(message, sk.sign(message + 'dummy')) is False
     assert vk.verify(message + 'dummy', sk.sign(message)) is False
     vk = VerifyingKey(
         VerifyingKey.encode(
             base64_add_padding(vk_ilp[2]['b64'])))
     assert vk.verify(message, sk.sign(message)) is False
Beispiel #12
0
def normalize_value(value, key):
    from cryptoconditions.crypto import (base64_add_padding,
                                         base64_remove_padding)
    if key in ('public_key', 'signature'):
        value = urlsafe_b64decode(base64_add_padding(value))
    if key in ('condition_binary', 'fingerprint_contents', 'message'):
        value = unhexlify(value.encode())
    if key == 'fulfillment':
        value = base64_remove_padding(
            urlsafe_b64encode(unhexlify(value.encode()))).decode()
    return value
Beispiel #13
0
def normalize_value(value, key):
    from cryptoconditions.crypto import (base64_add_padding,
                                         base64_remove_padding)
    if key in ('public_key', 'signature'):
        value = urlsafe_b64decode(base64_add_padding(value))
    if key in ('condition_binary', 'fingerprint_contents', 'message'):
        value = unhexlify(value.encode())
    if key == 'fulfillment':
        value = base64_remove_padding(
            urlsafe_b64encode(unhexlify(value.encode()))).decode()
    return value
 def test_generate_sign_verify(self, vk_ilp):
     sk_b58, vk_b58 = ed25519_generate_key_pair()
     sk = SigningKey(sk_b58)
     vk = VerifyingKey(vk_b58)
     message = 'Hello World!'
     assert vk.verify(message, sk.sign(message)) is True
     assert vk.verify(message, sk.sign(message + 'dummy')) is False
     assert vk.verify(message + 'dummy', sk.sign(message)) is False
     vk = VerifyingKey(
         VerifyingKey.encode(
             base64_add_padding(vk_ilp[2]['b64'])))
     assert vk.verify(message, sk.sign(message)) is False
Beispiel #15
0
    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 not isinstance(serialized_fulfillment, str):
            raise TypeError('Serialized fulfillment must be a string')
        uri_bytes = base64.urlsafe_b64decode(
            base64_add_padding(serialized_fulfillment))
        return Fulfillment.from_binary(uri_bytes)
    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 not isinstance(serialized_fulfillment, str):
            raise TypeError('Serialized fulfillment must be a string')
        uri_bytes = base64.urlsafe_b64decode(
            base64_add_padding(serialized_fulfillment))
        return Fulfillment.from_binary(uri_bytes)
Beispiel #17
0
def basic_ed25519_private_key(basic_ed25519_private_key_base64url):
    from cryptoconditions.crypto import base64_add_padding
    return urlsafe_b64decode(
        base64_add_padding(basic_ed25519_private_key_base64url))
 def test_signing_key_encode(self, sk_ilp):
     private_value_base58 = SigningKey.encode(base64_add_padding(sk_ilp['b64']))
     assert private_value_base58 == sk_ilp['b58']
Beispiel #19
0
 def parse_json(self, json):
     self.modulus = urlsafe_b64decode(base64_add_padding(json['modulus']))
     self.signature = urlsafe_b64decode(
         base64_add_padding(json['signature']))
 def test_signing_key_decode(self, sk_ilp):
     private_value = SigningKey.decode(sk_ilp['b58'])
     assert private_value == base64_add_padding(sk_ilp['b64'])
Beispiel #21
0
 def parse_json(self, json):
     self.modulus = urlsafe_b64decode(base64_add_padding(json['modulus']))
     self.signature = urlsafe_b64decode(
         base64_add_padding(json['signature']))
 def test_verifying_key_encode(self, vk_ilp):
     public_value_base58 = VerifyingKey.encode(base64_add_padding(vk_ilp['b64']))
     assert public_value_base58 == vk_ilp['b58']
 def test_verifying_key_decode(self, vk_ilp):
     public_value = VerifyingKey.decode(vk_ilp['b58'])
     assert public_value == base64_add_padding(vk_ilp['b64'])
Beispiel #24
0
 def parse_json(self, data):
     self.prefix = urlsafe_b64decode(base64_add_padding(data['prefix']))
     self.max_message_length = data['maxMessageLength']
     self._set_subfulfillment(Fulfillment.from_json(data['subfulfillment']))
Beispiel #25
0
 def parse_json(self, data):
     self.prefix = urlsafe_b64decode(base64_add_padding(data['prefix']))
     self.max_message_length = data['maxMessageLength']
     self._set_subfulfillment(Fulfillment.from_json(data['subfulfillment']))
Beispiel #26
0
def basic_ed25519_private_key(basic_ed25519_private_key_base64url):
    from cryptoconditions.crypto import base64_add_padding
    return urlsafe_b64decode(
        base64_add_padding(basic_ed25519_private_key_base64url))