Example #1
0
    def test_get_transaction(self):
        tx = Transaction()
        input = TransactionInput()
        input.address = ""
        tx.add_input(input)
        tx.generate_hash()

        mp = Mempool()
        mp.flush_mempool()
        mp.add_transaction(tx)
        self.assertEqual(mp.get_transaction().to_json(), tx.to_json())
        mp.close()
Example #2
0
    def test_remove_transaction(self):
        tx = Transaction()
        input = TransactionInput()
        input.address = ""
        tx.add_input(input)
        tx.generate_hash()

        mp = Mempool()
        mp.flush_mempool()
        mp.add_transaction(tx)
        self.assertEqual(mp.get_size(), 1)
        mp.remove_transaction(tx.hash)
        self.assertEqual(mp.get_size(), 0)
        mp.close()
Example #3
0
def create_tx():
    add_data = {
        "address": "1d3f347aada53547142da8edea5e0019e6ef31bb15",
        "vk": "-----BEGIN PUBLIC KEY-----\nMEkwEwYHKoZIzj0CAQYIKoZIzj0DAQEDMgAEHxzkpgh/lqgd1/rb7d+D+srhlhzG\ncOAveBQafVnHkffNR2aCiFHVQZKzhyO7iC/p\n-----END PUBLIC KEY-----\n",
        "sk": "-----BEGIN EC PRIVATE KEY-----\nMF8CAQEEGKtkXJ8xlejSSOl4mzNTYiXmb70wRByW1aAKBggqhkjOPQMBAaE0AzIA\nBB8c5KYIf5aoHdf62+3fg/rK4ZYcxnDgL3gUGn1Zx5H3zUdmgohR1UGSs4cju4gv\n6Q==\n-----END EC PRIVATE KEY-----\n"
    }
    tx = Transaction()
    add = Address()
    add.load(add_data["sk"], add_data["vk"])
    tx.inputs.append(TransactionInput.from_json({
        'previous_tx': "16e8dab3a9185d5329fac9cfdc0a81c7817826f701e747cb3751878061e4dc8c",
        'index': 0,
        'scriptSig': [add.sign_message(json.dumps({
            'previous_tx': "16e8dab3a9185d5329fac9cfdc0a81c7817826f701e747cb3751878061e4dc8c",
            'index': 0
        }).encode("utf-8")).hex()],
        'address': add_data["address"],
        'verifying_key': [add_data["vk"]]
    }))
    tx.outputs.append(TransactionOutput.from_json({
        'address': "yikgyyf67rr68t887tfc",
        'value': 25,
        'n': 0
    }))
    tx.generate_hash()
    txf = tx.to_json()
    return txf
Example #4
0
def create_fake_transaction(address: Address):
    tmp_tx = Transaction()
    add = Address()
    add.load(add_data["sk"], add_data["vk"])

    tmp_tx.inputs.append(
        TransactionInput.from_json({
            'previous_tx':
            "16e8dab3a9185d5329fac9cfdc0a81c7817826f701e747cb3751878061e4dc8c",
            'index':
            0,
            'scriptSig': [
                add.sign_message(
                    json.dumps({
                        'previous_tx':
                        "16e8dab3a9185d5329fac9cfdc0a81c7817826f701e747cb3751878061e4dc8c",
                        'index': 0
                    }).encode("utf-8")).hex()
            ],
            'address':
            add_data["address"],
            'verifying_key': [add_data["vk"]]
        }))
    tmp_tx.outputs.append(
        TransactionOutput.from_json({
            'address': "yikgyyf67rr68t887tfc",
            'value': 25,
            'n': 0
        }))
    tmp_tx.generate_hash()

    return tmp_tx
    def test_txs_by_address(self):
        blockchain = BlockChain()
        blockchain.flush_chain()
        block = Block()
        tx = Transaction()
        input1 = TransactionInput()
        input1.address = "test_address"
        tx.add_input(input1)
        tx.generate_hash()
        block.add_transaction(tx)

        tx1 = Transaction()
        output = TransactionOutput()
        output.address = "fake_address"
        output1 = TransactionOutput()
        output1.address = "test_address"
        tx1.add_output(output)
        tx1.add_output(output1)
        tx1.generate_hash()
        block.add_transaction(tx1)

        tx2 = Transaction()
        output2 = TransactionOutput()
        output2.address = "fake_address"
        tx2.add_output(output2)
        tx2.generate_hash()
        
        block.add_transaction(tx2)
        block.compute_hash()

        blockchain.add_block(block)
        txs = blockchain.get_txs_by_addr("test_address")
        getb = blockchain.get_block(-1)
        print(getb.to_json())
        for tx in txs:
            print(tx.hash, end='\n')

        self.assertEqual(len(txs), 2)
        blockchain.close()
Example #6
0
    def from_json(data):
        tmp = Transaction()
        tmp.timestamp = data['timestamp']
        tmp.version = data['version']
        tmp.hash = data['hash']
        tmp.inputs = [
            TransactionInput.from_json(input) for input in data['inputs']
        ]
        tmp.outputs = [
            TransactionOutput.from_json(output) for output in data['outputs']
        ]
        tmp.block = data['block']

        return tmp
Example #7
0
 def to_json(self):
     return {
         'timestamp':
         self.timestamp,
         'version':
         self.version,
         'hash':
         self.hash,
         'inputs':
         [TransactionInput.to_json(input) for input in self.inputs],
         'outputs':
         [TransactionOutput.to_json(output) for output in self.outputs],
         'block':
         self.block
     }
Example #8
0
    def test_merkle_root(self):
        block = Block()
        tx = Transaction()
        input = TransactionInput()
        input.address = "test_address"
        tx.add_input(input)
        tx.generate_hash()
        block.add_transaction(tx)
        block.calculate_merkle_root()
        initial_merkle_root = block.merkle_root

        tx1 = Transaction()
        input1 = TransactionInput()
        input1.address = "another_test_address"
        tx1.add_input(input1)
        tx1.generate_hash()
        block.add_transaction(tx1)
        block.calculate_merkle_root()
        final_merkle_root = block.merkle_root

        self.assertNotEqual(initial_merkle_root, final_merkle_root)