Ejemplo n.º 1
0
def test_hrp_option_overwrites_hrp_from_diem_account_config_file(
        runner: CliRunner,
        event_loop: asyncio.events.AbstractEventLoop) -> None:
    app_config_file = "app.json"
    stub_config_file = "stub.json"
    with runner.isolated_filesystem():
        LocalAccount(hrp=identifier.DM).write_to_file(app_config_file)
        LocalAccount(hrp=identifier.DM).write_to_file(stub_config_file)

        conf = start_target_server(runner,
                                   ["-i", app_config_file, "--hrp", "xdm"])
        c = RestClient(server_url=conf.base_url, name="x")
        account = event_loop.run_until_complete(c.create_account())
        account_identifier = event_loop.run_until_complete(
            account.generate_account_identifier())
        assert account_identifier[:3] == "xdm"

        result = start_test(
            runner,
            conf,
            [
                "-i",
                stub_config_file,
                "--stub-hrp",
                "xdm",
                "-k",
                "test_generate_account_identifier",
            ],
        )

        assert result.exit_code == 0, result.output
def test_send_payment_with_invalid_account_identifier_checksum_as_payee(
        stub_client: RestClient, target_client: RestClient,
        currency: str) -> None:
    """
    Test Plan:

    1. Generate valid receive payment account identifier.
    2. Manuplate valid account identifier's checksum chars.
    3. Call send payment `POST /accounts/{account_id}/payments` with invalid account identifier.
    4. Expect server response 400 client error, and sender account balance is not changed.
    """

    amount = 1_000_000
    sender_account = target_client.create_account(balances={currency: amount})
    receiver_account = stub_client.create_account()
    try:
        receiver_account_identifier = receiver_account.generate_account_identifier(
        )
        invalid_account_identifier = receiver_account_identifier[:-6] + "000000"
        with pytest.raises(requests.HTTPError, match="400 Client Error"):
            sender_account.send_payment(currency=currency,
                                        amount=amount,
                                        payee=invalid_account_identifier)
        assert sender_account.balance(currency) == amount
    finally:
        receiver_account.log_events()
        sender_account.log_events()
Ejemplo n.º 3
0
def test_create_an_account_with_invalid_initial_deposit_balance_currency(
    target_client: RestClient, balances: str, err_msg
) -> None:
    with pytest.raises(requests.exceptions.HTTPError, match="400 Client Error") as einfo:
        target_client.create("/accounts", balances=json.loads(balances))

    assert err_msg in einfo.value.response.text
def test_send_payment_with_invalid_account_identifier_onchain_account_address_as_payee(
        stub_client: RestClient, target_client: RestClient, currency: str,
        hrp: str) -> None:
    """
    Test Plan:

    1. Generate valid receive payment account identifier.
    2. Extract account onchain address and subaddress from receiving payment account identifier.
    3. Use an invalid onchain account address and extracted subaddress to create a new account identifier.
    4. Call send payment `POST /accounts/{account_id}/payments` with created account identifier.
    5. Expect server response 400 client error, and sender account balance is not changed.
    """

    amount = 1_000_000
    sender_account = target_client.create_account(balances={currency: amount})
    receiver_account = stub_client.create_account()
    try:
        receiver_account_identifier = receiver_account.generate_account_identifier(
        )
        _, subaddress = identifier.decode_account(receiver_account_identifier,
                                                  hrp)
        invalid_account_address = LocalAccount().account_address
        invalid_account_identifier = identifier.encode_account(
            invalid_account_address, subaddress, hrp)
        with pytest.raises(requests.HTTPError, match="400 Client Error"):
            sender_account.send_payment(currency=currency,
                                        amount=amount,
                                        payee=invalid_account_identifier)
        assert sender_account.balance(currency) == amount
    finally:
        receiver_account.log_events()
        sender_account.log_events()
Ejemplo n.º 5
0
def test_create_an_account_with_invalid_json_body(
        target_client: RestClient) -> None:
    with pytest.raises(requests.exceptions.HTTPError,
                       match="400 Client Error") as einfo:
        target_client.send("POST", "/accounts", data="invalid json")

    assert "invalid JSON" in einfo.value.response.text
def test_send_payment_meets_travel_rule_threshold_receiver_kyc_data_is_soft_match_then_accepted_after_reviewing_additional_kyc_data(
    currency: str,
    travel_rule_threshold: int,
    target_client: RestClient,
    stub_client: RestClient,
) -> None:
    """
    Test Plan:

    1. Create sender account with minimum valid kyc data and enough balance in target wallet application.
    2. Create receiver account with kyc data that will be soft matched by the target wallet application and 0 balance in stub wallet application.
    3. Send payment from sender account to receiver account, amount is equal to travel_rule threshold.
    4. Wait for stub wallet application account events include payment command states: ["S_INIT", "R_SEND", "S_SOFT", "R_SOFT_SEND", "READY"]
    5. Expect send payment success; receiver account balance increased by the amount sent; sender account balance decreased by the amount sent.
    """

    send_payment_meets_travel_rule_threshold(
        sender=target_client.create_account(
            balances={currency: travel_rule_threshold},
            kyc_data=stub_client.get_kyc_sample().minimum),
        receiver=stub_client.create_account(
            kyc_data=target_client.get_kyc_sample().soft_match),
        payment_command_states=[
            "S_INIT", "R_SEND", "S_SOFT", "R_SOFT_SEND", "READY"
        ],
        currency=currency,
        amount=travel_rule_threshold,
    )
def test_send_payment_with_valid_inputs_under_the_travel_rule_threshold(
    stub_client: RestClient,
    target_client: RestClient,
    amount: int,
    currency: str,
) -> None:
    """
    Test Plan:

    1. Generate valid receive payment account identifier.
    2. Call send payment `POST /accounts/{account_id}/payments` with the account identifier.
    3. Expect send payment success; receiver account balance increased by the amount sent; sender account balance decreased by the amount sent.
    """

    sender_account = target_client.create_account(balances={currency: amount})
    receiver_account = stub_client.create_account()
    try:
        receiver_account_identifier = receiver_account.generate_account_identifier(
        )
        sender_account.send_payment(currency=currency,
                                    amount=amount,
                                    payee=receiver_account_identifier)

        receiver_account.wait_for_balance(currency, amount)
        sender_account.wait_for_balance(currency, 0)
    finally:
        receiver_account.log_events()
        sender_account.log_events()
def test_send_payment_to_the_other_account_in_the_same_wallet(
    target_client: RestClient,
    currency: str,
    amount: int,
) -> None:
    """
    Test Plan:

    1. Create 2 accounts in target wallet application, one for sender, one for receiver.
    2. Generate valid receive payment account identifier from the receiver account.
    3. Send payment from sender account to receiver account.
    4. Expect send payment success; receiver account balance increased by the amount sent; sender account balance decreased by the amount sent.
    """

    sender_account = target_client.create_account(balances={currency: amount})
    receiver_account = target_client.create_account()
    try:
        receiver_account_identifier = receiver_account.generate_account_identifier(
        )
        sender_account.send_payment(currency,
                                    amount,
                                    payee=receiver_account_identifier)
        sender_account.wait_for_balance(currency, 0)
        receiver_account.wait_for_balance(currency, amount)
    finally:
        receiver_account.log_events()
        sender_account.log_events()
def test_send_payment_meets_travel_rule_threshold_sender_kyc_data_is_soft_match_then_receiver_aborts_for_sending_additional_kyc_data(
    currency: str,
    travel_rule_threshold: int,
    target_client: RestClient,
    stub_client: RestClient,
) -> None:
    """
    Test Plan:

    1. Create sender account with minimum valid kyc data and enough balance in target wallet application.
    2. Create receiver account with kyc data that will be soft matched by the target wallet application and 0 balance in stub wallet application.
    3. Setup the stub wallet applicatoin to abort the payment command if sender requests additional KYC data (soft match).
    4. Send payment from sender account to receiver account, amount is equal to travel_rule threshold.
    5. Wait for stub wallet application account events include payment command states: ["S_INIT", "R_SEND", "S_SOFT", "R_ABORT"]
    6. Expect sender and receiver accounts' balances are not changed.
    """

    send_payment_meets_travel_rule_threshold(
        sender=target_client.create_account(
            balances={currency: travel_rule_threshold},
            kyc_data=stub_client.get_kyc_sample().minimum),
        receiver=stub_client.create_account(
            kyc_data=target_client.get_kyc_sample().soft_match,
            reject_additional_kyc_data_request=True),
        payment_command_states=["S_INIT", "R_SEND", "S_SOFT", "R_ABORT"],
        currency=currency,
        amount=travel_rule_threshold,
    )
Ejemplo n.º 10
0
def test_receive_payment_with_travel_rule_metadata_and_valid_reference_id(
    stub_client: RestClient,
    target_client: RestClient,
    currency: str,
    travel_rule_threshold: int,
) -> None:
    """
    Test Plan:

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

    """
    amount = travel_rule_threshold
    sender_account = stub_client.create_account(
        balances={currency: amount},
        kyc_data=target_client.get_kyc_sample().minimum)
    receiver_account = target_client.create_account(
        kyc_data=stub_client.get_kyc_sample().minimum)
    try:
        account_identifier = receiver_account.generate_account_identifier()
        pay = sender_account.send_payment(currency,
                                          travel_rule_threshold,
                                          payee=account_identifier)
        wait_for_payment_transaction_complete(sender_account, pay.id)
        receiver_account.wait_for_balance(currency, travel_rule_threshold)
    finally:
        receiver_account.log_events()
        sender_account.log_events()
Ejemplo n.º 11
0
def test_create_account_with_balances_and_kyc_data(target_client: RestClient,
                                                   currency: str) -> None:
    kyc_data = target_client.new_kyc_data(sample="minimum")
    balances = {currency: 123}
    account = target_client.create_account(balances, kyc_data=kyc_data)
    assert account.kyc_data == kyc_data
    assert account.balances() == balances
Ejemplo n.º 12
0
def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_soft_match_then_rejected_after_reviewing_additional_kyc_data(
    currency: str,
    travel_rule_threshold: int,
    target_client: RestClient,
    stub_client: RestClient,
) -> None:
    """
    Test Plan:

    1. Create sender account with kyc data that will be soft matched and then rejected by the target wallet application in the stub wallet application.
    2. Create receiver account with minimum valid kyc data and 0 balance in the target wallet application.
    3. Send payment from sender account to receiver account, amount is equal to travel_rule threshold.
    4. Wait for stub wallet application account events include payment command states: ["S_INIT", "R_SOFT", "S_SOFT_SEND", "R_ABORT"]
    5. Expect sender and receiver accounts' balances are not changed.
    """

    receive_payment_meets_travel_rule_threshold(
        sender=stub_client.create_account(
            balances={currency: travel_rule_threshold},
            kyc_data=target_client.new_kyc_data(sample="soft_reject"),
        ),
        receiver=target_client.create_account(
            kyc_data=stub_client.new_kyc_data(sample="minimum")),
        payment_command_states=["S_INIT", "R_SOFT", "S_SOFT_SEND", "R_ABORT"],
        currency=currency,
        amount=travel_rule_threshold,
    )
def test_send_payment_meets_travel_rule_threshold_sender_kyc_data_is_soft_match_and_accepted_receiver_kyc_data_is_soft_match_and_rejected(
    currency: str,
    travel_rule_threshold: int,
    target_client: RestClient,
    stub_client: RestClient,
) -> None:
    """
    Test Plan:

    1. Create sender account with kyc data that will be soft matched and then accepted by the stub wallet application and enough balance in target wallet application.
    2. Create receiver account with kyc data that will be soft matched and then rejected by the target wallet application and 0 balance in stub wallet application.
    3. Send payment from sender account to receiver account, amount is equal to travel_rule threshold.
    4. Wait for stub wallet application account events include payment command states: ["S_INIT", "R_SOFT", "S_SOFT_SEND", "R_SEND", "S_SOFT", "R_SOFT_SEND", "S_ABORT"]
    5. Expect sender and receiver accounts' balances are not changed.
    """

    send_payment_meets_travel_rule_threshold(
        sender=target_client.create_account(
            balances={currency: travel_rule_threshold},
            kyc_data=stub_client.get_kyc_sample().soft_match),
        receiver=stub_client.create_account(
            kyc_data=target_client.get_kyc_sample().soft_reject),
        payment_command_states=[
            "S_INIT", "R_SOFT", "S_SOFT_SEND", "R_SEND", "S_SOFT",
            "R_SOFT_SEND", "S_ABORT"
        ],
        currency=currency,
        amount=travel_rule_threshold,
    )
Ejemplo n.º 14
0
def test_create_an_account_with_invalid_kyc_data(
        target_client: RestClient, kyc_data: Dict[str, Any]) -> None:
    with pytest.raises(requests.exceptions.HTTPError,
                       match="400 Client Error") as einfo:
        target_client.create("/accounts", kyc_data=kyc_data)

    assert "'kyc_data' must be JSON-encoded KycDataObject" in einfo.value.response.text
Ejemplo n.º 15
0
def test_receive_payment_with_general_metadata_and_valid_from_and_to_subaddresses(
    stub_client: RestClient,
    target_client: RestClient,
    currency: str,
    amount: int,
) -> None:
    """
    Test Plan:

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

    """

    sender_account = stub_client.create_account(balances={currency: amount})
    receiver_account = target_client.create_account()
    try:
        payee = receiver_account.generate_account_identifier()
        pay = sender_account.send_payment(currency=currency,
                                          amount=amount,
                                          payee=payee)
        wait_for_payment_transaction_complete(sender_account, pay.id)
        receiver_account.wait_for_balance(currency, amount)
    finally:
        receiver_account.log_events()
        sender_account.log_events()
Ejemplo n.º 16
0
def test_hrp_option_overwrites_hrp_from_diem_account_config_file(
        runner: CliRunner) -> None:
    app_config_file = "app.json"
    stub_config_file = "stub.json"
    with runner.isolated_filesystem():
        LocalAccount(hrp=identifier.DM).write_to_file(app_config_file)
        LocalAccount(hrp=identifier.DM).write_to_file(stub_config_file)

        conf = start_target_server(runner,
                                   ["-i", app_config_file, "--hrp", "xdm"])
        c = RestClient(server_url=conf.base_url, name="x").with_retry()
        assert c.create_account().generate_account_identifier()[:3] == "xdm"

        result = start_test(
            runner,
            conf,
            [
                "-i",
                stub_config_file,
                "--stub-hrp",
                "xdm",
                "-k",
                "test_generate_account_identifier",
            ],
        )

        assert result.exit_code == 0, result.output
Ejemplo n.º 17
0
def test_receive_payment_meets_travel_rule_threshold_sender_kyc_data_is_rejected_by_the_receiver(
    currency: str,
    travel_rule_threshold: int,
    target_client: RestClient,
    stub_client: RestClient,
    hrp: str,
) -> None:
    """
    Test Plan:

    1. Create sender account with kyc data that will be rejected by the target wallet application in the stub wallet application.
    2. Create receiver account with minimum valid kyc data and 0 balance in the target wallet application.
    3. Send payment from sender account to receiver account, amount is equal to travel_rule threshold.
    4. Wait for stub wallet application account events include payment command states: ["S_INIT", "R_ABORT"]
    5 . Expect sender and receiver accounts' balances are not changed.
    """

    receive_payment_meets_travel_rule_threshold(
        sender=stub_client.create_account(
            balances={currency: travel_rule_threshold},
            kyc_data=target_client.get_kyc_sample().reject,
        ),
        receiver=target_client.create_account(
            kyc_data=stub_client.get_kyc_sample().minimum),
        payment_command_states=["S_INIT", "R_ABORT"],
        currency=currency,
        amount=travel_rule_threshold,
    )
Ejemplo n.º 18
0
def test_receive_payment_meets_travel_rule_threshold_both_kyc_data_evaluations_are_accepted(
    currency: str,
    travel_rule_threshold: int,
    target_client: RestClient,
    stub_client: RestClient,
) -> None:
    """
    Test Plan:

    1. Create sender account with minimum valid kyc data and enough balance in the stub wallet application.
    2. Create receiver account with minimum valid kyc data with 0 balance in the target wallet application.
    3. Send payment from sender account to receiver account, amount is equal to travel_rule threshold.
    4. Wait for stub wallet application account events include payment command states: ["S_INIT", "R_SEND", "READY"]
    5 . Expect send payment success; receiver account balance increased by the amount sent; sender account balance decreased by the amount sent.
    """

    receive_payment_meets_travel_rule_threshold(
        sender=stub_client.create_account(
            balances={currency: travel_rule_threshold},
            kyc_data=target_client.get_kyc_sample().minimum,
        ),
        receiver=target_client.create_account(
            kyc_data=stub_client.get_kyc_sample().minimum),
        payment_command_states=["S_INIT", "R_SEND", "READY"],
        currency=currency,
        amount=travel_rule_threshold,
    )
def test_decoded_jws_message_body_is_not_json_encoded_string(
    stub_config: AppConfig,
    stub_client: RestClient,
    target_client: RestClient,
    diem_client: jsonrpc.Client,
    currency: str,
    hrp: str,
) -> None:
    """
    Test Plan:

    1. Send request to the target wallet application offchain API endpoint with text "hello world"
    2. Expect response status code is 400
    3. Expect response `CommandResponseObject` with failure status, `protocol_error` error type,
       and `invalid_json` error code.
    """

    receiver_address = target_client.create_account(
    ).generate_account_identifier()
    sender_address = stub_client.create_account().generate_account_identifier()
    status_code, resp = send_request_json(
        diem_client,
        stub_config.account,
        sender_address,
        receiver_address,
        "hello world",
        hrp,
    )
    assert status_code == 400
    assert resp.status == "failure"
    assert_response_error(resp, "invalid_json", "protocol_error")
def test_send_payment_with_invalid_amount(stub_client: RestClient,
                                          target_client: RestClient,
                                          invalid_amount: float,
                                          currency: str) -> None:
    """
    Test Plan:

    1. Generate valid receive payment account identifier.
    2. Call send payment `POST /accounts/{account_id}/payments` with invalid amount numbers.
    3. Expect server response 400 client error, and sender account balance is not changed.
    """

    amount = 1_000_000
    sender_account = target_client.create_account(balances={currency: amount})
    receiver_account = stub_client.create_account()
    try:
        initial_balances = sender_account.balances()
        receiver_account_identifier = receiver_account.generate_account_identifier(
        )
        with pytest.raises(requests.HTTPError, match="400 Client Error"):
            sender_account.send_payment(
                currency=currency,
                amount=invalid_amount,
                payee=receiver_account_identifier  # pyre-ignore
            )
        assert sender_account.balances() == initial_balances
    finally:
        receiver_account.log_events()
        sender_account.log_events()
def test_send_payment_with_an_amount_exceeding_account_balance(
        stub_client: RestClient, target_client: RestClient,
        currency: str) -> None:
    """
    Test Plan:

    1. Generate valid receive payment account identifier.
    2. Get sender account balance.
    3. Call send payment `POST /accounts/{account_id}/payments` with amount = sender account balance + 1.
    4. Expect server response 400 client error, and sender account balance is not changed.
    """

    amount = 1_000_000
    sender_account = target_client.create_account(balances={currency: amount})
    receiver_account = stub_client.create_account()
    try:
        receiver_account_identifier = receiver_account.generate_account_identifier(
        )
        with pytest.raises(requests.HTTPError, match="400 Client Error"):
            sender_account.send_payment(currency=currency,
                                        amount=amount + 1,
                                        payee=receiver_account_identifier)
        assert sender_account.balance(currency) == amount
    finally:
        receiver_account.log_events()
        sender_account.log_events()
Ejemplo n.º 22
0
def test_receive_payment_with_general_metadata_and_invalid_to_subaddress(
    stub_client: RestClient,
    target_client: RestClient,
    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 account identifier from receiver account as payee.
    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.

    """

    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: diem_types.AccountAddress = identifier.decode_account_address(
            receiver_account_identifier, hrp)

        sender_account_identifier = sender_account.generate_account_identifier(
        )
        valid_from_subaddress = identifier.decode_account_subaddress(
            sender_account_identifier, hrp)
        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
    finally:
        receiver_account.log_events()
        sender_account.log_events()
Ejemplo n.º 23
0
def test_receive_payment_with_refund_metadata_and_invalid_transaction_version(
    stub_client: RestClient,
    target_client: RestClient,
    currency: str,
    hrp: str,
    stub_config: AppConfig,
    diem_client: jsonrpc.Client,
    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 account identifier from receiver account as payee.
    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 account identifier.
    5. Assert receiver account received the valid payment.

    """

    amount = 120_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)

        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_account_identifier)
        wait_for_payment_transaction_complete(sender_account, pay.id)
        receiver_account.wait_for_balance(currency, amount)
    finally:
        receiver_account.log_events()
        sender_account.log_events()
Ejemplo n.º 24
0
def test_receive_payment_with_invalid_metadata(
    stub_client: RestClient,
    target_client: RestClient,
    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 account identifier from receiver account as payee.
    2. Submit a p2p transaction with invalid metadata, and wait for it is executed.
    3. Send a valid payment to the valid account identifier.
    4. Assert receiver account received the valid payment.

    """

    amount = 1_000_000
    sender_account = stub_client.create_account(balances={currency: amount})
    receiver_account = target_client.create_account()
    try:
        account_identifier = receiver_account.generate_account_identifier()
        receiver_account_address = identifier.decode_account_address(
            account_identifier, hrp)
        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=account_identifier)
        wait_for_payment_transaction_complete(sender_account, pay.id)
        receiver_account.wait_for_balance(currency, amount)
    finally:
        receiver_account.log_events()
        sender_account.log_events()
Ejemplo n.º 25
0
def test_receive_payment_with_general_metadata_and_invalid_from_subaddress(
    stub_client: RestClient,
    target_client: RestClient,
    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 account identifier from receiver account as payee.
    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.

    """

    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)

        valid_to_subaddress = identifier.decode_account_subaddress(
            receiver_account_identifier, hrp)
        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)
    finally:
        receiver_account.log_events()
        sender_account.log_events()
Ejemplo n.º 26
0
def test_send_with_json_content_type(monkeypatch):
    session = requests.Session()
    resp = requests.Response()
    resp.status_code = 200
    resp._content = b'{"id": "1"}'

    def assert_request(method: str, url: str, data: str,
                       headers: Dict[str, str]) -> requests.Response:
        assert headers["Content-Type"] == "application/json"
        assert headers["User-Agent"] == jsonrpc.client.USER_AGENT_HTTP_HEADER
        return resp

    monkeypatch.setattr(session, "request", assert_request)
    client = RestClient(name="name", server_url="server", session=session)
    client.create_account()
def test_invalid_jws_message_signature(
    stub_config: AppConfig,
    target_client: RestClient,
    diem_client: jsonrpc.Client,
    currency: str,
    travel_rule_threshold: int,
    hrp: str,
) -> None:
    """
    Test Plan:

    1. Create a new on-chain account with base_url and a new compliance key.
    2. Use new on-chain account address as sender address to create payment command request.
    3. Send request to the target wallet application offchain API endpoint with new on-chain account address and a different compliance key.
    4. Expect response status code is 400
    5. Expect response `CommandResponseObject` with failure status, `protocol_error` error type,
       and `invalid_jws_signature` error code.
    """

    new_stub_account = testnet.gen_account(diem_client)
    new_stub_account.hrp = hrp
    new_compliance_key = LocalAccount().compliance_public_key_bytes
    new_stub_account.rotate_dual_attestation_info(diem_client,
                                                  stub_config.server_url,
                                                  new_compliance_key)

    receiver_address = target_client.create_account(
    ).generate_account_identifier()
    sender_address = new_stub_account.account_identifier()
    request = payment_command_request_sample(
        sender_address=sender_address,
        sender_kyc_data=target_client.get_kyc_sample().minimum,
        receiver_address=receiver_address,
        currency=currency,
        amount=travel_rule_threshold,
    )

    status_code, resp = send_request_json(
        diem_client,
        new_stub_account,
        sender_address,
        receiver_address,
        json.dumps(request),
        hrp,
    )
    assert status_code == 400
    assert resp.status == "failure"
    assert_response_error(resp, "invalid_jws_signature", "protocol_error")
def test_create_an_account_with_valid_kyc_data_and_initial_deposit_balances(
    target_client: RestClient, currency: str
) -> None:
    minimum_kyc_data = offchain.to_json(offchain.individual_kyc_data())
    account = target_client.create_account(kyc_data=minimum_kyc_data, balances={currency: 123})
    assert account.id
    assert account.kyc_data == minimum_kyc_data
Ejemplo n.º 29
0
def test_send_ping_command(
    stub_config: AppConfig,
    target_client: RestClient,
    stub_client: RestClient,
    diem_client: jsonrpc.Client,
    hrp: str,
) -> None:
    """
    Test Plan:
    1. Create a new test account in the target wallet application.
    2. Generate a new account identifier from the new test account.
    3. Send a ping command request to the generated account identifier.
    4. Expect response status is success, and response cid is same with request cid.
    5. Send another ping command request to the generated account identifier.
    6. Expect response status is success.
    7. Expect cid is different for the 2 response status.
    """

    receiver_address = target_client.create_account().generate_account_identifier()
    offchain_client = offchain.Client(stub_config.account.account_address, diem_client, hrp)
    cid = str(uuid.uuid4())
    resp = offchain_client.ping(receiver_address, stub_config.account.compliance_key.sign, cid=cid)
    assert resp.cid == cid
    assert resp.status == "success"
    assert resp.error is None

    resp2 = offchain_client.ping(receiver_address, stub_config.account.compliance_key.sign)
    assert resp2.cid
    assert resp2.status == "success"
    assert resp2.error is None

    assert resp.cid != resp2.cid
Ejemplo n.º 30
0
def test_create_account_identifier_events(target_client: RestClient) -> None:
    account = target_client.create_account()
    index = len(account.events())
    ret = account.generate_account_identifier()
    assert ret
    assert len(account.events(index)) == 1
    assert account.events(index)[0].type == "created_subaddress"