Beispiel #1
0
def _verify_or_create_private_keys(aea_project_path: Path) -> None:
    """Verify or create private keys."""
    path_to_configuration = aea_project_path / DEFAULT_AEA_CONFIG_FILE
    agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig)
    fp_read = path_to_configuration.open(mode="r", encoding="utf-8")
    agent_configuration = agent_loader.load(fp_read)

    for identifier, _value in agent_configuration.private_key_paths.read_all():
        if identifier not in SUPPORTED_CRYPTOS:
            ValueError("Unsupported identifier in private key paths.")

    fetchai_private_key_path = agent_configuration.private_key_paths.read(FETCHAI)
    if fetchai_private_key_path is None:
        _create_fetchai_private_key(
            private_key_file=str(aea_project_path / FETCHAI_PRIVATE_KEY_FILE)
        )
        agent_configuration.private_key_paths.update(FETCHAI, FETCHAI_PRIVATE_KEY_FILE)
    else:
        try:
            _try_validate_fet_private_key_path(
                str(aea_project_path / fetchai_private_key_path), exit_on_error=False
            )
        except FileNotFoundError:  # pragma: no cover
            logger.error(
                "File {} for private key {} not found.".format(
                    repr(fetchai_private_key_path), FETCHAI,
                )
            )
            raise

    ethereum_private_key_path = agent_configuration.private_key_paths.read(ETHEREUM)
    if ethereum_private_key_path is None:
        _create_ethereum_private_key(
            private_key_file=str(aea_project_path / ETHEREUM_PRIVATE_KEY_FILE)
        )
        agent_configuration.private_key_paths.update(
            ETHEREUM, ETHEREUM_PRIVATE_KEY_FILE
        )
    else:
        try:
            _try_validate_ethereum_private_key_path(
                str(aea_project_path / ethereum_private_key_path), exit_on_error=False
            )
        except FileNotFoundError:  # pragma: no cover
            logger.error(
                "File {} for private key {} not found.".format(
                    repr(ethereum_private_key_path), ETHEREUM,
                )
            )
            raise

    fp_write = path_to_configuration.open(mode="w", encoding="utf-8")
    agent_loader.dump(agent_configuration, fp_write)
Beispiel #2
0
def _verify_or_create_private_keys(ctx: Context) -> None:
    """
    Verify or create private keys.

    :param ctx: Context
    """
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig)
    fp = path.open(mode="r", encoding="utf-8")
    aea_conf = agent_loader.load(fp)

    for identifier, _value in aea_conf.private_key_paths.read_all():
        if identifier not in SUPPORTED_CRYPTOS:
            ValueError("Unsupported identifier in private key paths.")

    fetchai_private_key_path = aea_conf.private_key_paths.read(FETCHAI)
    if fetchai_private_key_path is None:
        _create_fetchai_private_key()
        aea_conf.private_key_paths.update(FETCHAI, FETCHAI_PRIVATE_KEY_FILE)
    else:
        try:
            _try_validate_fet_private_key_path(fetchai_private_key_path)
        except FileNotFoundError:  # pragma: no cover
            logger.error("File {} for private key {} not found.".format(
                repr(fetchai_private_key_path),
                FETCHAI,
            ))
            sys.exit(1)

    ethereum_private_key_path = aea_conf.private_key_paths.read(ETHEREUM)
    if ethereum_private_key_path is None:
        _create_ethereum_private_key()
        aea_conf.private_key_paths.update(ETHEREUM, ETHEREUM_PRIVATE_KEY_FILE)
    else:
        try:
            _try_validate_ethereum_private_key_path(ethereum_private_key_path)
        except FileNotFoundError:  # pragma: no cover
            logger.error("File {} for private key {} not found.".format(
                repr(ethereum_private_key_path),
                ETHEREUM,
            ))
            sys.exit(1)

    # update aea config
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    fp = path.open(mode="w", encoding="utf-8")
    agent_loader.dump(aea_conf, fp)
    ctx.agent_config = aea_conf
Beispiel #3
0
 def test__create_ethereum_private_key_positive(self, *mocks):
     """Test _create_ethereum_private_key positive result."""
     _create_ethereum_private_key()
Beispiel #4
0
def _verify_or_create_private_keys(ctx: Context) -> None:
    """
    Verify or create private keys.

    :param ctx: Context
    """
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig)
    fp = open(str(path), mode="r", encoding="utf-8")
    aea_conf = agent_loader.load(fp)

    for identifier, value in aea_conf.private_key_paths.read_all():
        if identifier not in SUPPORTED_CRYPTOS:
            ValueError("Unsupported identifier in private key paths.")

    default_private_key_config = aea_conf.private_key_paths.read(DEFAULT)
    if default_private_key_config is None:
        _create_default_private_key()
        default_private_key_config = PrivateKeyPathConfig(
            DEFAULT, DEFAULT_PRIVATE_KEY_FILE)
        aea_conf.private_key_paths.create(default_private_key_config.ledger,
                                          default_private_key_config)
    else:
        default_private_key_config = cast(PrivateKeyPathConfig,
                                          default_private_key_config)
        try:
            _try_validate_private_key_pem_path(default_private_key_config.path)
        except FileNotFoundError:
            logger.error("File {} for private key {} not found.".format(
                repr(default_private_key_config.path),
                default_private_key_config.ledger))
            sys.exit(1)

    fetchai_private_key_config = aea_conf.private_key_paths.read(FETCHAI)
    if fetchai_private_key_config is None:
        _create_fetchai_private_key()
        fetchai_private_key_config = PrivateKeyPathConfig(
            FETCHAI, FETCHAI_PRIVATE_KEY_FILE)
        aea_conf.private_key_paths.create(fetchai_private_key_config.ledger,
                                          fetchai_private_key_config)
    else:
        fetchai_private_key_config = cast(PrivateKeyPathConfig,
                                          fetchai_private_key_config)
        try:
            _try_validate_fet_private_key_path(fetchai_private_key_config.path)
        except FileNotFoundError:
            logger.error("File {} for private key {} not found.".format(
                repr(fetchai_private_key_config.path),
                fetchai_private_key_config.ledger))
            sys.exit(1)

    ethereum_private_key_config = aea_conf.private_key_paths.read(ETHEREUM)
    if ethereum_private_key_config is None:
        _create_ethereum_private_key()
        ethereum_private_key_config = PrivateKeyPathConfig(
            ETHEREUM, ETHEREUM_PRIVATE_KEY_FILE)
        aea_conf.private_key_paths.create(ethereum_private_key_config.ledger,
                                          ethereum_private_key_config)
    else:
        ethereum_private_key_config = cast(PrivateKeyPathConfig,
                                           ethereum_private_key_config)
        try:
            _try_validate_ethereum_private_key_path(
                ethereum_private_key_config.path)
        except FileNotFoundError:
            logger.error("File {} for private key {} not found.".format(
                repr(ethereum_private_key_config.path),
                ethereum_private_key_config.ledger))
            sys.exit(1)

    # update aea config
    path = Path(DEFAULT_AEA_CONFIG_FILE)
    fp = open(str(path), mode="w", encoding="utf-8")
    agent_loader.dump(aea_conf, fp)
    ctx.agent_config = aea_conf