view.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. proto.xmr.tw.view: Monero protocol base class for tracking wallet view classes
  12. """
  13. from ....xmrwallet import op as xmrwallet_op
  14. from ....seed import SeedID
  15. from ....tw.view import TwView
  16. class MoneroTwView:
  17. class rpc:
  18. caps = ()
  19. is_remote = False
  20. async def get_rpc_data(self):
  21. from mmgen.tw.shared import TwMMGenID, TwLabel
  22. op = xmrwallet_op('dump_data', self.cfg, None, None, compat_call=True)
  23. await op.restart_wallet_daemon()
  24. wallets_data = await op.main()
  25. if wallets_data:
  26. self.sid = SeedID(sid=wallets_data[0]['seed_id'])
  27. self.total = self.proto.coin_amt('0')
  28. def gen_addrs():
  29. for wdata in wallets_data:
  30. bals_data = {i: {} for i in range(len(wdata['data'].accts_data['subaddress_accounts']))}
  31. for d in wdata['data'].bals_data.get('per_subaddress', []):
  32. bals_data[d['account_index']].update({d['address_index']: d['unlocked_balance']})
  33. for acct_idx, acct_data in enumerate(wdata['data'].addrs_data):
  34. for addr_data in acct_data['addresses']:
  35. addr_idx = addr_data['address_index']
  36. self.total += (bal := self.proto.coin_amt(
  37. bals_data[acct_idx].get(addr_idx, 0),
  38. from_unit = 'atomic'))
  39. if self.include_empty or bal:
  40. mmid = '{}:M:{}-{}/{}'.format(
  41. wdata['seed_id'],
  42. wdata['wallet_num'],
  43. acct_idx,
  44. addr_idx)
  45. yield (TwMMGenID(self.proto, mmid), {
  46. 'addr': addr_data['address'],
  47. 'amt': bal,
  48. 'recvd': bal,
  49. 'is_used': addr_data['used'],
  50. 'confs': 1,
  51. 'lbl': TwLabel(self.proto, mmid + ' ' + addr_data['label'])})
  52. return dict(gen_addrs())
  53. class action(TwView.action):
  54. async def a_sync_wallets(self, parent):
  55. from ....util import msg, msg_r
  56. from ....tw.view import CUR_HOME, ERASE_ALL
  57. msg('')
  58. op = xmrwallet_op('sync', parent.cfg, None, None, compat_call=True)
  59. await op.restart_wallet_daemon()
  60. await op.main()
  61. await parent.get_data()
  62. msg_r(CUR_HOME + ERASE_ALL)