Skip to content

Commit 26d562f

Browse files
committed
add peer command and sub command
1 parent 6ed5155 commit 26d562f

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

convex_api/tool/command/account_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
3-
Command 'get_ether'
3+
Account Commands
44
55
"""
66

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
3+
Command peer
4+
5+
"""
6+
7+
from .command_base import CommandBase
8+
from .help_command import HelpCommand
9+
from .peer_create_command import PeerCreateCommand
10+
11+
12+
class PeerCommand(CommandBase):
13+
14+
def __init__(self, sub_parser=None):
15+
self._command_list = []
16+
super().__init__('peer', sub_parser)
17+
18+
def create_parser(self, sub_parser):
19+
parser = sub_parser.add_parser(
20+
self._name,
21+
description='Tool tasks on peers',
22+
help='Tasks to perform on peers',
23+
24+
)
25+
peer_parser = parser.add_subparsers(
26+
title='Peer sub command',
27+
description='Peer sub command',
28+
help='Peer sub command',
29+
dest='peer_command'
30+
)
31+
32+
self._command_list = [
33+
PeerCreateCommand(peer_parser),
34+
HelpCommand(peer_parser, self)
35+
]
36+
return peer_parser
37+
38+
def execute(self, args, output):
39+
return self.process_sub_command(args, output, args.peer_command)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
3+
Tool command Peer Create
4+
5+
6+
"""
7+
import logging
8+
import secrets
9+
10+
from convex_api import KeyPair
11+
12+
from .command_base import CommandBase
13+
14+
logger = logging.getLogger(__name__)
15+
16+
17+
class PeerCreateCommand(CommandBase):
18+
19+
def __init__(self, sub_parser=None):
20+
super().__init__('create', sub_parser)
21+
22+
def create_parser(self, sub_parser):
23+
parser = sub_parser.add_parser(
24+
self._name,
25+
description='Create a new account',
26+
help='Create a new account'
27+
)
28+
29+
parser.add_argument(
30+
'--topup',
31+
action='store_true',
32+
help='Topup account with sufficient funds. This only works for development networks. Default: False',
33+
)
34+
35+
parser.add_argument(
36+
'-n',
37+
'--name',
38+
nargs='?',
39+
help='account name to register'
40+
)
41+
42+
return parser
43+
44+
def execute(self, args, output):
45+
convex = self.load_convex(args.url)
46+
47+
key_pair = self.import_key_pair(args)
48+
if key_pair is None:
49+
key_pair = KeyPair()
50+
51+
logger.debug('creating account')
52+
account = convex.create_account(key_pair)
53+
54+
if args.topup:
55+
logger.debug('auto topup of account balance')
56+
convex.topup_account(account)
57+
58+
if args.name:
59+
logger.debug(f'registering account name {args.name}')
60+
convex.topup_account(account)
61+
account = convex.register_account_name(args.name, account)
62+
if args.password:
63+
password = args.password
64+
else:
65+
password = secrets.token_hex(32)
66+
values = {
67+
'password': password,
68+
'address': account.address,
69+
'keyfile': key_pair.export_to_text(password),
70+
'keywords': key_pair.export_to_mnemonic,
71+
'balance': convex.get_balance(account)
72+
}
73+
if account.name:
74+
values['name'] = account.name
75+
output.set_values(values)
76+
output.add_line_values(values)

convex_api/tool/convex_tool.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from convex_api.tool.command.account_command import AccountCommand
1414
from convex_api.tool.command.command_base import DEFAULT_CONVEX_URL
15+
from convex_api.tool.command.peer_command import PeerCommand
1516
from convex_api.tool.command.query_command import QueryCommand
1617
from convex_api.tool.command.submit_command import SubmitCommand
1718
from convex_api.tool.output import Output
@@ -83,6 +84,7 @@ def convex_tool():
8384

8485
command_list = [
8586
AccountCommand(command_parser),
87+
PeerCommand(command_parser),
8688
QueryCommand(command_parser),
8789
SubmitCommand(command_parser)
8890
]

0 commit comments

Comments
 (0)