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
def test_sync_catch_error(): jsonrpc_client = testnet.create_client() faucet = testnet.Faucet(jsonrpc_client) parent_vasp = faucet.gen_account() account = jsonrpc_client.get_account(parent_vasp.account_address) processor = ProcessorStub(True) config = DEFL_CONFIG.copy() config["accounts"] = [account.address] config["processor"] = processor with tempfile.TemporaryDirectory() as tmpdir: config["progress_file_path"] = tmpdir + "/progress" client = LRWPubSubClient(config) # raise error by default with pytest.raises(Exception): client.sync({account.received_events_key: 0}) with pytest.raises(Exception): client.sync({account.received_events_key: 0}, False) # catch error, return old state new_state = client.sync({account.received_events_key: 0}, True) assert new_state == {account.received_events_key: 0}
def test_custodial_to_non_custodial(): client = testnet.create_client() faucet = testnet.Faucet(client) sender_custodial = CustodialApp.create(faucet.gen_account()) receiver = faucet.gen_account() amount = 1_000_000 currency_code = testnet.TEST_CURRENCY_CODE script = stdlib.encode_peer_to_peer_with_metadata_script( currency=utils.currency_code(currency_code), payee=utils.account_address(receiver.account_address), amount=amount, metadata=txnmetadata.general_metadata( sender_custodial.find_user_sub_address_by_id(0), None), metadata_signature=b"", # only travel rule metadata requires signature ) sender = sender_custodial.available_child_vasp() seq_num = client.get_account_sequence(sender.account_address) txn = create_transaction(sender, seq_num, script, currency_code) signed_txn = sender.sign(txn) client.submit(signed_txn) executed_txn = client.wait_for_transaction(signed_txn) assert executed_txn is not None
def test_get_account_by_hex_encoded_account_address(): client = testnet.create_client() account = client.get_account(utils.TREASURY_ADDRESS) assert account is not None assert isinstance(account, jsonrpc.Account) assert account.role is not None assert account.role.type == jsonrpc.ACCOUNT_ROLE_UNKNOWN
def test_get_account(): client = testnet.create_client() account = client.get_account(testnet.DESIGNATED_DEALER_ADDRESS) assert account is not None assert isinstance(account, jsonrpc.Account) assert account.role is not None assert account.role.type == jsonrpc.ACCOUNT_ROLE_DESIGNATED_DEALER
def test_get_vasp_domain_map(): client = testnet.create_client() if not client.support_diem_id(): pytest.skip("diem id not supported") faucet = testnet.Faucet(client) parent_vasp1 = faucet.gen_account() parent_vasp2 = faucet.gen_account() domain1 = "domain1" domain2 = "domain2" faucet.mint(parent_vasp1.auth_key.hex(), 1, testnet.TEST_CURRENCY_CODE, vasp_domain=domain1, is_remove_domain=False) faucet.mint(parent_vasp2.auth_key.hex(), 1, testnet.TEST_CURRENCY_CODE, vasp_domain=domain2, is_remove_domain=False) domain_map = client.get_vasp_domain_map() assert domain_map.get(domain1) == parent_vasp1.account_address.to_hex() assert domain_map.get(domain2) == parent_vasp2.account_address.to_hex() faucet.mint(parent_vasp1.auth_key.hex(), 1, testnet.TEST_CURRENCY_CODE, vasp_domain=domain1, is_remove_domain=True) domain_map = client.get_vasp_domain_map() assert domain_map.get(domain1) is None
def main(): # connect to testnet client = testnet.create_client() # generate private key private_key = Ed25519PrivateKey.generate() # generate auth key auth_key = AuthKey.from_public_key(private_key.public_key()) print( f"Generated address: {utils.account_address_hex(auth_key.account_address())}" ) # create new account faucet = testnet.Faucet(client) testnet.Faucet.mint(faucet, auth_key.hex(), 100_000_000, CURRENCY) # get account events key account = client.get_account(auth_key.account_address()) events_key = account.received_events_key # start minter to demonstrates events creation start_minter(client, auth_key) # demonstrates events subscription subscribe(client, events_key)
def test_could_not_find_compliance_key_of_x_request_sender_address(sender_app, receiver_app): account = testnet.gen_account(testnet.create_client()) account_id = identifier.encode_account(account.account_address, None, sender_app.hrp) request = minimum_required_fields_request_sample(sender_app, receiver_app) request["command"]["payment"]["sender"]["address"] = account_id resp = send_request(request, sender_app, receiver_app, "failure", sender_address=account_id) assert_response_protocol_error(resp, "invalid_http_header")
def test_send_and_deserialize_request(factory): client = testnet.create_client() receiver_port = offchain.http_server.get_available_port() sender = testnet.gen_vasp_account(client, "http://localhost:8888") receiver = testnet.gen_vasp_account(client, f"http://localhost:{receiver_port}") sender_client = factory.create_offchain_client(sender, client) receiver_client = factory.create_offchain_client(receiver, client) def process_inbound_request(x_request_id: str, jws_key_address: str, content: bytes): command = receiver_client.process_inbound_request( jws_key_address, content) resp = offchain.reply_request(command.cid) return (200, offchain.jws.serialize(resp, receiver.compliance_key.sign)) offchain.http_server.start_local(receiver_port, process_inbound_request) payment = factory.new_payment_object(sender, receiver) command = offchain.PaymentCommand(payment=payment, my_actor_address=payment.sender.address, inbound=False) resp = sender_client.send_command(command, sender.compliance_key.sign) assert resp
def test_get_account_state_with_proof(): client = testnet.create_client() state_proof = client.get_account_state_with_proof( testnet.DESIGNATED_DEALER_ADDRESS) assert state_proof is not None assert isinstance(state_proof, jsonrpc.AccountStateWithProof) assert state_proof.version == client.get_last_known_state().version
def start_server( name: str, host: str, port: int, jsonrpc: str, faucet: str, disable_events_api: bool, import_diem_account_config_file: Optional[TextIO], logfile: Optional[str], ) -> None: logging.basicConfig(level=logging.INFO, format=log_format, filename=logfile) configure_testnet(jsonrpc, faucet) conf = AppConfig(name=name, server_conf=ServerConfig(host=host, port=port), disable_events_api=disable_events_api) if import_diem_account_config_file: conf.account_config = json.load(import_diem_account_config_file) print("Server Config: %s" % conf) client = testnet.create_client() print("Diem chain id: %s" % client.get_metadata().chain_id) conf.start(client).join()
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_custodial_to_custodial_under_threshold(): client = testnet.create_client() faucet = testnet.Faucet(client) sender_custodial = CustodialApp.create(faucet.gen_account()) receiver_custodial = CustodialApp.create(faucet.gen_account()) intent_id = receiver_custodial.payment(user_id=0, amount=1_000_000) intent = identifier.decode_intent(intent_id, identifier.TDM) script = stdlib.encode_peer_to_peer_with_metadata_script( currency=utils.currency_code(intent.currency_code), payee=utils.account_address(intent.account_address), amount=intent.amount, metadata=txnmetadata.general_metadata( sender_custodial.find_user_sub_address_by_id(0), intent.sub_address), metadata_signature=b"", # only travel rule metadata requires signature ) sender = sender_custodial.available_child_vasp() seq_num = client.get_account_sequence(sender.account_address) txn = create_transaction(sender, seq_num, script, intent.currency_code) signed_txn = sender.sign(txn) client.submit(signed_txn) executed_txn = client.wait_for_transaction(signed_txn) assert executed_txn is not None
def test_get_parent_vasp_account_not_found(): client = testnet.create_client() faucet = testnet.Faucet(client) parent_vasp = LocalAccount.generate() with pytest.raises(jsonrpc.AccountNotFoundError): client.get_parent_vasp_account(parent_vasp.account_address)
def test_get_account_transaction(): client = testnet.create_client() txn = client.get_account_transaction(testnet.DESIGNATED_DEALER_ADDRESS, 0) assert txn is not None assert isinstance(txn, jsonrpc.Transaction) assert txn.version > 0 assert txn.hash is not None assert len(txn.events) == 0
def test_get_account_transaction_include_events(): client = testnet.create_client() txn = client.get_account_transaction(testnet.DESIGNATED_DEALER_ADDRESS, 0, include_events=True) assert txn is not None assert isinstance(txn, jsonrpc.Transaction) assert len(txn.events) > 0
def test_handle_stale_response_error(): client = testnet.create_client() last = client.get_metadata().version for i in range(0, 20): metadata = client.get_metadata() assert metadata.version >= last assert client.get_last_known_state().version == metadata.version last = metadata.version
def test_get_metadata(): client = testnet.create_client() metadata = client.get_metadata() assert metadata is not None assert isinstance(metadata, jsonrpc.Metadata) assert metadata.chain_id == testnet.CHAIN_ID.value assert metadata.version is not None assert metadata.timestamp is not None
def test_gen_account(): client = testnet.create_client() account = testnet.gen_account(client, base_url="http://hello.com") child_vasp = testnet.gen_child_vasp(client, account) assert client.get_account( account.account_address).role.type == "parent_vasp" assert client.get_account( child_vasp.account_address).role.type == "child_vasp"
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 test_submit_failed(): client = testnet.create_client() parent_vasp = LocalAccount.generate() child_vasp = LocalAccount.generate() signed_txn = create_child_vasp_txn(parent_vasp, child_vasp) with pytest.raises(jsonrpc.JsonRpcError): client.submit(signed_txn)
def test_refund_transaction_of_custodial_to_custodial_under_threshold(): client = testnet.create_client() faucet = testnet.Faucet(client) sender_custodial = CustodialApp.create(faucet.gen_account(), client) receiver_custodial = CustodialApp.create(faucet.gen_account(), client) # create a payment transaction intent_id = receiver_custodial.payment(user_id=0, amount=1_000_000) intent = identifier.decode_intent(intent_id, identifier.TDM) receiver_address = utils.account_address(intent.account_address) script = stdlib.encode_peer_to_peer_with_metadata_script( currency=utils.currency_code(intent.currency_code), payee=receiver_address, amount=intent.amount, metadata=txnmetadata.general_metadata( sender_custodial.find_user_sub_address_by_id(0), intent.sub_address), metadata_signature=b"", # only travel rule metadata requires signature ) sender = sender_custodial.available_child_vasp() txn = sender_custodial.create_transaction(sender, script, intent.currency_code) signed_txn = sender.sign(txn) client.submit(signed_txn) executed_txn = client.wait_for_transaction(signed_txn) # start to refund the transaction # find the event for the receiver, a p2p transaction may contains multiple receivers # in the future. event = txnmetadata.find_refund_reference_event(executed_txn, receiver_address) assert event is not None amount = event.data.amount.amount currency_code = event.data.amount.currency refund_receiver_address = utils.account_address(event.data.sender) metadata = txnmetadata.refund_metadata_from_event(event) refund_txn_script = stdlib.encode_peer_to_peer_with_metadata_script( currency=utils.currency_code(currency_code), payee=refund_receiver_address, amount=amount, metadata=metadata, metadata_signature=b"", # only travel rule metadata requires signature ) # receiver is sender of refund txn sender = receiver_custodial.available_child_vasp() txn = receiver_custodial.create_transaction(sender, refund_txn_script, currency_code) refund_executed_txn = receiver_custodial.submit_and_wait(sender.sign(txn)) assert refund_executed_txn is not None
def test_get_account_transaction_include_events(): client = testnet.create_client() account = testnet.gen_account(client, base_url="http://baseurl") txn = client.get_account_transaction(account.account_address, 0, include_events=True) assert txn is not None assert isinstance(txn, jsonrpc.Transaction) assert len(txn.events) > 0
def test_get_last_known_state(): client = testnet.create_client() metadata = client.get_metadata() assert metadata is not None state = client.get_last_known_state() assert state is not None assert metadata.chain_id == state.chain_id assert metadata.version == state.version assert metadata.timestamp == state.timestamp_usecs
def test_get_transactions(): client = testnet.create_client() txns = client.get_transactions(1, 1) assert txns is not None assert isinstance(txns, list) txn = txns[0] assert isinstance(txn, jsonrpc.Transaction) assert txn.version == 1 assert txn.hash is not None
def __init__( self, parent_vasp: LocalAccount, ) -> None: self._parent_vasp = parent_vasp # a custodial application runs with its own client self._client = testnet.create_client() self._chain_id = testnet.CHAIN_ID self._children = [] self._users = []
def test_sync(): jsonrpc_client = testnet.create_client() faucet = testnet.Faucet(jsonrpc_client) parent_vasp = faucet.gen_account() account = jsonrpc_client.get_account(parent_vasp.account_address) processor = ProcessorStub() config = DEFL_CONFIG.copy() config["accounts"] = [account.address] config["processor"] = processor with tempfile.TemporaryDirectory() as tmpdir: config["progress_file_path"] = tmpdir + "/progress" client = LRWPubSubClient(config) state = client.init_progress_state() assert state == {account.received_events_key: 0} new_state = client.sync(state) assert state == { account.received_events_key: 0 }, "state pass in should be not be changed" # new state seq num increased, as we found the event transfered from DD account when # we genreate the account. assert new_state == {account.received_events_key: 1} # new state is saved assert new_state == client.progress.fetch_state() assert len(processor.events) == 1 assert processor.events[0].sender == utils.account_address_hex( testnet.DESIGNATED_DEALER_ADDRESS) assert processor.events[0].receiver == account.address # nothing happened, do sync once, new state should be same new_state2 = client.sync(new_state) assert new_state2 == new_state assert new_state2 == client.progress.fetch_state() assert len(processor.events) == 1 # transfer coins to account, one new event should be fetched currency = account.balances[0].currency faucet.mint(parent_vasp.auth_key.hex(), 1_000, currency) sleep(2) new_state3 = client.sync(new_state2) assert new_state3 == {account.received_events_key: 2} assert new_state3 == client.progress.fetch_state() assert len(processor.events) == 2 # when init progress state, we should get back state last synced reload_state = client.init_progress_state() assert reload_state == new_state3
def test_no_child_accounts(): client = testnet.create_client() account = testnet.gen_account(client) da = DiemAccount(account, [], client) assert da.hrp == account.hrp assert da.account_identifier(None) == account.account_identifier(None) signed_txn_hex = da.submit_p2p(gen_txn(), (b"", b"")) signed_txn = diem_types.SignedTransaction.bcs_deserialize( bytes.fromhex(signed_txn_hex)) assert signed_txn.raw_txn.sender == account.account_address
def test_get_metadata_by_version(): client = testnet.create_client() metadata = client.get_metadata(1) assert metadata is not None assert isinstance(metadata, jsonrpc.Metadata) assert metadata.chain_id == testnet.CHAIN_ID.value assert metadata.version == 1 assert metadata.timestamp is not None # this is unexpected, will be updated to return None later with pytest.raises(jsonrpc.JsonRpcError): metadata = client.get_metadata(1999999999999)
def test_get_currencies(): client = testnet.create_client() currencies = client.get_currencies() assert currencies is not None assert isinstance(currencies, list) assert len(currencies) > 0 lbr = next( filter(lambda curr: curr.code == testnet.TEST_CURRENCY_CODE, currencies)) assert lbr is not None assert isinstance(lbr, jsonrpc.CurrencyInfo)