def wait_for_payment_transaction_complete(account: AccountResource, payment_id: str) -> None: # MiniWallet stub generates `updated_transaction` event when transaction is completed on-chain # Payment id is same with Transaction id. account.wait_for_event("updated_transaction", status=Transaction.Status.completed, id=payment_id)
def test_receive_payment_with_travel_rule_metadata_and_invalid_reference_id( sender_account: AccountResource, receiver_account: AccountResource, currency: str, hrp: str, stub_config: AppConfig, diem_client: jsonrpc.Client, pending_income_account: AccountResource, invalid_ref_id: Optional[str], ) -> None: """ There is no way to create travel rule metadata with invalid reference id when the payment amount meets travel rule threshold, because the metadata signature is verified by transaction script. Also, if metadata signature is provided, transaction script will also verify it regardless whether the amount meets travel rule threshold, thus no need to test invalid metadata signature case. This test bypasses the transaction script validation by sending payment amount under the travel rule threshold without metadata signature, and receiver should handle it properly and refund. Test Plan: 1. Generate a valid account identifier from receiver account as payee. 2. Submit payment under travel rule threshold transaction from sender to receiver on-chain account. 3. Wait for the transaction executed successfully. 4. Assert the payment is refund eventually. Note: the refund payment will be received by pending income account of the MiniWallet Stub, because no account owns the original invalid payment transaction which is sent by test. """ receiver_account_identifier = receiver_account.generate_account_identifier( ) receiver_account_address = identifier.decode_account_address( receiver_account_identifier, hrp) sender_account_identifier = sender_account.generate_account_identifier() sender_address = identifier.decode_account_address( sender_account_identifier, hrp) metadata, _ = txnmetadata.travel_rule(invalid_ref_id, sender_address, amount) # pyre-ignore original_payment_txn: jsonrpc.Transaction = stub_config.account.submit_and_wait_for_txn( diem_client, stdlib.encode_peer_to_peer_with_metadata_script( currency=utils.currency_code(currency), amount=amount, payee=receiver_account_address, metadata=metadata, metadata_signature=b"", ), ) pending_income_account.wait_for_event( "created_transaction", status=Transaction.Status.completed, refund_diem_txn_version=original_payment_txn.version, ) assert receiver_account.balance(currency) == 0
def test_receive_payment_with_general_metadata_and_invalid_subaddresses( stub_client: RestClient, target_client: RestClient, currency: str, hrp: str, stub_config: AppConfig, diem_client: jsonrpc.Client, invalid_to_subaddress: Optional[bytes], invalid_from_subaddress: Optional[bytes], stub_wallet_pending_income_account: AccountResource, ) -> None: """When received a payment with general metadata and invalid subaddresses, it is considered same with the case received invalid to subaddress, and receiver should refund the payment. Test Plan: 1. Generate a valid account identifier from receiver account as payee. 2. Create a general metadata with invalid from subaddress and invalid to subaddress. 3. Send payment transaction from sender to receiver on-chain account. 4. Wait for the transaction executed successfully. 5. Assert sender account received a payment transaction with refund metadata. 6. Assert receiver account does not receive funds. """ amount = 1_000_000 sender_account = stub_client.create_account(balances={currency: amount}) receiver_account = target_client.create_account() try: receiver_account_identifier = receiver_account.generate_account_identifier( ) receiver_account_address = identifier.decode_account_address( receiver_account_identifier, hrp) invalid_metadata = txnmetadata.general_metadata( invalid_from_subaddress, invalid_to_subaddress) original_payment_txn: jsonrpc.Transaction = stub_config.account.submit_and_wait_for_txn( diem_client, stdlib.encode_peer_to_peer_with_metadata_script( currency=utils.currency_code(currency), amount=amount, payee=receiver_account_address, metadata=invalid_metadata, metadata_signature=b"", ), ) stub_wallet_pending_income_account.wait_for_event( "created_transaction", status=Transaction.Status.completed, refund_diem_txn_version=original_payment_txn.version, refund_reason=RefundReason.invalid_subaddress, ) assert receiver_account.balance(currency) == 0 finally: receiver_account.log_events() sender_account.log_events()
def test_receive_payment_with_general_metadata_and_invalid_subaddresses( sender_account: AccountResource, receiver_account: AccountResource, currency: str, hrp: str, stub_config: AppConfig, diem_client: jsonrpc.Client, invalid_to_subaddress: Optional[bytes], invalid_from_subaddress: Optional[bytes], pending_income_account: AccountResource, ) -> None: """When received a payment with general metadata and invalid subaddresses, it is considered same with the case received invalid to subaddress, and receiver should refund the payment. Test Plan: 1. Generate a valid payment URI from the receiver account. 2. Create a general metadata with invalid from subaddress and invalid to subaddress. 3. Send payment transaction from sender to receiver on-chain account. 4. Wait for the transaction executed successfully. 5. Assert sender account received a payment transaction with refund metadata. 6. Assert receiver account does not receive funds. """ receiver_uri = receiver_account.generate_payment_uri() receiver_account_address: diem_types.AccountAddress = receiver_uri.intent( hrp).account_address invalid_metadata = txnmetadata.general_metadata(invalid_from_subaddress, invalid_to_subaddress) original_payment_txn: jsonrpc.Transaction = stub_config.account.submit_and_wait_for_txn( diem_client, stdlib.encode_peer_to_peer_with_metadata_script( currency=utils.currency_code(currency), amount=amount, payee=receiver_account_address, metadata=invalid_metadata, metadata_signature=b"", ), ) pending_income_account.wait_for_event( "created_transaction", status=Transaction.Status.completed, refund_diem_txn_version=original_payment_txn.version, refund_reason=RefundReason.invalid_subaddress, ) assert receiver_account.balance(currency) == 0