コード例 #1
0
def test_transfer():
    """Test transfer of wealth."""
    def try_transact(cc1, cc2, amount) -> str:
        attempts = 0
        while attempts < 3:
            fee = 1000
            tx_digest = cosmos_api.transfer(cc1, cc2.address, amount, fee)
            assert tx_digest is not None, "Failed to submit transfer!"
            not_settled = True
            elapsed_time = 0
            while not_settled and elapsed_time < 20:
                elapsed_time += 2
                time.sleep(2)
                is_settled = cosmos_api.is_transaction_settled(tx_digest)
                not_settled = not is_settled
            is_settled = cosmos_api.is_transaction_settled(tx_digest)
            if is_settled:
                attempts = 3
            else:
                attempts += 1
        assert is_settled, "Failed to complete tx on 3 attempts!"
        return tx_digest

    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    cc1 = CosmosCrypto(private_key_path=COSMOS_PRIVATE_KEY_PATH)
    cc2 = CosmosCrypto()
    amount = 10000
    tx_digest = try_transact(cc1, cc2, amount)
    # TODO remove requirement for "" tx nonce stub
    is_valid = cosmos_api.is_transaction_valid(tx_digest, cc2.address,
                                               cc1.address, "", amount)
    assert is_valid, "Failed to settle tx correctly!"
コード例 #2
0
def test_format_default():
    """Test if default CosmosSDK transaction is correctly formated."""
    account = CosmosCrypto()
    cc2 = CosmosCrypto()
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)

    amount = 10000

    transfer_transaction = cosmos_api.get_transfer_transaction(
        sender_address=account.address,
        destination_address=cc2.address,
        amount=amount,
        tx_fee=1000,
        tx_nonce="something",
    )

    signed_transaction = cc2.sign_transaction(transfer_transaction)

    assert "tx" in signed_transaction
    assert "signatures" in signed_transaction["tx"]
    assert len(signed_transaction["tx"]["signatures"]) == 1

    assert "pub_key" in signed_transaction["tx"]["signatures"][0]
    assert "value" in signed_transaction["tx"]["signatures"][0]["pub_key"]
    base64_pbk = signed_transaction["tx"]["signatures"][0]["pub_key"]["value"]

    assert "signature" in signed_transaction["tx"]["signatures"][0]
    signature = signed_transaction["tx"]["signatures"][0]["signature"]

    default_formated_transaction = cc2.format_default_transaction(
        transfer_transaction, signature, base64_pbk)

    # Compare default formatted transaction with signed transaction
    assert signed_transaction == default_formated_transaction
コード例 #3
0
def test_get_balance():
    """Test the balance is zero for a new account."""
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    cc = CosmosCrypto()
    balance = cosmos_api.get_balance(cc.address)
    assert balance == 0, "New account has a positive balance."
    cc = CosmosCrypto(private_key_path=COSMOS_PRIVATE_KEY_PATH)
    balance = cosmos_api.get_balance(cc.address)
    assert balance > 0, "Existing account has no balance."
コード例 #4
0
def test_sign_and_recover_message():
    """Test the signing and the recovery function for the eth_crypto."""
    account = CosmosCrypto(COSMOS_PRIVATE_KEY_PATH)
    sign_bytes = account.sign_message(message=b"hello")
    assert len(sign_bytes) > 0, "The len(signature) must not be 0"
    recovered_addresses = account.recover_message(message=b"hello",
                                                  signature=sign_bytes)
    assert (account.address
            in recovered_addresses), "Failed to recover the correct address."
コード例 #5
0
def test_format_cosmwasm():
    """Test if CosmWasm transaction is correctly formated."""
    cc2 = CosmosCrypto()

    # Dummy CosmWasm transaction
    wasm_transaction = {
        "account_number":
        "8",
        "chain_id":
        "agent-land",
        "fee": {
            "amount": [],
            "gas": "200000"
        },
        "memo":
        "",
        "msgs": [{
            "type": "wasm/execute",
            "value": {
                "sender": "cosmos14xjnl2mwwfz6pztpwzj6s89npxr0e3lhxl52nv",
                "contract": "cosmos1xzlgeyuuyqje79ma6vllregprkmgwgav5zshcm",
                "msg": {
                    "create_single": {
                        "item_owner":
                        "cosmos1fz0dcvvqv5at6dl39804jy92lnndf3d5saalx6",
                        "id": "1234",
                        "path": "SOME_URI",
                    }
                },
                "sent_funds": [],
            },
        }],
        "sequence":
        "25",
    }

    signed_transaction = cc2.sign_transaction(wasm_transaction)

    assert "value" in signed_transaction
    assert "signatures" in signed_transaction["value"]
    assert len(signed_transaction["value"]["signatures"]) == 1

    assert "pub_key" in signed_transaction["value"]["signatures"][0]
    assert "value" in signed_transaction["value"]["signatures"][0]["pub_key"]
    base64_pbk = signed_transaction["value"]["signatures"][0]["pub_key"][
        "value"]

    assert "signature" in signed_transaction["value"]["signatures"][0]
    signature = signed_transaction["value"]["signatures"][0]["signature"]

    wasm_formated_transaction = cc2.format_wasm_transaction(
        wasm_transaction, signature, base64_pbk)

    # Compare Wasm formatted transaction with signed transaction
    assert signed_transaction == wasm_formated_transaction
コード例 #6
0
def test_sign_and_recover_message_public_key():
    """Test the signing and the recovery function for the eth_crypto."""
    account = CosmosCrypto(COSMOS_PRIVATE_KEY_PATH)
    sign_bytes = account.sign_message(message=b"hello")
    assert len(sign_bytes) > 0, "The len(signature) must not be 0"
    recovered_public_keys = CosmosApi.recover_public_keys_from_message(
        message=b"hello", signature=sign_bytes)
    assert len(
        recovered_public_keys) == 2, "Wrong number of public keys recovered."
    assert (CosmosApi.get_address_from_public_key(
        recovered_public_keys[0]) == account.address
            ), "Failed to recover the correct address."
コード例 #7
0
def test_construct_sign_and_submit_transfer_transaction():
    """Test the construction, signing and submitting of a transfer transaction."""
    account = CosmosCrypto()
    balance = get_wealth(account.address)
    assert balance > 0, "Failed to fund account."
    cc2 = CosmosCrypto()
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)

    amount = 10000
    assert amount < balance, "Not enough funds."
    transfer_transaction = cosmos_api.get_transfer_transaction(
        sender_address=account.address,
        destination_address=cc2.address,
        amount=amount,
        tx_fee=1000,
        tx_nonce="something",
    )
    assert (isinstance(transfer_transaction, dict)
            and len(transfer_transaction)
            == 6), "Incorrect transfer_transaction constructed."

    signed_transaction = account.sign_transaction(transfer_transaction)
    assert (isinstance(signed_transaction, dict)
            and len(signed_transaction["tx"]) == 4
            and isinstance(signed_transaction["tx"]["signatures"],
                           list)), "Incorrect signed_transaction constructed."

    transaction_digest = cosmos_api.send_signed_transaction(signed_transaction)
    assert transaction_digest is not None, "Failed to submit transfer transaction!"

    not_settled = True
    elapsed_time = 0
    while not_settled and elapsed_time < 20:
        elapsed_time += 1
        time.sleep(2)
        transaction_receipt = cosmos_api.get_transaction_receipt(
            transaction_digest)
        if transaction_receipt is None:
            continue
        is_settled = cosmos_api.is_transaction_settled(transaction_receipt)
        not_settled = not is_settled
    assert transaction_receipt is not None, "Failed to retrieve transaction receipt."
    assert is_settled, "Failed to verify tx!"

    tx = cosmos_api.get_transaction(transaction_digest)
    is_valid = cosmos_api.is_transaction_valid(tx, cc2.address,
                                               account.address, "", amount)
    assert is_valid, "Failed to settle tx correctly!"
    assert tx == transaction_receipt, "Should be same!"
コード例 #8
0
def test_get_wealth_positive(caplog):
    """Test the balance is zero for a new account."""
    with caplog.at_level(logging.DEBUG, logger="aea.crypto.cosmos"):
        cosmos_faucet_api = CosmosFaucetApi()
        cc = CosmosCrypto()
        cosmos_faucet_api.get_wealth(cc.address)
        assert "Wealth generated" in caplog.text
コード例 #9
0
def test_get_handle_transaction_cosmwasm():
    """Test the get deploy transaction method."""
    cc2 = CosmosCrypto()
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    handle_msg = "handle_msg"
    sender_address = cc2.address
    contract_address = "contract_address"
    tx_fee = 1
    amount = 10
    handle_transaction = cosmos_api.get_handle_transaction(
        sender_address, contract_address, handle_msg, amount, tx_fee)

    assert type(handle_transaction) == dict and len(handle_transaction) == 6
    assert "account_number" in handle_transaction
    assert "chain_id" in handle_transaction
    assert "fee" in handle_transaction and handle_transaction["fee"] == {
        "amount": [{
            "denom": "atestfet",
            "amount": "{}".format(tx_fee)
        }],
        "gas": "80000",
    }
    assert "memo" in handle_transaction
    assert "msgs" in handle_transaction and len(
        handle_transaction["msgs"]) == 1
    msg = handle_transaction["msgs"][0]
    assert "type" in msg and msg["type"] == "wasm/execute"
    assert ("value" in msg and msg["value"]["sender"] == sender_address
            and msg["value"]["contract"] == contract_address
            and msg["value"]["msg"] == handle_msg
            and msg["value"]["sent_funds"] == [{
                "denom": "atestfet",
                "amount": str(amount)
            }])
    assert "sequence" in handle_transaction
コード例 #10
0
def test_get_deploy_transaction_cosmwasm():
    """Test the get deploy transaction method."""
    cc2 = CosmosCrypto()
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)

    contract_interface = {"wasm_byte_code": b""}
    deployer_address = cc2.address
    deploy_transaction = cosmos_api.get_deploy_transaction(
        contract_interface, deployer_address)

    assert type(deploy_transaction) == dict and len(deploy_transaction) == 6
    assert "account_number" in deploy_transaction
    assert "chain_id" in deploy_transaction
    assert "fee" in deploy_transaction and deploy_transaction["fee"] == {
        "amount": [{
            "amount": "0",
            "denom": "atestfet"
        }],
        "gas": "80000",
    }
    assert "memo" in deploy_transaction
    assert "msgs" in deploy_transaction and len(
        deploy_transaction["msgs"]) == 1
    msg = deploy_transaction["msgs"][0]
    assert "type" in msg and msg["type"] == "wasm/store-code"
    assert ("value" in msg and msg["value"]["sender"] == deployer_address
            and msg["value"]["wasm_byte_code"]
            == contract_interface["wasm_byte_code"])
    assert "sequence" in deploy_transaction
コード例 #11
0
def test_initialization():
    """Test the initialisation of the variables."""
    account = CosmosCrypto()
    assert account.entity is not None, "The property must return the account."
    assert (account.address
            is not None), "After creation the display address must not be None"
    assert (account.public_key
            is not None), "After creation the public key must no be None"
コード例 #12
0
def test_get_balance():
    """Test the balance is zero for a new account."""
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    cc = CosmosCrypto()
    balance = cosmos_api.get_balance(cc.address)
    assert balance == 0, "New account has a positive balance."
    balance = get_wealth(cc.address)
    assert balance > 0, "Existing account has no balance."
コード例 #13
0
def test_get_init_transaction_cosmwasm():
    """Test the get deploy transaction method."""
    cc2 = CosmosCrypto()
    cosmos_api = CosmosApi(**COSMOS_TESTNET_CONFIG)
    init_msg = "init_msg"
    code_id = 1
    deployer_address = cc2.address
    tx_fee = 1
    amount = 10
    deploy_transaction = cosmos_api.get_init_transaction(
        deployer_address, code_id, init_msg, amount, tx_fee)

    assert type(deploy_transaction) == dict and len(deploy_transaction) == 6
    assert "account_number" in deploy_transaction
    assert "chain_id" in deploy_transaction
    assert "fee" in deploy_transaction and deploy_transaction["fee"] == {
        "amount": [{
            "denom": "atestfet",
            "amount": "{}".format(tx_fee)
        }],
        "gas": "80000",
    }
    assert "memo" in deploy_transaction
    assert "msgs" in deploy_transaction and len(
        deploy_transaction["msgs"]) == 1
    msg = deploy_transaction["msgs"][0]
    assert "type" in msg and msg["type"] == "wasm/instantiate"
    assert ("value" in msg and msg["value"]["sender"] == deployer_address
            and msg["value"]["code_id"] == str(code_id)
            and msg["value"]["label"] == ""
            and msg["value"]["init_msg"] == init_msg
            and msg["value"]["init_funds"] == [{
                "denom": "atestfet",
                "amount": str(amount)
            }])
    assert "sequence" in deploy_transaction
コード例 #14
0
def test_dump_positive():
    """Test dump."""
    account = CosmosCrypto(COSMOS_PRIVATE_KEY_PATH)
    account.dump(MagicMock())
コード例 #15
0
def test_creation():
    """Test the creation of the crypto_objects."""
    assert CosmosCrypto(), "Did not manage to initialise the crypto module"
    assert CosmosCrypto(COSMOS_PRIVATE_KEY_PATH
                        ), "Did not manage to load the cosmos private key"
コード例 #16
0
def test_validate_address():
    """Test the is_valid_address functionality."""
    account = CosmosCrypto()
    assert CosmosApi.is_valid_address(account.address)
    assert not CosmosApi.is_valid_address(account.address + "wrong")