main_addrimport.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  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. mmgen-addrimport: Import addresses into a MMGen coin daemon tracking wallet
  20. """
  21. from collections import namedtuple
  22. from .cfg import gc,Config
  23. from .util import msg,suf,die,fmt,async_run
  24. from .addrlist import AddrList,KeyAddrList
  25. opts_data = {
  26. 'text': {
  27. 'desc': f'Import addresses into an {gc.proj_name} tracking wallet',
  28. 'usage':'[opts] [MMGen address file]',
  29. 'options': """
  30. -h, --help Print this help message
  31. --, --longhelp Print help message for long options (common options)
  32. -a, --address=a Import the single coin address 'a'
  33. -b, --batch Import all addresses in one RPC call
  34. -l, --addrlist Address source is a flat list of non-MMGen coin addresses
  35. -k, --keyaddr-file Address source is a key-address file
  36. -q, --quiet Suppress warnings
  37. -r, --rescan Update address balances by selectively rescanning the
  38. blockchain for unspent outputs that include the imported
  39. address(es). Required if any of the imported addresses
  40. are already in the blockchain and have a balance.
  41. -t, --token-addr=A Import addresses for ERC20 token with address 'A'
  42. """,
  43. 'notes': """
  44. This command can also be used to update the comment fields or balances of
  45. addresses already in the tracking wallet.
  46. Rescanning now uses the ‘scantxoutset’ RPC call and a selective scan of
  47. blocks containing the relevant UTXOs for much faster performance than the
  48. previous implementation. The rescan operation typically takes around two
  49. minutes total, independent of the number of addresses imported.
  50. Bear in mind that the UTXO scan will not find historical transactions: to add
  51. them to the tracking wallet, you must perform a full or partial rescan of the
  52. blockchain with the ‘mmgen-tool rescan_blockchain’ utility. A full rescan of
  53. the blockchain may take up to several hours.
  54. It’s recommended to use ‘--rpc-backend=aio’ with ‘--rescan’.
  55. """
  56. }
  57. }
  58. addrimport_msgs = {
  59. 'rescan': """
  60. WARNING: You’ve chosen the ‘--rescan’ option. Rescanning the blockchain is
  61. necessary only if an address you’re importing is already in an output or
  62. outputs in the blockchain but not all transactions involving the address
  63. are known to the tracking wallet.
  64. Rescanning is performed via the UTXO method, which is only minimally affected
  65. by the number of addresses imported and typically takes just a few minutes.
  66. """,
  67. 'bad_args': f"""
  68. You must specify either an {gc.proj_name} address file, a single address with
  69. the ‘--address’ option, or a flat list of non-{gc.proj_name} addresses with
  70. the ‘--addrlist’ option.
  71. """
  72. }
  73. def parse_cmd_args(rpc,cmd_args):
  74. def import_mmgen_list(infile):
  75. al = (AddrList,KeyAddrList)[bool(cfg.keyaddr_file)](cfg,proto,infile)
  76. if al.al_id.mmtype in ('S','B'):
  77. if not rpc.info('segwit_is_active'):
  78. die(2,'Segwit is not active on this chain. Cannot import Segwit addresses')
  79. return al
  80. if len(cmd_args) == 1:
  81. infile = cmd_args[0]
  82. from .fileutil import check_infile,get_lines_from_file
  83. check_infile(infile)
  84. if cfg.addrlist:
  85. al = AddrList(
  86. cfg = cfg,
  87. proto = proto,
  88. addrlist = get_lines_from_file(
  89. cfg,
  90. infile,
  91. f'non-{gc.proj_name} addresses',
  92. trim_comments = True ) )
  93. else:
  94. al = import_mmgen_list(infile)
  95. elif len(cmd_args) == 0 and cfg.address:
  96. al = AddrList( cfg, proto=proto, addrlist=[cfg.address] )
  97. infile = 'command line'
  98. else:
  99. die(1,addrimport_msgs['bad_args'])
  100. return al,infile
  101. def check_opts(twctl):
  102. batch = bool(cfg.batch)
  103. rescan = bool(cfg.rescan)
  104. if rescan and not 'rescan' in twctl.caps:
  105. msg(f"‘--rescan’ ignored: not supported by {type(twctl).__name__}")
  106. rescan = False
  107. if rescan and not cfg.quiet:
  108. from .ui import keypress_confirm
  109. if not keypress_confirm(
  110. cfg,
  111. f'\n{addrimport_msgs["rescan"]}\n\nContinue?',
  112. default_yes = True ):
  113. die(1,'Exiting at user request')
  114. if batch and not 'batch' in twctl.caps:
  115. msg(f"‘--batch’ ignored: not supported by {type(twctl).__name__}")
  116. batch = False
  117. return batch,rescan
  118. async def main():
  119. from .tw.ctl import TwCtl
  120. if cfg.token_addr:
  121. proto.tokensym = 'foo' # hack to trigger 'Token' in proto.base_proto_subclass()
  122. twctl = await TwCtl(
  123. cfg = cfg,
  124. proto = proto,
  125. token_addr = cfg.token_addr,
  126. mode = 'i' )
  127. if cfg.token or cfg.token_addr:
  128. msg(f'Importing for token {twctl.token.hl()} ({twctl.token.hlc(proto.tokensym)})')
  129. for k,v in addrimport_msgs.items():
  130. addrimport_msgs[k] = fmt(v,indent=' ',strip_char='\t').rstrip()
  131. al, infile = parse_cmd_args(twctl.rpc, cfg._args)
  132. cfg._util.qmsg(
  133. f'OK. {al.num_addrs} addresses'
  134. + (f' from Seed ID {al.al_id.sid.hl()}' if hasattr(al.al_id,'sid') else '') )
  135. msg(
  136. f'Importing {len(al.data)} address{suf(al.data,"es")} from {infile}'
  137. + (' (batch mode)' if cfg.batch else '') )
  138. batch,rescan = check_opts(twctl)
  139. def gen_args_list(al):
  140. _d = namedtuple('import_data',['addr','twmmid','comment'])
  141. for e in al.data:
  142. yield _d(
  143. addr = e.addr,
  144. twmmid = f'{al.al_id}:{e.idx}' if e.idx else f'{proto.base_coin.lower()}:{e.addr}',
  145. comment = e.comment )
  146. args_list = list(gen_args_list(al))
  147. await twctl.import_address_common( args_list, batch=batch )
  148. if rescan:
  149. await twctl.rescan_addresses({a.addr for a in args_list})
  150. del twctl
  151. cfg = Config( opts_data=opts_data, need_amt=False )
  152. proto = cfg._proto
  153. async_run(main())