Exemple #1
0
    def setup(self):
        """Setup."""
        self.ledger_api = ledger_apis_registry.make(FETCHAI,
                                                    **FETCHAI_TESTNET_CONFIG)
        self.faucet_api = faucet_apis_registry.make(FETCHAI)
        self.deployer_crypto = crypto_registry.make(FETCHAI)
        self.item_owner_crypto = crypto_registry.make(FETCHAI)

        # Test tokens IDs
        self.token_ids_a = [
            340282366920938463463374607431768211456,
            340282366920938463463374607431768211457,
            340282366920938463463374607431768211458,
            340282366920938463463374607431768211459,
            340282366920938463463374607431768211460,
            340282366920938463463374607431768211461,
            340282366920938463463374607431768211462,
            340282366920938463463374607431768211463,
            340282366920938463463374607431768211464,
            340282366920938463463374607431768211465,
        ]

        self.token_id_b = 680564733841876926926749214863536422912

        # Refill deployer account from faucet
        self.refill_from_faucet(self.ledger_api, self.faucet_api,
                                self.deployer_crypto.address)

        # Refill item owner account from faucet
        self.refill_from_faucet(self.ledger_api, self.faucet_api,
                                self.item_owner_crypto.address)
        self.set_contract()
Exemple #2
0
def make_certificate(ledger_id: str, crypto_private_key_path: str,
                     message: bytes, output_path: str) -> str:
    """Create certificate."""
    crypto = crypto_registry.make(ledger_id,
                                  private_key_path=crypto_private_key_path)
    signature = crypto.sign_message(message).encode("ascii").hex()
    ensure_dir(os.path.dirname(output_path))
    Path(output_path).write_bytes(signature.encode("ascii"))
    return signature
Exemple #3
0
def test_get_deploy_transaction_cosmwasm(dummy_contract):
    """Tests the deploy transaction classmethod for fetchai."""
    fetchai_crypto = crypto_registry.make(FETCHAI)
    ledger_api = ledger_apis_registry.make(FETCHAI, address=FETCHAI_DEFAULT_ADDRESS,)
    deploy_tx = dummy_contract.get_deploy_transaction(
        ledger_api, fetchai_crypto.address
    )
    assert deploy_tx is not None and len(deploy_tx) == 6
    assert all(
        key in ["account_number", "chain_id", "fee", "memo", "msgs", "sequence"]
        for key in deploy_tx.keys()
    )
Exemple #4
0
def test_get_deploy_transaction_cosmwasm(dummy_contract):
    """Tests the deploy transaction classmethod for cosmos."""
    cosmos_crypto = crypto_registry.make(COSMOS)
    ledger_api = ledger_apis_registry.make(
        COSMOS, address="https://rest-agent-land.prod.fetch-ai.com:443",
    )
    deploy_tx = dummy_contract.get_deploy_transaction(ledger_api, cosmos_crypto.address)
    assert deploy_tx is not None and len(deploy_tx) == 6
    assert all(
        key in ["account_number", "chain_id", "fee", "memo", "msgs", "sequence"]
        for key in deploy_tx.keys()
    )
Exemple #5
0
def _try_get_multiaddress(
    click_context,
    ledger_id: str,
    is_connection: bool,
    connection_id: Optional[PublicId],
    host_field: str,
    port_field: str,
    uri_field: str,
):
    """
    Try to get the multi-address.

    :param click_context: click context object.
    :param ledger_id: the ledger id.
    :param is_connection: whether the key to load is from the wallet or from connections.
    :param connection_id: the connection id.
    :param host_field: if connection_id specified, the config field to retrieve the host
    :param port_field: if connection_id specified, the config field to retrieve the port

    :return: address.
    """
    ctx = cast(Context, click_context.obj)
    # connection_id not None implies is_connection
    is_connection = connection_id is not None or is_connection

    private_key_paths = (
        ctx.agent_config.private_key_paths
        if not is_connection
        else ctx.agent_config.connection_private_key_paths
    )
    private_key_path = private_key_paths.read(ledger_id)

    if private_key_path is None:
        raise ClickException(
            f"Cannot find '{ledger_id}'. Please check {'private_key_path' if not is_connection else 'connection_private_key_paths'}."
        )

    path_to_key = Path(private_key_path)
    crypto = crypto_registry.make(ledger_id, private_key_path=path_to_key)

    if connection_id is None:
        return _try_get_peerid(crypto)
    return _try_get_connection_multiaddress(
        click_context,
        crypto,
        cast(PublicId, connection_id),
        host_field,
        port_field,
        uri_field,
    )
Exemple #6
0
def test_get_deploy_transaction_ethereum(dummy_contract):
    """Tests the deploy transaction classmethod for ethereum."""
    ethereum_crypto = crypto_registry.make(ETHEREUM)
    ledger_api = ledger_apis_registry.make(
        ETHEREUM,
        address="https://ropsten.infura.io/v3/f00f7b3ba0e848ddbdc8941c527447fe",
    )
    deploy_tx = dummy_contract.get_deploy_transaction(
        ledger_api, ethereum_crypto.address
    )
    assert deploy_tx is not None and len(deploy_tx) == 6
    assert all(
        key in ["from", "value", "gas", "gasPrice", "nonce", "data"]
        for key in deploy_tx.keys()
    )
Exemple #7
0
def test_get_deploy_transaction_ethereum(dummy_contract):
    """Tests the deploy transaction classmethod for ethereum."""
    ethereum_crypto = crypto_registry.make(ETHEREUM)
    ledger_api = ledger_apis_registry.make(ETHEREUM, address=ETHEREUM_DEFAULT_ADDRESS,)
    with patch(
        "web3.contract.ContractConstructor.buildTransaction",
        return_value={"data": "0xstub"},
    ):
        deploy_tx = dummy_contract.get_deploy_transaction(
            ledger_api, ethereum_crypto.address
        )
    assert deploy_tx is not None and len(deploy_tx) == 6
    assert all(
        key in ["from", "value", "gas", "gasPrice", "nonce", "data"]
        for key in deploy_tx.keys()
    )
Exemple #8
0
def _process_certificate(
    path_prefix: str,
    agent_config: AgentConfig,
    cert_request: CertRequest,
    connection_id: PublicId,
) -> None:
    """Process a single certificate request."""
    ledger_id = cert_request.ledger_id
    if cert_request.key_identifier is not None:
        key_identifier = cert_request.key_identifier
        connection_private_key_path = agent_config.connection_private_key_paths.read(
            key_identifier)
        if connection_private_key_path is None:
            raise ClickException(
                f"Cannot find connection private key with id '{key_identifier}'. Connection '{connection_id}' requires this. Please use `aea generate-key {key_identifier} connection_{key_identifier}_private_key.txt` and `aea add-key {key_identifier} connection_{key_identifier}_private_key.txt --connection` to add a connection private key with id '{key_identifier}'."
            )
        new_connection_private_key_path = prepend_if_not_absolute(
            connection_private_key_path, path_prefix)
        connection_crypto = crypto_registry.make(
            key_identifier, private_key_path=new_connection_private_key_path)
        public_key = connection_crypto.public_key
    else:
        public_key = cast(str, cert_request.public_key)
        enforce(
            public_key is not None,
            "Internal error - one of key_identifier or public_key must be not None.",
        )
    crypto_private_key_path = agent_config.private_key_paths.read(ledger_id)
    if crypto_private_key_path is None:
        raise ClickException(
            f"Cannot find private key with id '{ledger_id}'. Please use `aea generate-key {key_identifier}` and `aea add-key {key_identifier}` to add a private key with id '{key_identifier}'."
        )
    message = cert_request.get_message(public_key)
    output_path = cert_request.get_absolute_save_path(path_prefix)
    absolute_crypto_private_key_path = prepend_if_not_absolute(
        crypto_private_key_path, path_prefix)
    cert = make_certificate(
        ledger_id,
        str(absolute_crypto_private_key_path),
        message,
        str(output_path),
    )
    click.echo(f"Generated signature: '{cert}'")
    click.echo(
        f"Dumped certificate '{cert_request.identifier}' in '{output_path}' for connection {connection_id}."
    )
def _process_certificate(
    project_directory: str,
    agent_config: AgentConfig,
    cert_request: CertRequest,
    connection_id: PublicId,
):
    """Process a single certificate request."""
    ledger_id = cert_request.ledger_id
    output_path = cert_request.save_path
    if cert_request.key_identifier is not None:
        key_identifier = cert_request.key_identifier
        connection_private_key_path = agent_config.connection_private_key_paths.read(
            key_identifier)
        if connection_private_key_path is None:
            raise ClickException(
                f"Cannot find connection private key with id '{key_identifier}'. Connection '{connection_id}' requires this. Please use `aea generate-key {key_identifier} connection_{key_identifier}_private_key.txt` and `aea add-key {key_identifier} connection_{key_identifier}_private_key.txt --connection` to add a connection private key with id '{key_identifier}'."
            )
        connection_crypto = crypto_registry.make(
            key_identifier, private_key_path=connection_private_key_path)
        public_key = connection_crypto.public_key
    else:
        public_key = cast(str, cert_request.public_key)
        enforce(
            public_key is not None,
            "Internal error - one of key_identifier or public_key must be not None.",
        )
    crypto_private_key_path = agent_config.private_key_paths.read(ledger_id)
    if crypto_private_key_path is None:
        raise ClickException(
            f"Cannot find private key with id '{ledger_id}'. Please use `aea generate-key {key_identifier}` and `aea add-key {key_identifier}` to add a private key with id '{key_identifier}'."
        )
    message = cert_request.get_message(public_key)
    cert = make_certificate(
        ledger_id,
        crypto_private_key_path,
        message,
        os.path.join(project_directory, output_path),
    )
    click.echo(f"Generated signature: '{cert}'")
def crypto_api(request):
    crypto_id = request.param[0]
    api = crypto_registry.make(crypto_id)
    yield api
Exemple #11
0
def crypto_api(request):
    """Crypto api fixture."""
    crypto_id = request.param[0]
    api = crypto_registry.make(crypto_id)
    yield api