def json_eps(self, user, epname): if self.user_agent() == 'x': return 0 epname = decode(epname) cherrypy.response.headers['Content-Type'] = 'application/json; charset=utf-8' cherrypy.response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' cherrypy.response.headers['Pragma'] = 'no-cache' cherrypy.response.headers['Expires'] = 0 SuitesManager = self.project.getEpInfo(user, epname).get('suites', False) data = [] default_suite = { 'data': '', 'metadata': '', 'children': [], } for suite_id in SuitesManager.getSuites(): node = SuitesManager.findId(suite_id) current_suite = dict(default_suite) current_suite['data'] = node['name'] current_suite['metadata'] = suite_id current_suite['attr'] = dict({'id': suite_id, 'rel': 'suite'}, **node) del current_suite['attr']['children'] current_suite['children'] = [ {'data': v['file'], 'attr': dict({'id': k}, **v)} for k, v in node['children'].iteritems() if v.get('type', 'file') == 'file' ] data.append(current_suite) return json.dumps(data, indent=2)
def json_save_project(self, user, epname): """ save project """ logFull('CeWebUi:json_save_project user `{}`.'.format(user)) if self.user_agent() == 'x': return 0 c_l = cherrypy.request.headers['Content-Length'] raw_data = cherrypy.request.body.read(int(c_l)) json_data = json.loads(raw_data) del c_l, raw_data # Delete everything from XML Root self.project.del_settings_key(user, 'project', '//TestSuite') changes = 'Reset project file...\n' for suite_data in json_data: self.project.set_persistent_suite(user, suite_data['data'], {'ep': decode(epname)}) changes += 'Created suite: {0}.\n'.format(suite_data['data']) for file_data in suite_data.get('children', []): changes += 'Created file: {0}.\n'.format(file_data['data']) self.project.set_persistent_file(user, suite_data['data'], file_data['data'], {}) changes += '>.<\n' logDebug(changes) return 'true'
def valid_exchange(self, exchange): ''' Validate the exchange ''' # Check number number = H(bytes(str(exchange.from_addr) + str(exchange.to_addr) +\ str(exchange.file_hash) + str(exchange.file_size) + \ str(exchange.sig["from"]) + str(exchange.sig["to"]), "utf-8")).hexdigest() if number != exchange.number: print("Number is wrong") return False # Verify signature on file verify_key1 = nacl.signing.VerifyKey(exchange.from_pub, \ encoder=nacl.encoding.HexEncoder) verify_key2 = nacl.signing.VerifyKey(exchange.to_pub,\ encoder=nacl.encoding.HexEncoder) try: verify_key1.verify(decode(exchange.sig["from"])) verify_key2.verify(decode(exchange.sig["to"])) except nacl.exceptions.BadSignatureError: print("wrong key") return False return True
def json_logs(self, user='', log=''): if self.user_agent() == 'x': return 0 if user and log: logs = self.project.getUserInfo(user, 'log_types') logsPath = self.project.getUserInfo(user, 'logs_path') if log.startswith('logCli_'): epname = '_'.join(log.split('_')[1:]) log = logsPath + os.sep + decode(epname) + '_CLI.log' else: log = logs.get(log) cherrypy.response.headers['Content-Type'] = 'application/json; charset=utf-8' cherrypy.response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' cherrypy.response.headers['Pragma'] = 'no-cache' cherrypy.response.headers['Expires'] = 0 return json.dumps(prepareLog(log or LOG_FILE))
def json_logs(self, user='', log=''): if self.user_agent() == 'x': return 0 if user and log: logs = self.project.getUserInfo(user, 'log_types') logsPath = self.project.getUserInfo(user, 'logs_path') if log.startswith('logCli_'): epname = '_'.join(log.split('_')[1:]) logCli = os.path.split(logs.get('logCli', 'CLI.log'))[1] log = logsPath + os.sep + decode(epname) +'_'+ logCli else: log = logs.get(log) cherrypy.response.headers['Content-Type'] = 'application/json; charset=utf-8' cherrypy.response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' cherrypy.response.headers['Pragma'] = 'no-cache' cherrypy.response.headers['Expires'] = 0 return json.dumps(prepareLog(log or LOG_FILE))
def json_save_project(self, user, epname): if self.user_agent() == 'x': return 0 cl = cherrypy.request.headers['Content-Length'] raw_data = cherrypy.request.body.read(int(cl)) json_data = json.loads(raw_data) del cl, raw_data # Delete everything from XML Root self.project.delSettingsKey(user, 'project', '//TestSuite') changes = 'Reset project file...\n' for suite_data in json_data: self.project.setPersistentSuite(user, suite_data['data'], {'ep': decode(epname)}) changes += 'Created suite: {0}.\n'.format(suite_data['data']) for file_data in suite_data.get('children', []): changes += 'Created file: {0}.\n'.format(file_data['data']) self.project.setPersistentFile(user, suite_data['data'], file_data['data'], {}) changes += '>.<\n' logDebug(changes) return 'true'
def test_user(): # Handle some data filename = 'small.txt' # This text has 800 characters data = read_data(filename) size = check_filesize(filename) ''' CREATE USERS FOR EXCHANGE ''' # Create a user user1 = User_Node(1) user2 = User_Node(2) # Fund this user user1.fund(1000) user1.add_storage(100000) # Upload file locally address1 = user1.upload(data, size) # Check the file stored data1 = user1.get_data() ''' VERIFY SIGNATURE FROM FILE ''' sig = data1[address1].sig pub_key = user1.get_public_key() verify_key = nacl.signing.VerifyKey(pub_key, \ encoder=nacl.encoding.HexEncoder) try: verify_key.verify(decode(sig)) except nacl.exceptions.BadSignatureError: print("Key wrong") return False ''' CHECK UPLOAD FILE TWICE. SHOULD NOT ADD MORE DATA - Works ''' address = user1.upload(data, size) assert (len(user1.get_data()) == 1) assert (address == address1) ''' UPDATING NEW FILE - Works ''' # New file new_filename = 'second.txt' new_data = read_data(new_filename) new_size = check_filesize(new_filename) # Update file addr2 = user1.update(new_data, new_size, address1) # Check to see if the update is correct # print(user1.get_file(addr2)) # print(user1.get_file(address1)) ''' REMOVE FILES - Works ''' # Remove file user1.remove_file(address1) assert (len(user1.get_data()) == 1) ''' EXCHANGE BETWEEN TWO USERS - Checked! ''' # User 2 requests a file from User 1 requested_addr = user2.request(addr2) # User 1 sends a file over requested_file = user1.get_file(requested_addr) # User 2 receives the file. user2.add_storage(10000) user2.receive(requested_addr, requested_file) # User 1 asks for a challenge bit = user1.challenge(requested_addr) # User 2 sends over the response proof = user2.respond(requested_addr, bit) # User 1 verify the response result = user1.verify(requested_addr, bit, proof) print(result) # Generate an exchange if verifying correctly if result: exchange = Exchange(user1.address, user2.address, user1.get_public_key(),\ user2.get_public_key(), new_size, requested_addr) user1.sign_exchange(exchange, 'from') user2.sign_exchange(exchange, 'to') # Need to seal the exchange (avoid tampering) user2.seal_exchange(exchange) # print(exchange.print()) ''' VERIFY EXCHANGE ''' # Just one user to verify exchange for now valid = user1.valid_exchange(exchange) print(valid) ''' RUNNING PROOF OF STAKES ''' # Look at the simulation for proof of stakes # Assuming user2 will put in the exchange to the block block = user2.create_block(exchange) # User 1 will update the exchange valid = user1.add_block(block) print(valid) return