From eb2961be60c1dc256d978ef2f6a2273183cb2043 Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Thu, 19 Oct 2023 17:06:50 +0200 Subject: [PATCH 1/3] feat: added script to change password --- scripts/change_keys_json_password.py | 83 ++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 scripts/change_keys_json_password.py diff --git a/scripts/change_keys_json_password.py b/scripts/change_keys_json_password.py new file mode 100644 index 00000000..a14fc565 --- /dev/null +++ b/scripts/change_keys_json_password.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# ------------------------------------------------------------------------------ +# +# Copyright 2022-2023 Valory AG +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ------------------------------------------------------------------------------ + +"""Changes the key files password of the trader store.""" + +import argparse +import json +import tempfile +from pathlib import Path + +from aea.crypto.helpers import DecryptError +from aea_ledger_ethereum.ethereum import EthereumCrypto + + +def _change_keys_json_password( + keys_json_path: Path, pkey_txt_path: Path, current_password: str, new_password: str +) -> None: + keys_json_reencrypeted = [] + keys = json.load(keys_json_path.open("r")) + + with tempfile.TemporaryDirectory() as temp_dir: + for idx, key in enumerate(keys): + temp_file = Path(temp_dir, str(idx)) + temp_file.open("w+", encoding="utf-8").write(str(key["private_key"])) + try: + crypto = EthereumCrypto.load_private_key_from_path( + str(temp_file), password=current_password + ) + + private_key_reencrypted = crypto.encrypt(new_password) + keys_json_reencrypeted.append( + { + "address": crypto.address, + "private_key": f"{json.dumps(private_key_reencrypted)}", + } + ) + json.dump(keys_json_reencrypeted, keys_json_path.open("w+"), indent=2) + print(f"Changed password {keys_json_path}") + + json.dump(private_key_reencrypted, pkey_txt_path.open("w+")) + print(f"Ovewritten {keys_json_path}") + except DecryptError: + print("Bad password provided.") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Change key files password.") + parser.add_argument( + "store_path", type=str, help="Path to the trader store directory." + ) + parser.add_argument("current_password", type=str, help="Current password.") + parser.add_argument("new_password", type=str, help="New password.") + args = parser.parse_args() + + _change_keys_json_password( + Path(args.store_path, "keys.json"), + Path(args.store_path, "agent_pkey.txt"), + args.current_password, + args.new_password, + ) + _change_keys_json_password( + Path(args.store_path, "operator_keys.json"), + Path(args.store_path, "operator_pkey.txt"), + args.current_password, + args.new_password, + ) From cc750c191b896aac32f55b82b652769954ce532c Mon Sep 17 00:00:00 2001 From: jmoreira-valory Date: Thu, 19 Oct 2023 17:11:33 +0200 Subject: [PATCH 2/3] chore: update Readme.txt --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 839e00a1..e3a4197a 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,21 @@ rm -rf trader Then continue above with "Run the script". +## Change the password of your key files + +If you have started you script specifying a password to protect your key files, you can change it by running the following command: + +```bash +cd trader; poetry run python ../scripts/change_keys_json_password.py ../.trader_runner ; cd .. +``` + +This will change the password in the following files: + +- `.trader_runner/keys.json` +- `.trader_runner/operator_keys.json` +- `.trader_runner/agent_pkey.txt` +- `.trader_runner/operator_pkey.txt` + ## Advice for Mac users In Docker Desktop make sure that in `Settings -> Advanced` the following boxes are ticked From 053c707b4faee2f420aaf4c1510dd03e62fd45e0 Mon Sep 17 00:00:00 2001 From: jmoreira-valory <96571377+jmoreira-valory@users.noreply.github.com> Date: Thu, 19 Oct 2023 17:46:19 +0200 Subject: [PATCH 3/3] Update scripts/change_keys_json_password.py Co-authored-by: Adamantios Zaras --- scripts/change_keys_json_password.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/scripts/change_keys_json_password.py b/scripts/change_keys_json_password.py index a14fc565..4b27dd6f 100644 --- a/scripts/change_keys_json_password.py +++ b/scripts/change_keys_json_password.py @@ -69,15 +69,10 @@ def _change_keys_json_password( parser.add_argument("new_password", type=str, help="New password.") args = parser.parse_args() - _change_keys_json_password( - Path(args.store_path, "keys.json"), - Path(args.store_path, "agent_pkey.txt"), - args.current_password, - args.new_password, - ) - _change_keys_json_password( - Path(args.store_path, "operator_keys.json"), - Path(args.store_path, "operator_pkey.txt"), - args.current_password, - args.new_password, - ) + for json_file, pkey_file in (("keys", "agent_pkey"), ("operator_keys", "operator_pkey")): + filepaths = (Path(args.store_path, file) for file in (f"{json_file}.json", f"{pkey_file}.txt")) + _change_keys_json_password( + *filepaths, + args.current_password, + args.new_password, + )