From be2f2914bccf0717d80a3fb80bfa0917b45eea8b Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Tue, 3 Oct 2023 18:27:16 +0530 Subject: [PATCH 1/2] fix: prompt the user before overwriting keys.json file --- aea/cli/generate_key.py | 38 ++++++++++++++++++++++++++++++++++---- aea/crypto/helpers.py | 32 +------------------------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/aea/cli/generate_key.py b/aea/cli/generate_key.py index 4646e9c2c2..ac46fd2b79 100644 --- a/aea/cli/generate_key.py +++ b/aea/cli/generate_key.py @@ -18,6 +18,7 @@ # # ------------------------------------------------------------------------------ """Implementation of the 'aea generate_key' subcommand.""" +import json from pathlib import Path from typing import Dict, Optional, Union @@ -26,9 +27,14 @@ from aea.cli.add_key import _add_private_key from aea.cli.utils.click_utils import password_option from aea.cli.utils.decorators import _check_aea_project -from aea.configurations.constants import PRIVATE_KEY_PATH_SCHEMA -from aea.crypto.helpers import create_private_key, generate_multiple_keys -from aea.crypto.registries import crypto_registry +from aea.configurations.constants import ( + ADDRESS, + MULTIKEY_FILENAME, + PRIVATE_KEY, + PRIVATE_KEY_PATH_SCHEMA, +) +from aea.crypto.helpers import create_private_key +from aea.crypto.registries import crypto_registry, make_crypto @click.command() @@ -84,7 +90,7 @@ def generate_key( ) return - generate_multiple_keys( + _generate_multiple_keys( n=n, type_=type_, password=password, @@ -142,6 +148,30 @@ def _generate_private_key( return keys +def _generate_multiple_keys( + n: int, + type_: str, + password: Optional[str] = None, + extra_entropy: Union[str, bytes, int] = "", + file: Optional[str] = None, +) -> None: + """Generate n key pairs.""" + + key_pairs = [] + for _ in range(n): + crypto = make_crypto(type_, extra_entropy=extra_entropy) + priv_key = ( + crypto.encrypt(password=password) + if password is not None + else crypto.private_key + ) + key_pairs.append({ADDRESS: crypto.address, PRIVATE_KEY: priv_key}) + + file = file or MULTIKEY_FILENAME + if _can_write(file): + Path(file).write_text(json.dumps(obj=key_pairs, indent=2), encoding="utf-8") + + def _can_write(path: str) -> bool: if Path(path).exists(): value = click.confirm( diff --git a/aea/crypto/helpers.py b/aea/crypto/helpers.py index 33d7935596..7bca201880 100644 --- a/aea/crypto/helpers.py +++ b/aea/crypto/helpers.py @@ -18,19 +18,13 @@ # # ------------------------------------------------------------------------------ """Module wrapping the helpers of public and private key cryptography.""" -import json import logging import os from pathlib import Path from typing import Dict, Optional, Union from aea.configurations.base import AgentConfig -from aea.configurations.constants import ( - ADDRESS, - MULTIKEY_FILENAME, - PRIVATE_KEY, - PRIVATE_KEY_PATH_SCHEMA, -) +from aea.configurations.constants import PRIVATE_KEY_PATH_SCHEMA from aea.crypto.registries import crypto_registry, make_crypto, make_faucet_api from aea.crypto.wallet import Wallet from aea.helpers.base import ensure_dir @@ -214,27 +208,3 @@ def hex_to_bytes_for_key(data: str) -> bytes: return bytes.fromhex(data) except ValueError as e: raise KeyIsIncorrect(str(e)) from e - - -def generate_multiple_keys( - n: int, - type_: str, - password: Optional[str] = None, - extra_entropy: Union[str, bytes, int] = "", - file: Optional[str] = None, -) -> None: - """Generate n key pairs.""" - - key_pairs = [] - for _ in range(n): - crypto = make_crypto(type_, extra_entropy=extra_entropy) - priv_key = ( - crypto.encrypt(password=password) - if password is not None - else crypto.private_key - ) - key_pairs.append({ADDRESS: crypto.address, PRIVATE_KEY: priv_key}) - - file = file or MULTIKEY_FILENAME - with open(file, mode="w", encoding="utf-8") as fp: - json.dump(obj=key_pairs, fp=fp, indent=2) From 8c79993a87f7c80b360b44f419a5724658c62aa6 Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Tue, 3 Oct 2023 18:46:49 +0530 Subject: [PATCH 2/2] fix: copyright --- aea/cli/generate_key.py | 2 +- aea/crypto/helpers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/aea/cli/generate_key.py b/aea/cli/generate_key.py index ac46fd2b79..7471ca8a93 100644 --- a/aea/cli/generate_key.py +++ b/aea/cli/generate_key.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # ------------------------------------------------------------------------------ # -# Copyright 2021-2022 Valory AG +# Copyright 2021-2023 Valory AG # Copyright 2018-2020 Fetch.AI Limited # # Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/aea/crypto/helpers.py b/aea/crypto/helpers.py index 7bca201880..9d14a3e225 100644 --- a/aea/crypto/helpers.py +++ b/aea/crypto/helpers.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # ------------------------------------------------------------------------------ # -# Copyright 2021-2022 Valory AG +# Copyright 2021-2023 Valory AG # Copyright 2018-2019 Fetch.AI Limited # # Licensed under the Apache License, Version 2.0 (the "License");