new.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. xmrwallet.ops.new: Monero wallet ops for the MMGen Suite
  12. """
  13. from ...color import red, pink
  14. from ...util import msg, ymsg, make_timestr
  15. from ...obj import TwComment
  16. from ...ui import keypress_confirm
  17. from ..rpc import MoneroWalletRPC
  18. from .spec import OpMixinSpec
  19. from .wallet import OpWallet
  20. class OpNew(OpMixinSpec, OpWallet):
  21. spec_id = 'newaddr_spec'
  22. spec_key = ((1, 'source'),)
  23. wallet_offline = True
  24. async def main(self, add_timestr=True):
  25. h = MoneroWalletRPC(self, self.source)
  26. h.open_wallet('Monero', refresh=False)
  27. desc = 'account' if self.account is None else 'address'
  28. label = TwComment(
  29. None if self.label == '' else
  30. '{a}{b}'.format(
  31. a = self.label or (f'new {desc}' if self.compat_call else f'xmrwallet new {desc}'),
  32. b = ' [{}]'.format(make_timestr()) if add_timestr else ''))
  33. wallet_data = h.get_wallet_data()
  34. if desc == 'address':
  35. h.print_acct_addrs(wallet_data, self.account)
  36. if keypress_confirm(
  37. self.cfg,
  38. '\nCreating new {a} for wallet {b}{c} with {d}\nOK?'.format(
  39. a = desc,
  40. b = red(str(self.source.idx)),
  41. c = '' if desc == 'account' else f', account {red("#"+str(self.account))}',
  42. d = 'label ' + pink('‘'+label+'’') if label else 'empty label')):
  43. if desc == 'address':
  44. h.create_new_addr(self.account, label=label)
  45. else:
  46. h.create_acct(label=label)
  47. wallet_data = h.get_wallet_data(print=desc=='account')
  48. if desc == 'address':
  49. h.print_acct_addrs(wallet_data, self.account)
  50. ret = True
  51. else:
  52. ymsg('\nOperation cancelled by user request')
  53. ret = False
  54. # wallet must be left open: otherwise the 'stop_wallet' RPC call used to stop the daemon will fail
  55. if self.cfg.no_stop_wallet_daemon:
  56. h.close_wallet('Monero')
  57. msg('')
  58. return ret