-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkeys.py
52 lines (45 loc) · 1.54 KB
/
keys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
from getpass import getpass
from pathlib import Path
from eth_account import Account
from web3 import Web3
import json, logging
def make_new_keyfile(path_obj):
x = getpass(prompt='Paste private key: ')
y = getpass(prompt='Enter passphrase: ')
z = Account.encrypt(x,y)
if not path_obj.exists():
path_obj.touch()
with path_obj.open('w') as f:
f.write(json.dumps(z))
logger = logging.getLogger('dfkgrind')
logger.info('wrote encrypted keystore to ' + str(path_obj))
return z
def manage_keyfile(keyfile_path):
key = Path(keyfile_path)
logger = logging.getLogger('dfkgrind')
if key.exists():
logger.info('encrypted keystore file found, loading...')
with key.open() as f:
encrypted_key = json.loads(f.read())
else:
logger.info('keystore not found, making new key. get your MM private key...')
encrypted_key = make_new_keyfile(key)
return encrypted_key
def get_password(w3, encrypted_key):
p = getpass(prompt='Enter passphrase to decrypt and use: ')
try:
account_address = w3.eth.account.privateKeyToAccount(Account.decrypt(encrypted_key, p)).address
except:
logger.exception("Bad passphrase. Bailing out.")
return p
def get_address(w3, private_key):
account_address = None
try:
account_address = w3.eth.account.privateKeyToAccount(private_key).address
except:
logger.exception("Bad passphrase. Bailing out.")
if not account_address:
logger.exception("Could not decode checksum-enabled account address. Bailing out.")
del private_key
return account_address