Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt the user before overwriting keys.json file #673

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions aea/cli/generate_key.py
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -18,6 +18,7 @@
#
# ------------------------------------------------------------------------------
"""Implementation of the 'aea generate_key' subcommand."""
import json
from pathlib import Path
from typing import Dict, Optional, Union

Expand All @@ -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()
Expand Down Expand Up @@ -84,7 +90,7 @@ def generate_key(
)
return

generate_multiple_keys(
_generate_multiple_keys(
n=n,
type_=type_,
password=password,
Expand Down Expand Up @@ -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(
Expand Down
34 changes: 2 additions & 32 deletions aea/crypto/helpers.py
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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
Expand Down Expand Up @@ -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)
Loading