mmgen-addrimport 4.3 KB

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