|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +
|
| 5 | + Script to provide convex wallet functionality |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import argparse |
| 10 | +import json |
| 11 | +import logging |
| 12 | +import secrets |
| 13 | + |
| 14 | +from convex_api import Account as ConvexAccount |
| 15 | +from convex_api import ConvexAPI |
| 16 | + |
| 17 | +DEFAULT_URL = 'https://convex.world' |
| 18 | + |
| 19 | +logger = logging.getLogger('convex_wallet') |
| 20 | + |
| 21 | + |
| 22 | +def auto_topup_account(convex, account, min_balance=None): |
| 23 | + if isinstance(account, (list, tuple)): |
| 24 | + for account_item in account: |
| 25 | + auto_topup_account(convex, account_item, min_balance) |
| 26 | + return |
| 27 | + amount = 10000000 |
| 28 | + retry_counter = 100 |
| 29 | + if min_balance is None: |
| 30 | + min_balance = amount |
| 31 | + balance = convex.get_balance(account) |
| 32 | + while balance < min_balance and retry_counter > 0: |
| 33 | + convex.request_funds(amount, account) |
| 34 | + balance = convex.get_balance(account) |
| 35 | + retry_counter -= 1 |
| 36 | + |
| 37 | + |
| 38 | +def main(): |
| 39 | + |
| 40 | + parser = argparse.ArgumentParser(description='Convex Wallet') |
| 41 | + |
| 42 | + parser.add_argument( |
| 43 | + '-a', |
| 44 | + '--auto-topup', |
| 45 | + action='store_true', |
| 46 | + help='Auto topup account with sufficient funds. This only works for development networks. Default: False', |
| 47 | + ) |
| 48 | + |
| 49 | + parser.add_argument( |
| 50 | + '-d', |
| 51 | + '--debug', |
| 52 | + action='store_true', |
| 53 | + help='Debug mode on or off. Default: False', |
| 54 | + ) |
| 55 | + |
| 56 | + parser.add_argument( |
| 57 | + '-k', |
| 58 | + '--keyfile', |
| 59 | + help='account key file' |
| 60 | + ) |
| 61 | + |
| 62 | + parser.add_argument( |
| 63 | + '-p', |
| 64 | + '--password', |
| 65 | + help='password to access the account' |
| 66 | + ) |
| 67 | + |
| 68 | + parser.add_argument( |
| 69 | + '-w', |
| 70 | + '--keywords', |
| 71 | + help='account private key as words' |
| 72 | + ) |
| 73 | + |
| 74 | + parser.add_argument( |
| 75 | + '-u', |
| 76 | + '--url', |
| 77 | + default=DEFAULT_URL, |
| 78 | + help=f'URL of the network node. Default: {DEFAULT_URL}', |
| 79 | + ) |
| 80 | + |
| 81 | + parser.add_argument( |
| 82 | + 'command', |
| 83 | + help='Wallet command' |
| 84 | + ) |
| 85 | + |
| 86 | + args = parser.parse_args() |
| 87 | + |
| 88 | + if args.debug: |
| 89 | + logging.basicConfig(level=logging.DEBUG) |
| 90 | + logging.getLogger('urllib3').setLevel(logging.INFO) |
| 91 | + |
| 92 | + if args.command == 'create' or args.command == 'new': |
| 93 | + account = ConvexAccount.create_new() |
| 94 | + convex = ConvexAPI(args.url) |
| 95 | + if not convex: |
| 96 | + print(f'Cannot connect to the convex network at {args.url}') |
| 97 | + return |
| 98 | + |
| 99 | + if args.auto_topup: |
| 100 | + logger.debug('auto topup of account balance') |
| 101 | + auto_topup_account(convex, account) |
| 102 | + |
| 103 | + values = {} |
| 104 | + if args.password: |
| 105 | + password = args.password |
| 106 | + else: |
| 107 | + password = secrets.token_hex(32) |
| 108 | + |
| 109 | + values = { |
| 110 | + 'password': password, |
| 111 | + 'address': account.address_checksum, |
| 112 | + 'keyfile': account.export_to_text(password), |
| 113 | + 'keywords': account.export_to_mnemonic, |
| 114 | + 'balance': convex.get_balance(account) |
| 115 | + } |
| 116 | + print(json.dumps(values, sort_keys=True, indent=4)) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments