Пример #1
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)
Пример #2
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)
Пример #3
0
 def sign(self,
          private_key,
          claim_address,
          cert_claim_id,
          curve=SECP256k1,
          name=None,
          force_detached=False):
     signer = get_signer(curve).load_pem(private_key)
     signed, signature = signer.sign_stream_claim(self, claim_address,
                                                  cert_claim_id, name,
                                                  force_detached)
     return ClaimDict.load_protobuf(signed, signature)
Пример #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')
Пример #5
0
    async def test_new_signature_model_from_unserializable_claim(self):
        address1, address2 = await self.account.receiving.get_addresses(
            limit=2, only_usable=True)
        sendtxid1 = await self.blockchain.send_to_address(address1, 5)
        sendtxid2 = await self.blockchain.send_to_address(address2, 5)
        await self.blockchain.generate(1)
        await asyncio.wait([
            self.on_transaction_id(sendtxid1),
            self.on_transaction_id(sendtxid2)
        ])

        self.assertEqual(d2l(await self.account.get_balance()), '10.0')

        cert, key = generate_certificate()
        cert_tx = await Transaction.claim('@bar', cert, l2d('1.0'), address1,
                                          [self.account], self.account)
        original = ClaimDict.load_dict(example_claim_dict).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(key)
        signature = signer.sign(
            b'foo',
            decode_address(address1),
            altered,
            binascii.unhexlify(cert_tx.outputs[0].claim_id),
        )
        detached_sig = Signature(
            NAMED_SECP256K1(signature,
                            binascii.unhexlify(cert_tx.outputs[0].claim_id),
                            altered))
        claim_tx = await Transaction.claim('foo', detached_sig, l2d('1.0'),
                                           address1, [self.account],
                                           self.account)

        await self.broadcast(cert_tx)
        await self.broadcast(claim_tx)
        await self.ledger.wait(claim_tx)
        await self.blockchain.generate(1)
        await self.ledger.wait(claim_tx)

        response = await self.ledger.resolve(0, 10, 'lbry://@bar/foo')
        self.assertIn('lbry://@bar/foo', response)
        self.assertIn('claim', response['lbry://@bar/foo'])
Пример #6
0
def generate_certificate():
    secp256k1_private_key = get_signer(
        SECP256k1).generate().private_key.to_pem()
    return ClaimDict.generate_certificate(
        secp256k1_private_key, curve=SECP256k1), secp256k1_private_key
Пример #7
0
 def sign(self, private_key, claim_address, cert_claim_id, curve=NIST256p):
     signer = get_signer(curve).load_pem(private_key)
     signed = signer.sign_stream_claim(self, claim_address, cert_claim_id)
     return ClaimDict.load_protobuf(signed)
Пример #8
0
 def generate_certificate(cls, private_key, curve=NIST256p):
     signer = get_signer(curve).load_pem(private_key)
     return cls.load_protobuf(signer.certificate)