def notarize_file(self, path_to_file, metadata): ''' the main method to notarize a file. Parameters ---------- path_to_file : the fp to the file. ( Not file name). Need to support file name. metadata : a JSON object containing metadata Returns ------- returns the transaction hash and document hash. ''' # hash the file and generate the document hashh if type(path_to_file) is str: document_hash = hashfile.hash_file(path_to_file) else: document_hash = hashfile.hash_file_fp(path_to_file) metadata['document_hash'] = document_hash # create a secure payload notarization_payload = self.get_payload(metadata) # Have to authenticate cookies = self.authenticate() if cookies is not None: response = requests.put(self.notary_server.get_notarization_url(self.address, document_hash), cookies=cookies, data=notarization_payload, verify=self.ssl_verify_mode) if response.status_code == 200: payload = json.loads(response.content) if self.secure_message.verify_secure_payload(self.notary_server.get_address(), payload): message = self.secure_message.get_message_from_secure_payload(payload) return json.loads(message) return None
def checkroms(roms): checks = {} for romname in tqdm(roms, desc='roms', unit='rom'): try: romcheck = {} romcheck['path'] = romname romcheck['files'] = {} with tempzip(romname) as temp: wildcard = os.path.join(temp, '**\\*') files = glob.glob(wildcard, recursive=True) for file in files: path = Path(file) if not path.is_file(): continue filename = file.replace(temp, '')[1:] info = {} size = os.path.getsize(file) info['size'] = size info['name'] = filename crc = hashfile.checksum_file(file, 'crc32') info['crc'] = crc sha1 = hashfile.hash_file(file, 'sha1') info['sha1'] = sha1 romcheck['files'][filename] = info checks[romname] = romcheck except Exception as e: print(e) print(f'Bad rom zip {romname}') continue return checks
def upload_file(self, path_to_file): """ uploads a file to server Parameters ---------- path_to_file : file full path name. Returns ------- the http status from the server """ try: cookies = self.authenticate() except NotaryException as e: raise NotaryException(e.error_code, e.message) document_hash = hashfile.hash_file(path_to_file) try: files = {"document_content": open(path_to_file, "rb")} upload_response = requests.put( self.notary_server.get_document_url(self.address, document_hash), cookies=cookies, files=files, verify=False, ) if upload_response.status_code != 200: raise NotaryException(upload_response.status_code, "Problem uploading file!") return upload_response.status_code except requests.ConnectionError as e: raise NotaryException(-1, e.message) except NotaryException as ne: raise NotaryException(ne.error_code, ne.message)
def upload_file_encrypted(self, path_to_file): """ uploads a file to server encrypting along the way Parameters ---------- path_to_file : file full path name. Returns ------- the http status from the server """ try: file_encryption_wallet = self.get_file_encryption_wallet() cookies = self.authenticate() except NotaryException as e: raise NotaryException(e.error_code, e.message) document_hash = hashfile.hash_file(path_to_file) try: file_stream_encrypt.encrypt_file( path_to_file, path_to_file + ".encrypted", file_encryption_wallet.get_public_key() ) files = {"document_content": open(path_to_file + ".encrypted", "rb")} upload_response = requests.put( self.notary_server.get_document_url(self.address, document_hash), cookies=cookies, files=files, verify=False, ) return upload_response.status_code except requests.ConnectionError as e: raise NotaryException(upload_response.status_code, "Problem uploading file!")
def upload_file(self, path_to_file, encrypted=False): ''' uploads a file to server Parameters ---------- path_to_file : give a file pointer,i.e. file pointer. Need change code support file full path name. Returns ------- the http status from the server ''' if encrypted: reg_status = self.register_user_status() private_key_hex = str(reg_status['file_encryption_key']) private_key_wif = base58.base58_check_encode(0x80, private_key_hex.decode("hex")) private_key = CBitcoinSecret(private_key_wif) public_key = private_key.pub if type(path_to_file) is str: document_hash = hashfile.hash_file(path_to_file) else: document_hash = hashfile.hash_file_fp(path_to_file) cookies = self.authenticate() if cookies is not None: check_notarized = requests.get(self.notary_server.get_notarization_status_url(self.address, document_hash), cookies=cookies, verify=False) if check_notarized is not None: if check_notarized.status_code == 404: return None elif check_notarized.status_code == 200: try: cookies = requests.utils.dict_from_cookiejar(check_notarized.cookies) if encrypted: file_stream_encrypt.encrypt_file(path_to_file,path_to_file+".encrypted", public_key) files = {'document_content': open(path_to_file+".encrypted", 'rb')} else: files = {'document_content': open(path_to_file, 'rb')} upload_response = requests.put( self.notary_server.get_upload_url(self.address, document_hash), cookies=cookies, files=files, verify=False) return upload_response.status_code except requests.ConnectionError as e: print (e.message) return None
def notarize_file(self, path_to_file, metadata): """ the main method to notarize a file. Parameters ---------- path_to_file : the fp to the file. ( Not file name). Need to support file name. metadata : a JSON object containing metadata Returns ------- returns the transaction hash and document hash. """ # hash the file and generate the document hashh try: cookies = self.authenticate() except NotaryException as e: raise NotaryException(e.error_code, e.message) document_hash = hashfile.hash_file(path_to_file) metadata["document_hash"] = document_hash # create a secure payload notarization_payload = self.get_payload(metadata) # Have to authenticate response = requests.put( self.notary_server.get_notarization_url(self.address, document_hash), cookies=cookies, data=notarization_payload, verify=self.ssl_verify_mode, ) if response.status_code == 200: payload = json.loads(response.content) if self.secure_message.verify_secure_payload(self.notary_server.get_address(), payload): message = self.secure_message.get_message_from_secure_payload(payload) return json.loads(message) else: raise NotaryException(response.status_code, "Error notarizing!")
'title': 'Stillwater Shame', 'creator': 'Ploughman, J.J.', 'subject': 'Rock Music', 'description': 'A song about lying politicians', 'publisher': 'J.J. Ploughman', 'contributor': 'J.J. Ploughman', 'date': '2001-08-03T03:00:00.000000', 'type': 'Music', 'format': 'm4a', 'source': 'Green Beans Album', 'language': 'en', 'relation': 'Unknown', 'coverage': 'Unknown', 'rights': 'Unknown' } document_hash = hashfile.hash_file(test_data.notary_file_name) metadata['document_hash'] = document_hash response = requests.get(notary_url+'/api/v1/account/' + address + '/notarization/' + document_hash + '/status', cookies=cookies, verify=False) if response.status_code == 404: print ('No notarization!') elif response.content is not None: str_content = response.content payload = json.loads(response.content) if secure_message.verify_secure_payload(other_party_address, payload): message = secure_message.get_message_from_secure_payload(payload) print(message)
other_party_public_key_decoded = other_party_public_key_hex.decode("hex") other_party_public_key = CPubKey(other_party_public_key_decoded) other_party_address = P2PKHBitcoinAddress.from_pubkey(other_party_public_key) address = str(wallet.get_bitcoin_address()) ## Test GET challenge response = requests.get(notary_url+'/api/v1/challenge/' + address, verify=False) payload = json.loads(response.content) if secure_message.verify_secure_payload(other_party_address, payload): message = secure_message.get_message_from_secure_payload(payload) print(message) payload = secure_message.create_secure_payload(other_party_public_key_hex, message) response = requests.put(notary_url+'/api/v1/challenge/' + address, data=payload, verify=False) cookies = requests.utils.dict_from_cookiejar(response.cookies) document_hash = hashfile.hash_file(test_data.notary_file_name) response = requests.get(notary_url+'/api/v1/account/' + address + '/document/' + document_hash + '/status', cookies=cookies, verify=False) if response.content is not None: if response.status_code == 404: print ("Document not found!") elif response.status_code == 200: str_content = response.content payload = json.loads(response.content) if secure_message.verify_secure_payload(other_party_address, payload): message = secure_message.get_message_from_secure_payload(payload) print(message)
## Test GET challenge response = requests.get(notary_url + '/api/v1/challenge/' + address, verify=False) payload = json.loads(response.content) if secure_message.verify_secure_payload(other_party_address, payload): message = secure_message.get_message_from_secure_payload(payload) print(message) payload = secure_message.create_secure_payload(other_party_public_key_hex, message) response = requests.put(notary_url + '/api/v1/challenge/' + address, data=payload, verify=False) cookies = requests.utils.dict_from_cookiejar(response.cookies) govern8r_token = cookies['govern8r_token'] print("Token from authentication: %s" % govern8r_token) print("Status: %s" % response.status_code) file_name = test_data.notary_file_name document_hash = hashfile.hash_file(file_name) url = notary_url + '/api/v1/account/' + address + '/document/' + document_hash local_filename = 'test.txt' r = requests.get(url, cookies=cookies, allow_redirects=True, verify=False) with open(local_filename, 'wb') as f: for chunk in r.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks f.write(chunk)
def getMetaData(): meta_data = { 'title': 'My favorite beaver', 'creator': 'Ploughman, J.J.', 'subject': 'TV show', 'description': 'A show about a blake... that you hate the best...', 'publisher': 'J.J. Ploughman', 'contributor': 'J.J. Ploughman', 'date': '2001-08-03T03:00:00.000000', 'type': 'Video', 'format': 'mpeg', 'source': 'CBS', 'language': 'en', 'relation': 'Unknown', 'coverage': 'Unknown', 'rights': 'Unknown' } return meta_data config_file_name = "notaryconfig.ini" email_address = "*****@*****.**" notary_file_name = '/Users/tssbi08/govern8r/IP/README.txt' # with open(notary_file_name, 'wb') as output: # output.write(os.urandom(64).encode("hex")) document_hash = hashfile.hash_file(notary_file_name) storing_file_name = '/Users/tssbi08/govern8r/IP/downloadedfile.txt'
payload = secure_message.create_secure_payload(other_party_public_key_hex, message) response = requests.put(notary_url+'/api/v1/challenge/'+address, data=payload, verify=False) cookies = requests.utils.dict_from_cookiejar(response.cookies) govern8r_token = cookies['govern8r_token'] print("Token from authentication: %s" % govern8r_token) print("Status: %s" % response.status_code) #Upload using PUT #file_name = "/Users/tssbi08/Downloads/jdk-8u65-macosx-x64.dmg" #encrypted_file = "/Users/tssbi08/Downloads/encrypt_jdk-8u65-macosx-x64.dmg" file_name = test_data.notary_file_name # encrypted_file = '/Users/tssbi08/govern8r/IP/Encrypted_README.txt' #public_key = CPubKey(wallet.get_public_key_hex().decode("hex")) #file_stream_encrypt.encrypt_file(file_name,encrypted_file,public_key) document_hash = hashfile.hash_file(file_name) response = requests.get(notary_url+'/api/v1/account/' + address + '/document/' + document_hash + '/status', cookies=cookies, verify=False) if response.content is not None: if response.status_code == 404: print ("Document not found!") elif response.status_code == 200: try: files = {'document_content': open(file_name, 'rb')} r = requests.put(notary_url+'/api/v1/account/'+address+'/document/'+document_hash, cookies=cookies, files=files, verify=False) print r.status_code except requests.ConnectionError as e: print(e.message)
def getMetaData(): meta_data= { 'title': 'My favorite beaver', 'creator': 'Ploughman, J.J.', 'subject': 'TV show', 'description': 'A show about a blake... that you hate the best...', 'publisher': 'J.J. Ploughman', 'contributor': 'J.J. Ploughman', 'date': '2001-08-03T03:00:00.000000', 'type': 'Video', 'format': 'mpeg', 'source': 'CBS', 'language': 'en', 'relation': 'Unknown', 'coverage': 'Unknown', 'rights': 'Unknown' } return meta_data config_file_name="notaryconfig.ini" email_address="*****@*****.**" notary_file_name='/Users/tssbi08/govern8r/IP/README.txt' # with open(notary_file_name, 'wb') as output: # output.write(os.urandom(64).encode("hex")) document_hash = hashfile.hash_file(notary_file_name) storing_file_name='/Users/tssbi08/govern8r/IP/downloadedfile.txt'