mmgen-wallet/mmgen-addrimport

127 lines
3.3 KiB
Text
Raw Normal View History

#!/usr/bin/env python
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C) 2013-2014 by philemon <mmgen-py@yandex.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
2014-02-07 00:33:13 +04:00
mmgen-addrimport: Import addresses into a bitcoind watching wallet.
"""
import sys
from mmgen.Opts import *
from mmgen.license import *
2014-04-09 22:45:23 +04:00
from mmgen.util import *
from mmgen.tx import connect_to_bitcoind,parse_addrs_file
help_data = {
'prog_name': sys.argv[0].split("/")[-1],
2014-02-07 00:33:13 +04:00
'desc': """Import addresses (both mmgen and non-mmgen) into a bitcoind
watching wallet""",
'usage':"[opts] [mmgen address file]",
'options': """
-h, --help Print this help message
-l, --addrlist f Import the non-mmgen Bitcoin addresses listed in file 'f'
-q, --quiet Suppress warnings
"""
}
2014-02-07 00:33:13 +04:00
short_opts = "hl:q"
long_opts = "help", "addrlist=", "quiet"
opts,cmd_args = process_opts(sys.argv,help_data,"".join(short_opts),long_opts)
2014-04-09 22:45:23 +04:00
if 'quiet' in opts: g.quiet = True
2014-02-07 00:33:13 +04:00
if len(cmd_args) != 1 and not 'addrlist' in opts:
msg("You must specify an mmgen address list (and/or non-mmgen addresses with the '--addrlist' option)")
sys.exit(1)
check_opts(opts,long_opts)
2014-02-07 00:33:13 +04:00
if cmd_args:
check_infile(cmd_args[0])
seed_id,addr_data = parse_addrs_file(cmd_args[0])
else:
seed_id,addr_data = "",[]
2014-02-07 00:33:13 +04:00
if 'addrlist' in opts:
2014-04-09 22:45:23 +04:00
l = get_lines_from_file(opts['addrlist'],"non-mmgen addresses",
remove_comments=True)
2014-02-07 00:33:13 +04:00
addr_data += [(None,i) for i in l]
2014-04-09 22:45:23 +04:00
from mmgen.bitcoin import verify_addr
qmsg_r("Validating addresses...")
for i in addr_data:
2014-04-09 22:45:23 +04:00
if not verify_addr(i[1],verbose=True):
2014-02-07 00:33:13 +04:00
msg("%s: invalid address" % i)
sys.exit(2)
2014-04-09 22:45:23 +04:00
qmsg("OK")
import mmgen.config as g
g.http_timeout = 3600
c = connect_to_bitcoind()
2014-04-09 22:45:23 +04:00
m = """
2014-02-07 00:33:13 +04:00
Importing addresses can take a long time (>30 min.) on a low-powered computer.
2014-04-09 22:45:23 +04:00
""".strip()
confirm_or_exit(m, "continue", expect="YES")
2014-02-07 00:33:13 +04:00
import threading
import time
err_flag = False
def import_address(addr,label):
try:
c.importaddress(addr,label)
except:
global err_flag
err_flag = True
2014-02-07 00:33:13 +04:00
w1 = len(str(len(addr_data))) * 2 + 2
w2 = len(str(max([i[0] for i in addr_data if i[0]]))) + 12
msg_fmt = "\r%s %-" + str(w1) + "s %-34s %-" + str(w2) + "s"
msg("Importing addresses")
for n,i in enumerate(addr_data):
2014-02-07 00:33:13 +04:00
if i[0]:
comment = " " + i[2] if len(i) == 3 else ""
label = "%s:%s%s" % (seed_id,i[0],comment)
else: label = "non-mmgen"
t = threading.Thread(target=import_address, args = (i[1],label))
2014-02-07 00:33:13 +04:00
t.daemon = True
t.start()
start = int(time.time())
while True:
if t.is_alive():
elapsed = int(time.time() - start)
msg_r(msg_fmt % (
secs_to_hms(elapsed),
("%s/%s:" % (n+1,len(addr_data))),
i[1], "(" + label + ")"
)
)
time.sleep(1)
2014-02-07 00:33:13 +04:00
else:
if err_flag:
msg("\nImport failed")
sys.exit(2)
msg("\nOK")
2014-02-07 00:33:13 +04:00
break