Browse Source

mmgen-addrimport improvements

philemon 11 years ago
parent
commit
62ddcececc
5 changed files with 74 additions and 37 deletions
  1. 62 24
      mmgen-addrimport
  2. 0 9
      mmgen/bitcoin.py
  3. 7 2
      mmgen/tx.py
  4. 3 0
      mmgen/utils.py
  5. 2 2
      setup.py

+ 62 - 24
mmgen-addrimport

@@ -17,56 +17,94 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
 """
 """
-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
 import sys
 from mmgen.Opts   import *
 from mmgen.Opts   import *
 from mmgen.license 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.tx import connect_to_bitcoind,parse_addrs_file
+from mmgen.bitcoin import verify_addr
 
 
 help_data = {
 help_data = {
 	'prog_name': sys.argv[0].split("/")[-1],
 	'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': """
 	'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)
 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
 import mmgen.config
 mmgen.config.http_timeout = 3600
 mmgen.config.http_timeout = 3600
 
 
 c = connect_to_bitcoind()
 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):
 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))),
 				("%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)

+ 0 - 9
mmgen/bitcoin.py

@@ -72,15 +72,6 @@ def verify_addr(addr):
 		print "%s: Invalid address" % addr
 		print "%s: Invalid address" % addr
 		return False
 		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:])
   	num = _b58tonum(addr[1:])
 	if num == False: return False
 	if num == False: return False
   	addr_hex = hex(num)[2:].rstrip("L").zfill(48)
   	addr_hex = hex(num)[2:].rstrip("L").zfill(48)

+ 7 - 2
mmgen/tx.py

@@ -399,7 +399,12 @@ def parse_addrs_file(f):
 	lines = get_lines_from_file(f,"address data")
 	lines = get_lines_from_file(f,"address data")
 	lines = remove_blanks_comments(lines)
 	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]
 	cbrace = lines[-1]
 
 
 	if   obrace != '{':
 	if   obrace != '{':
@@ -431,7 +436,7 @@ def parse_addrs_file(f):
 
 
 			if len(d) == 3: check_wallet_addr_comment(d[2])
 			if len(d) == 3: check_wallet_addr_comment(d[2])
 
 
-			ret.append(d)
+			ret.append(tuple(d))
 
 
 		return seed_id,ret
 		return seed_id,ret
 
 

+ 3 - 0
mmgen/utils.py

@@ -521,6 +521,9 @@ def make_timestr():
 	tv = time.gmtime(time.time())[:6]
 	tv = time.gmtime(time.time())[:6]
 	return "{:04d}/{:02d}/{:02d} {:02d}:{:02d}:{:02d}".format(*tv)
 	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):
 def write_wallet_to_file(seed, passwd, key_id, salt, enc_seed, opts):
 
 
 	seed_id = make_chksum_8(seed)
 	seed_id = make_chksum_8(seed)

+ 2 - 2
setup.py

@@ -47,11 +47,11 @@ setup(
 			'mmgen-walletgen',
 			'mmgen-walletgen',
 			'mmgen-txcreate',
 			'mmgen-txcreate',
 			'mmgen-txsign',
 			'mmgen-txsign',
-			'mmgen-txsend'
+			'mmgen-txsend',
+			'mmgen-pywallet'
 		])],
 		])],
 		scripts=[
 		scripts=[
 			'scripts/bitcoind-walletunlock.py',
 			'scripts/bitcoind-walletunlock.py',
-			'scripts/pywallet.py',
 			'scripts/deinstall.sh'
 			'scripts/deinstall.sh'
 		]
 		]
 	)
 	)