diff --git a/examples/config.json.example b/examples/config.json.example index 4e442544..e7651794 100644 --- a/examples/config.json.example +++ b/examples/config.json.example @@ -4,11 +4,14 @@ Fill in your secret key e.g. 0x0000000000000000000000000000000000000000000000000000000000000000 If you are using an Agent/API Wallet you MUST also specify the public address of your account, not the address of the Agent/API Wallet. - Otherwise, feel free to leave it blank and it will be automatically derived from the secret key. + Otherwise, feel free to leave it blank and it will be automatically derived from the secret key. Alternatively, + you can specify a local `keystore_path` which will prompt you for the keystore password interactively. + Note: If both `secret_key` and `keystore_path` are provided, the `secret_key` will take precedence. You can also populate the "multi_sig" section with the secret keys of the authorized user wallets that you wish to sign multi-sig actions for. ", + "keystore_path": "", "secret_key": "", "account_address": "", "multi_sig": { diff --git a/examples/example_utils.py b/examples/example_utils.py index 907c223d..390a5433 100644 --- a/examples/example_utils.py +++ b/examples/example_utils.py @@ -1,3 +1,4 @@ +import getpass import json import os @@ -12,7 +13,7 @@ def setup(base_url=None, skip_ws=False, perp_dexs=None): config_path = os.path.join(os.path.dirname(__file__), "config.json") with open(config_path) as f: config = json.load(f) - account: LocalAccount = eth_account.Account.from_key(config["secret_key"]) + account: LocalAccount = eth_account.Account.from_key(get_secret_key(config)) address = config["account_address"] if address == "": address = account.address @@ -32,6 +33,25 @@ def setup(base_url=None, skip_ws=False, perp_dexs=None): return address, info, exchange +def get_secret_key(config): + if config["secret_key"]: + secret_key = config["secret_key"] + else: + keystore_path = config["keystore_path"] + keystore_path = os.path.expanduser(keystore_path) + if not os.path.isabs(keystore_path): + keystore_path = os.path.join(os.path.dirname(__file__), keystore_path) + if not os.path.exists(keystore_path): + raise FileNotFoundError(f"Keystore file not found: {keystore_path}") + if not os.path.isfile(keystore_path): + raise ValueError(f"Keystore path is not a file: {keystore_path}") + with open(keystore_path) as f: + keystore = json.load(f) + password = getpass.getpass("Enter keystore password: ") + secret_key = eth_account.Account.decrypt(keystore, password) + return secret_key + + def setup_multi_sig_wallets(): config_path = os.path.join(os.path.dirname(__file__), "config.json") with open(config_path) as f: