Ejemplo n.º 1
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 crypto_registry.supported_ids:
            ValueError("Unsupported identifier in private key paths.")

    for identifier, private_key_path in IDENTIFIER_TO_KEY_FILES.items():
        config_private_key_path = aea_conf.private_key_paths.read(identifier)
        if config_private_key_path is None:
            create_private_key(identifier)
            aea_conf.private_key_paths.update(identifier, private_key_path)
        else:
            try:
                try_validate_private_key_path(identifier, private_key_path)
            except FileNotFoundError:  # pragma: no cover
                raise click.ClickException(
                    "File {} for private key {} not found.".format(
                        repr(private_key_path), identifier,
                    )
                )

    # 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
Ejemplo n.º 2
0
 def test_try_validate_private_key_path_positive(self):
     """Test _validate_private_key_path positive result."""
     try_validate_private_key_path(
         FetchAICrypto.identifier, FETCHAI_PRIVATE_KEY_PATH
     )
     try_validate_private_key_path(
         EthereumCrypto.identifier, ETHEREUM_PRIVATE_KEY_PATH
     )
Ejemplo n.º 3
0
def _add_private_key(click_context: click.core.Context, type_: str,
                     file: str) -> None:
    """
    Add private key to the wallet.

    :param click_context: click context object.
    :param type_: type.
    :param file: path to file.

    :return: None
    """
    ctx = cast(Context, click_context.obj)
    try_validate_private_key_path(type_, file)
    _try_add_key(ctx, type_, file)
Ejemplo n.º 4
0
def _add_private_key(
    click_context: click.core.Context, type_: str, file: str, connection: bool = False
) -> None:
    """
    Add private key to the wallet.

    :param click_context: click context object.
    :param type_: type.
    :param file: path to file.
    :param connection: whether or not it is a private key for a connection

    :return: None
    """
    ctx = cast(Context, click_context.obj)
    try_validate_private_key_path(type_, file)
    _try_add_key(ctx, type_, file, connection)
Ejemplo n.º 5
0
def _add_private_key(
    click_context: click.core.Context,
    type_: str,
    file: Optional[str] = None,
    connection: bool = False,
) -> None:
    """
    Add private key to the wallet.

    :param click_context: click context object.
    :param type_: type.
    :param file: path to file.
    :param connection: whether or not it is a private key for a connection

    :return: None
    """
    ctx = cast(Context, click_context.obj)
    if file is None:
        file = PRIVATE_KEY_PATH_SCHEMA.format(type_)
    try_validate_private_key_path(type_, file)
    _try_add_key(ctx, type_, file, connection)
Ejemplo n.º 6
0
    def tests_private_keys(self):
        """Test the private keys."""
        try_validate_private_key_path(FetchAICrypto.identifier,
                                      FETCHAI_PRIVATE_KEY_PATH)
        with pytest.raises(SystemExit):
            private_key_path = os.path.join(CUR_PATH, "data",
                                            "fet_private_key_wrong.txt")
            try_validate_private_key_path(FetchAICrypto.identifier,
                                          private_key_path)

        try_validate_private_key_path(EthereumCrypto.identifier,
                                      ETHEREUM_PRIVATE_KEY_PATH)
        with pytest.raises(SystemExit):
            private_key_path = os.path.join(CUR_PATH, "data",
                                            "fet_private_key_wrong.txt")
            try_validate_private_key_path(EthereumCrypto.identifier,
                                          private_key_path)
Ejemplo n.º 7
0
    def tests_private_keys(self):
        """Test the private keys."""
        private_key_path = os.path.join(CUR_PATH, "data",
                                        "fet_private_key.txt")
        try_validate_private_key_path(FetchAICrypto.identifier,
                                      private_key_path)
        with pytest.raises(SystemExit):
            private_key_path = os.path.join(CUR_PATH, "data",
                                            "fet_private_key_wrong.txt")
            try_validate_private_key_path(FetchAICrypto.identifier,
                                          private_key_path)

        private_key_path = os.path.join(CUR_PATH, "data",
                                        "eth_private_key.txt")
        try_validate_private_key_path(EthereumCrypto.identifier,
                                      private_key_path)
        with pytest.raises(SystemExit):
            private_key_path = os.path.join(CUR_PATH, "data",
                                            "fet_private_key_wrong.txt")
            try_validate_private_key_path(EthereumCrypto.identifier,
                                          private_key_path)