示例#1
0
def test_send_and_deserialize_request(factory):
    client = testnet.create_client()
    receiver_port = offchain.http_server.get_available_port()
    sender = testnet.gen_account(client, base_url="http://localhost:8888")
    receiver = testnet.gen_account(
        client, base_url=f"http://localhost:{receiver_port}")
    sender_client = factory.create_offchain_client(sender, client)
    receiver_client = factory.create_offchain_client(receiver, client)

    def process_inbound_request(x_request_id: str, jws_key_address: str,
                                content: bytes):
        request = receiver_client.deserialize_inbound_request(
            jws_key_address, content)
        resp = offchain.reply_request(request.cid)
        return (200, offchain.jws.serialize(resp,
                                            receiver.compliance_key.sign))

    offchain.http_server.start_local(receiver_port, process_inbound_request)

    payment = factory.new_payment_object(sender, receiver)
    command = offchain.PaymentCommand(payment=payment,
                                      my_actor_address=payment.sender.address,
                                      inbound=False)
    resp = sender_client.send_command(command, sender.compliance_key.sign)
    assert resp
def test_could_not_find_compliance_key_of_x_request_sender_address(sender_app, receiver_app):
    account = testnet.gen_account(testnet.create_client())
    account_id = identifier.encode_account(account.account_address, None, sender_app.hrp)
    request = minimum_required_fields_request_sample(sender_app, receiver_app)
    request["command"]["payment"]["sender"]["address"] = account_id
    resp = send_request(request, sender_app, receiver_app, "failure", sender_address=account_id)
    assert_response_protocol_error(resp, "invalid_http_header")
def test_submit_p2p_with_unknown_address():
    client = testnet.create_client()
    account = testnet.gen_account(client)
    da = DiemAccount(account, [], client)
    with pytest.raises(ValueError):
        da.submit_p2p(gen_txn(), (b"", b""),
                      by_address=LocalAccount().account_address)
def test_gen_account():
    client = testnet.create_client()
    account = testnet.gen_account(client, base_url="http://hello.com")
    child_vasp = testnet.gen_child_vasp(client, account)

    assert client.get_account(
        account.account_address).role.type == "parent_vasp"
    assert client.get_account(
        child_vasp.account_address).role.type == "child_vasp"
def test_get_account_transaction_include_events():
    client = testnet.create_client()
    account = testnet.gen_account(client, base_url="http://baseurl")

    txn = client.get_account_transaction(account.account_address,
                                         0,
                                         include_events=True)
    assert txn is not None
    assert isinstance(txn, jsonrpc.Transaction)
    assert len(txn.events) > 0
def test_no_child_accounts():
    client = testnet.create_client()
    account = testnet.gen_account(client)
    da = DiemAccount(account, [], client)
    assert da.hrp == account.hrp
    assert da.account_identifier(None) == account.account_identifier(None)

    signed_txn_hex = da.submit_p2p(gen_txn(), (b"", b""))
    signed_txn = diem_types.SignedTransaction.bcs_deserialize(
        bytes.fromhex(signed_txn_hex))
    assert signed_txn.raw_txn.sender == account.account_address
示例#7
0
def test_submit_ignores_stale_resposne_error():
    client = testnet.create_client()
    account = testnet.gen_account(client)
    payload = stdlib.encode_rotate_dual_attestation_info_script_function(
        new_url="http://localhost".encode("utf-8"),
        new_key=account.compliance_public_key_bytes)
    txn = account.create_signed_txn(0, payload)
    state = client.get_last_known_state()
    client._last_known_server_state.version = state.version + 1_000_000_000
    client.submit(txn)
    client._last_known_server_state = state
    assert client.wait_for_transaction(txn)
示例#8
0
def test_get_account_transactions_with_events():
    client = testnet.create_client()
    account = testnet.gen_account(client, base_url="url")
    txns = client.get_account_transactions(account.account_address,
                                           0,
                                           1,
                                           include_events=True)
    assert txns is not None
    assert isinstance(txns, list)

    txn = txns[0]
    assert isinstance(txn, jsonrpc.Transaction)
    assert len(txn.events) > 0
示例#9
0
    def generate(name: str, client: jsonrpc.Client) -> "WalletApp":
        """generate a WalletApp running on testnet"""

        offchain_service_port = offchain.http_server.get_available_port()
        account = testnet.gen_account(client, base_url=f"http://localhost:{offchain_service_port}")
        w = WalletApp(
            name=name,
            jsonrpc_client=client,
            parent_vasp=account,
            offchain_service_port=offchain_service_port,
        )
        w.add_child_vasp()
        return w
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_get_base_url_and_compliance_key():
    client = testnet.create_client()

    parent_vasp = testnet.gen_account(client, base_url="http://hello.com")
    child_vasp = testnet.gen_child_vasp(client, parent_vasp)

    base_url, key = client.get_base_url_and_compliance_key(
        child_vasp.account_address)
    assert base_url == "http://hello.com"
    assert utils.public_key_bytes(
        key) == parent_vasp.compliance_public_key_bytes
    base_url, key = client.get_base_url_and_compliance_key(
        parent_vasp.account_address)
    assert base_url == "http://hello.com"
    assert utils.public_key_bytes(
        key) == parent_vasp.compliance_public_key_bytes
def test_get_account_transactions_with_events():
    client = testnet.create_client()
    account = testnet.gen_account(client, base_url="url")
    txns = client.get_account_transactions(account.account_address,
                                           0,
                                           1,
                                           include_events=True)
    assert txns is not None
    assert isinstance(txns, list)

    txn = txns[0]
    assert isinstance(txn, jsonrpc.Transaction)
    assert len(txn.events) > 0

    script_call = utils.decode_transaction_script(txn)
    assert type(
        script_call).__name__ == "ScriptCall__RotateDualAttestationInfo"
    assert script_call.new_url == b"url"
def test_x_request_sender_is_valid_but_no_compliance_key(
    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 without base_url and 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.
    4. Expect response status code is 400
    5. Expect response `CommandResponseObject` with failure status, `protocol_error` error type,
       and `invalid_http_header` error code.
    """

    new_stub_account = testnet.gen_account(diem_client)
    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_http_header", "protocol_error")
def test_gen_dd_account():
    client = testnet.create_client()
    account = testnet.gen_account(client, dd_account=True)
    onchain_account = client.get_account(account.account_address)
    assert onchain_account.role.type == "designated_dealer"