forked from coccoinomane/web3cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyfile_controller.py
More file actions
49 lines (42 loc) · 1.44 KB
/
Copy pathkeyfile_controller.py
File metadata and controls
49 lines (42 loc) · 1.44 KB
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
import json
from cement import ex
from web3cli.framework.controller import Controller
from web3cli.helpers.crypto import decrypt_keyfile, encrypt_to_keyfile
from web3cli.helpers.render import render
class KeyfileController(Controller):
"""Handler of the `w3 keyfile` commands"""
class Meta:
label = "keyfile"
help = "create and read JSON keyfiles, like the ones used by geth, brownie, ape, etc"
stacked_type = "nested"
stacked_on = "base"
@ex(
help="Print the private key of the given keyfile",
arguments=[
(["path"], {"help": "path to input keyfile (JSON)"}),
],
aliases=["decrypt"],
)
def decode(self) -> None:
key = decrypt_keyfile(self.app.pargs.path)
render(self.app, key.replace("0x", ""))
@ex(
help="Create a new keyfile from a private key",
arguments=[
(
["path"],
{
"help": "path of output keyfile; omit to print to screen",
"nargs": "?",
},
),
],
aliases=["encode", "encrypt"],
)
def create(self) -> None:
keyfile_dict = encrypt_to_keyfile()
if self.app.pargs.path:
json.dump(keyfile_dict, open(self.app.pargs.path, "w"))
self.app.log.info(f"Keyfile saved to {self.app.pargs.path}")
else:
render(self.app, json.dumps(keyfile_dict))