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 payment URI from receiver account.
    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_uri = receiver_account.generate_payment_uri()
    receiver_account_address: diem_types.AccountAddress = receiver_uri.intent(
        hrp).account_address

    sender_uri = sender_account.generate_payment_uri()
    sender_address = sender_uri.intent(hrp).account_address
    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_to_subaddress(
    sender_account: AccountResource,
    receiver_account: AccountResource,
    currency: str,
    hrp: str,
    stub_config: AppConfig,
    diem_client: jsonrpc.Client,
    invalid_to_subaddress: Optional[bytes],
) -> None:
    """When received a payment with general metadata and invalid to subaddress,
    receiver should refund the payment by using RefundMetadata with reason `invalid subaddress`.

    Test Plan:

    1. Generate a valid payment URI from the receiver account.
    2. Create a general metadata with valid 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

    sender_uri = sender_account.generate_payment_uri()
    valid_from_subaddress = sender_uri.intent(hrp).subaddress
    invalid_metadata = txnmetadata.general_metadata(valid_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"",
        ),
    )

    sender_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
Пример #3
0
def test_generate_account_payment_URI_should_include_unique_subaddress(
        account: AccountResource, hrp: str) -> None:
    def subaddress(uri: str) -> str:
        return utils.hex(identifier.decode_intent(uri, hrp).subaddress)

    subaddresses = [
        subaddress(account.generate_payment_uri().payment_uri)
        for _ in range(10)
    ]
    assert sorted(subaddresses) == sorted(list(set(subaddresses)))
Пример #4
0
def test_generate_account_payment_uri_with_currency_and_amount(
        account: AccountResource, currency: str, hrp: str) -> None:
    amount = 1123
    uri = account.generate_payment_uri(currency=currency, amount=amount)
    assert uri.id
    assert uri.account_id == account.id
    assert uri.currency == currency
    assert uri.amount == amount

    intent = identifier.decode_intent(uri.payment_uri, hrp)
    assert intent.account_address
    assert intent.subaddress
    assert intent.currency_code == currency
    assert intent.amount == amount
Пример #5
0
def test_generate_account_payment_uri_without_currency_and_amount(
        account: AccountResource, hrp: str) -> None:
    uri = account.generate_payment_uri()
    assert uri.id
    assert uri.account_id == account.id
    assert uri.currency is None
    assert uri.amount is None
    assert uri.payment_uri

    intent = identifier.decode_intent(uri.payment_uri, hrp)
    assert intent.account_address
    assert intent.subaddress
    assert intent.currency_code is None
    assert intent.amount is None
def test_receive_payment_with_refund_metadata_and_invalid_transaction_version(
    sender_account: AccountResource,
    receiver_account: AccountResource,
    currency: str,
    hrp: str,
    stub_config: AppConfig,
    diem_client: jsonrpc.Client,
    pending_income_account: AccountResource,
    invalid_refund_txn_version: int,
) -> None:
    """
    When received a payment transaction with invalid refund metadata, it's up to wallet
    application to decide how to handle, this test makes sure the wallet application
    should be able to continue to receive following up valid payment transactions.

    Test Plan:

    1. Generate a valid payment URI from receiver account.
    2. Submit payment transaction with refund metadata and invalid txn version.
    3. Wait for the transaction executed successfully.
    4. Send a valid payment to the payment URI.
    5. Assert receiver account received the valid payment.

    """

    receiver_uri = receiver_account.generate_payment_uri()
    receiver_account_address: diem_types.AccountAddress = receiver_uri.intent(
        hrp).account_address

    reason = diem_types.RefundReason__OtherReason()
    metadata = txnmetadata.refund_metadata(invalid_refund_txn_version, reason)
    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 * 2,
            payee=receiver_account_address,
            metadata=metadata,
            metadata_signature=b"",
        ),
    )
    assert receiver_account.balance(currency) == 0

    pay = sender_account.send_payment(
        currency, amount, payee=receiver_uri.intent(hrp).account_id)
    wait_for_payment_transaction_complete(sender_account, pay.id)
    receiver_account.wait_for_balance(currency, amount)
def test_receive_payment_with_invalid_metadata(
    sender_account: AccountResource,
    receiver_account: AccountResource,
    currency: str,
    hrp: str,
    stub_config: AppConfig,
    diem_client: jsonrpc.Client,
    invalid_metadata: bytes,
) -> None:
    """When received a payment with invalid metadata, it is up to the wallet application how to handle it.
    This test makes sure target wallet application should continue to process valid transactions after
    received such an on-chain transaction.

    Test Plan:

    1. Generate a valid payment URI from receiver account.
    2. Submit a p2p transaction with invalid metadata, and wait for it is executed.
    3. Send a valid payment to the payment URI.
    4. Assert receiver account received the valid payment.

    """

    uri = receiver_account.generate_payment_uri()
    receiver_account_address = uri.intent(hrp).account_address
    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 * 2,
            payee=receiver_account_address,
            metadata=invalid_metadata,
            metadata_signature=b"",
        ),
    )
    assert receiver_account.balance(currency) == 0

    pay = sender_account.send_payment(currency=currency,
                                      amount=amount,
                                      payee=uri.intent(hrp).account_id)
    wait_for_payment_transaction_complete(sender_account, pay.id)
    receiver_account.wait_for_balance(currency, amount)
def test_receive_payment_with_general_metadata_and_invalid_from_subaddress(
    sender_account: AccountResource,
    receiver_account: AccountResource,
    currency: str,
    hrp: str,
    stub_config: AppConfig,
    diem_client: jsonrpc.Client,
    invalid_from_subaddress: Optional[bytes],
) -> None:
    """When received a payment with general metadata and invalid from subaddress,
    receiver is not required to take any action on it as long as to subaddress is valid.

    Test Plan:

    1. Generate a valid payment URI from the receiver account.
    2. Create a general metadata with invalid from subaddress and valid to subaddress.
    3. Send payment transaction from sender to receiver on-chain account.
    4. Wait for the transaction executed successfully.
    5. Assert receiver account received funds eventually.

    """

    receiver_uri = receiver_account.generate_payment_uri()
    receiver_account_address: diem_types.AccountAddress = receiver_uri.intent(
        hrp).account_address

    valid_to_subaddress = receiver_uri.intent(hrp).subaddress
    invalid_metadata = txnmetadata.general_metadata(invalid_from_subaddress,
                                                    valid_to_subaddress)
    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"",
        ),
    )
    receiver_account.wait_for_balance(currency, amount)
def test_receive_payment_with_general_metadata_and_valid_from_and_to_subaddresses(
    sender_account: AccountResource,
    receiver_account: AccountResource,
    currency: str,
    hrp: str,
    amount: int,
) -> None:
    """
    Test Plan:

    1. Generate a valid payment URI from receiver account.
    2. Send a payment to the payee from the valid payment URI.
    3. Wait for the transaction executed successfully.
    4. Assert receiver account received the fund.

    """

    uri = receiver_account.generate_payment_uri()
    pay = sender_account.send_payment(currency=currency,
                                      amount=amount,
                                      payee=uri.intent(hrp).account_id)
    wait_for_payment_transaction_complete(sender_account, pay.id)
    receiver_account.wait_for_balance(currency, amount)
def test_receive_payment_with_travel_rule_metadata_and_valid_reference_id(
    sender_account: AccountResource,
    receiver_account: AccountResource,
    currency: str,
    hrp: str,
    travel_rule_threshold: int,
) -> None:
    """
    Test Plan:

    1. Generate a valid payment URI from receiver account.
    2. Send a payment meeting travel rule threshold to the payee from the valid payment URI.
    3. Wait for the transaction executed successfully.
    4. Assert receiver account received the fund.

    """

    uri = receiver_account.generate_payment_uri()
    pay = sender_account.send_payment(currency,
                                      travel_rule_threshold,
                                      payee=uri.intent(hrp).account_id)
    wait_for_payment_transaction_complete(sender_account, pay.id)
    receiver_account.wait_for_balance(currency, travel_rule_threshold)
Пример #11
0
def test_generate_account_payment_uri_with_integer_overflow_amount(
        account: AccountResource) -> None:
    amount = 324234324342234234234234242234234324323423432432423423243242342342342342342342342334234
    with pytest.raises(requests.HTTPError, match="400 Client Error"):
        account.generate_payment_uri(amount=amount)
Пример #12
0
def test_generate_account_payment_uri_with_invalid_amount(
        account: AccountResource, amount: Union[str, int]) -> None:
    with pytest.raises(requests.HTTPError, match="400 Client Error"):
        account.generate_payment_uri(amount=amount)  # pyre-ignore
Пример #13
0
def test_generate_account_payment_uri_with_invalid_currency(
        account: AccountResource, invalid_currency: str) -> None:
    with pytest.raises(requests.HTTPError, match="400 Client Error"):
        account.generate_payment_uri(currency=invalid_currency)