From 62ddcececc2fa6092ca26fbeaf6a229fc52e0c12 Mon Sep 17 00:00:00 2001 From: philemon Date: Fri, 7 Feb 2014 00:33:13 +0400 Subject: [PATCH] mmgen-addrimport improvements --- mmgen-addrimport | 86 ++++++++++++++++++++++++++++++++++-------------- mmgen/bitcoin.py | 9 ----- mmgen/tx.py | 9 +++-- mmgen/utils.py | 3 ++ setup.py | 4 +-- 5 files changed, 74 insertions(+), 37 deletions(-) diff --git a/mmgen-addrimport b/mmgen-addrimport index d06c38e5..05364dec 100755 --- a/mmgen-addrimport +++ b/mmgen-addrimport @@ -17,56 +17,94 @@ # along with this program. If not, see . """ -mmgen-addrimport: Import addresses generated by mmgen-addrgen into an - online bitcoind watching wallet. +mmgen-addrimport: Import addresses into a bitcoind watching wallet. """ import sys from mmgen.Opts import * from mmgen.license import * -from mmgen.utils import check_infile,confirm_or_exit +from mmgen.utils import check_infile,confirm_or_exit,msg,msg_r,secs_to_hms,get_lines_from_file from mmgen.tx import connect_to_bitcoind,parse_addrs_file +from mmgen.bitcoin import verify_addr help_data = { 'prog_name': sys.argv[0].split("/")[-1], - 'desc': """Import addresses generated by mmgen-addrgen into an - online bitcoind watching wallet""", - 'usage':"[opts] [infile]", + '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 --q, --quiet Suppress warnings --v, --verbose Produce more verbose output +-h, --help Print this help message +-l, --addrlist f Import the non-mmgen Bitcoin addresses listed in file 'f' +-q, --quiet Suppress warnings """ } -short_opts = "hqv" -long_opts = "help", "quiet", "verbose" +short_opts = "hl:q" +long_opts = "help", "addrlist=", "quiet" opts,cmd_args = process_opts(sys.argv,help_data,"".join(short_opts),long_opts) -if len(cmd_args) != 1: usage(help_data) +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_infile(cmd_args[0]) +if cmd_args: + check_infile(cmd_args[0]) + seed_id,addr_data = parse_addrs_file(cmd_args[0]) +else: + seed_id,addr_data = "",[] -seed_id,addr_data = parse_addrs_file(cmd_args[0]) +if 'addrlist' in opts: + check_infile(opts['addrlist']) + l = get_lines_from_file(opts['addrlist'],"non-mmgen addresses") + addr_data += [(None,i) for i in l] + +msg_r("Validating addresses...") +for i in [i[1] for i in addr_data]: + if not verify_addr(i): + msg("%s: invalid address" % i) + sys.exit(2) +msg("OK") import mmgen.config mmgen.config.http_timeout = 3600 c = connect_to_bitcoind() -message = """ -Importing addresses can take a long time - up to 30 min. per address on a -low-powered computer such as a netbook. -""" -confirm_or_exit(message, "continue", expect="YES") +if not 'quiet' in opts: + message = """ +Importing addresses can take a long time (>30 min.) on a low-powered computer. + """ + confirm_or_exit(message, "continue", expect="YES") +import threading +import time + +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 = "\rImporting %-" + str(w1) + "s %-34s %-" + str(w2) + "s %s" for n,i in enumerate(addr_data): - comment = " " + i[2] if len(i) == 3 else "" - label = "%s:%s%s" % (seed_id,i[0],comment) - msg("Importing %-6s %-34s (%s)" % ( + 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=c.importaddress, args = (i[1],label)) + t.daemon = True + t.start() + + start = int(time.time()) + + while True: + if t.is_alive(): + elapsed = int(time.time() - start) + msg_r(msg_fmt % ( ("%s/%s:" % (n+1,len(addr_data))), - i[1], label) + i[1], "(" + label + ")", secs_to_hms(elapsed)) ) - c.importaddress(i[1],label) + else: + msg("") + break + + time.sleep(1) diff --git a/mmgen/bitcoin.py b/mmgen/bitcoin.py index eb2204b1..d9ab2ce2 100755 --- a/mmgen/bitcoin.py +++ b/mmgen/bitcoin.py @@ -72,15 +72,6 @@ def verify_addr(addr): print "%s: Invalid address" % addr return False -# addr,lz = addr[1:],0 -# while addr[0] == "1": addr = addr[1:]; lz += 1 -# -# addr_hex = lz * "00" + hex(_b58tonum(addr))[2:].rstrip("L") - -# if len(addr_hex) != 48: -# print "%s: Invalid address hex length: %s" % ("1"+addr, len(addr_hex)) -# return False - num = _b58tonum(addr[1:]) if num == False: return False addr_hex = hex(num)[2:].rstrip("L").zfill(48) diff --git a/mmgen/tx.py b/mmgen/tx.py index 308112f7..756d5f01 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -399,7 +399,12 @@ def parse_addrs_file(f): lines = get_lines_from_file(f,"address data") lines = remove_blanks_comments(lines) - seed_id,obrace = lines[0].split() + try: + seed_id,obrace = lines[0].split() + except: + msg("Invalid first line: '%s'" % lines[0]) + sys.exit(3) + cbrace = lines[-1] if obrace != '{': @@ -431,7 +436,7 @@ def parse_addrs_file(f): if len(d) == 3: check_wallet_addr_comment(d[2]) - ret.append(d) + ret.append(tuple(d)) return seed_id,ret diff --git a/mmgen/utils.py b/mmgen/utils.py index abd1ec2b..250345b6 100755 --- a/mmgen/utils.py +++ b/mmgen/utils.py @@ -521,6 +521,9 @@ def make_timestr(): tv = time.gmtime(time.time())[:6] return "{:04d}/{:02d}/{:02d} {:02d}:{:02d}:{:02d}".format(*tv) +def secs_to_hms(secs): + return "{:02d}:{:02d}:{:02d}".format(secs/3600, (secs/60) % 60, secs % 60) + def write_wallet_to_file(seed, passwd, key_id, salt, enc_seed, opts): seed_id = make_chksum_8(seed) diff --git a/setup.py b/setup.py index 9eb26469..fb86aaeb 100755 --- a/setup.py +++ b/setup.py @@ -47,11 +47,11 @@ setup( 'mmgen-walletgen', 'mmgen-txcreate', 'mmgen-txsign', - 'mmgen-txsend' + 'mmgen-txsend', + 'mmgen-pywallet' ])], scripts=[ 'scripts/bitcoind-walletunlock.py', - 'scripts/pywallet.py', 'scripts/deinstall.sh' ] )