def __init__(self, certificates, prefix): self.prefix = prefix for c in certificates: c.id = self.prefix + str(c.id) self.certificates = {str(c.id) : c for c in certificates} self.private_key, self.public_key = generate_keys() self.base_url = 'http://janky.satyarth.me:5000/' certificates_strings = [str(c) for c in certificates] self.mht = MerkTree(certificates_strings) self.mht.create_tree() self.mht_root = self.mht.Get_Root_leaf() self.signature = sign(self.mht_root, self.private_key) self.transaction = None
def save_tx_list(the_list, filename): fp = open(filename, 'wb') pickle.dump(the_list, fp) fp.close() return True if __name__ == '__main__': import signatures import threading import time my_pr, my_pu = signatures.generate_keys() t1 = threading.Thread(target=miner_server, args=(('localhost', 5005), )) t2 = threading.Thread(target=nonce_finder, args=(wallet_list, my_pu)) server = socketutils.new_server_connection('localhost', 5006) t1.start() t2.start() pr1, pu1 = signatures.generate_keys() pr2, pu2 = signatures.generate_keys() pr3, pu3 = signatures.generate_keys() Tx1 = transaction.Tx() Tx2 = transaction.Tx()
this_block = b while this_block != None: if new_block.previous_hash == this_block.previous_hash: found = True new_block.previous_block = this_block.previous_block if not new_block in head_blocks: head_blocks.append(new_block) if verbose: print("Added new sister block") this_block = this_block.previous_block if not found: print("Error! Couldn't find a parent for new_block") if __name__ == '__main__': (pr1, pu1) = generate_keys() (pr2, pu2) = generate_keys() (pr3, pu3) = generate_keys() pu_indeces = {} def indexed_input(txn_inout, public_key, amt, index_map): if not public_key in index_map: index_map[public_key] = 0 txn_inout.add_input(public_key, amt, index_map[public_key]) index_map[public_key] = index_map[public_key] + 1 tx1 = txn() indexed_input(tx1, pu1, 1, pu_indeces) tx1.add_output(pu2, 1) tx1.sign(pr1)
def make_new_keys(): wallet.my_private, wallet.my_public = signatures.generate_keys() signatures.save_public(wallet.my_public, "public.key") signatures.save_private(wallet.my_private, "private.key") return None
repr_str = repr_str + 'OUTPUTS:/n' for addr, amt in self.outputs: repr_str = repr_str + str(amt) + ' to ' + str(addr) + '/n' repr_str = repr_str + 'REQUIRED:/n' for r in self.required_sigs: repr_str = repr_str + str(r) + '/n' repr_str = repr_str + 'SIGS:/n' for s in self.sigs: repr_str = repr_str + str(s) + '/n' repr_str = repr_str + 'END/n' return repr_str #testing if __name__ == "__main__": pr1, pu1 = signatures.generate_keys() pr2, pu2 = signatures.generate_keys() pr3, pu3 = signatures.generate_keys() pr4, pu4 = signatures.generate_keys() Tx1 = Tx() Tx1.add_input(pu1, 1) Tx1.add_output(pu2, 1) Tx1.sign(pr1) if Tx1.is_valid(): print("Success! Tx is valid") else: print("ERROR! Tx is invalid") Tx2 = Tx() Tx2.add_input(pu1, 2)
repr_str += "\t" + str(r) + "\n" repr_str = "SIGNATURES: \n" for s in self.sigs: repr_str += "\t" + str(s) + "\n" return repr_str def __gather(self): data = [] data.append(self.inputs) data.append(self.outputs) data.append(self.reqd) return data if __name__ == "__main__": pr1, pu1 = generate_keys() pr2, pu2 = generate_keys() pr3, pu3 = generate_keys() pr4, pu4 = generate_keys() Tx1 = Tx() Tx1.add_input(pu1, 1) Tx1.add_output(pu2, 1) Tx1.sign(pr1) if Tx1.is_valid(): print("Success! Tx is valid") else: print("ERROR! Tx is invalid") Tx2 = Tx() Tx2.add_input(pu1, 2)
#Wallet import socket_utils import transactions import transaction_block import pickle import signatures head_blocks = [None] wallets = [('localhost', 5006)] miners = [('localhost', 5005), ('localhost', 5007)] miners = [('localhost', 5005)] break_now = False verbose = True my_public, my_private = signatures.generate_keys() txn_index = {} def thief(my_addr): my_ip, my_port = my_addr server = socket_utils.new_server_connection(my_ip, my_port) # Get Txs from wallets while not break_now: new_txn = socket_utils.recv_object(server) if isinstance(new_txn, transactions.txn): for ip, port in miners: if not (ip == my_ip and port == my_port): socket_utils.send_object(ip, new_txn, port) def stop_all(): global break_now