Exemplo n.º 1
0
    def test_transaction(self):
        sender_wallet = Wallet()
        recipient = "richard_west"
        amount = 100
        transaction = Transaction(sender_wallet, recipient, amount)

        # Assert that transaction output contains the correct recipient amount
        self.assertEqual(transaction.output[recipient], amount)

        # Assert that transaction output contains the correct new/remaining balance of senders wallet
        self.assertEqual(transaction.output[sender_wallet.address],
                         sender_wallet.balance - amount)

        # Assert a timestamp exists in the transaction input
        self.assertIn("timestamp", transaction.input)

        # Assert correct values in the transaction input
        self.assertEqual(transaction.input["amount"], sender_wallet.balance)
        self.assertEqual(transaction.input["address"], sender_wallet.address)
        self.assertEqual(transaction.input["public_key"],
                         sender_wallet.public_key)

        # Verify the signature to ensure correctness
        self.assertTrue(
            Wallet.verify(
                transaction.input["public_key"],
                transaction.output,
                transaction.input["signature"],
            ))
Exemplo n.º 2
0
    def test_verify_valid_signature(self):
        data = {"SHUcoin": "Is the best new cryptocurrency"}

        wallet = Wallet()
        signature = wallet.sign(data)

        self.assertTrue(Wallet.verify(wallet.public_key, data, signature))
    def isValidTransaction(transaction):
        '''
        validates and raises an exception if invalid.
        '''

        if not Wallet.verify(transaction.input['publicKey'],
                             transaction.output,
                             transaction.input['signature']):
            raise Exception('Invalid signature')
Exemplo n.º 4
0
    def is_valid(transaction):
        """Validates a transaction"""
        output_total = sum(transaction.output.values())

        if transaction.input == MINING_REWARD_INPUT:
            if list(transaction.output.values()) != [MINING_REWARD]:
                raise MiningRewardError()
            # return if mining reward is valid.
            return

        if transaction.input["amount"] != output_total:
            raise TransactionValuesError()

        if not Wallet.verify(
            transaction.input["public_key"],
            transaction.output,
            transaction.input["signature"],
        ):
            raise TransactionSignatureError()
Exemplo n.º 5
0
    def test_verify_invalid_signature(self):
        data = {"SHUcoin": "Is the best new cryptocurrency"}
        signature = Wallet().sign(data)

        # Pass an incorrect public key to verify method. This fails as the public key isn't a pair with the private key used to generate the signature.
        self.assertFalse(Wallet.verify(Wallet().public_key, data, signature))
def test_verifyInvalidSignature():
    data = {'foo': 'test data'}
    wallet = Wallet()
    signature = wallet.sign(data)

    assert not Wallet.verify(Wallet().public_key, data, signature) 
Exemplo n.º 7
0
    def test_transaction_update(self):
        sender_wallet = Wallet()
        first_recipient_address = "richard_west"
        first_recipient_amount = 25

        second_recipient_address = "jing_wang"
        second_recipient_amount = 25

        transaction = Transaction(sender_wallet, first_recipient_address,
                                  first_recipient_amount)

        # Update transaction
        transaction.update(sender_wallet, second_recipient_address,
                           second_recipient_amount)

        # Assert that transaction output contains the correct first recipient amount
        self.assertEqual(transaction.output[first_recipient_address],
                         first_recipient_amount)

        # Assert that transaction output contains the correct second recipients amount
        self.assertEqual(transaction.output[second_recipient_address],
                         second_recipient_amount)

        # Assert that transaction output contains the correct new/remaining balance of senders wallet
        self.assertEqual(
            transaction.output[sender_wallet.address],
            sender_wallet.balance - first_recipient_amount -
            second_recipient_amount,
        )

        # Verify the signature to ensure correctness. This will have been resigned.
        self.assertTrue(
            Wallet.verify(
                transaction.input["public_key"],
                transaction.output,
                transaction.input["signature"],
            ))

        # Test sending an additional amount to the same recipient
        first_recipient_additional_amount = 25
        transaction.update(sender_wallet, first_recipient_address,
                           first_recipient_additional_amount)

        # Assert that transaction output contains the correct first recipient amount
        self.assertEqual(
            transaction.output[first_recipient_address],
            first_recipient_amount + first_recipient_additional_amount,
        )

        # Assert that transaction output contains the correct new/remaining balance of senders wallet
        self.assertEqual(
            transaction.output[sender_wallet.address],
            sender_wallet.balance - first_recipient_amount -
            second_recipient_amount - first_recipient_additional_amount,
        )

        # Verify the signature to ensure correctness. This will have been resigned.
        self.assertTrue(
            Wallet.verify(
                transaction.input["public_key"],
                transaction.output,
                transaction.input["signature"],
            ))