コード例 #1
0
 def create_genesis_block(self, node_pool=None):
     assert len(
         node_pool
     ) > 0, "The system cannot have 0 nodes initially during the creation of genesis block"
     genesis_txn = Transaction(version=0,
                               lock_time=0,
                               coin=100,
                               address=node_pool[0].get_address())
     for node in node_pool[1:]:
         pubkey = node.get_address()
         genesis_txn.add_output(pubkey, 100)
     genesis_txn.finalize()
     return genesis_txn
コード例 #2
0
	def create_random_txn(self):
		# look at the block chain and see how much money you have
		if self.first_txn and (self.len_list == len(self.temp_utxo_list)):
			return None
		total_cash = self.blockchain.get_max_height_node_UTXO_pool().get_total_unspent_utxo()
		if len(total_cash) > 0:
			my_utxos_and_values = total_cash[hex(generate_hash(str((self.public_key.e,self.public_key.n))))]
			if not self.first_txn:
				self.len_list = len(my_utxos_and_values)

			#create blank Transactions
			new_txn = Transaction()
			# choose random (utxo, op) and add them to inputs

			if len(my_utxos_and_values) == 0:
				return None
			r = randint(1, len(my_utxos_and_values)) if len(my_utxos_and_values) > 1 else 1
			chosen_utxos = sample(my_utxos_and_values, r)
			self.temp_utxo_list += chosen_utxos
			value = 0
			for utxo, op_value in chosen_utxos:
				new_txn.add_input(utxo.txn_hash, utxo.index)
				value += op_value
			# Choose a 'k' random neighbours and send a random amts to them
			k = randint(1, len(self.node_pool))
			lucky_neighbours = sample(self.node_pool.keys(), k)
			for neigh in lucky_neighbours:
				if value <= 0:
					break
				neigh_value = randint(0, value)
				if neigh_value == 0:
					continue
				new_txn.add_output(self.node_pool_address[neigh], neigh_value)
				value -= neigh_value
			if value > 0:
				new_txn.add_output(self.public_key, value)
			for i in range(new_txn.total_inputs()):
				message = new_txn.get_raw_signature(i)
				signature = sign_message(message=message, keyPair=self.key_pair)
				new_txn.add_signature(i, signature)
				new_txn.get_input(i).add_unlocking_script(signature, self.public_key)
			new_txn.finalize()
			self.first_txn = True
			return new_txn
		else:
			print(f'Node {self.id} could not create a transaction. not utxos ')
		return None
コード例 #3
0
from Signature import *
from Transaction import Transaction
from TransactionBlockChain import TransactionBlockChain, TransactionBlockChainNode
import pickle

if __name__ == "__main__":
    public_key_1, private_key_1 = generate_keys()
    public_key_2, private_key_2 = generate_keys()
    public_key_3, private_key_3 = generate_keys()

    transaction_1 = Transaction("Transaction meant for dumping")
    transaction_1.add_input(public_key_1, 1)
    transaction_1.add_output(public_key_2, 1)
    transaction_1.sign(private_key_1)

    print(transaction_1)
    with open("tx.dat", "wb") as storage_file:
        pickle.dump(transaction_1, storage_file)

    with open("tx.dat", "rb") as read_file:
        transaction_1 = pickle.load(read_file)
    print(transaction_1)

    my_blockchain = TransactionBlockChain("my_blockchain for testing how it works")
    my_blockchain.add_transaction(transaction_1)

    transaction_2 = Transaction("Another Transaction")
    transaction_2.add_input(public_key_2, 3)
    transaction_2.add_output(public_key_3, 3)
    transaction_2.sign(private_key_2)
    my_blockchain.add_transaction(transaction_2)
コード例 #4
0
from Transaction import Transaction
from Signature import *

if __name__ == "__main__":
    transactions = []
    public_key_1, private_key_1 = generate_keys()
    public_key_2, private_key_2 = generate_keys()
    public_key_3, private_key_3 = generate_keys()
    public_key_4, private_key_4 = generate_keys()

    new_transaction = Transaction("correct_transaction_1_to_1")
    new_transaction.add_input(public_key_1, 1)
    new_transaction.add_output(public_key_2, 1)
    new_transaction.sign(private_key_1)
    transactions.append(new_transaction)

    new_transaction = Transaction("correct_transaction_1_to_2")
    new_transaction.add_input(public_key_1, 2)
    new_transaction.add_output(public_key_2, 1)
    new_transaction.add_output(public_key_3, 1)
    new_transaction.sign(private_key_1)
    transactions.append(new_transaction)

    new_transaction = Transaction("correct_transaction_1_to_1_with_leftover")
    new_transaction.add_input(public_key_1, 1.2)
    new_transaction.add_output(public_key_2, 1.1)
    new_transaction.sign(private_key_1)
    transactions.append(new_transaction)

    new_transaction = Transaction(
        "incorrect_transaction_1_to_1_invalid_private_key")