Skip to content

Commit

Permalink
Add support for encrypted wallet
Browse files Browse the repository at this point in the history
Signed-off-by: cyc60 <avsysoev60@gmail.com>
  • Loading branch information
cyc60 committed Feb 1, 2023
1 parent faf8987 commit c0b1e4c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
5 changes: 3 additions & 2 deletions deploy/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ DATABASE_DIR=./database/

# operator
# Uncomment and provide values
#OPERATOR_PRIVATE_KEY=<operator_private_key>

WALLET_PRIVATE_KEY=<private key of the hot wallet goes here>
#WALLET_KEYSTORE_PATH="/home/user/UTC--wallet-keystore"
#WALLET_KEYSTORE_PASSWORD_PATH="/home/user/wallet-pass.txt"
# network
NETWORK=goerli
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 c0b1e4c

Please sign in to comment.