def create_coinbase_transaction(self): # Adds coinbase transaction to each mined block tx = Transaction() output = TransactionOutput() read_file = open("node_data.json", "r") raw = read_file.read() my_node = json.loads(raw) output.value = 50 output.address = my_node["address"] output.n = 0 tx.add_output(output) tx.generate_hash() self.add_transaction(tx)
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
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 }
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) print(len(txs)) blockchain.close()
def create_tx(addr): 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"]) # print(addr) tx.inputs = [] 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': addr, 'verifying_key': [add_data["vk"]] })) tx.outputs.append( TransactionOutput.from_json({ 'address': "yikgyyf67rr68t887tfc", 'value': 25, 'n': 0 })) tx.generate_hash() txf = tx.to_json() # print(txf['inputs'][0]['address']) return txf
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': "12345", 'value': 25, 'n': 0 })) tmp_tx.generate_hash() return tmp_tx
from trucoin.Block import Block from trucoin.Transaction import Transaction from trucoin.TransactionInput import TransactionInput from trucoin.TransactionOutput import TransactionOutput import time import sys from utils import getsize, getbytes output = TransactionOutput() output.address = "1d3f347aada53547142da8edea5e0019e6ef31bb15" output.n = 0 output.value = 50 transaction = Transaction() transaction.add_output(output) transaction.is_coinbase = True transaction.hash = "eef9fda50a6bf6c11c5078d8772d94jk" block = Block() block.add_transaction(transaction) block.calculate_merkle_root() block.compute_hash() block.miner = "1d3f347aada53547142da8edea5e0019e6ef31bb15jk" block.size = getbytes(block) print(block.__dict__) message = { "hash": block.hash, "timestamp": block.timestamp, "transaction": block.transactions[0].broadcast_transaction(), "previous_block_hash": block.previous_block_hash, "merkle_root": block.merkle_root, "height": block.height, "miner": block.miner, "version": block.version,