def submitTransaction(): content = request.json try: transaction = decryptData(PRIVATE_KEY, content['transaction']) proposingNode = getName() blockindex = getLastIndex() timeStamp = str(datetime.datetime.now()) uid = str(uuid.uuid4()) addPendingTransaction(uid, proposingNode, transaction, blockindex, timeStamp) ll = retrieveNodeList() for i in ll: try: TEMP_KEY = "keys/tempKey.pem" saveKeyinFile(TEMP_KEY, i.publicKey) data = {} data["transaction"] = encryptData(TEMP_KEY, transaction) data["proposingNode"] = encryptData(TEMP_KEY, proposingNode) data["blockindex"] = encryptData(TEMP_KEY, blockindex) data["timeStamp"] = encryptData(TEMP_KEY, timeStamp) data["uid"] = encryptData(TEMP_KEY, uid) rr = requests.post(i.ip + BASE_API + '/getTransaction', json=data) except Exception as e: print(e) return jsonify({ "code": 0, "status": "Transaction is submitted to client", "uid": uid }) except Exception as e: print(e) return jsonify({"code": 1, "status": "Something Went Wrong"})
def clientTranSubmit(): content = request.json try: uid = decryptData(PRIVATE_KEY, content['uid']) status = decryptData(PRIVATE_KEY, content['status']) if status == "1": setVoteCount(uid, getVoteCount(uid) + 1) if calScore(uid) == True and str(getFlag(uid)) == "0": addToBlockChain(uid) #call others here ll = retrieveNodeList() for i in ll: try: TEMP_KEY = "keys/tempKey.pem" saveKeyinFile(TEMP_KEY, i.publicKey) data = {} data["uid"] = encryptData(TEMP_KEY, uid) data["status"] = encryptData(TEMP_KEY, status) rr = requests.post(i.ip + BASE_API + '/nodeTranSubmit', json=data) except Exception as e: print(e) return jsonify({"code": 0, "status": "OK Got IT"}) except Exception as e: print(e) return jsonify({"code": 1, "status": "Something Went Wrong"})
def addNodeRequest(): content = request.json try: print("in request") name = decryptData(PRIVATE_KEY, content['name']) ipaddr = decryptData(PRIVATE_KEY, content['ipaddr']) publicKey = content['publicKey'] companyName = decryptData(PRIVATE_KEY, content['companyName']) companyID = decryptData(PRIVATE_KEY, content['companyID']) #verify if the node doesn't exist? addNodes(name, ipaddr, publicKey) ll = getAllClients() for i in ll: try: TEMP_KEY = "keys/tempKey.pem" saveKeyinFile(TEMP_KEY, i.publicKey) data = {} data["name"] = encryptData(TEMP_KEY, name) data["companyName"] = encryptData(TEMP_KEY, companyName) data["companyID"] = encryptData(TEMP_KEY, companyID) try: rr = requests.post(i.callback_URL + '/addNode', json=data) except Exception as e: print(e) except Exception as e: print(e) return jsonify({"code": 0, "status": "Node added"}) except: return jsonify({"code": 1, "status": "Something Went Wrong here"})
def joinBlockchain(): content = request.form f = request.files['file'] f.save(secure_filename(f.filename)) json_data = open(f.filename, 'r').read() # json_data = json.dumps(json_data) json_data = eval(json_data) print(type(json_data)) try: print(content) nodes = json_data['nodes'] print(nodes) # new_content = content["new_node_data"] # name = decryptData(PRIVATE_KEY, new_content['name']) # ipaddr = decryptData(PRIVATE_KEY, new_content['ipaddr']) # info = decryptData(PRIVATE_KEY, new_content['info']) # companyName = decryptData(PRIVATE_KEY, new_content['companyName']) # companyID = decryptData(PRIVATE_KEY, new_content['companyID']) name = content["name"] ipaddr = content["ipaddr"] info = content["info"] companyName = content["companyName"] companyID = content["companyID"] publicKey = str(open(PUBLIC_KEY, 'r').read()) setBlockChain(name, ipaddr, info) print(nodes) z = 0 for i in nodes: TEMP_KEY = "keys/tempKey.pem" saveKeyinFile(TEMP_KEY, i["publicKey"]) data = {} addNodes(i["name"], i["ipaddr"], i["publicKey"]) z = z + 1 data["name"] = encryptData(TEMP_KEY, name) data["ipaddr"] = encryptData(TEMP_KEY, ipaddr) data["publicKey"] = publicKey data["companyName"] = encryptData(TEMP_KEY, companyName) data["companyID"] = encryptData(TEMP_KEY, companyID) URL = "http://" + str(i["ipaddr"]) + BASE_API + "/nodeRequest" print(URL) try: rr = requests.post(URL, json=data) print(rr.json()) except Exception as e: print(e) setInit() return jsonify({"code": 0, "status": "Joined to blockchain"}) except Exception as e: print(e) return jsonify({"code": 1, "status": "Something Went Wrong"})
def getTransaction(): content = request.json try: proposingNode = decryptData(PRIVATE_KEY, content['proposingNode']) transaction = decryptData(PRIVATE_KEY, content['transaction']) blockindex = decryptData(PRIVATE_KEY, content['blockindex']) timeStamp = decryptData(PRIVATE_KEY, content['timeStamp']) uid = decryptData(PRIVATE_KEY, content['uid']) addPendingTransaction(uid, proposingNode, transaction, blockindex, timeStamp) #call client for transaction strucutre check ll = getAllClients() for i in ll: try: TEMP_KEY = "keys/tempKey.pem" saveKeyinFile(TEMP_KEY, i.publicKey) data = {} data["transaction"] = encryptData(TEMP_KEY, transaction) data["proposingNode"] = encryptData(TEMP_KEY, proposingNode) data["blockindex"] = encryptData(TEMP_KEY, blockindex) data["uid"] = encryptData(TEMP_KEY, uid) rr = requests.post(i.callback_URL + '/addTransaction', json=data) except Exception as e: print(e) ## #save in pending db #call client for verify return jsonify({ "code": 0, "status": "Transaction is submitted to client" }) except Exception as e: print(e) return jsonify({"code": 1, "status": "Something Went Wrong"})
def checkInit(): rr = requests.get(BASE_URL + "check") data = rr.json() saveKeyinFile(PUBLIC_KEY, data['publicKey']) return data['code']
def save_node_publicKey(): response = requests.get("http://" + NODE_URL + BASE_API + "/check") response = response.json() print(response) saveKeyinFile(nodePublicKey_path, response["publicKey"])