def generate_transaction(self, amount): self.signature_dict['signature_type'] = self.signature_type self.receiver_signature_dict['signature_type'] = self.signature_type new_transaction = Transaction( debit_user_public_key=blockchain.get_user_public_key( self.signature_dict), credit_user_public_key=blockchain.get_user_public_key( self.receiver_signature_dict), transaction_value=amount) new_transaction.sign(self.signature_dict, self.signature_type) return new_transaction.serialize()
def create_gift_coin(self, blockchain): """ This method creates a new coin for the new client """ # Get address of the client hash_public_key = self.hash_pubkey() coin_gift_tx_input = TransactionInput(prev_tx="1" * 64, pk_spender="1" * 64, signature=bytes( "\x11" * 64, encoding="utf-8")) coin_gift_tx_output = TransactionOutput( value=100, hash_pubkey_recipient=hash_public_key) coin_gift_tx = Transaction(tx_input=coin_gift_tx_input, tx_output=coin_gift_tx_output) transactions = [coin_gift_tx] nonce = 0 new_block = None while True: if len(blockchain.blocks) != 0: hash_prev_block = blockchain.blocks[len(blockchain.blocks) - 1].get_hash() new_block = Block(transactions, nonce, hash_prev_block) else: new_block = Block(transactions=transactions, nonce=nonce, prev_block_hash="0" * 64) if new_block.get_hash().startswith("0" * blockchain.difficulty): print("Nonce found:", nonce) break nonce += 1 print(coin_gift_tx.serialize()) message = "\x12" + new_block.serialize() self.socket.sendall(message.encode('utf-8'))
def send_message(self): while True: input_command = input() # This variable is set to True when is a server command send_message_to_server = True if input_command.startswith("cmd_new_tx"): # Show available transactions block_number = 0 print("==> List of available transactions:") print(self.blockchain.blocks) tx_unspent_counter = 0 for block in self.blockchain.blocks: transaction_number = 0 for transaction in block.transactions: if transaction.tx_output.hash_pubkey_recipient == self.hash_pubkey() and \ not transaction.is_already_spent(self.blockchain): tx_unspent_counter += 1 print("\t -- [ B:", block_number, " - T:", transaction_number, "]", "Value:", transaction.tx_output.value) transaction_number += 1 block_number += 1 if tx_unspent_counter > 0: block_number_spend = int(input("\tSelect block number: ")) transaction_number_spend = int( input("\tSelect transaction number: ")) transaction_spend = self.blockchain.blocks[ block_number_spend].transactions[ transaction_number_spend] address = input("\tAddress: ") value = int(input("\tValue: ")) signature_information = transaction_spend.get_hash() + \ transaction_spend.tx_output.hash_pubkey_recipient + \ address + \ str(value) route_private_key = "private_keys/" + str( self.socket.getsockname()[1]) + "_private_key.pem" hash_message = SHA256.new( signature_information.encode("utf-8")) private_key = ECC.import_key( open(route_private_key).read()) signer = DSS.new(private_key, "fips-186-3") signature = signer.sign(hash_message) new_tx_input = TransactionInput( prev_tx=transaction_spend.get_hash(), signature=signature, pk_spender=self.load_public_key()) new_tx_output = TransactionOutput( value=value, hash_pubkey_recipient=address) new_transaction = Transaction(tx_input=new_tx_input, tx_output=new_tx_output) print("\t-- Signing and sending transaction.") message = "\x10" + new_transaction.serialize() else: print("\t-- You do not have unspent transactions") send_message_to_server = False elif input_command.startswith("cmd_show_addresses"): # This is not a server command send_message_to_server = False base_path = "public_keys/" for file in os.listdir(base_path): pubkey_file = open(base_path + file) pubkey = pubkey_file.read() hash_object = RIPEMD160.new(data=pubkey.encode("utf-8")) print("\t>>", hash_object.hexdigest(), "[", file, "]") pubkey_file.close() elif input_command.startswith("cmd_gift"): send_message_to_server = False self.create_gift_coin(self.blockchain) else: message = input_command if send_message_to_server: self.socket.sendall(message.encode('utf-8'))