main_addrimport.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2014 Philemon <mmgen-py@yandex.com>
  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 bitcoind watching wallet
  20. """
  21. import sys
  22. from mmgen.Opts import *
  23. from mmgen.license import *
  24. from mmgen.util import *
  25. from mmgen.tx import connect_to_bitcoind,parse_addrfile,parse_keyaddr_file
  26. help_data = {
  27. 'prog_name': g.prog_name,
  28. 'desc': """Import addresses (both {pnm} and non-{pnm}) into a bitcoind
  29. watching wallet""".format(pnm=g.proj_name),
  30. 'usage':"[opts] [mmgen address file]",
  31. 'options': """
  32. -h, --help Print this help message
  33. -l, --addrlist Address source is a flat list of addresses
  34. -k, --keyaddr-file Address source is a key-address file
  35. -q, --quiet Suppress warnings
  36. -r, --rescan Rescan the blockchain. Required if address to import is
  37. on the blockchain and has a balance. Rescanning is slow.
  38. """
  39. }
  40. opts,cmd_args = parse_opts(sys.argv,help_data)
  41. if len(cmd_args) == 1:
  42. infile = cmd_args[0]
  43. check_infile(infile)
  44. if 'addrlist' in opts:
  45. lines = get_lines_from_file(infile,"non-{} addresses".format(g.proj_name),
  46. trim_comments=True)
  47. addr_list = [(None,l) for l in lines]
  48. seed_id = ""
  49. else:
  50. addr_data = {}
  51. pf = parse_keyaddr_file if 'keyaddr_file' in opts else parse_addrfile
  52. pf(infile,addr_data)
  53. seed_id = addr_data.keys()[0]
  54. e = addr_data[seed_id]
  55. addr_list = [(k,e[k][0],e[k][1]) for k in e.keys()]
  56. else:
  57. msg_r("You must specify an mmgen address list (or a list of ")
  58. msg("non-%s addresses with\nthe '--addrlist' option)" % g.proj_name)
  59. sys.exit(1)
  60. from mmgen.bitcoin import verify_addr
  61. qmsg_r("Validating addresses...")
  62. for n,i in enumerate(addr_list,1):
  63. if not verify_addr(i[1],verbose=True):
  64. msg("%s: invalid address" % i)
  65. sys.exit(2)
  66. qmsg("OK. %s addresses%s" % (n," from seed ID "+seed_id if seed_id else ""))
  67. import mmgen.config as g
  68. g.http_timeout = 3600
  69. c = connect_to_bitcoind()
  70. m = """
  71. WARNING: You've chosen the '--rescan' option. Rescanning the block chain is
  72. necessary only if an address you're importing is already on the block chain
  73. and has a balance. Note that the rescanning process is very slow (>30 min.
  74. for each imported address on a low-powered computer).
  75. """.strip() if "rescan" in opts else """
  76. WARNING: If any of the addresses you're importing is already on the block chain
  77. and has a balance, you must exit the program now and rerun it using the
  78. '--rescan' option. Otherwise you may ignore this message and continue.
  79. """.strip()
  80. if g.quiet: m = ""
  81. confirm_or_exit(m, "continue", expect="YES")
  82. err_flag = False
  83. def import_address(addr,label,rescan):
  84. try:
  85. c.importaddress(addr,label,rescan)
  86. except:
  87. global err_flag
  88. err_flag = True
  89. w1 = len(str(len(addr_list))) * 2 + 2
  90. w2 = "" if 'addrlist' in opts else \
  91. len(str(max([i[0] for i in addr_list if i[0]]))) + 12 \
  92. if "rescan" in opts:
  93. import threading
  94. import time
  95. msg_fmt = "\r%s %-" + str(w1) + "s %-34s %-" + str(w2) + "s"
  96. else:
  97. msg_fmt = "\r%-" + str(w1) + "s %-34s %-" + str(w2) + "s"
  98. msg("Importing addresses")
  99. for n,i in enumerate(addr_list):
  100. if i[0]:
  101. label = "%s:%s%s" % (seed_id,i[0], (" "+i[2] if i[2] else ""))
  102. else: label = "non-mmgen"
  103. if "rescan" in opts:
  104. t = threading.Thread(target=import_address, args=(i[1],label,True))
  105. t.daemon = True
  106. t.start()
  107. start = int(time.time())
  108. while True:
  109. if t.is_alive():
  110. elapsed = int(time.time() - start)
  111. msg_r(msg_fmt % (
  112. secs_to_hms(elapsed),
  113. ("%s/%s:" % (n+1,len(addr_list))),
  114. i[1], "(" + label + ")"
  115. )
  116. )
  117. time.sleep(1)
  118. else:
  119. if err_flag: msg("\nImport failed"); sys.exit(2)
  120. msg("\nOK")
  121. break
  122. else:
  123. import_address(i[1],label,rescan=False)
  124. msg_r(msg_fmt % (("%s/%s:" % (n+1,len(addr_list))),
  125. i[1], "(" + label + ")"))
  126. if err_flag: msg("\nImport failed"); sys.exit(2)
  127. msg(" - OK")