main_addrimport.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 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 .common import *
  23. from .addrlist import AddrList,KeyAddrList
  24. from .tw.common import TwLabel
  25. opts_data = {
  26. 'text': {
  27. 'desc': f'Import addresses into an {g.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 {g.proj_name} address file, a single address with
  69. the ‘--address’ option, or a flat list of non-{g.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(opt.keyaddr_file)](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 opt.addrlist:
  85. al = AddrList(
  86. proto = proto,
  87. addrlist = get_lines_from_file(
  88. infile,
  89. f'non-{g.proj_name} addresses',
  90. trim_comments = True ) )
  91. else:
  92. al = import_mmgen_list(infile)
  93. elif len(cmd_args) == 0 and opt.address:
  94. al = AddrList(proto=proto,addrlist=[opt.address])
  95. infile = 'command line'
  96. else:
  97. die(1,addrimport_msgs['bad_args'])
  98. return al,infile
  99. def check_opts(tw):
  100. batch = bool(opt.batch)
  101. rescan = bool(opt.rescan)
  102. if rescan and not 'rescan' in tw.caps:
  103. msg(f"‘--rescan’ ignored: not supported by {type(tw).__name__}")
  104. rescan = False
  105. if rescan and not opt.quiet:
  106. from .ui import keypress_confirm
  107. if not keypress_confirm(
  108. '\n{}\n\nContinue?'.format(addrimport_msgs['rescan']),
  109. default_yes = True ):
  110. die(1,'Exiting at user request')
  111. if batch and not 'batch' in tw.caps:
  112. msg(f"‘--batch’ ignored: not supported by {type(tw).__name__}")
  113. batch = False
  114. return batch,rescan
  115. async def main():
  116. from .tw.ctl import TrackingWallet
  117. if opt.token_addr:
  118. proto.tokensym = 'foo' # hack to trigger 'Token' in proto.base_proto_subclass()
  119. tw = await TrackingWallet(
  120. proto = proto,
  121. token_addr = opt.token_addr,
  122. mode = 'i' )
  123. if opt.token or opt.token_addr:
  124. msg(f'Importing for token {tw.token.hl()} ({tw.token.hlc(proto.tokensym)})')
  125. from .rpc import rpc_init
  126. tw.rpc = await rpc_init(proto)
  127. for k,v in addrimport_msgs.items():
  128. addrimport_msgs[k] = fmt(v,indent=' ',strip_char='\t').rstrip()
  129. al,infile = parse_cmd_args(tw.rpc,cmd_args)
  130. qmsg(
  131. f'OK. {al.num_addrs} addresses'
  132. + (f' from Seed ID {al.al_id.sid}' if hasattr(al.al_id,'sid') else '') )
  133. msg(
  134. f'Importing {len(al.data)} address{suf(al.data,"es")} from {infile}'
  135. + (' (batch mode)' if opt.batch else '') )
  136. batch,rescan = check_opts(tw)
  137. def gen_args_list(al):
  138. _d = namedtuple('import_data',['addr','twmmid','comment'])
  139. for num,e in enumerate(al.data,1):
  140. yield _d(
  141. addr = e.addr,
  142. twmmid = f'{al.al_id}:{e.idx}' if e.idx else f'{proto.base_coin.lower()}:{e.addr}',
  143. comment = e.comment )
  144. args_list = list(gen_args_list(al))
  145. await tw.import_address_common( args_list, batch=batch )
  146. if rescan:
  147. await tw.rescan_addresses({a.addr for a in args_list})
  148. del tw
  149. cmd_args = opts.init(opts_data)
  150. from .protocol import init_proto_from_opts
  151. proto = init_proto_from_opts()
  152. import asyncio
  153. async_run(main())