Пример #1
0
 def test_alpha2(self):
     prefixes = [
         'en', 'aa', 'ab', 'ae', 'af', 'ak', 'am', 'an', 'ar', 'as', 'av',
         'ay', 'az', 'ba', 'be', 'bg', 'bh', 'bi', 'bm', 'bn', 'bo', 'br',
         'bs', 'ca', 'ce', 'ch', 'co', 'cr', 'cs', 'cu', 'cv', 'cy', 'da',
         'de', 'dv', 'dz', 'ee', 'el', 'eo', 'es', 'et', 'eu', 'fa', 'ff',
         'fi', 'fj', 'fo', 'fr', 'fy', 'ga', 'gd', 'gl', 'gn', 'gu', 'gv',
         'ha', 'he', 'hi', 'ho', 'hr', 'ht', 'hu', 'hy', 'hz', 'ia', 'id',
         'ie', 'ig', 'ii', 'ik', 'io', 'is', 'it', 'iu', 'ja', 'jv', 'ka',
         'kg', 'ki', 'kj', 'kk', 'kl', 'km', 'kn', 'ko', 'kr', 'ks', 'ku',
         'kv', 'kw', 'ky', 'la', 'lb', 'lg', 'li', 'ln', 'lo', 'lt', 'lu',
         'lv', 'mg', 'mh', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms', 'mt', 'my',
         'na', 'nb', 'nd', 'ne', 'ng', 'nl', 'nn', 'no', 'nr', 'nv', 'ny',
         'oc', 'oj', 'om', 'or', 'os', 'pa', 'pi', 'pl', 'ps', 'pt', 'qu',
         'rm', 'rn', 'ro', 'ru', 'rw', 'sa', 'sc', 'sd', 'se', 'sg', 'si',
         'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'ss', 'st', 'su', 'sv',
         'sw', 'ta', 'te', 'tg', 'th', 'ti', 'tk', 'tl', 'tn', 'to', 'tr',
         'ts', 'tt', 'tw', 'ty', 'ug', 'uk', 'ur', 'uz', 've', 'vi', 'vo',
         'wa', 'wo', 'xh', 'yi', 'yo', 'za', 'zh', 'zu'
     ]
     for prefix in prefixes:
         metadata = deepcopy(example_010)
         metadata['stream']['metadata']['language'] = prefix
         claim = ClaimDict.load_dict(metadata)
         serialized = claim.serialized
         self.assertDictEqual(
             metadata, dict(ClaimDict.deserialize(serialized).claim_dict))
Пример #2
0
 def test_fail_to_sign_with_no_claim_address(self):
     cert = ClaimDict.generate_certificate(secp256k1_private_key,
                                           curve=SECP256k1)
     self.assertDictEqual(cert.claim_dict, secp256k1_cert)
     self.assertRaises(Exception,
                       ClaimDict.load_dict(example_010).sign,
                       secp256k1_private_key,
                       None,
                       claim_id_1,
                       curve=SECP256k1)
Пример #3
0
 def test_fail_to_validate_fake_ecdsa_signature(self):
     signed = ClaimDict.load_dict(example_010).sign(secp256k1_private_key,
                                                    claim_address_1,
                                                    claim_id_1,
                                                    curve=SECP256k1)
     signed_copy = ClaimDict.load_protobuf(signed.protobuf)
     fake_key = get_signer(SECP256k1).generate().private_key.to_pem()
     fake_cert = ClaimDict.generate_certificate(fake_key, curve=SECP256k1)
     self.assertRaises(ecdsa.keys.BadSignatureError,
                       signed_copy.validate_signature, claim_address_2,
                       fake_cert)
Пример #4
0
 def test_validate_ecdsa_signature(self):
     cert = ClaimDict.generate_certificate(nist384p_private_key,
                                           curve=NIST384p)
     signed = ClaimDict.load_dict(example_010).sign(nist384p_private_key,
                                                    claim_address_2,
                                                    claim_id_1,
                                                    curve=NIST384p)
     self.assertDictEqual(signed.claim_dict, claim_010_signed_nist384p)
     signed_copy = ClaimDict.load_protobuf(signed.protobuf)
     self.assertEquals(
         signed_copy.validate_signature(claim_address_2, cert), True)
Пример #5
0
 def test_encode_decode(self):
     test_claim = ClaimDict.load_dict(example_010)
     self.assertEquals(test_claim.is_certificate, False)
     self.assertDictEqual(test_claim.claim_dict, example_010)
     test_pb = test_claim.protobuf
     self.assertDictEqual(
         ClaimDict.load_protobuf(test_pb).claim_dict, example_010)
     self.assertEquals(test_pb.ByteSize(),
                       ClaimDict.load_protobuf(test_pb).protobuf_len)
     self.assertEquals(test_claim.json_len,
                       ClaimDict.load_protobuf(test_pb).json_len)
Пример #6
0
 def test_fail_to_validate_with_no_claim_address(self):
     cert = ClaimDict.generate_certificate(secp256k1_private_key,
                                           curve=SECP256k1)
     self.assertDictEqual(cert.claim_dict, secp256k1_cert)
     signed = ClaimDict.load_dict(example_010).sign(secp256k1_private_key,
                                                    claim_address_2,
                                                    claim_id_1,
                                                    curve=SECP256k1)
     self.assertDictEqual(signed.claim_dict, claim_010_signed_secp256k1)
     signed_copy = ClaimDict.load_protobuf(signed.protobuf)
     self.assertRaises(Exception, signed_copy.validate_signature, None,
                       cert)
Пример #7
0
 def test_fail_to_validate_ecdsa_sig_for_altered_claim(self):
     cert = ClaimDict.generate_certificate(secp256k1_private_key,
                                           curve=SECP256k1)
     altered = ClaimDict.load_dict(example_010).sign(secp256k1_private_key,
                                                     claim_address_1,
                                                     claim_id_1,
                                                     curve=SECP256k1)
     sd_hash = altered['stream']['source']['source']
     altered['stream']['source']['source'] = sd_hash[::-1]
     altered_copy = ClaimDict.load_dict(altered.claim_dict)
     self.assertRaises(ecdsa.keys.BadSignatureError,
                       altered_copy.validate_signature, claim_address_1,
                       cert)
Пример #8
0
 def test_remove_signature_equals_unsigned(self):
     unsigned = ClaimDict.load_dict(example_010)
     signed = unsigned.sign(secp256k1_private_key,
                            claim_address_1,
                            claim_id_1,
                            curve=SECP256k1)
     self.assertEquals(unsigned.serialized, signed.serialized_no_signature)
Пример #9
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
Пример #10
0
def migrate_003_to_010(value):
    migrated_to_003 = LegacyMetadata(value)
    metadata = {"version": "_0_1_0"}
    for k in [
            "author", "description", "language", "license", "nsfw",
            "thumbnail", "title", "preview"
    ]:
        if k in migrated_to_003:
            metadata.update({k: migrated_to_003[k]})

    if 'license_url' in migrated_to_003:
        metadata['licenseUrl'] = migrated_to_003['license_url']

    if "fee" in migrated_to_003:
        fee = migrated_to_003["fee"]
        currency = fee.keys()[0]
        amount = fee[currency]['amount']
        address = fee[currency]['address']
        metadata.update(
            dict(
                fee={
                    "currency": currency,
                    "version": "_0_0_1",
                    "amount": amount,
                    "address": address
                }))
    source = {
        "source": migrated_to_003['sources']['ddcs_sd_hash'],
        "contentType": migrated_to_003['content_type'],
        "sourceType": "ddcs_sd_hash",
        "version": "_0_0_1"
    }

    migrated = {
        "version": "_0_0_1",
        "claimType": "streamType",
        "stream": {
            "version": "_0_0_1",
            "metadata": metadata,
            "source": source
        }
    }
    return ClaimDict.load_dict(migrated)
Пример #11
0
 def test_invalid_cert_curve(self):
     with self.assertRaises(Exception):
         ClaimDict.load_dict(malformed_secp256k1_cert)
Пример #12
0
 def test_make_ecdsa_cert(self):
     cert = ClaimDict.generate_certificate(nist384p_private_key,
                                           curve=NIST384p)
     self.assertEquals(cert.is_certificate, True)
     self.assertDictEqual(cert.claim_dict, nist384p_cert)
Пример #13
0
 def test_stream_is_not_certificate(self):
     deserialized_claim = ClaimDict.deserialize(
         example_010_serialized.decode('hex'))
     self.assertEquals(deserialized_claim.is_certificate, False)
Пример #14
0
 def test_deserialize(self):
     deserialized_claim = ClaimDict.deserialize(
         example_010_serialized.decode('hex'))
     self.assertDictEqual(
         ClaimDict.load_dict(example_010).claim_dict,
         deserialized_claim.claim_dict)
Пример #15
0
}

cert_claim_id = "63f2da17b0d90042c559cc73b6b17f853945c43e"
stream_claim_address = "bDtL6qriyimxz71DSYjojTBsm6cpM1bqmj"
stream_claim_address_2 = "bUG7VaMzLEqqyZQAyg9srxQzvf1wwnJ48w"

claim_010_unsigned = migrate.migrate(unmigrated_003)

# NIST256p test data
nist256p_private_key = get_signer(NIST256p).generate().private_key.to_pem()
claim_010_signed_nist256p = claim_010_unsigned.sign(nist256p_private_key,
                                                    stream_claim_address,
                                                    cert_claim_id,
                                                    curve=NIST256p)

nist256p_cert = ClaimDict.generate_certificate(nist256p_private_key,
                                               curve=NIST256p)

# NIST384p test data
nist384p_private_key = get_signer(NIST384p).generate().private_key.to_pem()
claim_010_signed_nist384p = claim_010_unsigned.sign(nist384p_private_key,
                                                    stream_claim_address,
                                                    cert_claim_id,
                                                    curve=NIST384p)
nist384p_cert = ClaimDict.generate_certificate(nist384p_private_key,
                                               curve=NIST384p)

# SECP256k1 test data
secp256k1_private_key = get_signer(SECP256k1).generate().private_key.to_pem()
claim_010_signed_secp256k1 = claim_010_unsigned.sign(secp256k1_private_key,
                                                     stream_claim_address,
                                                     cert_claim_id,
Пример #16
0
 def test_valid_private_key_for_cert(self):
     cert_claim = ClaimDict.load_dict(secp256k1_cert)
     self.assertEqual(
         cert_claim.validate_private_key(secp256k1_private_key, claim_id_1),
         True)
Пример #17
0
 def test_fail_to_load_wrong_private_key_for_cert(self):
     cert_claim = ClaimDict.load_dict(secp256k1_cert)
     self.assertEqual(
         cert_claim.validate_private_key(nist256p_private_key, claim_id_1),
         False)
Пример #18
0
 def test_make_ecdsa_cert(self):
     cert = ClaimDict.generate_certificate(secp256k1_private_key,
                                           curve=SECP256k1)
     self.assertEquals(cert.is_certificate, True)
     self.assertDictEqual(cert.claim_dict, secp256k1_cert)