Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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