Beispiel #1
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)

    try:
        claim_value = binascii.unhexlify(claim_value)
    except (TypeError, ValueError):
        pass

    if claim_value[0] in ['{', ord('{')]:
        try:
            if six.PY3 and isinstance(claim_value, six.binary_type):
                claim_value = claim_value.decode()
            decoded_json = json.loads(claim_value)
            return migrate_json_claim_value(decoded_json)
        except (ValueError, TypeError):
            pass
    try:
        if six.PY3 and isinstance(claim_value, six.text_type):
            claim_value = claim_value.encode()
        return ClaimDict.deserialize(claim_value)
    except (DecodeError, InvalidAddress, KeyError, TypeError):
            raise DecodeError(claim_value)
Beispiel #2
0
    def test_validate_what_cant_be_serialized_back_even_by_loading_back_from_dictionary(
            self):
        cert = ClaimDict.generate_certificate(secp256k1_private_key,
                                              curve=SECP256k1)
        self.assertDictEqual(cert.claim_dict, secp256k1_cert)
        original = ClaimDict.load_dict(example_010).serialized
        altered = original + b'\x00\x01\x02\x30\x50\x80\x99'  # pretend this extra trash is from some unknown protobuf

        # manually sign
        signer = get_signer(SECP256k1).load_pem(secp256k1_private_key)
        signature = signer.sign(
            b'example',
            decode_address(claim_address_2),
            altered,
            binascii.unhexlify(claim_id_1),
        )
        detached_sig = Signature(
            NAMED_SECP256K1(signature, binascii.unhexlify(claim_id_1),
                            altered))

        signed = detached_sig.serialized
        self.assertEqual(signed[85:], altered)
        signed_copy = ClaimDict.deserialize(signed)
        signed_copy = ClaimDict.load_dict(signed_copy.claim_dict)
        self.assertEqual(
            signed_copy.validate_signature(claim_address_2,
                                           cert,
                                           name='example'), True)
        self.assertEqual(signed, signed_copy.serialized)
Beispiel #3
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))
Beispiel #4
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,
                                                    name='example',
                                                    force_detached=True)
     signed_copy = ClaimDict.deserialize(signed.serialized)
     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, 'example')
Beispiel #5
0
 async def send_claim_to_address(self,
                                 claim_id: str,
                                 destination_address: str,
                                 amount: Optional[int],
                                 account=None):
     account = account or self.default_account
     claims = await account.ledger.db.get_utxos(claim_id=claim_id)
     if not claims:
         raise NameError(f"Claim not found: {claim_id}")
     tx = await Transaction.update(
         claims[0], ClaimDict.deserialize(claims[0].script.value['claim']),
         amount, destination_address.encode(), [account], account)
     await self.ledger.broadcast(tx)
     return tx
Beispiel #6
0
def _format_claim_response(outpoint, claim_id, name, amount, height, serialized, channel_id, address, claim_sequence):
    r = {
        "name": name,
        "claim_id": claim_id,
        "address": address,
        "claim_sequence": claim_sequence,
        "value": ClaimDict.deserialize(unhexlify(serialized)).claim_dict,
        "height": height,
        "amount": dewies_to_lbc(amount),
        "nout": int(outpoint.split(":")[1]),
        "txid": outpoint.split(":")[0],
        "channel_claim_id": channel_id,
        "channel_name": None
    }
    return r
Beispiel #7
0
 def test_validate_detached_named_ecdsa_signature(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,
                                                    name='example',
                                                    force_detached=True)
     signed_copy = ClaimDict.deserialize(signed.serialized)
     self.assertEqual(
         signed_copy.validate_signature(claim_address_2,
                                        cert,
                                        name='example'), True)
Beispiel #8
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,
                                                        name='example',
                                                        force_detached=True)
        original_serialization = altered.serialized
        sd_hash = altered['stream']['source']['source']
        altered['stream']['source']['source'] = sd_hash[::-1]
        altered_serialization = altered.protobuf.SerializeToString()

        # keep signature, but replace serialization with the altered claim (check signature.py for slice sizes)
        altered_copy = ClaimDict.deserialize(original_serialization[:85] +
                                             altered_serialization)
        self.assertRaises(ecdsa.keys.BadSignatureError,
                          altered_copy.validate_signature, claim_address_1,
                          cert, 'example')
Beispiel #9
0
    def _save_content_claim(transaction, claim_outpoint, stream_hash):
        # get the claim id and serialized metadata
        claim_info = transaction.execute(
            "select claim_id, serialized_metadata from claim where claim_outpoint=?",
            (claim_outpoint, )).fetchone()
        if not claim_info:
            raise Exception("claim not found")
        new_claim_id, claim = claim_info[0], ClaimDict.deserialize(
            binascii.unhexlify(claim_info[1]))

        # certificate claims should not be in the content_claim table
        if not claim.is_stream:
            raise Exception("claim does not contain a stream")

        # get the known sd hash for this stream
        known_sd_hash = transaction.execute(
            "select sd_hash from stream where stream_hash=?",
            (stream_hash, )).fetchone()
        if not known_sd_hash:
            raise Exception("stream not found")
        # check the claim contains the same sd hash
        if known_sd_hash[0].encode() != claim.source_hash:
            raise Exception("stream mismatch")

        # if there is a current claim associated to the file, check that the new claim is an update to it
        current_associated_content = transaction.execute(
            "select claim_outpoint from content_claim where stream_hash=?",
            (stream_hash, )).fetchone()
        if current_associated_content:
            current_associated_claim_id = transaction.execute(
                "select claim_id from claim where claim_outpoint=?",
                current_associated_content).fetchone()[0]
            if current_associated_claim_id != new_claim_id:
                raise Exception(
                    f"mismatching claim ids when updating stream {current_associated_claim_id} vs {new_claim_id}"
                )

        # update the claim associated to the file
        transaction.execute(
            "insert or replace into content_claim values (?, ?)",
            (stream_hash, claim_outpoint))
Beispiel #10
0
 async def send_claim_to_address(self,
                                 claim_id: str,
                                 destination_address: str,
                                 amount: Optional[int],
                                 account=None):
     account = account or self.default_account
     claims = await account.get_claims(
         claim_name_type__any={
             'is_claim': 1,
             'is_update': 1
         },  # exclude is_supports
         claim_id=claim_id)
     if not claims:
         raise NameError(f"Claim not found: {claim_id}")
     if not amount:
         amount = claims[0].get_estimator(self.ledger).effective_amount
     tx = await Transaction.update(
         claims[0], ClaimDict.deserialize(claims[0].script.values['claim']),
         amount, destination_address.encode(), [account], account)
     await self.ledger.broadcast(tx)
     return tx
Beispiel #11
0
 def test_stream_is_not_certificate(self):
     deserialized_claim = ClaimDict.deserialize(binascii.unhexlify(example_010_serialized))
     self.assertEqual(deserialized_claim.is_certificate, False)
Beispiel #12
0
 def test_deserialize(self):
     deserialized_claim = ClaimDict.deserialize(binascii.unhexlify(example_010_serialized))
     self.assertDictEqual(ClaimDict.load_dict(example_010).claim_dict,
                          deserialized_claim.claim_dict)