addrdata.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. addrdata: MMGen AddrData and related classes
  20. """
  21. from .cfg import gc
  22. from .util import fmt, die
  23. from .base_obj import AsyncInit
  24. from .obj import MMGenObject, MMGenDict, get_obj
  25. from .addr import MMGenID, AddrListID
  26. from .addrlist import AddrListEntry, AddrListData, AddrList
  27. class AddrData(MMGenObject):
  28. def __init__(self, proto, *args, **kwargs):
  29. self.al_ids = {}
  30. self.proto = proto
  31. self.rpc = None
  32. def seed_ids(self):
  33. return list(self.al_ids.keys())
  34. def addrlist(self, al_id):
  35. # TODO: Validate al_id
  36. if al_id in self.al_ids:
  37. return self.al_ids[al_id]
  38. def mmaddr2coinaddr(self, mmaddr):
  39. al_id, idx = MMGenID(self.proto, mmaddr).rsplit(':', 1)
  40. coinaddr = ''
  41. if al_id in self.al_ids:
  42. coinaddr = self.addrlist(al_id).coinaddr(int(idx))
  43. return coinaddr or None
  44. def coinaddr2mmaddr(self, coinaddr):
  45. d = self.make_reverse_dict([coinaddr])
  46. return (list(d.values())[0][0]) if d else None
  47. def add(self, addrlist):
  48. if isinstance(addrlist, AddrList):
  49. self.al_ids[addrlist.al_id] = addrlist
  50. return True
  51. else:
  52. raise TypeError(f'Error: object {addrlist!r} is not an instance of AddrList')
  53. def make_reverse_dict(self, coinaddrs):
  54. d = MMGenDict()
  55. for al_id in self.al_ids:
  56. d.update(self.al_ids[al_id].make_reverse_dict_addrlist(coinaddrs))
  57. return d
  58. class TwAddrData(AddrData, metaclass=AsyncInit):
  59. def __new__(cls, cfg, proto, *args, **kwargs):
  60. return MMGenObject.__new__(proto.base_proto_subclass(cls, 'addrdata'))
  61. async def __init__(self, cfg, proto, twctl=None):
  62. from .rpc import rpc_init
  63. from .tw.shared import TwLabel
  64. from .seed import SeedID
  65. self.cfg = cfg
  66. self.proto = proto
  67. self.rpc = await rpc_init(cfg, proto)
  68. self.al_ids = {}
  69. twd = await self.get_tw_data(twctl)
  70. out, i = {}, 0
  71. for acct, addr_array in twd:
  72. l = get_obj(TwLabel, proto=self.proto, text=acct, silent=True)
  73. if l and l.mmid.type == 'mmgen':
  74. obj = l.mmid.obj
  75. if len(addr_array) != 1:
  76. message = self.msgs['multiple_acct_addrs'].strip().format(acct=acct, proj=gc.proj_name)
  77. die(3, fmt(message, indent=' '))
  78. al_id = AddrListID(
  79. sid = SeedID(sid=obj.sid),
  80. mmtype = self.proto.addr_type(obj.mmtype))
  81. if al_id not in out:
  82. out[al_id] = []
  83. out[al_id].append(AddrListEntry(self.proto, idx=obj.idx, addr=addr_array[0], comment=l.comment))
  84. i += 1
  85. self.cfg._util.vmsg(f'{i} {gc.proj_name} addresses found, {len(twd)} accounts total')
  86. for al_id in out:
  87. self.add(AddrList(
  88. self.cfg,
  89. self.proto,
  90. al_id = al_id,
  91. adata = AddrListData(sorted(out[al_id], key=lambda a: a.idx))
  92. ))