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 = identifier.encode_account( sender.account_address, identifier.gen_subaddress(), self.hrp()) sender_kyc_data = offchain.individual_kyc_data( given_name="Jack", surname="G", address=offchain.AddressObject(city="San Francisco"), ) receiver_account_id = identifier.encode_account( receiver.account_address, identifier.gen_subaddress(), self.hrp(), ) return offchain.new_payment_object( sender_account_id, sender_kyc_data, receiver_account_id, amount, currency, )
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)
def test_submit_txn_when_both_ready(monkeypatch): user = OneUser.run(db_session, account_amount=100_000_000_000, account_currency=currency) amount = 10_000_000_000 receiver = LocalAccount.generate() subaddress = identifier.gen_subaddress() txn = save_outbound_transaction(user.account_id, receiver.account_address, subaddress, amount, currency) cmd = _txn_payment_command(txn) receiver_cmd = dataclasses.replace( cmd, my_actor_address=cmd.payment.receiver.address) receiver_ready_cmd = receiver_cmd.new_command( recipient_signature=b"recipient_signature".hex(), status=offchain.Status.ready_for_settlement, kyc_data=_user_kyc_data(user.account_id), ) with monkeypatch.context() as m: client = context.get().offchain_client m.setattr( client, "process_inbound_request", lambda _, c: client.create_inbound_payment_command( c.cid, c.payment), ) code, resp = process_inbound_command(cmd.payment.receiver.address, receiver_ready_cmd) assert code == 200 assert resp txn = get_transaction_by_reference_id(cmd.reference_id()) assert txn assert txn.status == TransactionStatus.OFF_CHAIN_INBOUND cmd = _txn_payment_command(txn) assert cmd.is_inbound(), str(cmd) process_offchain_tasks() db_session.refresh(txn) assert txn.status == TransactionStatus.OFF_CHAIN_READY # sync command and submit with monkeypatch.context() as m: m.setattr( context.get().offchain_client, "send_command", lambda cmd, _: offchain.reply_request(cmd.cid), ) m.setattr( context.get(), "p2p_by_travel_rule", jsonrpc_txn_sample, ) process_offchain_tasks() db_session.refresh(txn) assert txn.status == TransactionStatus.COMPLETED assert txn.sequence == 5 assert txn.blockchain_version == 3232
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
async def test_p2p_under_threshold_by_subaddress(): client = create_client() faucet = Faucet(client) sender = await faucet.gen_account() sender_subaddress = identifier.gen_subaddress() receiver = await faucet.gen_account() receiver_subaddress = identifier.gen_subaddress() amount = 2_000_000 payload = stdlib.encode_peer_to_peer_with_metadata_script_function( currency=utils.currency_code(XUS), payee=receiver.account_address, amount=amount, metadata=txnmetadata.general_metadata(sender_subaddress, receiver_subaddress), metadata_signature=b"", # only travel rule metadata requires signature ) seq_num = await client.get_account_sequence(sender.account_address) txn = diem_types.RawTransaction( sender=sender.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, ) signed_txn = sender.sign(txn) await client.submit(signed_txn) executed_txn = await client.wait_for_transaction(signed_txn) assert executed_txn is not None
def test_process_inbound_command(monkeypatch): hrp = context.get().config.diem_address_hrp() user = OneUser.run(db_session, account_amount=100_000_000_000, account_currency=currency) amount = 10_000_000_000 sender = LocalAccount.generate() sender_subaddress = identifier.gen_subaddress() receiver_subaddress = generate_new_subaddress(user.account_id) cmd = offchain.PaymentCommand.init( identifier.encode_account(sender.account_address, sender_subaddress, hrp), _user_kyc_data(user.account_id), identifier.encode_account(context.get().config.vasp_address, receiver_subaddress, hrp), amount, currency.value, ) with monkeypatch.context() as m: client = context.get().offchain_client m.setattr( client, "process_inbound_request", lambda _, cmd: client.create_inbound_payment_command( cmd.cid, cmd.payment), ) code, resp = process_inbound_command(cmd.payment.sender.address, cmd) assert code == 200 assert resp txn = get_transaction_by_reference_id(cmd.reference_id()) assert txn assert txn.status == TransactionStatus.OFF_CHAIN_INBOUND cmd = _txn_payment_command(txn) assert cmd.is_inbound(), str(cmd) with monkeypatch.context() as m: m.setattr( context.get().offchain_client, "send_command", lambda cmd, _: offchain.reply_request(cmd.cid), ) process_offchain_tasks() db_session.refresh(txn) assert txn.status == TransactionStatus.OFF_CHAIN_OUTBOUND
def test_save_outbound_transaction(monkeypatch): user = OneUser.run(db_session, account_amount=100_000_000_000, account_currency=currency) amount = 10_000_000_000 receiver = LocalAccount.generate() subaddress = identifier.gen_subaddress() txn = save_outbound_transaction(user.account_id, receiver.account_address, subaddress, amount, currency) assert txn.id in get_account_transaction_ids(user.account_id) assert txn.reference_id is not None assert txn.command_json is not None with monkeypatch.context() as m: m.setattr( context.get().offchain_client, "send_command", lambda cmd, _: offchain.reply_request(cmd.cid), ) process_offchain_tasks() db_session.refresh(txn) assert txn.status == TransactionStatus.OFF_CHAIN_WAIT
def add_user(self): self._users.append(identifier.gen_subaddress())
def gen_user_account_id(self, user_name: str) -> str: subaddress = identifier.gen_subaddress() self.users[user_name].subaddresses.append(subaddress) return identifier.encode_account(self._available_child_vasp().account_address, subaddress, self.hrp)