Beispiel #1
0
    def test_checkdatasig_raise_on_uncompressed_pubkey(self):
        block = self.genesis_blocks[0]
        data = b'some_random_data'

        from hathor.transaction import Transaction, TxInput, TxOutput
        txin = TxInput(tx_id=block.hash, index=0, data=b'')
        txout = TxOutput(value=block.outputs[0].value, script=b'')
        tx = Transaction(inputs=[txin], outputs=[txout])

        import hashlib
        data_to_sign = tx.get_sighash_all()
        hashed_data = hashlib.sha256(data_to_sign).digest()
        signature = self.genesis_private_key.sign(hashed_data,
                                                  ec.ECDSA(hashes.SHA256()))

        from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
        pubkey_uncompressed = self.genesis_public_key.public_bytes(
            Encoding.X962, PublicFormat.UncompressedPoint)
        # ScriptError if pubkey is not a valid compressed public key
        # with wrong signature
        stack = [data, b'123', pubkey_uncompressed]
        with self.assertRaises(ScriptError):
            op_checkdatasig(stack, log=[], extras=None)
        # or with rigth one
        # this will make sure the signature is not made when parameters are wrong
        stack = [data, signature, pubkey_uncompressed]
        with self.assertRaises(ScriptError):
            op_checkdatasig(stack, log=[], extras=None)
Beispiel #2
0
    def test_checkdatasig_check_for_compressed_pubkey(self):
        # ScriptError if pubkey is not a public key but starts with 2 or 3
        with self.assertRaises(ScriptError):
            op_checkdatasig([b'\x0233', b'\x0233', b'\x0233'],
                            log=[],
                            extras=None)
        with self.assertRaises(ScriptError):
            op_checkdatasig([b'\x0321', b'\x0321', b'\x0321'],
                            log=[],
                            extras=None)

        # ScriptError if pubkey is not a public key
        with self.assertRaises(ScriptError):
            op_checkdatasig([b'\x0123', b'\x0123', b'\x0123'],
                            log=[],
                            extras=None)
Beispiel #3
0
    def test_checkdatasig(self):
        with self.assertRaises(MissingStackItems):
            op_checkdatasig([1, 1], log=[], extras=None)

        data = b'some_random_data'
        signature = self.genesis_private_key.sign(data, ec.ECDSA(hashes.SHA256()))
        pubkey_bytes = get_public_key_bytes_compressed(self.genesis_public_key)

        stack = [data, signature, pubkey_bytes]
        # no exception should be raised and data is left on stack
        op_checkdatasig(stack, log=[], extras=None)
        self.assertEqual(data, stack.pop())

        stack = [b'data_not_matching', signature, pubkey_bytes]
        with self.assertRaises(OracleChecksigFailed):
            op_checkdatasig(stack, log=[], extras=None)