示例#1
0
def migrate_json_claim_value(decoded_json):
    try:
        if 'fee' in decoded_json:
            old_fee = decoded_json['fee']
            if not old_fee[old_fee.keys()[0]]['amount']:
                del decoded_json['fee']
                return migrate_json_claim_value(decoded_json)
    except (TypeError, AttributeError, InvalidAddress):
        raise DecodeError("Failed to decode claim")
    try:
        pb_migrated = schema_migrator(decoded_json)
        return pb_migrated
    except json_format.ParseError as parse_error:
        raise DecodeError("Failed to parse protobuf: %s" % parse_error)
    except Exception as err:
        raise DecodeError("Failed to migrate claim: %s" % err)
def encode_fields(claim_dictionary):
    """Encode bytes to hex and b58 for return by ClaimDict"""
    claim_dictionary = deepcopy(claim_dictionary)
    claim_type = CLAIM_TYPES[claim_dictionary[CLAIM_TYPE]]
    claim_value = claim_dictionary[claim_type]
    if claim_type == CLAIM_TYPES[STREAM_TYPE]:
        claim_value['source']['source'] = binascii.hexlify(
            claim_value['source']['source']).decode()
        if 'fee' in claim_value['metadata']:
            try:
                address = encode_address(
                    claim_value['metadata']['fee']['address'])
            except InvalidAddress as err:
                raise DecodeError("Invalid fee address: %s" % err)
            claim_value['metadata']['fee']['address'] = address
    elif claim_type == CLAIM_TYPES[CERTIFICATE_TYPE]:
        public_key = claim_value["publicKey"]
        claim_value["publicKey"] = binascii.hexlify(public_key).decode()
    if SIGNATURE in claim_dictionary:
        encoded_sig = binascii.hexlify(
            claim_dictionary[SIGNATURE]['signature']).decode()
        encoded_cert_id = binascii.hexlify(
            claim_dictionary[SIGNATURE]['certificateId']).decode()
        claim_dictionary[SIGNATURE]['signature'] = encoded_sig
        claim_dictionary[SIGNATURE]['certificateId'] = encoded_cert_id
    claim_dictionary[claim_type] = claim_value
    return claim_dictionary
def decode_fields(claim_dictionary):
    """Decode hex and b58 encoded bytes in dictionaries given to ClaimDict"""
    claim_dictionary = deepcopy(claim_dictionary)
    claim_type = CLAIM_TYPES[claim_dictionary[CLAIM_TYPE]]
    claim_value = claim_dictionary[claim_type]
    if claim_type == CLAIM_TYPES[STREAM_TYPE]:
        claim_value['source']['source'] = binascii.unhexlify(
            claim_value['source']['source'])
        if 'fee' in claim_value['metadata']:
            try:
                address = decode_address(
                    claim_value['metadata']['fee']['address'])
            except InvalidAddress as err:
                raise DecodeError("Invalid fee address: %s" % err)
            claim_value['metadata']['fee']['address'] = address
    elif claim_type == CLAIM_TYPES[CERTIFICATE_TYPE]:
        public_key = binascii.unhexlify(claim_value["publicKey"])
        claim_value["publicKey"] = public_key
    if SIGNATURE in claim_dictionary:
        decoded_sig = binascii.unhexlify(
            claim_dictionary[SIGNATURE]['signature'])
        decoded_cert_id = binascii.unhexlify(
            claim_dictionary[SIGNATURE]['certificateId'])
        claim_dictionary[SIGNATURE]['signature'] = decoded_sig
        claim_dictionary[SIGNATURE]['certificateId'] = decoded_cert_id
    claim_dictionary[claim_type] = claim_value
    return claim_dictionary
示例#4
0
def smart_decode(claim_value):
    """
    Decode a claim value

    Try decoding claim protobuf, if this fails try decoding json and migrating it.
    If unable to decode or migrate, raise DecodeError
    """

    # if already decoded, return
    if isinstance(claim_value, ClaimDict):
        return claim_value
    elif isinstance(claim_value, dict):
        return ClaimDict.load_dict(claim_value)

    # see if we were given a hex string, try decoding it
    skip_hex = sum(1 if char not in hex_chars else 0 for char in claim_value)
    if not skip_hex:
        try:
            decoded = claim_value.decode('hex')
            claim_value = decoded
        except (TypeError, ValueError):
            pass

    if claim_value.startswith("{"):
        # try deserializing protobuf, if that fails try parsing from json
        try:
            decoded_json = json.loads(claim_value)
        except (ValueError, TypeError):
            try:
                decoded_claim = ClaimDict.deserialize(claim_value)
                return decoded_claim
            except (DecodeError, InvalidAddress, KeyError):
                raise DecodeError()
        migrated_claim = migrate_json_claim_value(decoded_json)
        return migrated_claim
    else:
        try:
            decoded_claim = ClaimDict.deserialize(claim_value)
            return decoded_claim
        except (DecodeError, InvalidAddress, KeyError):
            try:
                decoded_json = json.loads(claim_value)
            except (ValueError, TypeError):
                raise DecodeError()
            migrated_claim = migrate_json_claim_value(decoded_json)
            return migrated_claim
示例#5
0
    def deserialize(cls, serialized):
        """Load a ClaimDict from a serialized protobuf string"""

        temp_claim = claim_pb2.Claim()
        try:
            temp_claim.ParseFromString(serialized)
        except DecodeError_pb:
            raise DecodeError(DecodeError_pb)
        return cls.load_protobuf(temp_claim)
 def load_dict(cls, claim_dict):
     """Load ClaimDict from a dictionary with hex and base58 encoded bytes"""
     try:
         return cls.load_protobuf(cls(decode_fields(claim_dict)).protobuf)
     except json_format.ParseError as err:
         raise DecodeError(str(err))