def test_auth_get_account(client): """`GET <auth>` succeeds with a valid TransactionEnvelope XDR.""" response = client.get(f"/auth?account={STELLAR_ACCOUNT_1}", follow=True) content = json.loads(response.content) assert content["network_passphrase"] == "Test SDF Network ; September 2015" assert content["transaction"] envelope_xdr = content["transaction"] envelope_object = TransactionEnvelope.from_xdr(envelope_xdr) transaction_object = envelope_object.tx assert transaction_object.sequence == 0 assert len(transaction_object.operations) == 1 manage_data_op = transaction_object.operations[0] assert manage_data_op.type_code() == Xdr.const.MANAGE_DATA assert manage_data_op.data_name == "SEP 6 Reference auth" assert len(manage_data_op.data_value) == 64 signatures = envelope_object.signatures assert len(signatures) == 1 server_signature = signatures[0] tx_hash = envelope_object.hash_meta() server_public_key = Keypair.from_address(settings.STELLAR_ACCOUNT_ADDRESS) server_public_key.verify(tx_hash, server_signature.signature)
def _validate_envelope_xdr(envelope_xdr): """ Validate the provided TransactionEnvelope XDR (base64 string). Return the appropriate error if it fails, else the empty string. """ envelope_object = TransactionEnvelope.from_xdr(envelope_xdr) transaction_object = envelope_object.tx if str(transaction_object.source.decode() ) != settings.STELLAR_ACCOUNT_ADDRESS: return "incorrect source account" if transaction_object.sequence != 0: return "incorrect sequence" if len(transaction_object.operations) != 1: return "incorrect number of operations" manage_data_op = transaction_object.operations[0] if manage_data_op.type_code() != Xdr.const.MANAGE_DATA: return "incorrect operation type" if manage_data_op.data_name != ANCHOR_NAME + " auth": return "incorrect data key in managedata" if len(manage_data_op.data_value) != 64: return "invalid data value in managedata" signatures = envelope_object.signatures if len(signatures) != 2: return "incorrect number of signatures" transaction_hash = envelope_object.hash_meta() server_signature = signatures[0] server_public_keypair = Keypair.from_address( settings.STELLAR_ACCOUNT_ADDRESS) try: server_public_keypair.verify(transaction_hash, server_signature.signature) except ed25519.BadSignatureException: return "invalid server signature" client_signature = signatures[1] client_account = manage_data_op.source client_public_keypair = Keypair.from_address(client_account) try: client_public_keypair.verify(transaction_hash, client_signature.signature) except ed25519.BadSignatureError: return "invalid client signature" return ""
def build(self, developer_secret, address, network=None): dev_keypair = Keypair.from_seed(developer_secret) dev_account = AccountBuilder().build(dev_keypair) user_keypair = Keypair.from_address(address) user_account = AccountBuilder().build(user_keypair) return App(dev_account,user_account,network)
def balance_match(self, asset, balance): balance = dict(balance) # Bugfix for python 3.5 if asset.is_native(): return balance['asset_type'] == 'native' else: asset_code = balance['asset_code'] issuer = balance['asset_issuer'] asset_issuer_addr = Keypair.from_address( asset.issuer).address().decode() return asset_code == asset.code and issuer == asset_issuer_addr
def verify(pubkey, data): if pubkey not in pool_signers: return False data = bytes(base64.b64decode(data)) pubkp = Keypair.from_address(pubkey) try: pubkp.verify(phrase, data) except: return False else: return True
def _validate(cls, message): ''' `transaction_message` is just for simple json string. `validate()` will return the `Transaction` instance. ''' j = json.loads(message) body = json.loads(j['body']) # verification of address try: Keypair.from_address(body['source_address']) except stellar_base_DecodeError as e: log.debug('failed to check `source_address`: %s', e) return None try: Keypair.from_address(body['receiver_address']) except stellar_base_DecodeError as e: log.debug('failed to check `receiver_address`: %s', e) return None # verification of signature and body try: Keypair.from_address(body['source_address']).verify(j['body'].encode('utf-8'), base64.b64decode(j['signature'])) except Exception as e: import traceback traceback.print_exc() log.debug('failed to check signature from message, "%s": %s', message, e) return None if body['amount'] < 0: log.debug('failed to check amount: invalid `amount`, %d', body['amount']) return None return cls.from_message(message)
# # decrypt text using stellar EdDAS keys # import sys, getopt, binascii, json, libnacl, libnacl.public from stellar_base.keypair import Keypair # get the message line = sys.stdin.readline() data = json.loads(line) sk = b'SCMEVUFPDAH7YOY6QFPTRQT3AXUXBHBYCLI7CEWVEF7IBJK6ZXKP6VV5' pk = data["public_key"] # make keypairs kp1 = Keypair.from_seed(sk) kp2 = Keypair.from_address(pk) # convert to curve keys curve_kp1_sk = libnacl.crypto_sign_ed25519_sk_to_curve25519(kp1.signing_key.to_bytes() ) curve_kp2_pk = libnacl.crypto_sign_ed25519_pk_to_curve25519(kp2.verifying_key.to_bytes() ) # setup decryption box crypt_box = libnacl.public.Box(curve_kp1_sk, curve_kp2_pk) # decode data["details"] = json.loads ( crypt_box.decrypt( binascii.unhexlify( data["encrypted"] ) ).decode("utf-8") ) del data["encrypted"] print( json.dumps(data, indent=4, sort_keys=False) )
def test_sign_missing_signing_key_raise(self): keypair = Keypair.from_address(self.keypair0.address()) raises(MissingSigningKeyError, keypair.sign, "")
def dev_keypair(self): return Keypair.from_address(self.address)
def user_keypair(self): return Keypair.from_address(self.address)
def _is_valid_key(self, public_key): try: Keypair.from_address(address=public_key) return True except Exception: raise InvalidStellarAddressException("Invalid stellar address")