txview.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.txview: Monero wallet ops for the MMGen Suite
  12. """
  13. from pathlib import Path
  14. from ...util import die
  15. from ..file.tx import MoneroMMGenTX
  16. from . import OpBase
  17. class OpTxview(OpBase):
  18. view_method = 'get_info'
  19. opts = ('watch_only', 'autosign')
  20. hdr = ''
  21. col_hdr = ''
  22. footer = ''
  23. do_umount = False
  24. async def main(self, *, cols=None):
  25. self.mount_removable_device()
  26. if self.cfg.autosign:
  27. files = [f for f in self.asi.xmr_tx_dir.iterdir()
  28. if f.name.endswith('.'+MoneroMMGenTX.Submitted.ext)]
  29. else:
  30. files = self.uargs.infile
  31. txs = sorted(
  32. (MoneroMMGenTX.View(self.cfg, Path(fn)) for fn in files),
  33. # old TX files have no ‘submit_time’ field:
  34. key = lambda x: getattr(x.data, 'submit_time', None) or x.data.create_time
  35. )
  36. if self.cfg.autosign:
  37. self.asi.do_umount()
  38. addr_w = None if self.cfg.full_address or cols is None else cols - self.fixed_cols_w
  39. self.cfg._util.stdout_or_pager(
  40. (self.hdr if len(files) > 1 else '')
  41. + self.col_hdr
  42. + '\n'.join(getattr(tx, self.view_method)(addr_w=addr_w) for tx in txs)
  43. + self.footer
  44. )
  45. class OpTxlist(OpTxview):
  46. view_method = 'get_info_oneline'
  47. add_nl = True
  48. footer = '\n'
  49. fixed_cols_w = MoneroMMGenTX.Base.oneline_fixed_cols_w
  50. min_addr_w = 10
  51. @property
  52. def hdr(self):
  53. return ('SUBMITTED ' if self.cfg.autosign else '') + 'MONERO TRANSACTIONS\n'
  54. @property
  55. def col_hdr(self):
  56. return MoneroMMGenTX.View.oneline_fs.format(
  57. a = 'Network',
  58. b = 'Seed ID',
  59. c = 'Submitted' if self.cfg.autosign else 'Date',
  60. d = 'TxID',
  61. e = 'Type',
  62. f = 'Src',
  63. g = 'Dest',
  64. h = ' Amount',
  65. j = 'Dest Address',
  66. x = '',
  67. ) + '\n'
  68. async def main(self):
  69. if self.cfg.pager:
  70. cols = None
  71. else:
  72. from ...term import get_terminal_size
  73. cols = self.cfg.columns or get_terminal_size().width
  74. if cols < self.fixed_cols_w + self.min_addr_w:
  75. die(1, f'A terminal at least {self.fixed_cols_w + self.min_addr_w} columns wide is required '
  76. 'to display this output (or use --columns or --pager)')
  77. await super().main(cols=cols)