Skip to content

Commit

Permalink
Add support for encrypted wallet (#20)
Browse files Browse the repository at this point in the history
Signed-off-by: cyc60 <avsysoev60@gmail.com>
  • Loading branch information
cyc60 authored Feb 2, 2023
1 parent 5795f4e commit 0ec0cc5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
36 changes: 34 additions & 2 deletions src/common/accounts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import json
import os.path

from eth_account import Account
from eth_account.account import LocalAccount

from src.config.settings import (
WALLET_KEYSTORE_PASSWORD_PATH,
WALLET_KEYSTORE_PATH,
WALLET_PRIVATE_KEY,
)


def get_operator_account() -> LocalAccount:
if WALLET_PRIVATE_KEY:
return Account().from_key(WALLET_PRIVATE_KEY)

if WALLET_KEYSTORE_PATH and WALLET_KEYSTORE_PASSWORD_PATH:
if not os.path.isfile(WALLET_KEYSTORE_PATH):
raise ValueError(f"Can't open WALLET_KEYSTORE_PATH file. "
f' Path: {WALLET_KEYSTORE_PATH}')
if not os.path.isfile(WALLET_KEYSTORE_PASSWORD_PATH):
raise ValueError(f"Can't open WALLET_KEYSTORE_PASSWORD_PATH file. "
f'Path: {WALLET_KEYSTORE_PASSWORD_PATH}')

with open(WALLET_KEYSTORE_PATH, 'r', encoding='utf-8') as f:
keyfile_json = json.load(f)
with open(WALLET_KEYSTORE_PASSWORD_PATH, 'r', encoding='utf-8') as f:
password = f.read().strip()
key = Account().decrypt(keyfile_json, password)
return Account().from_key(key)

raise ValueError('Provide WALLET_PRIVATE_KEY setting or combination of '
'WALLET_KEYSTORE_PATH and WALLET_KEYSTORE_PASSWORD_PATH settings')

from src.config.settings import OPERATOR_PRIVATE_KEY

operator_account = Account().from_key(OPERATOR_PRIVATE_KEY)
operator_account = get_operator_account()
6 changes: 4 additions & 2 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
# deposit data
DEPOSIT_DATA_PATH = config('DEPOSIT_DATA_PATH')

# operator
OPERATOR_PRIVATE_KEY = config('OPERATOR_PRIVATE_KEY')
# wallet private key
WALLET_PRIVATE_KEY = config('WALLET_PRIVATE_KEY', default=None)
WALLET_KEYSTORE_PATH = config('WALLET_KEYSTORE_PATH', default=None)
WALLET_KEYSTORE_PASSWORD_PATH = config('WALLET_KEYSTORE_PASSWORD_PATH', default=None)

# remote IPFS
IPFS_FETCH_ENDPOINTS = config(
Expand Down

0 comments on commit 0ec0cc5

Please sign in to comment.