def instance(self): if not self.wallet_exists(): self.create_new_wallet() self.private_key_hex = self.read_private_key() self.private_key_wif = base58.base58_check_encode( 0x80, self.private_key_hex.decode("hex")) self.private_key = CBitcoinSecret(self.private_key_wif)
def download_file(self, document_hash, storing_file_name, encrypted=False): 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 cookies = self.authenticate() if cookies is not None: download_response = requests.get(self.notary_server.get_upload_url(self.address, document_hash), cookies=cookies, allow_redirects=True, verify=False) if download_response.status_code == 200: # Need to add error handling ultimate_file_name = str(storing_file_name) if encrypted: ultimate_file_name = storing_file_name+".download_encrypted" with open(ultimate_file_name, 'wb') as f: for chunk in download_response.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks f.write(chunk) if encrypted: file_stream_encrypt.decrypt_file(storing_file_name+".download_encrypted", storing_file_name, private_key_wif) return storing_file_name return None
def test_sign_message_simple(self): key = CBitcoinSecret( "L4vB5fomsK8L95wQ7GFzvErYGht49JsCPJyJMHpB4xGM6xgi2jvG") address = "1F26pNMrywyZJdr22jErtKcjF8R3Ttt55G" message = address message = BitcoinMessage(message) signature = SignMessage(key, message) self.assertTrue(signature) self.assertTrue(VerifyMessage(address, message, signature))
def test_sign_message_vectors(self): for vector in load_test_vectors('signmessage.json'): key = CBitcoinSecret(vector['wif']) message = BitcoinMessage(vector['address']) signature = SignMessage(key, message) self.assertTrue(signature, "Failed to sign for [%s]" % vector['address']) self.assertTrue( VerifyMessage(vector['address'], message, vector['signature']), "Failed to verify signature for [%s]" % vector['address'])
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 instance(self): try: self.kms = boto3.client('kms', region_name='us-east-1') self.dynamodb = resource_factory.get_dynamodb(self.config) except botocore.exceptions.ClientError as e: self.logger.exception("Error creating server wallet %s " % e.message) if not self.wallet_exists(): self.create_new_wallet() try: self.private_key_hex = self.read_private_key() self.private_key_wif = base58.base58_check_encode( 0x80, self.private_key_hex.decode("hex")) self.private_key = CBitcoinSecret(self.private_key_wif) except ValueError as e: self.logger.exception("Problem with wallet %s " % e.message)
def dumpprivkey(self, addr): """Return the private key matching an address """ r = self._call('dumpprivkey', str(addr)) return CBitcoinSecret(r)
def instance(self): self.private_key_wif = base58.base58_check_encode( 0x80, self.private_key_hex.decode("hex")) self.private_key = CBitcoinSecret(self.private_key_wif)
message = "bitid://localhost:5000/callback?x=30f56bc022dde976&u=1" print("\nClear: %s" % message) encrypted = encrypt.encrypt(wallet.get_public_key(), message) print("\nEncrypted: %s" % encrypted) decrypted = encrypt.decrypt(wallet.get_private_key_wif(), encrypted) print("\nDecrypted: %s" % decrypted) signature = wallet.sign(message) print("\nSignature: %s" % signature) print("\nVerified: %s" % wallet.verify(message, signature)) test1_raw_hex = '3e52050b58e1765ca9abfce576aa0efc27eaa4dd11a4051affabd050e6b92324' test1_private_key_wif = privateKeyToWif(test1_raw_hex) test1_key = CBitcoinSecret(test1_private_key_wif) test1_pub = test1_key.pub test2_private_key_wif = wallet.get_private_key_wif() test2_key = wallet.get_private_key() test2_pub = test2_key.pub message = "foobar" print("\nClear: %s" % message) encrypted = encrypt.encrypt(test1_pub, message) print("\nEncrypted1: %s" % encrypted) decrypted = encrypt.decrypt(test1_private_key_wif, encrypted) print("\nDecrypted1: %s" % decrypted)