def test_p2p_by_general(): ctx = context.for_local_dev() testnet.Faucet(ctx.jsonrpc_client).mint(ctx.auth_key().hex(), 1_000_000_000, ctx.config.gas_currency_code) txn = ctx.p2p_by_general( testnet.TEST_CURRENCY_CODE, 1000, receiver_vasp_address=testnet.DESIGNATED_DEALER_ADDRESS.to_hex(), receiver_sub_address="aaaaa28bdeb62af3", sender_sub_address="ccccc28bdeb62af2", ) assert txn assert txn.transaction.sender.lower() == ctx.config.vasp_address script = txn.transaction.script assert script.receiver.lower() == testnet.DESIGNATED_DEALER_ADDRESS.to_hex( ) assert script.amount == 1000 assert script.metadata_signature == "" metadata = diem_types.Metadata.bcs_deserialize( bytes.fromhex(script.metadata)) assert isinstance(metadata, diem_types.Metadata__GeneralMetadata) assert metadata.value.value.from_subaddress.hex() == "ccccc28bdeb62af2" assert metadata.value.value.to_subaddress.hex() == "aaaaa28bdeb62af3"
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_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 _trade_and_execute_buy(self, quote, trade, diem_deposit_address): if not diem_deposit_address: raise TradeError("Can't execute trade without a deposit address") receiver_vasp, receiver_sub_address = identifier.decode_account( diem_deposit_address, identifier.HRPS[testnet.CHAIN_ID.to_int()]) client = testnet.create_client() account = client.get_account(receiver_vasp) faucet = testnet.Faucet(client) faucet.mint(account.authentication_key, quote.amount, quote.currency_pair.value.base) trade.executed(-1)
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 account faucet = testnet.Faucet(client) testnet.Faucet.mint(faucet, auth_key.hex(), 100_000_000, "XUS") # get account information account = client.get_account(auth_key.account_address()) print("Account info:") print(account)
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_p2p_by_travel_rule(): sender = context.generate(1) receiver = context.generate(2) faucet = testnet.Faucet(sender.jsonrpc_client) faucet.mint(sender.auth_key().hex(), 2_000_000_000, sender.config.gas_currency_code) faucet.mint(receiver.auth_key().hex(), 1_000, receiver.config.gas_currency_code) receiver.reset_dual_attestation_info() reference_id = "reference_id" amount = 1_800_000_000 metadata, sig_msg = txnmetadata.travel_rule( reference_id, sender.config.vasp_account_address(), amount, ) metadata_signature = receiver.config.compliance_private_key().sign(sig_msg) txn = sender.p2p_by_travel_rule( receiver.config.vasp_address, testnet.TEST_CURRENCY_CODE, amount, metadata, metadata_signature, ) assert txn assert txn.transaction.sender.lower() == sender.config.vasp_address script = txn.transaction.script assert script.receiver.lower() == receiver.config.vasp_address assert script.amount == amount assert script.metadata_signature == metadata_signature.hex() metadata = diem_types.Metadata.bcs_deserialize( bytes.fromhex(script.metadata)) assert isinstance(metadata, diem_types.Metadata__TravelRuleMetadata) assert metadata.value.value.off_chain_reference_id == reference_id