def test_deserialize_error_for_invalid_signature(): account = LocalAccount.generate() response = offchain.CommandResponseObject( status=offchain.CommandResponseStatus.success, cid="3185027f05746f5526683a38fdb5de98", ) data = offchain.jws.serialize(response, account.compliance_key.sign) account2 = LocalAccount.generate() with pytest.raises(cryptography.exceptions.InvalidSignature): offchain.jws.deserialize( data, offchain.CommandResponseObject, account2.compliance_key.public_key().verify, )
def test_create_child_vasp(): client = testnet.create_client() faucet = testnet.Faucet(client) parent_vasp = faucet.gen_account() seq_num = client.get_account_sequence(parent_vasp.account_address) child_vasp = LocalAccount.generate() currency = testnet.TEST_CURRENCY_CODE raw_txn = diem_types.RawTransaction( sender=parent_vasp.account_address, sequence_number=seq_num, payload=diem_types.TransactionPayload__Script( stdlib.encode_create_child_vasp_account_script( coin_type=utils.currency_code(currency), child_address=child_vasp.account_address, auth_key_prefix=child_vasp.auth_key.prefix(), add_all_currencies=False, child_initial_balance=100_000_000, ) ), max_gas_amount=1_000_000, gas_unit_price=0, gas_currency_code=currency, expiration_timestamp_secs=int(time.time()) + 30, chain_id=testnet.CHAIN_ID, ) txn = parent_vasp.sign(raw_txn) client.submit(txn) executed_txn = client.wait_for_transaction(txn) assert executed_txn is not None
async def test_create_child_vasp(): client = create_client() faucet = Faucet(client) parent_vasp = await faucet.gen_account() seq_num = await client.get_account_sequence(parent_vasp.account_address) child_vasp = LocalAccount.generate() payload = stdlib.encode_create_child_vasp_account_script_function( coin_type=utils.currency_code(XUS), child_address=child_vasp.account_address, auth_key_prefix=child_vasp.auth_key.prefix(), add_all_currencies=False, child_initial_balance=100_000_000, ) raw_txn = diem_types.RawTransaction( sender=parent_vasp.account_address, sequence_number=seq_num, payload=payload, max_gas_amount=1_000_000, gas_unit_price=0, gas_currency_code=XUS, expiration_timestamp_secs=int(time.time()) + 30, chain_id=chain_ids.TESTNET, ) txn = parent_vasp.sign(raw_txn) await client.submit(txn) executed_txn = await client.wait_for_transaction(txn) assert executed_txn is not None
async def test_get_account_sequence(client: AsyncClient): seq = await client.get_account_sequence(DD_ADDRESS) assert isinstance(seq, int) assert seq > 0 local = LocalAccount.generate() with pytest.raises(jsonrpc.AccountNotFoundError): await client.get_account_sequence(local.account_address)
def test_get_account_sequence(): client = testnet.create_client() seq = client.get_account_sequence(testnet.DESIGNATED_DEALER_ADDRESS) assert isinstance(seq, int) assert seq > 0 local = LocalAccount.generate() with pytest.raises(jsonrpc.AccountNotFoundError): client.get_account_sequence(local.account_address)
def new_payment_object(self, sender=LocalAccount.generate(), receiver=LocalAccount.generate()): amount = 1_000_000_000_000 currency = testnet.TEST_CURRENCY_CODE sender_account_id = sender.account_identifier(identifier.gen_subaddress()) sender_kyc_data = offchain.individual_kyc_data( given_name="Jack", surname="G", address=offchain.AddressObject(city="San Francisco"), ) receiver_account_id = receiver.account_identifier(identifier.gen_subaddress()) return offchain.new_payment_object( sender_account_id, sender_kyc_data, receiver_account_id, amount, currency, )
async def test_ensure_account_balance_is_always_enough(): client = create_client() faucet = Faucet(client) account = LocalAccount.generate() await faucet.mint(account.auth_key.hex(), 1, XUS) da = DiemAccount(account, [], client) account_data = await client.must_get_account(account.account_address) amount = utils.balance(account_data, XUS) + 1 payee = await faucet.gen_account() txn = await da.submit_p2p(gen_txn(payee=payee.account_identifier(), amount=amount), (b"", b"")) await client.wait_for_transaction(txn)
def test_serialize_deserialize(): account = LocalAccount.generate() response = offchain.CommandResponseObject( status=offchain.CommandResponseStatus.success, cid="3185027f05746f5526683a38fdb5de98", ) ret = offchain.jws.serialize(response, account.compliance_key.sign) resp = offchain.jws.deserialize( ret, offchain.CommandResponseObject, account.compliance_key.public_key().verify, ) assert resp == response
def test_serialize_and_deserialize_with_additional_protocted_headers(): account = LocalAccount.generate() response = offchain.CommandResponseObject( status=offchain.CommandResponseStatus.success, cid="3185027f05746f5526683a38fdb5de98", ) ret = offchain.jws.serialize(response, account.private_key.sign, headers={"keyId": "hello", "alg": "EdDSA"}) resp = offchain.jws.deserialize( ret, offchain.CommandResponseObject, account.private_key.public_key().verify, ) assert resp == response
def test_get_parent_vasp_account(): client = testnet.create_client() faucet = testnet.Faucet(client) parent_vasp = faucet.gen_account() child_vasp = LocalAccount.generate() signed_txn = create_child_vasp_txn(parent_vasp, child_vasp) client.submit(signed_txn) client.wait_for_transaction(signed_txn) account = client.get_parent_vasp_account(child_vasp.account_address) expected_address = utils.account_address_hex(parent_vasp.account_address) assert account.address == expected_address account = client.get_parent_vasp_account(parent_vasp.account_address) assert account.address == expected_address
def test_submit_create_child_vasp(): client = testnet.create_client() faucet = testnet.Faucet(client) parent_vasp = faucet.gen_account() child_vasp = LocalAccount.generate() signed_txn = create_child_vasp_txn(parent_vasp, child_vasp) client.submit(signed_txn) executed_txn = client.wait_for_transaction(signed_txn) assert executed_txn is not None assert isinstance(executed_txn, jsonrpc.Transaction) assert executed_txn.vm_status.type == jsonrpc.VM_STATUS_EXECUTED # wait for transaction by signed txn hex string signed_txn_hex = signed_txn.bcs_serialize().hex() executed_txn = client.wait_for_transaction(signed_txn_hex) assert executed_txn is not None assert isinstance(executed_txn, jsonrpc.Transaction) assert executed_txn.vm_status.type == jsonrpc.VM_STATUS_EXECUTED # should include events assert len(executed_txn.events) > 0
def test_get_account_not_exist(): local_account = LocalAccount.generate() client = testnet.create_client() account = client.get_account(local_account.account_address) assert account is None
def test_account_not_found_error_when_get_base_url_and_compliance_key_for_invalid_account( ): client = testnet.create_client() account = LocalAccount.generate() with pytest.raises(jsonrpc.AccountNotFoundError): client.get_base_url_and_compliance_key(account.account_address)
def test_get_parent_vasp_account_not_found(): client = testnet.create_client() parent_vasp = LocalAccount.generate() with pytest.raises(jsonrpc.AccountNotFoundError): client.get_parent_vasp_account(parent_vasp.account_address)
def test_from_private_key_hex(): account = LocalAccount.generate() hex_key = utils.private_key_bytes(account.private_key).hex() new_account = LocalAccount.from_private_key_hex(hex_key) assert utils.private_key_bytes(new_account.private_key).hex() == hex_key
def test_new_payment_request_and_object(factory): sender = LocalAccount.generate() receiver = LocalAccount.generate() payment = factory.new_payment_object(sender, receiver) request = offchain.new_payment_request(payment) reference_id = payment.reference_id assert reference_id assert_cid(request.cid) assert uuid.UUID(reference_id) assert "-" in reference_id payment = offchain.from_dict(request.command, offchain.PaymentCommandObject).payment address, subaddress = identifier.decode_account(payment.sender.address, identifier.TDM) assert subaddress is not None assert address == sender.account_address address, subaddress = identifier.decode_account(payment.receiver.address, identifier.TDM) assert subaddress is not None assert address == receiver.account_address expected = f"""{{ "cid": "{request.cid}", "command_type": "PaymentCommand", "command": {{ "_ObjectType": "PaymentCommand", "payment": {{ "reference_id": "{reference_id}", "sender": {{ "address": "{payment.sender.address}", "status": {{ "status": "needs_kyc_data" }}, "kyc_data": {{ "type": "individual", "payload_version": 1, "given_name": "Jack", "surname": "G", "address": {{ "city": "San Francisco" }} }} }}, "receiver": {{ "address": "{payment.receiver.address}", "status": {{ "status": "none" }} }}, "action": {{ "amount": 1000000000000, "currency": "XUS", "action": "charge", "timestamp": {payment.action.timestamp} }} }} }}, "_ObjectType": "CommandRequestObject" }}""" assert json.loads(offchain.to_json(request)) == json.loads(expected) assert request == offchain.from_json(expected, offchain.CommandRequestObject) assert request == offchain.from_json(expected)
async def test_get_account_not_exist(client: AsyncClient): local_account = LocalAccount.generate() account = await client.get_account(local_account.account_address) assert account is None
async def test_account_not_found_error_when_get_base_url_and_compliance_key_for_invalid_account(client: AsyncClient): account = LocalAccount.generate() with pytest.raises(jsonrpc.AccountNotFoundError): await client.get_base_url_and_compliance_key(account.account_address)
async def test_get_parent_vasp_account_not_found(client: AsyncClient): parent_vasp = LocalAccount.generate() with pytest.raises(jsonrpc.AccountNotFoundError): await client.get_parent_vasp_account(parent_vasp.account_address)