json.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2023 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
  9. # https://gitlab.com/mmgen/mmgen
  10. """
  11. proto.btc.tw.json: export and import tracking wallet to JSON format
  12. """
  13. from collections import namedtuple
  14. from ....tw.json import TwJSON
  15. from ....tw.shared import TwMMGenID
  16. class BitcoinTwJSON(TwJSON):
  17. class Base(TwJSON.Base):
  18. can_prune = True
  19. @property
  20. def mappings_json(self):
  21. return self.json_dump([(e.mmgen_id,e.address) for e in self.entries])
  22. @property
  23. def num_entries(self):
  24. return len(self.entries)
  25. class Import(TwJSON.Import,Base):
  26. info_msg = """
  27. This utility will create a new tracking wallet, import the addresses from
  28. the JSON dump into it and update their balances. The operation may take a
  29. few minutes.
  30. """
  31. blockchain_rescan_warning = """
  32. Balances have been updated in the new tracking wallet. However, the wallet
  33. is unaware of the used state of any spent addresses without balances, which
  34. creates the danger of address reuse, especially when automatic change address
  35. selection is in effect.
  36. To avoid this danger and restore full tracking wallet functionality, rescan
  37. the blockchain for used addresses by running ‘mmgen-tool rescan_blockchain’.
  38. """
  39. @property
  40. async def tracking_wallet_exists(self):
  41. return await self.twctl.rpc.tracking_wallet_exists
  42. async def create_tracking_wallet(self):
  43. try:
  44. await self.twctl.rpc.check_or_create_daemon_wallet()
  45. return True
  46. except:
  47. return False
  48. async def get_entries(self):
  49. entries_in = [self.entry_tuple_in(*e) for e in self.data['data']['entries']]
  50. return sorted(
  51. [self.entry_tuple(
  52. TwMMGenID(self.proto,d.mmgen_id),
  53. d.address,
  54. getattr(d,'amount',None),
  55. d.comment)
  56. for d in entries_in],
  57. key = lambda x: x.mmgen_id.sort_key )
  58. async def do_import(self,batch):
  59. import_tuple = namedtuple('import_data',['addr','twmmid','comment'])
  60. await self.twctl.import_address_common(
  61. [import_tuple(e.address, e.mmgen_id, e.comment) for e in self.entries],
  62. batch = batch )
  63. return [e.address for e in self.entries]
  64. class Export(TwJSON.Export,Base):
  65. @property
  66. async def addrlist(self):
  67. if not hasattr(self,'_addrlist'):
  68. if self.prune:
  69. from .prune import TwAddressesPrune
  70. self._addrlist = al = await TwAddressesPrune(
  71. self.cfg,
  72. self.proto,
  73. get_data = True,
  74. warn_used = self.warn_used )
  75. await al.view_filter_and_sort()
  76. self.pruned = al.do_prune()
  77. else:
  78. from .addresses import TwAddresses
  79. self._addrlist = await TwAddresses(self.cfg,self.proto,get_data=True)
  80. return self._addrlist
  81. async def get_entries(self): # TODO: include 'received' field
  82. return sorted(
  83. [self.entry_tuple(d.twmmid, d.addr, d.amt, d.comment)
  84. for d in (await self.addrlist).data],
  85. key = lambda x: x.mmgen_id.sort_key )
  86. @property
  87. async def entries_out(self):
  88. return [[getattr(d,k) for k in self.keys] for d in self.entries]
  89. @property
  90. async def total(self):
  91. return (await self.addrlist).total