def test_from_invalid_pubkeys(self): """Create P2PKHBitcoinAddress's from invalid pubkeys""" # first test with accept_invalid=True def T(invalid_pubkey, expected_str_addr): addr = P2PKHBitcoinAddress.from_pubkey(invalid_pubkey, accept_invalid=True) self.assertEqual(str(addr), expected_str_addr) T(x(''), '1HT7xU2Ngenf7D4yocz2SAcnNLW7rK8d4E') T( x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72' ), '1L9V4NXbNtZsLjrD3nkU7gtEYLWRBWXLiZ') # With accept_invalid=False we should get CBitcoinAddressError's with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_pubkey(x('')) with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_pubkey( x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72' )) with self.assertRaises(CBitcoinAddressError): P2PKHBitcoinAddress.from_pubkey( CPubKey( x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c72' )))
def VerifyMessage(address, message, sig): sig = base64.b64decode(sig) hash = message.GetHash() pubkey = CPubKey.recover_compact(hash, sig) return str(P2PKHBitcoinAddress.from_pubkey(pubkey)) == str(address)
def create_account(self, address, account): if not check_account(account): return False if self.get_account_by_address(address) is None: client_public_key = account['public_key'] decoded = client_public_key.decode("hex") pubkey = CPubKey(decoded) raw_address = P2PKHBitcoinAddress.from_pubkey(pubkey) derived_address = str(raw_address) if derived_address == address: account['nonce'] = generate_nonce() account['date_created'] = datetime.now().isoformat(' ') account['account_status'] = 'PENDING' account['address'] = str(address) try: account[ 'file_encryption_key'] = self.wallet.generate_encrypted_private_key( ) self.account_table.put_item(Item=account) except botocore.exceptions.ClientError as e: self.logger.exception("Houston, we have a problem: %s " % e.response) return self.send_confirmation_email(account) else: return None else: return None
def create_secure_payload(self, other_party_public_key_hex, message): other_party_public_key = CPubKey( other_party_public_key_hex.decode("hex")) encrypted = bitcoin_asymmetric_encrypt.encrypt(other_party_public_key, message) signature = self.wallet.sign(encrypted) payload = {'signature': signature, 'message': encrypted} return payload
def __init__(self, config): self.config = config requests.packages.urllib3.disable_warnings() self.notary_url = self.get_notary_url() response = requests.get(self.notary_url + '/api/v1/pubkey', verify=self.config.get_ssl_verify_mode()) data = response.json() self.other_party_public_key_hex = data['public_key'] other_party_public_key_decoded = self.other_party_public_key_hex.decode("hex") self.other_party_public_key = CPubKey(other_party_public_key_decoded) self.other_party_address = P2PKHBitcoinAddress.from_pubkey(self.other_party_public_key)
def test_from_valid_pubkey(self): """Create P2PKHBitcoinAddress's from valid pubkeys""" def T(pubkey, expected_str_addr): addr = P2PKHBitcoinAddress.from_pubkey(pubkey) self.assertEqual(str(addr), expected_str_addr) T( x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71' ), '1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8') T( x('0478d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71a1518063243acd4dfe96b66e3f2ec8013c8e072cd09b3834a19f81f659cc3455' ), '1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T') T( CPubKey( x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71' )), '1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8') T( CPubKey( x('0478d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71a1518063243acd4dfe96b66e3f2ec8013c8e072cd09b3834a19f81f659cc3455' )), '1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T')
notary_url = config.get_remote_server_url() else: notary_url = config.get_local_server_url() requests.packages.urllib3.disable_warnings() wallet = wallet.create_wallet(config.get_wallet_type(), config.get_key_id()) secure_message = SecureMessage(wallet) ## Test GET pubkey pubkey_response = requests.get(notary_url+'/api/v1/pubkey', verify=False) data = pubkey_response.json() other_party_public_key_hex = data['public_key'] print data['public_key'] 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)
import file_stream_encrypt import wallet from bitcoinlib.core.key import CPubKey import requests requests.packages.urllib3.disable_warnings() file_name = "/Users/tssbi08/Downloads/jdk-8u65-macosx-x64.dmg" encrypted_file = "/Users/tssbi08/Downloads/encrypt_jdk-8u65-macosx-x64.dmg" decrypted_file = "/Users/tssbi08/Downloads/decrypt-8u65-macosx-x64.dmg" wallet = wallet.PlainWallet() public_key = CPubKey(wallet.get_public_key_hex().decode("hex")) file_stream_encrypt.encrypt_file(file_name,encrypted_file,public_key) file_stream_encrypt.decrypt_file(encrypted_file,decrypted_file,wallet.get_private_key_wif())