Esempio n. 1
0
    async def setup_account(self, client: AsyncClient) -> None:
        self.logger.info("faucet: mint %s",
                         self.account.account_address.to_hex())
        faucet = Faucet(client)
        account = await client.get_account(self.account.account_address)
        domain = None if account and self.diem_id_domain in account.role.diem_id_domains else self.diem_id_domain
        await faucet.mint(self.account.auth_key.hex(),
                          self.initial_amount,
                          self.initial_currency,
                          diem_id_domain=domain)

        self.logger.info("rotate dual attestation info for %s",
                         self.account.account_address.to_hex())
        self.logger.info("set base url to: %s", self.server_url)
        await self.account.rotate_dual_attestation_info(
            client, self.server_url)

        self.logger.info("generate child VASP accounts: %s",
                         self.child_account_size)
        child_account_initial_amount = int(self.initial_amount /
                                           (self.child_account_size + 1))
        for i in range(self.child_account_size):
            child = await self.account.gen_child_vasp(
                client, child_account_initial_amount, self.initial_currency)
            self.logger.info("generate child VASP account(%s): %s", i, child)
            self.child_account_configs.append(child.to_dict())
async def test_create_child_vasp():
    client = create_client()
    faucet = Faucet(client)

    parent_vasp = await faucet.gen_account()
    seq_num = await client.get_account_sequence(parent_vasp.account_address)

    child_vasp = LocalAccount.generate()
    payload = stdlib.encode_create_child_vasp_account_script_function(
        coin_type=utils.currency_code(XUS),
        child_address=child_vasp.account_address,
        auth_key_prefix=child_vasp.auth_key.prefix(),
        add_all_currencies=False,
        child_initial_balance=100_000_000,
    )
    raw_txn = diem_types.RawTransaction(
        sender=parent_vasp.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,
    )
    txn = parent_vasp.sign(raw_txn)
    await client.submit(txn)
    executed_txn = await client.wait_for_transaction(txn)
    assert executed_txn is not None
async def test_get_account_transaction_include_events(client: AsyncClient):
    faucet = Faucet(client)
    parent_vasp, _ = await faucet.gen_vasp()

    txn = await client.get_account_transaction(parent_vasp.account_address, 0, include_events=True)
    assert txn is not None
    assert isinstance(txn, jsonrpc.Transaction)
    assert len(txn.events) > 0
async def test_submit_p2p_with_unknown_address():
    client = create_client()
    faucet = Faucet(client)
    account = await faucet.gen_account()
    da = DiemAccount(account, [], client)
    with pytest.raises(ValueError):
        payee = LocalAccount().account_identifier()
        await da.submit_p2p(gen_txn(payee=payee), (b"", b""), by_address=LocalAccount().account_address)
async def test_get_base_url_and_compliance_key(client: AsyncClient):
    faucet = Faucet(client)
    parent_vasp, child_vasp = await faucet.gen_vasp(base_url="http://hello.com")

    base_url, key = await 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 = await 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
async def test_wait_for_transaction_timeout_and_expire(client: AsyncClient):
    faucet = Faucet(client)

    parent_vasp = await faucet.gen_account()

    with pytest.raises(jsonrpc.TransactionExpired):
        await client.wait_for_transaction2(parent_vasp.account_address, 1, time.time() + 0.2, "hash")

    with pytest.raises(jsonrpc.WaitForTransactionTimeout):
        await client.wait_for_transaction2(parent_vasp.account_address, 1, time.time() + 5, "hash", 0.1)
async def test_ensure_account_balance_is_always_enough():
    client = create_client()
    faucet = Faucet(client)
    account = LocalAccount.generate()
    await faucet.mint(account.auth_key.hex(), 1, XUS)
    da = DiemAccount(account, [], client)
    account_data = await client.must_get_account(account.account_address)
    amount = utils.balance(account_data, XUS) + 1
    payee = await faucet.gen_account()
    txn = await da.submit_p2p(gen_txn(payee=payee.account_identifier(), amount=amount), (b"", b""))
    await client.wait_for_transaction(txn)
async def test_no_child_accounts():
    client = create_client()
    faucet = Faucet(client)
    account = await faucet.gen_account()
    da = DiemAccount(account, [], client)
    assert da.hrp == account.hrp
    assert da.account_identifier() == account.account_identifier()

    payee = await faucet.gen_account()
    signed_txn = await da.submit_p2p(gen_txn(payee=payee.account_identifier()), (b"", b""))
    assert signed_txn.raw_txn.sender == account.account_address
async def test_get_parent_vasp_account(client: AsyncClient):
    faucet = Faucet(client)

    parent_vasp, child_vasp = await faucet.gen_vasp()

    account = await client.get_parent_vasp_account(child_vasp.account_address)

    expected_address = utils.account_address_hex(parent_vasp.account_address)
    assert account.address == expected_address

    account = await client.get_parent_vasp_account(parent_vasp.account_address)
    assert account.address == expected_address
Esempio n. 10
0
async def test_wait_for_transaction_hash_mismatched_and_execution_failed(client: AsyncClient):
    faucet = Faucet(client)

    parent_vasp = await faucet.gen_account()
    # parent vasp as child, invalid transaction
    signed_txn = create_child_vasp_txn(parent_vasp, parent_vasp)
    await client.submit(signed_txn)

    txn = signed_txn.raw_txn
    with pytest.raises(jsonrpc.TransactionHashMismatchError):
        await client.wait_for_transaction2(
            txn.sender, txn.sequence_number, txn.expiration_timestamp_secs, "mismatched hash"
        )

    with pytest.raises(jsonrpc.TransactionExecutionFailed):
        await client.wait_for_transaction(signed_txn)
Esempio n. 11
0
async def test_get_diem_id_domain_map(client: AsyncClient):
    faucet = Faucet(client)
    parent_vasp1 = await faucet.gen_account()
    parent_vasp2 = await faucet.gen_account()
    domain1 = "domain1"
    domain2 = "domain2"

    await faucet.mint(parent_vasp1.auth_key.hex(), 1, XUS, diem_id_domain=domain1, is_remove_domain=False)
    await faucet.mint(parent_vasp2.auth_key.hex(), 1, XUS, diem_id_domain=domain2, is_remove_domain=False)

    domain_map = await client.get_diem_id_domain_map()
    assert domain_map.get(domain1) == parent_vasp1.account_address.to_hex()
    assert domain_map.get(domain2) == parent_vasp2.account_address.to_hex()

    await faucet.mint(parent_vasp1.auth_key.hex(), 1, XUS, diem_id_domain=domain1, is_remove_domain=True)
    domain_map = await client.get_diem_id_domain_map()
    assert domain_map.get(domain1) is None
Esempio n. 12
0
async def test_get_vasp_domain_map(client: AsyncClient):
    if not await client.support_diem_id():
        pytest.skip("Diem ID is not supported")
    faucet = Faucet(client)
    parent_vasp1 = await faucet.gen_account()
    parent_vasp2 = await faucet.gen_account()
    domain1 = "domain1"
    domain2 = "domain2"

    await faucet.mint(parent_vasp1.auth_key.hex(), 1, XUS, vasp_domain=domain1, is_remove_domain=False)
    await faucet.mint(parent_vasp2.auth_key.hex(), 1, XUS, vasp_domain=domain2, is_remove_domain=False)

    domain_map = await client.get_vasp_domain_map()
    assert domain_map.get(domain1) == parent_vasp1.account_address.to_hex()
    assert domain_map.get(domain2) == parent_vasp2.account_address.to_hex()

    await faucet.mint(parent_vasp1.auth_key.hex(), 1, XUS, vasp_domain=domain1, is_remove_domain=True)
    domain_map = await client.get_vasp_domain_map()
    assert domain_map.get(domain1) is None
Esempio n. 13
0
async def test_p2p_above_threshold_by_reference_id():
    client = create_client()
    faucet = Faucet(client)

    sender = await faucet.gen_account()
    receiver = await faucet.gen_account()
    await receiver.rotate_dual_attestation_info(client,
                                                base_url="http://localhost")

    amount = 20_000_000_000
    reference_id = str(uuid.uuid4())

    metadata, metadata_signing_msg = txnmetadata.travel_rule(
        reference_id, sender.account_address, amount)
    metadata_signature = receiver.compliance_key.sign(metadata_signing_msg)
    payload = stdlib.encode_peer_to_peer_with_metadata_script_function(
        currency=utils.currency_code(XUS),
        payee=receiver.account_address,
        amount=amount,
        metadata=metadata,
        metadata_signature=metadata_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
Esempio n. 14
0
async def test_gen_child_vasp():
    client = create_client()
    faucet = Faucet(client)
    account = LocalAccount(
        hrp=identifier.DM,
        txn_gas_currency_code="XUS",
        txn_max_gas_amount=1_000_001,
        txn_gas_unit_price=1,
        txn_expire_duration_secs=60,
    )
    await faucet.mint(account.auth_key.hex(), 10_000_000, "XUS")

    child_account = await account.gen_child_vasp(client, 1, "XUS")
    assert child_account.hrp == account.hrp
    assert child_account.txn_gas_currency_code == account.txn_gas_currency_code
    assert child_account.txn_max_gas_amount == account.txn_max_gas_amount
    assert child_account.txn_gas_unit_price == account.txn_gas_unit_price
    assert child_account.txn_expire_duration_secs == account.txn_expire_duration_secs
    assert child_account.compliance_key == account.compliance_key
    assert child_account.private_key != account.private_key
    child_diem_account = await client.must_get_account(
        child_account.account_address)
    assert child_diem_account.role.type == "child_vasp"
Esempio n. 15
0
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
Esempio n. 16
0
async def test_init_faucet_with_url(client: AsyncClient):
    faucet = Faucet(client, "invalid-url")
    with pytest.raises(ValueError):
        await faucet.gen_account()
Esempio n. 17
0
async def test_gen_account(client: AsyncClient):
    faucet = Faucet(client)
    vasp = await faucet.gen_account()

    account = await client.get_account(vasp.account_address)
    assert account.role.type == "parent_vasp"