def test_hrp_option_overwrites_hrp_from_diem_account_config_file( runner: CliRunner, event_loop: asyncio.events.AbstractEventLoop) -> None: app_config_file = "app.json" stub_config_file = "stub.json" with runner.isolated_filesystem(): LocalAccount(hrp=identifier.DM).write_to_file(app_config_file) LocalAccount(hrp=identifier.DM).write_to_file(stub_config_file) conf = start_target_server(runner, ["-i", app_config_file, "--hrp", "xdm"]) c = RestClient(server_url=conf.base_url, name="x") account = event_loop.run_until_complete(c.create_account()) account_identifier = event_loop.run_until_complete( account.generate_account_identifier()) assert account_identifier[:3] == "xdm" result = start_test( runner, conf, [ "-i", stub_config_file, "--stub-hrp", "xdm", "-k", "test_generate_account_identifier", ], ) assert result.exit_code == 0, result.output
def test_generate_keys(): account = LocalAccount() sig1 = account.private_key.sign(b"test") sig2 = account.compliance_key.sign(b"test") load_account = LocalAccount.from_dict(account.to_dict()) assert sig1 == load_account.private_key.sign(b"test") assert sig2 == load_account.compliance_key.sign(b"test")
async def test_submit_p2p_with_unknown_address(): client = create_client() faucet = Faucet(client) account = await faucet.gen_account() da = DiemAccount(account, [], client) with pytest.raises(ValueError): payee = LocalAccount().account_identifier() await da.submit_p2p(gen_txn(payee=payee), (b"", b""), by_address=LocalAccount().account_address)
def test_account_identifier(): account = LocalAccount() assert account.account_identifier() == identifier.encode_account(account.account_address, None, account.hrp) subaddress = identifier.gen_subaddress() assert account.account_identifier(subaddress) == identifier.encode_account( account.account_address, subaddress, account.hrp ) assert account.account_identifier(subaddress.hex()) == identifier.encode_account( account.account_address, subaddress, account.hrp )
def test_load_diem_account_config_file(runner: CliRunner) -> None: app_config_file = "app.json" stub_config_file = "stub.json" with runner.isolated_filesystem(): LocalAccount(hrp=identifier.DM).write_to_file(app_config_file) LocalAccount(hrp=identifier.DM).write_to_file(stub_config_file) conf = start_target_server(runner, ["-i", app_config_file]) result = start_test(runner, conf, ["-i", stub_config_file]) assert result.exit_code == 0, result.output
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_submit_p2p_with_unknown_address(): client = testnet.create_client() account = testnet.gen_account(client) da = DiemAccount(account, [], client) with pytest.raises(ValueError): da.submit_p2p(gen_txn(), (b"", b""), by_address=LocalAccount().account_address)
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 gen_txn() -> Transaction: return Transaction( id="txn", account_id="id", currency="XUS", amount=1, status=Transaction.Status.pending, type=Transaction.Type.sent_payment, payee=LocalAccount().account_identifier(None), )
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, )
def test_from_and_to_dict(): config = { "private_key": "ab70ae3aa603641f049a3356927d0ba836f775e862f559073a6281782479fd1e", "compliance_key": "f75b74a94250bda7abfab2045205e05c56e5dcba24ecea6aff75aac9463cdc2f", "hrp": "tdm", "txn_gas_currency_code": "XDX", "txn_max_gas_amount": 1000000, "txn_gas_unit_price": 0, "txn_expire_duration_secs": 30, } account = LocalAccount.from_dict(config) assert account.to_dict() == config
def test_gen_child_vasp(): client = testnet.create_client() faucet = testnet.Faucet(client) account = LocalAccount( hrp=identifier.DM, txn_gas_currency_code="XUS", txn_max_gas_amount=1_000_001, txn_gas_unit_price=1, txn_expire_duration_secs=60, ) faucet.mint(account.auth_key.hex(), 10_000_000, "XUS") child_account = account.gen_child_vasp(client, 1, "XUS") assert child_account.hrp == account.hrp assert child_account.txn_gas_currency_code == account.txn_gas_currency_code assert child_account.txn_max_gas_amount == account.txn_max_gas_amount assert child_account.txn_gas_unit_price == account.txn_gas_unit_price assert child_account.txn_expire_duration_secs == account.txn_expire_duration_secs assert child_account.compliance_key == account.compliance_key assert child_account.private_key != account.private_key child_diem_account = client.must_get_account(child_account.account_address) assert child_diem_account.role.type == "child_vasp"
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_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_decode_account_identifier(): account = LocalAccount() id1 = account.account_identifier() address, subaddress = account.decode_account_identifier(id1) assert address == account.account_address assert subaddress is None subaddress = identifier.gen_subaddress() id2 = account.account_identifier(subaddress) address, subaddress = account.decode_account_identifier(id2) assert address == account.account_address assert subaddress == subaddress