def blocklast(socket): #get last block connections.send(s, "blocklast", 10) hash_last = connections.receive(s, 10) print("Last block number: {}".format(hash_last[0])) print("Last block hash: {}".format(hash_last[1]))
def nodes_block_submit(block_send): # connect to all nodes global peer_dict peer_dict = {} with open(peerlist) as f: for line in f: line = re.sub("[\)\(\:\\n\'\s]", "", line) peer_dict[line.split(",")[0]] = line.split(",")[1] for k, v in peer_dict.items(): peer_ip = k # app_log.info(HOST) peer_port = int(v) # app_log.info(PORT) # connect to all nodes try: s_peer = socks.socksocket() s_peer.settimeout(0.3) if tor_conf == 1: s_peer.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s_peer.connect((peer_ip, peer_port)) # connect to node in peerlist print("Connected") print("Miner: Proceeding to submit mined block to node") connections.send(s_peer, "block", 10) connections.send(s_peer, block_send, 10) print("Miner: Block submitted to node {}".format(peer_ip)) except Exception as e: print("Miner: Could not submit block to node {} because {}".format(peer_ip, e)) pass
def send_confirm(amount_input, recipient_input, keep_input, openfield_input): top10 = Toplevel() top10.title("Confirm") # encr check if encrypt_var.get() == 1: #get recipient's public key connections.send(s, "pubkeyget", 10) connections.send(s, recipient_input, 10) target_public_key_hashed = connections.receive(s, 10) recipient_key = RSA.importKey(base64.b64decode(target_public_key_hashed).decode("utf-8")) #openfield_input = str(target_public_key.encrypt(openfield_input.encode("utf-8"), 32)) data = openfield_input.encode("utf-8") # print (open("pubkey.der").read()) session_key = get_random_bytes(16) cipher_aes = AES.new(session_key, AES.MODE_EAX) # Encrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(recipient_key) # Encrypt the data with the AES session key ciphertext, tag = cipher_aes.encrypt_and_digest(data) enc_session_key = (cipher_rsa.encrypt(session_key)) openfield_input = str([x for x in (cipher_aes.nonce, tag, ciphertext, enc_session_key)]) # encr check # msg check if encode_var.get() == 1: openfield_input = base64.b64encode(openfield_input.encode("utf-8")).decode("utf-8") # msg check if msg_var.get() == 1 and encode_var.get() == 1: openfield_input = "bmsg=" + openfield_input if msg_var.get() == 1 and encode_var.get() == 0: openfield_input = "msg=" + openfield_input if encrypt_var.get() == 1: openfield_input = "enc=" + str(openfield_input) fee = fee_calculate(openfield_input, keep_var.get()) confirmation_dialog = Text(top10, width=100) confirmation_dialog.insert(INSERT, ("Amount: {}\nFee: {}\nTotal: {}\nTo: {}\nKeep Entry: {}\nOpenField:\n\n{}".format(amount_input, fee, '%.8f' % (float(amount_input)+float(fee)), recipient_input, keep_input, openfield_input))) confirmation_dialog.grid(row=0, pady=0) enter = Button(top10, text="Confirm", command=lambda: send_confirmed(amount_input, recipient_input, keep_input, openfield_input, top10)) enter.grid(row=1, column=0, sticky=W + E, padx=15, pady=(5, 5)) done = Button(top10, text="Cancel", command=top10.destroy) done.grid(row=2, column=0, sticky=W + E, padx=15, pady=(5, 5))
def msg_received_get(addlist): for x in addlist: if x[11].startswith(("msg=", "bmsg=", "enc=msg=", "enc=bmsg=")) and x[3] == address: #print(x[11]) connections.send(s, "aliasget", 10) connections.send(s, x[2], 10) msg_address = connections.receive(s,10)[0][0] if x[11].startswith("enc=msg="): msg_received_digest = x[11].lstrip("enc=msg=") try: #msg_received_digest = key.decrypt(ast.literal_eval(msg_received_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(msg_received_digest) private_key = RSA.import_key(open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_received_digest = cipher_aes.decrypt_and_verify(ciphertext, tag).decode("utf-8") except: msg_received_digest = "Could not decrypt message" elif x[11].startswith("enc=bmsg="): msg_received_digest = x[11].lstrip("enc=bmsg=") try: msg_received_digest = base64.b64decode(msg_received_digest).decode("utf-8") #msg_received_digest = key.decrypt(ast.literal_eval(msg_received_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(msg_received_digest) private_key = RSA.import_key(open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_received_digest = cipher_aes.decrypt_and_verify(ciphertext, tag).decode("utf-8") except: msg_received_digest = "Could not decrypt message" elif x[11].startswith("bmsg="): msg_received_digest = x[11].lstrip("bmsg=") try: msg_received_digest = base64.b64decode(msg_received_digest).decode("utf-8") except: msg_received_digest = "Could not decode message" elif x[11].startswith("msg="): msg_received_digest = x[11].lstrip("msg=") msg_received.insert(INSERT, ((time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(float(x[1])))) + " From " + msg_address.lstrip("alias=") + ": " + msg_received_digest) + "\n")
def addlist(socket, arg1): #get all txs for an address connections.send(s, "addlist", 10) connections.send(s, arg1, 10) address_tx_list = connections.receive(s, 10) print("All transactions for requested address:") for row in address_tx_list: print(row)
def listlim(socket, arg1): #get all txs for an address connections.send(s, "listlim", 10) connections.send(s, arg1, 10) tx_list = connections.receive(s, 10) print("All transactions for requested range:") for row in tx_list: print(row)
def aliasesget(socket, arg1): arg_split = arg1.split(",") print(arg_split) connections.send(s, "aliasesget", 10) connections.send(s, arg_split, 10) alias_results = connections.receive(s, 10) print(alias_results)
def difflast(socket): #ask for last difficulty connections.send(s, "difflast", 10) response = connections.receive(s, 10) blocklast = response[0] difflast = response[1] print("Last block: {}".format(blocklast)) print("Last difficulty: {}".format(difflast))
def blockget(socket, arg1): #get block connections.send(s, "blockget", 10) connections.send(s, arg1, 10) block_get = connections.receive(s, 10) print("Requested block: {}".format(block_get)) print("Requested block number of transactions: {}".format(len(block_get))) print("Requested block height: {}".format(block_get[0][0]))
def keygen(socket): #generate address #RECEIVES PRIVATE KEY FROM NODE connections.send(s, "keygen", 10) keys_generated = connections.receive(s, 10) print("Private key: {}".format(keys_generated[0])) print("Public key: {}".format(keys_generated[1])) print("Address: {}".format(keys_generated[2]))
def balanceget(socket, arg1): #get balance connections.send(s, "balanceget", 10) connections.send(s, arg1, 10) #balance_ledger = connections.receive(s, 10) balance_ledger = connections.receive(s, 10) print("Address balance: {}".format(balance_ledger[0])) print("Address credit: {}".format(balance_ledger[1])) print("Address debit: {}".format(balance_ledger[2])) print("Address fees: {}".format(balance_ledger[3])) print("Address rewards: {}".format(balance_ledger[4]))
def alias_register(alias_desired): connections.send(s, "aliascheck", 10) connections.send(s, alias_desired, 10) result = connections.receive(s, 10) if result == "Alias free": send("0", myaddress, "1", "alias="+alias_desired) pass else: top9 = Toplevel() top9.title("Name already registered") registered_label = Label(top9, text="Name already registered") registered_label.grid(row=0, column=0, sticky=N + W, padx=15, pady=(5, 0)) dismiss = Button(top9, text="Dismiss", command=top9.destroy) dismiss.grid(row=3, column=0, sticky=W + E, padx=15, pady=(5, 5))
def aliases_list(): top12 = Toplevel() top12.title("Your aliases") aliases_box = Text(top12, width=100) aliases_box.grid(row=0, pady=0) connections.send(s, "aliasget", 10) connections.send(s, myaddress, 10) aliases_self = connections.receive(s, 10) for x in aliases_self: aliases_box.insert(INSERT, x[0].lstrip("alias=")) aliases_box.insert(INSERT,"\n") close = Button(top12, text="Close", command=top12.destroy) close.grid(row=3, column=0, sticky=W + E, padx=15, pady=(5, 5))
def txsend(socket, arg1, arg2, arg3, arg4, arg5): #generate transaction #SENDS PRIVATE KEY TO NODE connections.send(s, "txsend", 10) remote_tx_timestamp = '%.2f' % time.time() remote_tx_privkey = arg1 #node will dump pubkey+address from this remote_tx_recipient = arg2 remote_tx_amount = arg3 remote_tx_keep = arg4 remote_tx_openfield = arg5 #connections.send(s, (remote_tx_timestamp, remote_tx_privkey, remote_tx_recipient, remote_tx_amount, remote_tx_keep, remote_tx_openfield), 10) connections.send(s, (str(remote_tx_timestamp), str(remote_tx_privkey), str(remote_tx_recipient), str(remote_tx_amount), str(remote_tx_keep), str(remote_tx_openfield)), 10) #generate transaction signature = connections.receive(s, 10) print(signature)
def statusget(socket): connections.send(s, "statusget", 10) response = connections.receive(s, 10) node_address = response[0] nodes_count = response[1] nodes_list = response[2] threads_count = response[3] uptime = response[4] consensus = response[5] consensus_percentage = response[6] version = response[7] print("Node address:", node_address) print("Number of nodes:", nodes_count) print("List of nodes:", nodes_list) print("Number of threads:", threads_count) print("Uptime:", uptime) print("Consensus:", consensus) print("Consensus percentage:", consensus_percentage) print("Version:", version)
def txsend(socket, arg1, arg2, arg3, arg4, arg5): #generate transaction #SENDS PRIVATE KEY TO NODE connections.send(s, "txsend", 10) timestamp = '%.2f' % time.time() privkey = str(arg) #node will dump pubkey+address from this recipient = str(arg2) amount = str(arg3) keep = str(arg4) openfield = str(arg5) #connections.send(s, (remote_tx_timestamp, remote_tx_privkey, remote_tx_recipient, remote_tx_amount, remote_tx_keep, remote_tx_openfield), 10) connections.send(s, (timestamp, privkey, recipient, amount, keep, openfield), 10) #generate transaction signature = connections.receive(s, 10) print(signature)
def shutdown(socket): connections.send(s, "shutdown", 10)
def blockgetjson(socket, arg1): #get block connections.send(s, "blockgetjson") connections.send(s, arg1) response_list = connections.receive(s) print(json.dumps(response_list))
def api_gettransaction_for_recipients(self, socket_handler, db_handler, peers): """ Warning: this is currently very slow Returns the full transaction matching a tx id for a list of recipient addresses. Takes txid anf format as params (json output if format is True) :param socket_handler: :param db_handler: :param peers: :return: """ transaction = {} try: # get the txid transaction_id = connections.receive(socket_handler) # then the recipient list addresses = connections.receive(socket_handler) # and format format = connections.receive(socket_handler) recipients = json.dumps(addresses).replace("[", "(").replace( ']', ')') # format as sql # raw tx details db_handler.execute_param( db_handler.h, "SELECT * FROM transactions WHERE recipient IN {} AND signature LIKE ?" .format(recipients), (transaction_id + '%', )) raw = db_handler.h.fetchone() if not format: connections.send(socket_handler, raw) print('api_gettransaction_for_recipients', format, raw) return # current block height, needed for confirmations # db_handler.execute(db_handler.h, "SELECT MAX(block_height) FROM transactions") block_height = db_handler.h.fetchone()[0] transaction['txid'] = transaction_id transaction['time'] = raw[1] transaction['hash'] = raw[5] transaction['address'] = raw[2] transaction['recipient'] = raw[3] transaction['amount'] = raw[4] transaction['fee'] = raw[8] transaction['reward'] = raw[9] transaction['operation'] = raw[10] transaction['openfield'] = raw[11] transaction['pubkey'] = base64.b64decode(raw[6]).decode('utf-8') transaction['blockhash'] = raw[7] transaction['blockheight'] = raw[0] transaction['confirmations'] = block_height - raw[0] # Get more info on the block the tx is in. db_handler.execute_param( db_handler.h, "SELECT timestamp, recipient FROM transactions WHERE block_height= ? AND reward > 0", (raw[0], )) block_data = db_handler.h.fetchone() transaction['blocktime'] = block_data[0] transaction['blockminer'] = block_data[1] print('api_gettransaction_for_recipients', format, transaction) connections.send(socket_handler, transaction) except Exception as e: raise
def difflastjson(socket): #ask for last difficulty connections.send(s, "difflastjson") response = connections.receive(s) print(json.dumps(response))
def peersget(socket): connections.send(s, "peersget", 10) peers_received = connections.receive(s, 10) print(peers_received)
def diffget(s): connections.send(s, "diffget", 10) diff = float(connections.receive(s, 10)[1]) return diff
def balancegethyper(socket, arg1): #get balance connections.send(s, "balancegethyper") connections.send(s, arg1) balanceget_result = connections.receive(s) print("Address balance: {}".format(balanceget_result))
def addvalidate(socket, arg1): connections.send(s, "addvalidate") connections.send(s, arg1) validate_result = connections.receive(s) print(validate_result)
def stop(socket): connections.send(s, "stop")
def peersget(socket): connections.send(s, "peersget") peers_received = connections.receive(s) print(peers_received)
def statusget(socket): connections.send(s, "statusjson") response = connections.receive(s) print(json.dumps(response))
def addfromalias(socket, arg1): connections.send(s, "addfromalias") connections.send(s, arg1) address_fetch = connections.receive(s) print(address_fetch)
def tokensget(socket, arg1): connections.send(s, "tokensget") connections.send(s, arg1) tokens_results = connections.receive(s) print(tokens_results)
def aliasget(socket, arg1): connections.send(s, "aliasget") connections.send(s, arg1) alias_results = connections.receive(s) print(alias_results)
def blocklastjson(socket): #get last block connections.send(s, "blocklastjson") response = connections.receive(s) print(json.dumps(response))
def annget(socket): connections.send(s, "annget") result = connections.receive(s) print(result)
def mpgetjson(socket): #ask for mempool connections.send(s, "mpgetjson") response_list = connections.receive(s) print("Current mempool:") print(json.dumps(response_list))
def send_confirm(amount_input, recipient_input, keep_input, openfield_input): top10 = Toplevel() top10.title("Confirm") # encr check if encrypt_var.get() == 1: #get recipient's public key connections.send(s, "pubkeyget", 10) connections.send(s, recipient_input, 10) target_public_key_hashed = connections.receive(s, 10) recipient_key = RSA.importKey( base64.b64decode(target_public_key_hashed).decode("utf-8")) #openfield_input = str(target_public_key.encrypt(openfield_input.encode("utf-8"), 32)) data = openfield_input.encode("utf-8") # print (open("pubkey.der").read()) session_key = get_random_bytes(16) cipher_aes = AES.new(session_key, AES.MODE_EAX) # Encrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(recipient_key) # Encrypt the data with the AES session key ciphertext, tag = cipher_aes.encrypt_and_digest(data) enc_session_key = (cipher_rsa.encrypt(session_key)) openfield_input = str( [x for x in (cipher_aes.nonce, tag, ciphertext, enc_session_key)]) # encr check # msg check if encode_var.get() == 1: openfield_input = base64.b64encode( openfield_input.encode("utf-8")).decode("utf-8") # msg check if msg_var.get() == 1 and encode_var.get() == 1: openfield_input = "bmsg=" + openfield_input if msg_var.get() == 1 and encode_var.get() == 0: openfield_input = "msg=" + openfield_input if encrypt_var.get() == 1: openfield_input = "enc=" + str(openfield_input) fee = fee_calculate(openfield_input, keep_var.get()) confirmation_dialog = Text(top10, width=100) confirmation_dialog.insert(INSERT, ( "Amount: {}\nFee: {}\nTotal: {}\nTo: {}\nKeep Entry: {}\nOpenField:\n\n{}" .format(amount_input, fee, '%.8f' % (float(amount_input) + float(fee)), recipient_input, keep_input, openfield_input))) confirmation_dialog.grid(row=0, pady=0) enter = Button( top10, text="Confirm", command=lambda: send_confirmed(amount_input, recipient_input, keep_input, openfield_input, top10)) enter.grid(row=1, column=0, sticky=W + E, padx=15, pady=(5, 5)) done = Button(top10, text="Cancel", command=top10.destroy) done.grid(row=2, column=0, sticky=W + E, padx=15, pady=(5, 5))
def handle(self): peer_ip = self.request.getpeername()[0] data = connections.receive(self.request, 10) app_log.warning("Received: {} from {}".format(data, peer_ip)) # will add custom ports later if data == 'diffp': app_log.warning("Sending the share qualification difficulty requirement: {}%".format(diff_percent_number)) connections.send(self.request, diff_percent_number, 10) if data == "block": # from miner to node # sock s1 = socks.socksocket() if tor_conf == 1: s1.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s1.connect((node_ip_conf, int(port))) # connect to local node, # sock # receive block miner_address = connections.receive(self.request, 10) app_log.warning("Received a block from miner {} ({})".format(peer_ip,miner_address)) block_send = connections.receive(self.request, 10) nonce = (block_send[-1][7]) app_log.warning("Combined mined segments: {}".format(block_send)) #print(nonce) #print(block_send) #print(miner_address) # check difficulty app_log.warning("Asking node for difficulty") diff = int(diffget(s1)) app_log.warning("Calculated difficulty: {}".format(diff)) # check difficulty app_log.warning("Asking node for last block") # get last block connections.send(s1, "blocklast", 10) blocklast = connections.receive(s1, 10) db_block_hash = blocklast[7] # get last block app_log.warning("Last Hash: {}".format(db_block_hash)) mining_hash = bin_convert(hashlib.sha224((address + nonce + db_block_hash).encode("utf-8")).hexdigest()) mining_condition = bin_convert(db_block_hash)[0:diff] if mining_condition in mining_hash: app_log.warning("Difficulty requirement satisfied for mining") app_log.warning("Sending block to node {}".format(peer_ip)) global peer_dict peer_dict = {} with open("peers.txt") as f: for line in f: line = re.sub("[\)\(\:\\n\'\s]", "", line) peer_dict[line.split(",")[0]] = line.split(",")[1] for k, v in peer_dict.items(): peer_ip = k # app_log.info(HOST) peer_port = int(v) # app_log.info(PORT) # connect to all nodes try: s = socks.socksocket() s.settimeout(0.3) if tor_conf == 1: s.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s.connect((peer_ip, int(peer_port))) # connect to node in peerlist app_log.warning("Connected") app_log.warning("Pool: Proceeding to submit mined block") connections.send(s, "block", 10) connections.send(s, block_send, 10) app_log.warning("Pool: Block submitted to {}".format(peer_ip)) except Exception as e: app_log.warning("Pool: Could not submit block to {} because {}".format(peer_ip, e)) pass diff_percentage = percentage(diff_percent_number, diff) app_log.warning("Pool: Current difficulty: Pool: {} Real: {}".format(diff_percentage,diff)) if diff < diff_percentage: diff_shares = diff else: diff_shares = diff_percentage shares = sqlite3.connect('shares.db') shares.text_factory = str s = shares.cursor() # protect against used share resubmission execute_param(s, ("SELECT nonce FROM nonces WHERE nonce = ?"), (nonce,)) try: result = s.fetchone()[0] app_log.warning("Miner trying to reuse a share, ignored") except: # protect against used share resubmission mining_condition = bin_convert(db_block_hash)[0:diff_shares] #floor set by pool if mining_condition in mining_hash: app_log.warning("Difficulty requirement satisfied for saving shares") execute_param(s, ("INSERT INTO nonces VALUES (?)"), (nonce,)) commit(shares) timestamp = '%.2f' % time.time() s.execute("INSERT INTO shares VALUES (?,?,?,?)", (str(miner_address), str(1), timestamp, "0")) shares.commit() else: app_log.warning("Difficulty requirement not satisfied for anything") s.close() s1.close()
def addvalidate(socket, arg1): connections.send(s, "addvalidate", 10) connections.send(s, arg1, 10) validate_result = connections.receive(s, 10) print(validate_result)
def diffget(socket): #check difficulty connections.send(s, "diffget") diff = connections.receive(s) print("Current difficulty: {}".format(diff))
def diffget(socket): #check difficulty connections.send(s, "diffget", 10) diff = connections.receive(s, 10) print ("Current difficulty: {}".format(diff))
def aliasget(socket, arg1): connections.send(s, "aliasget", 10) connections.send(s, arg1, 10) alias_results = connections.receive(s, 10) print(alias_results)
def mpget(socket): #ask for mempool connections.send(s, "mpget", 10) mempool = connections.receive(s, 10) print("Current mempool: {}".format(mempool))
def mpinsert(s, transaction): connections.send(s, "mpinsert") connections.send(s, transaction) confirmation = connections.receive(s) print(confirmation)
def keygenjson(socket): #generate address #RECEIVES PRIVATE KEY FROM NODE connections.send(s, "keygenjson") response = connections.receive(s) print(json.dumps(response))
connections.send(s, arg_split) alias_results = connections.receive(s) print(alias_results) def api_getaddresssince(socket, arg1, arg2, arg3): connections.send(s, "api_getaddresssince") connections.send(s, arg1) connections.send(s, arg2) connections.send(s, arg3) response = connections.receive(s) print(json.dumps(response)) if command == "getversion": connections.send(s, "getversion") print(connections.receive(s)) if command == "generate": if not is_regnet: print("Only available on regnet") sys.exit() connections.send(s, "regtest_generate") connections.send(s, arg1) print(connections.receive(s)) if command == "mpfill": if not is_regnet: print("Only available on regnet") sys.exit() connections.send(s, "regtest_mpfill")
def msg_dialogue(address): connections.send(s, "addlist", 10) connections.send(s, myaddress, 10) addlist = connections.receive(s, 10) print(addlist) def msg_received_get(addlist): for x in addlist: if x[11].startswith(("msg=", "bmsg=", "enc=msg=", "enc=bmsg=")) and x[3] == address: #print(x[11]) connections.send(s, "aliasget", 10) connections.send(s, x[2], 10) msg_address = connections.receive(s,10)[0][0] if x[11].startswith("enc=msg="): msg_received_digest = x[11].lstrip("enc=msg=") try: #msg_received_digest = key.decrypt(ast.literal_eval(msg_received_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(msg_received_digest) private_key = RSA.import_key(open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_received_digest = cipher_aes.decrypt_and_verify(ciphertext, tag).decode("utf-8") except: msg_received_digest = "Could not decrypt message" elif x[11].startswith("enc=bmsg="): msg_received_digest = x[11].lstrip("enc=bmsg=") try: msg_received_digest = base64.b64decode(msg_received_digest).decode("utf-8") #msg_received_digest = key.decrypt(ast.literal_eval(msg_received_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(msg_received_digest) private_key = RSA.import_key(open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_received_digest = cipher_aes.decrypt_and_verify(ciphertext, tag).decode("utf-8") except: msg_received_digest = "Could not decrypt message" elif x[11].startswith("bmsg="): msg_received_digest = x[11].lstrip("bmsg=") try: msg_received_digest = base64.b64decode(msg_received_digest).decode("utf-8") except: msg_received_digest = "Could not decode message" elif x[11].startswith("msg="): msg_received_digest = x[11].lstrip("msg=") msg_received.insert(INSERT, ((time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(float(x[1])))) + " From " + msg_address.lstrip("alias=") + ": " + msg_received_digest) + "\n") def msg_sent_get(addlist): for x in addlist: if x[11].startswith(("msg=", "bmsg=", "enc=msg=", "enc=bmsg=")) and x[2] == address: # print(x[11]) connections.send(s, "aliasget", 10) connections.send(s, x[3], 10) received_aliases = connections.receive(s, 10) msg_recipient = received_aliases[0][0] if x[11].startswith("enc=msg="): msg_sent_digest = x[11].lstrip("enc=msg=") try: #msg_sent_digest = key.decrypt(ast.literal_eval(msg_sent_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(msg_sent_digest) private_key = RSA.import_key(open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_sent_digest = cipher_aes.decrypt_and_verify(ciphertext, tag).decode("utf-8") except: msg_sent_digest = "Could not decrypt message" elif x[11].startswith("enc=bmsg="): msg_sent_digest = x[11].lstrip("enc=bmsg=") try: msg_sent_digest = base64.b64decode(msg_sent_digest).decode("utf-8") #msg_sent_digest = key.decrypt(ast.literal_eval(msg_sent_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(msg_sent_digest) private_key = RSA.import_key(open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_sent_digest = cipher_aes.decrypt_and_verify(ciphertext, tag).decode("utf-8") except: msg_sent_digest = "Could not decrypt message" elif x[11].startswith("bmsg="): msg_sent_digest = x[11].lstrip("bmsg=") try: msg_sent_digest = base64.b64decode(msg_sent_digest).decode("utf-8") except: msg_received_digest = "Could not decode message" elif x[11].startswith("msg="): msg_sent_digest = x[11].lstrip("msg=") msg_sent.insert(INSERT, ((time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(float(x[1])))) + " To " + msg_recipient.lstrip("alias=") + ": " + msg_sent_digest) + "\n") # popup top11 = Toplevel() top11.title("Messaging") Label(top11, text="Received:", width=20).grid(row=0) msg_received = Text(top11, width=100, height=20, font=("Tahoma", 8)) msg_received.grid(row=1, column=0, sticky=W, padx=5, pady=(5, 5)) msg_received_get(addlist) Label(top11, text="Sent:", width=20).grid(row=2) msg_sent = Text(top11, width=100, height=20, font=("Tahoma", 8)) msg_sent.grid(row=3, column=0, sticky=W, padx=5, pady=(5, 5)) msg_sent_get(addlist) dismiss = Button(top11, text="Dismiss", command=top11.destroy) dismiss.grid(row=5, column=0, sticky=W + E, padx=15, pady=(5, 5))
def miner(q, privatekey_readable, public_key_hashed, address): from Crypto.PublicKey import RSA Random.atfork() key = RSA.importKey(privatekey_readable) rndfile = Random.new() tries = 0 firstrun = True begin = time.time() if pool_conf == 1: #do not use pools public key to sign, signature will be invalid self_address = address address = pool_address #ask for diff percentage s_pool = socks.socksocket() s_pool.settimeout(0.3) if tor_conf == 1: s_pool.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s_pool.connect((pool_ip_conf, 8525)) # connect to pool print("Connected") print("Miner: Asking pool for share qualification difficulty requirement") connections.send(s_pool, "diffp", 10) pool_diff_percentage = int(connections.receive(s_pool, 10)) print("Miner: Received pool for share qualification difficulty requirement: {}%".format(pool_diff_percentage)) s_pool.close() #ask for diff percentage while True: try: # calculate new hash nonces = 0 # calculate difficulty s_node = socks.socksocket() if tor_conf == 1: s_node.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s_node.connect((node_ip_conf, int(port))) # connect to local node connections.send(s_node, "blocklast", 10) blocklast = connections.receive(s_node, 10) db_block_hash = blocklast[7] db_block_hieght = blocklast[0] db_timestamp = blocklast[1] connections.send(s_node, "diffget", 10) diff = float(connections.receive(s_node, 10)) s_node.close() if db_block_hieght > 235000: diff = int(diff[0]) time_now = time.time() if time_now > db_timestamp + 180: diff = int(diff[1]) else: diff = int(diff) diff_real = int(diff) if pool_conf == 0: diff = int(diff) else: # if pooled diff_pool = diff_real diff = percentage(pool_diff_percentage, diff_real) if diff > diff_pool: diff = diff_pool if diff < 37: diff = 37 mining_condition = bin_convert(db_block_hash)[0:diff] # block_hash = hashlib.sha224(str(block_send) + db_block_hash).hexdigest() while tries < diff_recalc_conf: start = time.time() nonce = hashlib.sha224(rndfile.read(16)).hexdigest()[:32] mining_hash = bin_convert(hashlib.sha224((address + nonce + db_block_hash).encode("utf-8")).hexdigest()) end = time.time() if tries % 2500 == 0: #limit output try: cycles_per_second = 1/(end - start) print("Thread{} {} @ {:.2f} cycles/second, difficulty: {}({}), iteration: {}".format(q, db_block_hash[:10], cycles_per_second, diff, diff_real, tries)) except: pass tries = tries + 1 if mining_condition in mining_hash: tries = 0 print("Thread {} found a good block hash in {} cycles".format(q, tries)) # serialize txs block_send = [] del block_send[:] # empty removal_signature = [] del removal_signature[:] # empty s_node = socks.socksocket() if tor_conf == 1: s_node.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s_node.connect((node_ip_conf, int(port))) # connect to config.txt node connections.send(s_node, "mpget", 10) data = connections.receive(s_node, 10) s_node.close() if data != "[]": mempool = data for mpdata in mempool: transaction = ( str(mpdata[0]), str(mpdata[1][:56]), str(mpdata[2][:56]), '%.8f' % float(mpdata[3]), str(mpdata[4]), str(mpdata[5]), str(mpdata[6]), str(mpdata[7])) # create tuple # print transaction block_send.append(transaction) # append tuple to list for each run removal_signature.append(str(mpdata[4])) # for removal after successful mining # claim reward block_timestamp = '%.2f' % time.time() transaction_reward = (str(block_timestamp), str(address[:56]), str(address[:56]), '%.8f' % float(0), "0", str(nonce)) # only this part is signed! # print transaction_reward h = SHA.new(str(transaction_reward).encode("utf-8")) signer = PKCS1_v1_5.new(key) signature = signer.sign(h) signature_enc = base64.b64encode(signature) if signer.verify(h, signature) == True: print("Signature valid") block_send.append((str(block_timestamp), str(address[:56]), str(address[:56]), '%.8f' % float(0), str(signature_enc.decode("utf-8")), str(public_key_hashed), "0", str(nonce))) # mining reward tx print("Block to send: {}".format(block_send)) # claim reward # include data tries = 0 # submit mined block to node if sync_conf == 1: check_uptodate(300) if pool_conf == 1: mining_condition = bin_convert(db_block_hash)[0:diff_real] if mining_condition in mining_hash: print("Miner: Submitting block to all nodes, because it satisfies real difficulty too") nodes_block_submit(block_send) try: s_pool = socks.socksocket() s_pool.settimeout(0.3) if tor_conf == 1: s_pool.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s_pool.connect((pool_ip_conf, 8525)) # connect to pool print("Connected") print("Miner: Proceeding to submit mined block to pool") connections.send(s_pool, "block", 10) connections.send(s_pool, self_address, 10) connections.send(s_pool, block_send, 10) s_pool.close() print("Miner: Block submitted to pool") except Exception as e: print("Miner: Could not submit block to pool") pass if pool_conf == 0: nodes_block_submit(block_send) else: print("Invalid signature") tries = 0 except Exception as e: print(e) time.sleep(0.1) if debug_conf == 1: raise else: pass
def table(address, addlist_20): # transaction table # data datasheet = ["Time", "From", "To", "Amount", "Type"] # show mempool txs connections.send(s, "mpget", 10) # senders mempool_total = connections.receive(s, 10) print (mempool_total) colors = [] for tx in mempool_total: if tx[1] == address: datasheet.append("Unconfirmed") datasheet.append(tx[1]) datasheet.append(tx[2]) datasheet.append(tx[3]) datasheet.append("Transaction") colors.append("bisque") # show mempool txs # retrieve aliases in bulk addlist_addressess = [] reclist_addressess = [] for x in addlist_20: addlist_addressess.append(x[2]) #append address reclist_addressess.append(x[3]) #append recipient #print(addlist_addressess) # define row color for x in addlist_20: if x[3] == address: colors.append("green4") else: colors.append("indianred") # define row color if resolve_var.get() == 1: connections.send(s, "aliasesget", 10) #senders connections.send(s, addlist_addressess, 10) aliases_address_results = connections.receive(s, 10) connections.send(s, "aliasesget", 10) #recipients connections.send(s, reclist_addressess, 10) aliases_rec_results = connections.receive(s, 10) # retrieve aliases in bulk i = 0 for row in addlist_20: db_timestamp = row[1] datasheet.append(datetime.fromtimestamp(float(db_timestamp)).strftime('%Y-%m-%d %H:%M:%S')) if resolve_var.get() == 1: db_address = aliases_address_results[i].lstrip("alias=") else: db_address = row[2] datasheet.append(db_address) if resolve_var.get() == 1: db_recipient = aliases_rec_results[i].lstrip("alias=") else: db_recipient = row[3] datasheet.append(db_recipient) db_amount = row[4] db_reward = row[9] db_openfield = row[11] datasheet.append('%.8f' % (float(db_amount) + float(db_reward))) if float(db_reward) > 0: symbol = "Mined" elif db_openfield.startswith("bmsg"): symbol = "b64 Message" elif db_openfield.startswith("msg"): symbol = "Message" else: symbol = "Transaction" datasheet.append(symbol) i = i+1 # data app_log.warning(datasheet) app_log.warning(len(datasheet)) if len(datasheet) == 5: app_log.warning("Looks like a new address") elif len(datasheet) < 20 * 5: app_log.warning(len(datasheet)) table_limit = len(datasheet) / 5 else: table_limit = 20 if len(datasheet) > 5: k = 0 for child in f4.winfo_children(): # prevent hangup child.destroy() for i in range(int(table_limit)): for j in range(5): datasheet_compare = [datasheet[k], datasheet[k - 1], datasheet[k - 2], datasheet[k - 3], datasheet[k - 4]] if "Time" in datasheet_compare: #header e = Entry(f4, width=0) e.configure(readonlybackground='linen') elif j == 0: #first row e = Entry(f4, width=0) e.configure(readonlybackground='linen') elif "Unconfirmed" in datasheet_compare: # unconfirmed txs e = Entry(f4, width=0) e.configure(readonlybackground='linen') elif j == 3: #sent e = Entry(f4, width=0) e.configure(readonlybackground=colors[i - 1]) elif j == 4: #last row e = Entry(f4, width=0) e.configure(readonlybackground='bisque') else: e = Entry(f4, width=0) e.configure(readonlybackground='bisque') e.grid(row=i + 1, column=j, sticky=EW) e.insert(END, datasheet[k]) e.configure(state="readonly") k = k + 1
else: debit_mempool = 0 # include mempool fees if full_ledger == 1: conn = sqlite3.connect(ledger_path) else: conn = sqlite3.connect(hyper_path) conn.text_factory = str c = conn.cursor() s = socks.socksocket() s.settimeout(10) s.connect(("127.0.0.1", 5658)) connections.send(s, "balanceget", 10) connections.send(s, address, 10) # change address here to view other people's transactions stats_account = connections.receive(s, 10) balance = stats_account[0] #credit = stats_account[1] #debit = stats_account[2] #fees = stats_account[3] #rewards = stats_account[4] print("Transction address: %s" % address) print("Transction address balance: %s" % balance) # get balance def address_validate(address):
def miner(q, pool_address, db_block_hash, diff, mining_condition, mining_condition_bin, netdiff, hq, thr, dh): tries = 0 my_hash_rate = 0 address = pool_address count = 0 timeout = time.time() + nonce_time #print(pool_address) while time.time() < timeout: try: t1 = time.time() tries = tries + 1 # generate the "address" of a random backyard that we will sample in this try seed = ('%0x' % getrandbits(128 - 32)) # this part won't change, so concat once only prefix = pool_address + seed # This is where the actual hashing takes place possibles = [ nonce for nonce in try_arr if mining_condition in ( sha224((prefix + nonce + db_block_hash).encode("utf-8")).hexdigest()) ] #hashrate calculation try: t2 = time.time() h1 = int(((nonce_time * unitcount) / (t2 - t1)) / 1000) except Exception as e: h1 = 1 #hashit bit if possibles: #print(possibles) for nonce in possibles: # add the seed back to get a full 128 bits nonce nonce = seed + nonce xdiffx = diffme(str(address[:56]), str(nonce), db_block_hash) if xdiffx < diff: pass else: print( "{} the equation is solved in {} answers ".format( q, tries)) wname = "{}{}".format(mname, str(q)) print("{} running at {} speed".format(wname, str(h1))) block_send = [] del block_send[:] # empty block_timestamp = '%.2f' % time.time() block_send.append( (block_timestamp, nonce, db_block_hash, netdiff, xdiffx, dh, mname, thr, str(q))) print("Sending solution: {}".format(block_send)) tries = 0 # submit mined nonce to pool try: s1 = socks.socksocket() if tor_conf == 1: s1.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s1.connect( (server_ip_conf, int(port))) # connect to pool print( "connected to server, proceeding to submit solution" ) connections.send(s1, "block", 10) connections.send(s1, self_address, 10) connections.send(s1, block_send, 10) print("solution submitted to cluster") time.sleep(0.2) s1.close() except Exception as e: print("Could not submit solution to pool") pass except Exception as e: print(e) time.sleep(0.1) raise my_hash_rate = str(h1) hq.put(str(h1))
import socks, connections, ast s = socks.socksocket() s.connect(("127.0.0.1", 5658)) #check difficulty connections.send(s, "diffget", 10) diff = connections.receive(s, 10) print "Current difficulty: {}".format(diff) #check difficulty #get balance connections.send(s, "balanceget", 10) connections.send(s, "f1e5133ff3685f70b9291922dd99a891d1ff4d6226fc6404a16729bf", 10) balance_ledger = connections.receive(s, 10) balance_ledger_mempool = connections.receive(s, 10) print "Address balance with mempool: {}".format(balance_ledger_mempool) print "Address balance without mempool: {}".format(balance_ledger) #get balance #insert to mempool connections.send(s, "mpinsert", 10) transaction = "('1494941203.13', '4edadac9093d9326ee4b17f869b14f1a2534f96f9c5d7b48dc9acaed', '4edadac9093d9326ee4b17f869b14f1a2534f96f9c5d7b48dc9acaed', '1.00000000', 'AnCAkXrBhqgKItLrbrho3+KNro5GuQNB7zcYlhxMELbiTIOcHZpv/oUazqwDvybp6xKxLWMYt2rmmGPmZ49Q3WG4ikIPkFgYY6XV9Uq+ZsnwjJNTKTwXfj++M/kGle7omUVCsi7PDeijz0HlORRySOM/G0rBnObUahMSvlGnCyo=', 'LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlHZk1BMEdDU3FHU0liM0RRRUJBUVVBQTRHTkFEQ0JpUUtCZ1FES3ZMVGJEeDg1YTF1Z2IvNnhNTWhWT3E2VQoyR2VZVDgrSXEyejlGd0lNUjQwbDJ0dEdxTks3dmFyTmNjRkxJdThLbjRvZ0RRczNXU1dRQ3hOa2haaC9GcXpGCllZYTMvSXRQUGZ6clhxZ2Fqd0Q4cTRadDRZbWp0OCsyQmtJbVBqakZOa3VUUUl6Mkl1M3lGcU9JeExkak13N24KVVZ1OXRGUGlVa0QwVm5EUExRSURBUUFCCi0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQ==', '0', '')" connections.send(s, transaction, 10) confirmation = connections.receive(s, 10) print confirmation #insert to mempool #ask for mempool connections.send(s, "mpget", 10) mempool = connections.receive(s, 10)
def balancegethyperjson(socket, arg1): #get balance connections.send(s, "balancegethyperjson") connections.send(s, arg1) response = connections.receive(s) print(json.dumps(response))
def refresh(address, s): global balance # print "refresh triggered" try: connections.send(s, "balanceget", 10) connections.send(s, address, 10) # change address here to view other people's transactions stats_account = connections.receive(s, 10) balance = stats_account[0] credit = stats_account[1] debit = stats_account[2] fees = stats_account[3] rewards = stats_account[4] app_log.warning("Transaction address balance: {}".format(balance)) connections.send(s, "blocklast", 10) block_get = connections.receive(s, 10) bl_height = block_get[0] db_timestamp_last = block_get[1] except: pass try: if encode_var.get() == 1: openfield_input = base64.b64encode(str(openfield.get("1.0", END).strip())) else: openfield_input = str(openfield.get("1.0", END)).strip() fee = '%.8f' % float(0.01 + (float(len(openfield_input)) / 100000) + int(keep_var.get())) # 0.01 dust #app_log.warning("Fee: {}".format(fee)) except Exception as e: fee = 0.01 app_log.warning("Fee error: {}".format(e)) # calculate fee # check difficulty connections.send(s, "diffget", 10) diff = connections.receive(s, 10) # check difficulty diff_msg = diff[1] # network status time_now = str(time.time()) last_block_ago = float(time_now) - float(db_timestamp_last) if last_block_ago > 300: sync_msg = "{}m behind".format((int(last_block_ago / 60))) sync_msg_label.config(fg='red') else: sync_msg = "Up to date\nLast block: {}s ago".format((int(last_block_ago))) sync_msg_label.config(fg='green') # network status # fees_current_var.set("Current Fee: {}".format('%.8f' % float(fee))) balance_var.set("Balance: {}".format('%.8f' % float(balance))) balance_raw.set('%.8f' % float(balance)) debit_var.set("Spent Total: {}".format('%.8f' % float(debit))) credit_var.set("Received Total: {}".format('%.8f' % float(credit))) fees_var.set("Fees Paid: {}".format('%.8f' % float(fees))) rewards_var.set("Rewards: {}".format('%.8f' % float(rewards))) bl_height_var.set("Block Height: {}".format(bl_height)) diff_msg_var.set("Mining Difficulty: {}".format('%.2f' % float(diff_msg))) sync_msg_var.set("Network: {}".format(sync_msg)) connections.send(s, "addlistlim", 10) connections.send(s, address, 10) connections.send(s, "20", 10) addlist = connections.receive(s, 10) addlist_20 = addlist[:20] # limit table(address, addlist_20)
def get(self): s = socks.socksocket() s.settimeout(10) s.connect(("127.0.0.1", 5658)) connections.send(s, "statusget", 10) response = connections.receive(s, 10) s.close() nodes_list = response[2] ips = nodes_list markers = [] print("IPs:",ips) with open('geo.json', 'w') as f: for ip in ips: getgeo = requests.request("GET", "http://freegeoip.net/json/{}".format(ip)) response_web = json.loads(getgeo.text) try: print(response_web).encode("utf-8") except: pass markers.append("{{lat: {},".format(response_web["latitude"])) markers.append(" lng: {}}},\n".format(response_web["longitude"])) html = [] html.append("<!DOCTYPE html>\n") html.append("<html>\n") html.append("<head>\n") html.append("<meta name='viewport' content='initial-scale=1.0, user-scalable=no'>\n") html.append("<meta charset='utf-8'>\n") html.append("<title>Bismuth Node Statistics</title>\n") html.append('<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >') html.append("<style>\n") html.append("/* Always set the map height explicitly to define the size of the div\n") html.append("* element that contains the map. */\n") html.append("#map {\n") html.append("height: 100%;\n") html.append("}\n") html.append("/* Optional: Makes the sample page fill the window. */\n") html.append("html, body {\n") html.append("height: 100%;\n") html.append("margin: 0;\n") html.append("padding: 0;\n") html.append("}\n") html.append("</style>\n") html.append("</head>\n") html.append("<body>\n") html.append("<div id='map'></div>\n") html.append("<script>\n") html.append("\n") html.append("function initMap() {\n") html.append("var map = new google.maps.Map(document.getElementById('map'), {\n") html.append("zoom: 3,\n") html.append("center: {lat: -28.024, lng: 140.887}\n") html.append("});\n") html.append("// Create an array of alphabetical characters used to label the markers.\n") html.append("var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';\n") html.append("// Add some markers to the map.\n") html.append("// Note: The code uses the JavaScript Array.prototype.map() method to\n") html.append("// create an array of markers based on a given 'locations' array.\n") html.append("// The map() method here has nothing to do with the Google Maps API.\n") html.append("var markers = locations.map(function(location, i) {\n") html.append("return new google.maps.Marker({\n") html.append("position: location,\n") html.append("label: labels[i % labels.length]\n") html.append("});\n") html.append("});\n") html.append("// Add a marker clusterer to manage the markers.\n") html.append("var markerCluster = new MarkerClusterer(map, markers,\n") html.append("{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});\n") html.append("}\n") html.append("var locations = [\n") html.append(''.join(markers)) html.append("]\n") html.append("</script>\n") html.append("<script src='https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js'>\n") html.append("</script>\n") html.append("<script async defer\n") html.append("src='https://maps.googleapis.com/maps/api/js?key={}&callback=initMap'>".format(api_key)) html.append("</script>\n") """ node_address = response[0] nodes_count = response[1] nodes_list = response[2] threads_count = response[3] uptime = response[4] consensus = response[5] consensus_percentage = response[6] version = response[7] html.append("<div class = 'col-md-8'>") html.append("Node address: {}<br>".format(node_address)) html.append("Number of nodes: {}<br>".format(nodes_count)) html.append("List of nodes: {}<br>".format(nodes_list)) html.append("Number of threads: {}<br>".format(threads_count)) html.append("Uptime: {}<br>".format(uptime)) html.append("Consensus: {}<br>".format(consensus)) html.append("Consensus percentage: {}<br>".format(consensus_percentage)) html.append("Version: {}<br>".format(version)) html.append("</div>") """ html.append("</body>\n") html.append("</html>\n") self.write(''.join(html))
def send(amount_input, recipient_input, keep_input, openfield_input): try: key except: top5 = Toplevel() top5.title("Locked") Label(top5, text="Wallet is locked", width=20).grid(row=0, pady=0) done = Button(top5, text="Cancel", command=top5.destroy) done.grid(row=1, column=0, sticky=W + E, padx=15, pady=(5, 5)) app_log.warning("Received tx command") try: float(amount_input) except: top7 = Toplevel() top7.title("Invalid amount") Label(top7, text="Amount must be a number", width=20).grid(row=0, pady=0) done = Button(top7, text="Cancel", command=top7.destroy) done.grid(row=1, column=0, sticky=W + E, padx=15, pady=(5, 5)) # alias check # alias check if len(recipient_input) != 56: top6 = Toplevel() top6.title("Invalid address") Label(top6, text="Wrong address length", width=20).grid(row=0, pady=0) done = Button(top6, text="Cancel", command=top6.destroy) done.grid(row=1, column=0, sticky=W + E, padx=15, pady=(5, 5)) else: app_log.warning("Amount: {}".format(amount_input)) app_log.warning("Recipient: {}".format(recipient_input)) app_log.warning("Keep Forever: {}".format(keep_input)) app_log.warning("OpenField Data: {}".format(openfield_input)) timestamp = '%.2f' % time.time() transaction = (str(timestamp), str(myaddress), str(recipient_input), '%.8f' % float(amount_input), str(keep_input), str(openfield_input)) # this is signed h = SHA.new(str(transaction).encode("utf-8")) signer = PKCS1_v1_5.new(key) signature = signer.sign(h) signature_enc = base64.b64encode(signature) app_log.warning("Client: Encoded Signature: {}".format( signature_enc.decode("utf-8"))) verifier = PKCS1_v1_5.new(key) if verifier.verify(h, signature) == True: fee = fee_calculate(openfield_input, keep_var.get()) if float(amount_input) < 0: app_log.warning( "Client: Signature OK, but cannot use negative amounts") elif (float(amount_input) + float(fee) > float(balance)): app_log.warning("Mempool: Sending more than owned") else: app_log.warning( "Client: The signature is valid, proceeding to save transaction, signature, new txhash and the public key to mempool" ) # print(str(timestamp), str(address), str(recipient_input), '%.8f' % float(amount_input),str(signature_enc), str(public_key_hashed), str(keep_input), str(openfield_input)) tx_submit = (str(timestamp), str(myaddress), str(recipient_input), '%.8f' % float(amount_input), str(signature_enc.decode("utf-8")), str(public_key_hashed.decode("utf-8")), str(keep_input), str(openfield_input)) while True: connections.send(s, "mpinsert", 10) connections.send( s, [tx_submit], 10 ) # change address here to view other people's transactions reply = connections.receive(s, 10) app_log.warning("Client: {}".format(reply)) break else: app_log.warning("Client: Invalid signature")
def mpget(socket): #ask for mempool connections.send(s, "mpget") mempool = connections.receive(s) print("Current mempool: {}".format(mempool))
def send(amount_input, recipient_input, keep_input, openfield_input): try: key except: top5 = Toplevel() top5.title("Locked") Label(top5, text="Wallet is locked", width=20).grid(row=0, pady=0) done = Button(top5, text="Cancel", command=top5.destroy) done.grid(row=1, column=0, sticky=W + E, padx=15, pady=(5, 5)) app_log.warning("Received tx command") try: float(amount_input) except: top7 = Toplevel() top7.title("Invalid amount") Label(top7, text="Amount must be a number", width=20).grid(row=0, pady=0) done = Button(top7, text="Cancel", command=top7.destroy) done.grid(row=1, column=0, sticky=W + E, padx=15, pady=(5, 5)) # alias check # alias check if len(recipient_input) != 56: top6 = Toplevel() top6.title("Invalid address") Label(top6, text="Wrong address length", width=20).grid(row=0, pady=0) done = Button(top6, text="Cancel", command=top6.destroy) done.grid(row=1, column=0, sticky=W + E, padx=15, pady=(5, 5)) else: app_log.warning("Amount: {}".format(amount_input)) app_log.warning("Recipient: {}".format(recipient_input)) app_log.warning("Keep Forever: {}".format(keep_input)) app_log.warning("OpenField Data: {}".format(openfield_input)) timestamp = '%.2f' % time.time() transaction = (str(timestamp), str(myaddress), str(recipient_input), '%.8f' % float(amount_input), str(keep_input), str(openfield_input)) # this is signed h = SHA.new(str(transaction).encode("utf-8")) signer = PKCS1_v1_5.new(key) signature = signer.sign(h) signature_enc = base64.b64encode(signature) app_log.warning("Client: Encoded Signature: {}".format(signature_enc.decode("utf-8"))) verifier = PKCS1_v1_5.new(key) if verifier.verify(h, signature) == True: fee = fee_calculate(openfield_input, keep_var.get()) if float(amount_input) < 0: app_log.warning("Client: Signature OK, but cannot use negative amounts") elif (float(amount_input) + float(fee) > float(balance)): app_log.warning("Mempool: Sending more than owned") else: app_log.warning("Client: The signature is valid, proceeding to save transaction, signature, new txhash and the public key to mempool") # print(str(timestamp), str(address), str(recipient_input), '%.8f' % float(amount_input),str(signature_enc), str(public_key_hashed), str(keep_input), str(openfield_input)) tx_submit = (str(timestamp), str(myaddress), str(recipient_input), '%.8f' % float(amount_input), str(signature_enc.decode("utf-8")), str(public_key_hashed.decode("utf-8")), str(keep_input), str(openfield_input)) while True: connections.send(s, "mpinsert", 10) connections.send(s, [tx_submit], 10) # change address here to view other people's transactions reply = connections.receive(s, 10) app_log.warning("Client: {}".format(reply)) break else: app_log.warning("Client: Invalid signature")
def msg_dialogue(address): connections.send(s, "addlist", 10) connections.send(s, myaddress, 10) addlist = connections.receive(s, 10) print(addlist) def msg_received_get(addlist): for x in addlist: if x[11].startswith(("msg=", "bmsg=", "enc=msg=", "enc=bmsg=")) and x[3] == address: #print(x[11]) connections.send(s, "aliasget", 10) connections.send(s, x[2], 10) msg_address = connections.receive(s, 10)[0][0] if x[11].startswith("enc=msg="): msg_received_digest = x[11].lstrip("enc=msg=") try: #msg_received_digest = key.decrypt(ast.literal_eval(msg_received_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key ) = ast.literal_eval(msg_received_digest) private_key = RSA.import_key( open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_received_digest = cipher_aes.decrypt_and_verify( ciphertext, tag).decode("utf-8") except: msg_received_digest = "Could not decrypt message" elif x[11].startswith("enc=bmsg="): msg_received_digest = x[11].lstrip("enc=bmsg=") try: msg_received_digest = base64.b64decode( msg_received_digest).decode("utf-8") #msg_received_digest = key.decrypt(ast.literal_eval(msg_received_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key ) = ast.literal_eval(msg_received_digest) private_key = RSA.import_key( open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_received_digest = cipher_aes.decrypt_and_verify( ciphertext, tag).decode("utf-8") except: msg_received_digest = "Could not decrypt message" elif x[11].startswith("bmsg="): msg_received_digest = x[11].lstrip("bmsg=") try: msg_received_digest = base64.b64decode( msg_received_digest).decode("utf-8") except: msg_received_digest = "Could not decode message" elif x[11].startswith("msg="): msg_received_digest = x[11].lstrip("msg=") msg_received.insert(INSERT, ( (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(float( x[1])))) + " From " + msg_address.lstrip("alias=") + ": " + msg_received_digest) + "\n") def msg_sent_get(addlist): for x in addlist: if x[11].startswith(("msg=", "bmsg=", "enc=msg=", "enc=bmsg=")) and x[2] == address: # print(x[11]) connections.send(s, "aliasget", 10) connections.send(s, x[3], 10) received_aliases = connections.receive(s, 10) msg_recipient = received_aliases[0][0] if x[11].startswith("enc=msg="): msg_sent_digest = x[11].lstrip("enc=msg=") try: #msg_sent_digest = key.decrypt(ast.literal_eval(msg_sent_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(msg_sent_digest) private_key = RSA.import_key( open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_sent_digest = cipher_aes.decrypt_and_verify( ciphertext, tag).decode("utf-8") except: msg_sent_digest = "Could not decrypt message" elif x[11].startswith("enc=bmsg="): msg_sent_digest = x[11].lstrip("enc=bmsg=") try: msg_sent_digest = base64.b64decode( msg_sent_digest).decode("utf-8") #msg_sent_digest = key.decrypt(ast.literal_eval(msg_sent_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(msg_sent_digest) private_key = RSA.import_key( open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_sent_digest = cipher_aes.decrypt_and_verify( ciphertext, tag).decode("utf-8") except: msg_sent_digest = "Could not decrypt message" elif x[11].startswith("bmsg="): msg_sent_digest = x[11].lstrip("bmsg=") try: msg_sent_digest = base64.b64decode( msg_sent_digest).decode("utf-8") except: msg_received_digest = "Could not decode message" elif x[11].startswith("msg="): msg_sent_digest = x[11].lstrip("msg=") msg_sent.insert( INSERT, ((time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(float(x[1])))) + " To " + msg_recipient.lstrip("alias=") + ": " + msg_sent_digest) + "\n") # popup top11 = Toplevel() top11.title("Messaging") Label(top11, text="Received:", width=20).grid(row=0) msg_received = Text(top11, width=100, height=20, font=("Tahoma", 8)) msg_received.grid(row=1, column=0, sticky=W, padx=5, pady=(5, 5)) msg_received_get(addlist) Label(top11, text="Sent:", width=20).grid(row=2) msg_sent = Text(top11, width=100, height=20, font=("Tahoma", 8)) msg_sent.grid(row=3, column=0, sticky=W, padx=5, pady=(5, 5)) msg_sent_get(addlist) dismiss = Button(top11, text="Dismiss", command=top11.destroy) dismiss.grid(row=5, column=0, sticky=W + E, padx=15, pady=(5, 5))
def msg_sent_get(addlist): for x in addlist: if x[11].startswith(("msg=", "bmsg=", "enc=msg=", "enc=bmsg=")) and x[2] == address: # print(x[11]) connections.send(s, "aliasget", 10) connections.send(s, x[3], 10) received_aliases = connections.receive(s, 10) msg_recipient = received_aliases[0][0] if x[11].startswith("enc=msg="): msg_sent_digest = x[11].lstrip("enc=msg=") try: #msg_sent_digest = key.decrypt(ast.literal_eval(msg_sent_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(msg_sent_digest) private_key = RSA.import_key( open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_sent_digest = cipher_aes.decrypt_and_verify( ciphertext, tag).decode("utf-8") except: msg_sent_digest = "Could not decrypt message" elif x[11].startswith("enc=bmsg="): msg_sent_digest = x[11].lstrip("enc=bmsg=") try: msg_sent_digest = base64.b64decode( msg_sent_digest).decode("utf-8") #msg_sent_digest = key.decrypt(ast.literal_eval(msg_sent_digest)).decode("utf-8") (cipher_aes_nonce, tag, ciphertext, enc_session_key) = ast.literal_eval(msg_sent_digest) private_key = RSA.import_key( open("privkey.der").read()) # Decrypt the session key with the public RSA key cipher_rsa = PKCS1_OAEP.new(private_key) session_key = cipher_rsa.decrypt(enc_session_key) # Decrypt the data with the AES session key cipher_aes = AES.new(session_key, AES.MODE_EAX, cipher_aes_nonce) msg_sent_digest = cipher_aes.decrypt_and_verify( ciphertext, tag).decode("utf-8") except: msg_sent_digest = "Could not decrypt message" elif x[11].startswith("bmsg="): msg_sent_digest = x[11].lstrip("bmsg=") try: msg_sent_digest = base64.b64decode( msg_sent_digest).decode("utf-8") except: msg_received_digest = "Could not decode message" elif x[11].startswith("msg="): msg_sent_digest = x[11].lstrip("msg=") msg_sent.insert( INSERT, ((time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(float(x[1])))) + " To " + msg_recipient.lstrip("alias=") + ": " + msg_sent_digest) + "\n")
def miner(q, privatekey_readable, public_key_hashed, address): from Crypto.PublicKey import RSA Random.atfork() key = RSA.importKey(privatekey_readable) rndfile = Random.new() tries = 0 firstrun = True begin = time.time() if pool_conf == 1: #do not use pools public key to sign, signature will be invalid self_address = address address = pool_address #ask for diff percentage s_pool = socks.socksocket() s_pool.settimeout(0.3) if tor_conf == 1: s_pool.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s_pool.connect((pool_ip_conf, 8525)) # connect to pool print("Connected") print("Miner: Asking pool for share qualification difficulty requirement") connections.send(s_pool, "diffp", 10) pool_diff_percentage = int(connections.receive(s_pool, 10)) print("Miner: Received pool for share qualification difficulty requirement: {}%".format(pool_diff_percentage)) s_pool.close() #ask for diff percentage while True: try: # calculate new hash nonces = 0 # calculate difficulty s_node = socks.socksocket() if tor_conf == 1: s_node.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s_node.connect((node_ip_conf, port)) # connect to local node connections.send(s_node, "blocklast", 10) blocklast = connections.receive(s_node, 10) db_block_hash = blocklast[7] connections.send(s_node, "diffget", 10) diff = connections.receive(s_node, 10) s_node.close() diff = int(diff[1]) diff_real = diff else: # if pooled diff_pool = diff_real diff = percentage(pool_diff_percentage, diff_real) if diff > diff_pool: diff = diff_pool mining_condition = bin_convert(db_block_hash)[0:diff] # block_hash = hashlib.sha224(str(block_send) + db_block_hash).hexdigest() while tries < diff_recalc_conf: start = time.time() nonce = hashlib.sha224(rndfile.read(16)).hexdigest()[:32] mining_hash = bin_convert(hashlib.sha224((address + nonce + db_block_hash).encode("utf-8")).hexdigest()) end = time.time() if tries % 2500 == 0: #limit output try: cycles_per_second = 1/(end - start) print("Thread{} {} @ {:.2f} cycles/second, difficulty: {}({}), iteration: {}".format(q, db_block_hash[:10], cycles_per_second, diff, diff_real, tries)) except: pass tries += 1 if mining_condition in mining_hash: tries = 0 print("Thread {} found a good block hash in {} cycles".format(q, tries)) # serialize txs block_send = [] del block_send[:] # empty removal_signature = [] del removal_signature[:] # empty s_node = socks.socksocket() if tor_conf == 1: s_node.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s_node.connect((node_ip_conf, port)) # connect to config.txt node connections.send(s_node, "mpget", 10) data = connections.receive(s_node, 10) s_node.close() if data != "[]": mempool = data for mpdata in mempool: transaction = ( str(mpdata[0]), str(mpdata[1][:56]), str(mpdata[2][:56]), '%.8f' % float(mpdata[3]), str(mpdata[4]), str(mpdata[5]), str(mpdata[6]), str(mpdata[7])) # create tuple # print transaction block_send.append(transaction) # append tuple to list for each run removal_signature.append(str(mpdata[4])) # for removal after successful mining # claim reward block_timestamp = '%.2f' % time.time() transaction_reward = (str(block_timestamp), str(address[:56]), str(address[:56]), '%.8f' % float(0), "0", str(nonce)) # only this part is signed! # print transaction_reward h = SHA.new(str(transaction_reward).encode("utf-8")) signer = PKCS1_v1_5.new(key) signature = signer.sign(h) signature_enc = base64.b64encode(signature) if signer.verify(h, signature): print("Signature valid") block_send.append((str(block_timestamp), str(address[:56]), str(address[:56]), '%.8f' % float(0), str(signature_enc.decode("utf-8")), str(public_key_hashed), "0", str(nonce))) # mining reward tx print("Block to send: {}".format(block_send)) if not any(isinstance(el, list) for el in block_send): # if it's not a list of lists (only the mining tx and no others) new_list = [] new_list.append(block_send) block_send = new_list # make it a list of lists # claim reward # include data tries = 0 # submit mined block to node if sync_conf == 1: check_uptodate(300) if pool_conf == 1: mining_condition = bin_convert(db_block_hash)[0:diff_real] if mining_condition in mining_hash: print("Miner: Submitting block to all nodes, because it satisfies real difficulty too") nodes_block_submit(block_send) try: s_pool = socks.socksocket() s_pool.settimeout(0.3) if tor_conf == 1: s_pool.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) s_pool.connect((pool_ip_conf, 8525)) # connect to pool print("Connected") print("Miner: Proceeding to submit mined block to pool") connections.send(s_pool, "block", 10) connections.send(s_pool, self_address, 10) connections.send(s_pool, block_send, 10) print("Miner: Block submitted to pool") except Exception as e: print("Miner: Could not submit block to pool") pass finally: s_pool.close() if pool_conf == 0: nodes_block_submit(block_send) else: print("Invalid signature") tries = 0
def refresh(): global balance # print "refresh triggered" try: s = socks.socksocket() s.connect((node_ip_conf, int(port))) connections.send(s, "balanceget", 10) connections.send(s, address, 10) # change address here to view other people's transactions stats_account = connections.receive(s, 10) balance = stats_account[0] credit = stats_account[1] debit = stats_account[2] fees = stats_account[3] rewards = stats_account[4] app_log.warning("Transaction address balance: {}".format(balance)) connections.send(s, "blocklast", 10) block_get = connections.receive(s, 10) bl_height = block_get[0] db_timestamp_last = block_get[1] s.close() except: # get locally app_log.warning("Unable to start in light mode, using local db for balance calculation") # global balance # print "refresh triggered" m.execute("SELECT count(amount), sum(amount) FROM transactions WHERE address = ?;", (address,)) result = m.fetchall()[0] if result[1] != None: debit_mempool = float(result[1]) + float(result[1]) * 0.001 + int(result[0]) * 0.01 else: debit_mempool = 0 c.execute("SELECT sum(amount) FROM transactions WHERE recipient = ?;", (address,)) credit = c.fetchone()[0] c.execute("SELECT sum(amount) FROM transactions WHERE address = ?;", (address,)) debit = c.fetchone()[0] c.execute("SELECT sum(fee) FROM transactions WHERE address = ?;", (address,)) fees = c.fetchone()[0] c.execute("SELECT sum(reward) FROM transactions WHERE address = ?;", (address,)) rewards = c.fetchone()[0] c.execute("SELECT MAX(block_height) FROM transactions") bl_height = c.fetchone()[0] debit = 0 if debit is None else float('%.8f' % debit) fees = 0 if fees is None else float('%.8f' % fees) rewards = 0 if rewards is None else float('%.8f' % rewards) credit = 0 if credit is None else float('%.8f' % credit) balance = '%.8f' % (credit - debit - fees + rewards - debit_mempool) app_log.warning("Node: Transction address balance: {}".format(balance)) # calculate diff c.execute("SELECT * FROM transactions WHERE reward != 0 ORDER BY block_height DESC LIMIT 1;") # or it takes the first result = c.fetchall() db_timestamp_last = float(result[0][1]) # print db_timestamp_last db_block_height = result[0][0] # print timestamp_avg try: if encode_var.get() == 1: openfield_input = base64.b64encode(str(openfield.get("1.0", END).strip())) else: openfield_input = str(openfield.get("1.0", END)).strip() fee = '%.8f' % float(0.01 + (float(len(openfield_input)) / 100000) + int(keep_var.get())) # 0.01 dust app_log.warning("Fee: {}".format(fee)) except Exception as e: fee = 0.01 app_log.warning("Fee error: {}".format(e)) # calculate fee # check difficulty try: s = socks.socksocket() s.connect((node_ip_conf, int(port))) connections.send(s, "diffget", 10) diff = connections.receive(s, 10) s.close() except: # get locally app_log.warning("Unable to start in light mode, using local db for difficulty calculation") diff = difficulty(c) # check difficulty diff_msg = diff[1] # network status time_now = str(time.time()) last_block_ago = float(time_now) - float(db_timestamp_last) if last_block_ago > 300: sync_msg = "{}m behind".format((int(last_block_ago / 60))) sync_msg_label.config(fg='red') else: sync_msg = "Up to date\nLast block: {}s ago".format((int(last_block_ago))) sync_msg_label.config(fg='green') # network status # aliases # c.execute("SELECT openfield FROM transactions WHERE address = ? AND openfield LIKE ?;",(address,)+("alias="+'%',)) # aliases = c.fetchall() # app_log.warning("Aliases: "+str(aliases)) # aliases # fees_current_var.set("Current Fee: {}".format('%.8f' % float(fee))) balance_var.set("Balance: {}".format('%.8f' % float(balance))) debit_var.set("Spent Total: {}".format('%.8f' % float(debit))) credit_var.set("Received Total: {}".format('%.8f' % float(credit))) fees_var.set("Fees Paid: {}".format('%.8f' % float(fees))) rewards_var.set("Rewards: {}".format('%.8f' % float(rewards))) bl_height_var.set("Block Height: {}".format(bl_height)) diff_msg_var.set("Mining Difficulty: {}".format('%.2f' % float(diff_msg))) sync_msg_var.set("Network: {}".format(sync_msg)) table()
import socks, connections, time, sys, json import options config = options.Get() config.read() version = config.version_conf s = socks.socksocket() port = 5658 if "testnet" in version: port = 2829 print("tesnet mode") elif "regnet" in version: is_regnet = True print("Regtest mode") port = 3030 while True: try: s.connect(("127.0.0.1", port)) print("Sending stop command...") connections.send(s, "stop") print("Stop command delivered.") break except: print("Cannot reach node, retrying...") s.close()