__init__.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2024 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. tx.__init__: transaction class initializer
  12. """
  13. from ..objmethods import MMGenObject
  14. def _base_proto_subclass(clsname,modname,proto):
  15. if proto:
  16. clsname = ('Token' if proto.tokensym else '') + clsname
  17. modname = f'mmgen.proto.{proto.base_proto_coin.lower()}.tx.{modname}'
  18. else:
  19. modname = 'mmgen.tx.base'
  20. import importlib
  21. return getattr( importlib.import_module(modname), clsname )
  22. def _get_cls_info(clsname, modname, kwargs):
  23. if 'proto' in kwargs:
  24. proto = kwargs['proto']
  25. elif 'data' in kwargs:
  26. proto = kwargs['data']['proto']
  27. elif 'filename' in kwargs:
  28. from .file import MMGenTxFile
  29. proto = MMGenTxFile.get_proto( kwargs['cfg'], kwargs['filename'], quiet_open=True )
  30. elif clsname == 'Base':
  31. proto = None
  32. else:
  33. raise ValueError(
  34. f"{clsname} must be instantiated with 'proto','data' or 'filename' keyword" )
  35. if clsname == 'Completed':
  36. from ..util import get_extension,die
  37. from .completed import Completed
  38. ext = get_extension( kwargs['filename'] )
  39. cls = Completed.ext_to_cls( ext, proto )
  40. if not cls:
  41. die(1,f'{ext!r}: unrecognized file extension for CompletedTX')
  42. clsname = cls.__name__
  43. modname = cls.__module__.rsplit('.',maxsplit=1)[-1]
  44. kwargs['proto'] = proto
  45. if 'automount' in kwargs:
  46. if kwargs['automount']:
  47. clsname = 'Automount' + clsname
  48. del kwargs['automount']
  49. return (clsname, modname, kwargs)
  50. def _get_obj(_clsname, _modname, **kwargs):
  51. """
  52. determine cls/mod/proto and pass them to _base_proto_subclass() to get a transaction instance
  53. """
  54. clsname, modname, kwargs = _get_cls_info(_clsname, _modname, kwargs)
  55. return _base_proto_subclass(clsname, modname, kwargs['proto'])(**kwargs)
  56. async def _get_obj_async(_clsname, _modname, **kwargs):
  57. clsname, modname, kwargs = _get_cls_info(_clsname, _modname, kwargs)
  58. proto = kwargs['proto']
  59. # NB: tracking wallet needed to retrieve the 'symbol' and 'decimals' parameters of token addr
  60. # (see twctl:import_token()).
  61. # No twctl required for the Unsigned and Signed(data=unsigned.__dict__) classes used during
  62. # signing.
  63. if proto and proto.tokensym and clsname in (
  64. 'New',
  65. 'OnlineSigned',
  66. 'AutomountOnlineSigned',
  67. 'Sent',
  68. 'AutomountSent'):
  69. from ..tw.ctl import TwCtl
  70. kwargs['twctl'] = await TwCtl(kwargs['cfg'], proto, no_rpc=True)
  71. return _base_proto_subclass(clsname, modname, proto)(**kwargs)
  72. def _get(clsname,modname):
  73. return lambda **kwargs: _get_obj(clsname, modname, **kwargs)
  74. def _get_async(clsname,modname):
  75. return lambda **kwargs: _get_obj_async(clsname, modname, **kwargs)
  76. BaseTX = _get('Base', 'base')
  77. UnsignedTX = _get('Unsigned', 'unsigned')
  78. NewTX = _get_async('New', 'new')
  79. CompletedTX = _get_async('Completed', 'completed')
  80. SignedTX = _get_async('Signed', 'signed')
  81. OnlineSignedTX = _get_async('OnlineSigned', 'online')
  82. SentTX = _get_async('Sent', 'online')
  83. BumpTX = _get_async('Bump', 'bump')