def searchDevice(): data = json.loads(request.form.get('data')) deviceID = data['id'] #print(deviceID) trans = queryAllDB(transaction_db) t = [] for item in trans: key = str(item["id"], encoding='utf-8') if key[:key.find(':')] == deviceID: t.append(item["value"]) #print(t) result = [] for item in t: item = json.loads(item) block_hash = item["block_hash"] number = item["transaction_number"] block_num = queryDB(block_db, block_hash) #print(block_num) if block_num: one = {} filename = "../blocks/blk" + block_num + ".dat" fp = open(filename, "rb") value = json.loads(fp.read()) fp.close() #print(value) one["sender"] = value["transactions"][int(number)]["sender"] one["receiver"] = value["transactions"][int(number)]["receive"] one["ctime1"] = value["transactions"][int(number)]["ctime"] one["ctime2"] = value["ctime"] result.append(one) result = sorted(result, key=lambda x: x["ctime2"]) print(result) return jsonify(result), 200
def input_device(): userID = session.get("userID") device = {} deviceID = request.args.get('device_id') deviceName = request.args.get('device_name') buyTime = request.args.get('buy_time') device["name"] = deviceName device["id"] = deviceID device["date"] = buyTime device_info = json.dumps(device, ensure_ascii=False).encode(encoding="utf-8") md5 = hashlib.md5(device_info).hexdigest() device["md5"] = md5 device_info = json.dumps(device, ensure_ascii=False).encode(encoding="utf-8") if md5 in device_db: return """<script>alert('device exist');location.replace("/manager");</script>""" else: insertDB(device_db, deviceID, device_info) value = queryDB(user_db, userID) info = json.loads(value) info["device"].append(deviceID) info = json.dumps(info, ensure_ascii=False).encode(encoding="utf-8") insertDB(user_db, userID, info) return """<script>alert('submit success');location.replace("/manager");</script>"""
def new_transaction(): values = request.form print(values) # Check that the required fields are in the POST'ed data required = ['sender_address', 'recipient_address', 'amount','signature'] #if not all(k in values for k in required): # return 'Missing values', 400 # Create a new Transaction #valid Transaction,so delete deviceID deviceID=values['amount'] senderID=values['senderID'] receiverID=values['receiverID'] #print('%s->%s:%s'%(senderID,receiverID,deviceID)) transaction_result = blockchain.submit_transaction(values['sender_address'],\ values['recipient_address'], values['amount'], values['signature'],\ senderID,receiverID) print(transaction_result) if transaction_result == False: response = {'message': 'Invalid Transaction!'} return jsonify(response), 406 else: #sender delete device value=queryDB(user_db,senderID) info=json.loads(value) print(info["device"]) info["device"].remove(deviceID) info=json.dumps(info,ensure_ascii=False).encode(encoding="utf-8") insertDB(user_db,senderID,info) #receiver append device value=queryDB(user_db,receiverID) info=json.loads(value) print(info["device"]) info["device"].append(deviceID) info=json.dumps(info,ensure_ascii=False).encode(encoding="utf-8") insertDB(user_db,receiverID,info) response = {'message': 'Transaction will be added to Block '+ str(transaction_result)} return jsonify(response), 201
def new_wallet(): userID = session.get("userID") random_gen = Crypto.Random.new().read private_key = RSA.generate(1024, random_gen) public_key = private_key.publickey() response = { 'private_key': binascii.hexlify(private_key.exportKey(format='DER')).decode('ascii'), 'public_key': binascii.hexlify(public_key.exportKey(format='DER')).decode('ascii') } print(userID) print(queryDB(user_db, str(userID))) value = json.loads(queryDB(user_db, userID)) value["public_key"] = response["public_key"] value = json.dumps(value, ensure_ascii=False).encode(encoding="utf-8") insertDB(user_db, userID, value) #print(user_db[username]) #user_db.close() #user_db.open("DB/users_info") #user_db=dbm.open('DB/users_info','c') return jsonify(response), 200
def check(): if request.method == "POST": userID = request.form.get('id') tempPsw = request.form.get('password').encode(encoding="utf-8") passWord = hashlib.md5(tempPsw).hexdigest() #print('%s,%s,%s'%(UserName,tempPsw,PassWord)) value = queryDB(user_db, userID) if value: info = json.loads(value) print(info) passw = info["password"] if passWord == passw: session["userID"] = userID if userID == "manager": return """<script>location.replace("/manager");</script>""" else: return """<script>location.replace("/make/transaction");</script>""" return """<script>alert('studentID or password error');location.replace("/login");</script>"""
def make_transaction(): userID = session.get("userID") value = queryDB(user_db, userID) info = json.loads(value) device = info["device"] if userID != None: users = queryAllDB(user_db) result = [] if len(users) != 0: for user in users: one = {} one["id"] = user["id"].decode("utf-8") if one["id"] == userID: continue value = json.loads(user["value"]) one["public_key"] = value["public_key"] one["device"] = device #value=queryDB(device_db,deviceID) #info=json.loads(value) #device_hash=info["md5"] #one["deviceID"]=deviceID #one["md5"]=device_hash result.append(one) print(result) json_value = json.dumps(result) #devices=[] #for device in device_db: # d={} # info=eval(device_db[device].decode("utf-8")) # d['name']=info["name"] # d['md5']=device.decode("utf-8") # devices.append(d) # print(d) #json_value2=json.dumps(devices) return render_template('./make_transaction.html', userid=userID, users=json_value) else: return login()