Skip to content

Commit 13090a6

Browse files
feat(examples): option to derive account from an encrypted keystore (#186)
1 parent d01888e commit 13090a6

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

examples/config.json.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
Fill in your secret key e.g. 0x0000000000000000000000000000000000000000000000000000000000000000
55
If you are using an Agent/API Wallet you MUST also specify the public address of your account, not the
66
address of the Agent/API Wallet.
7-
Otherwise, feel free to leave it blank and it will be automatically derived from the secret key.
7+
Otherwise, feel free to leave it blank and it will be automatically derived from the secret key. Alternatively,
8+
you can specify a local `keystore_path` which will prompt you for the keystore password interactively.
9+
Note: If both `secret_key` and `keystore_path` are provided, the `secret_key` will take precedence.
810

911
You can also populate the "multi_sig" section with the secret keys of the authorized user wallets that you
1012
wish to sign multi-sig actions for.
1113
",
14+
"keystore_path": "",
1215
"secret_key": "",
1316
"account_address": "",
1417
"multi_sig": {

examples/example_utils.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import getpass
12
import json
23
import os
34

@@ -12,7 +13,7 @@ def setup(base_url=None, skip_ws=False, perp_dexs=None):
1213
config_path = os.path.join(os.path.dirname(__file__), "config.json")
1314
with open(config_path) as f:
1415
config = json.load(f)
15-
account: LocalAccount = eth_account.Account.from_key(config["secret_key"])
16+
account: LocalAccount = eth_account.Account.from_key(get_secret_key(config))
1617
address = config["account_address"]
1718
if address == "":
1819
address = account.address
@@ -32,6 +33,25 @@ def setup(base_url=None, skip_ws=False, perp_dexs=None):
3233
return address, info, exchange
3334

3435

36+
def get_secret_key(config):
37+
if config["secret_key"]:
38+
secret_key = config["secret_key"]
39+
else:
40+
keystore_path = config["keystore_path"]
41+
keystore_path = os.path.expanduser(keystore_path)
42+
if not os.path.isabs(keystore_path):
43+
keystore_path = os.path.join(os.path.dirname(__file__), keystore_path)
44+
if not os.path.exists(keystore_path):
45+
raise FileNotFoundError(f"Keystore file not found: {keystore_path}")
46+
if not os.path.isfile(keystore_path):
47+
raise ValueError(f"Keystore path is not a file: {keystore_path}")
48+
with open(keystore_path) as f:
49+
keystore = json.load(f)
50+
password = getpass.getpass("Enter keystore password: ")
51+
secret_key = eth_account.Account.decrypt(keystore, password)
52+
return secret_key
53+
54+
3555
def setup_multi_sig_wallets():
3656
config_path = os.path.join(os.path.dirname(__file__), "config.json")
3757
with open(config_path) as f:

0 commit comments

Comments
 (0)