From afa3fe609a90abb33c68dfa5d9ab604785bdc8ca Mon Sep 17 00:00:00 2001 From: philemon Date: Sat, 15 Mar 2014 23:03:24 +0400 Subject: [PATCH] Code cleanups --- mmgen-addrgen | 8 +---- mmgen-txsign | 75 ++++++++++++++++++-------------------------- mmgen-walletgen | 9 ++---- mmgen/addr.py | 4 +++ mmgen/tx.py | 59 +++++++++++++++++++++------------- mmgen/utils.py | 13 +++++++- scripts/deinstall.sh | 6 +++- 7 files changed, 92 insertions(+), 82 deletions(-) diff --git a/mmgen-addrgen b/mmgen-addrgen index 15a6cd54..e88eb62a 100755 --- a/mmgen-addrgen +++ b/mmgen-addrgen @@ -163,13 +163,7 @@ if invoked_as == "addrgen": else: if not 'no_addresses' in opts: opts['print_secret'] = True -# Repeat on incorrect pw entry -silent = False -while True: - seed = get_seed(infile,opts,silent=silent) - silent = True - if seed: break - +seed = get_seed_retry(infile,opts) seed_id = make_chksum_8(seed) addr_data = generate_addrs(seed, addr_list, opts) addr_data_str = format_addr_data(addr_data, seed_id, opts) diff --git a/mmgen-txsign b/mmgen-txsign index 49bf1485..eda1bfe6 100755 --- a/mmgen-txsign +++ b/mmgen-txsign @@ -74,25 +74,20 @@ short_opts = "hd:efik:qb:ms" long_opts = "help","outdir=","echo_passphrase","force_wallet_dat","info",\ "keys_from_file=","quiet","from_brain=","from_mnemonic","from_seed" -opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts) +opts,infiles = process_opts(sys.argv,help_data,short_opts,long_opts) # Exits on invalid input check_opts(opts, ('outdir','from_brain')) if 'keys_from_file' in opts: check_infile(opts['keys_from_file']) -if debug: - print "Processed options: %s" % repr(opts) - print "Cmd args: %s" % repr(cmd_args) - -if len(cmd_args) > 0: - for i in cmd_args: check_infile(i) -else: usage(help_data) +if not infiles: usage(help_data) +for i in infiles: check_infile(i) # Begin execution c = connect_to_bitcoind() -tx_file = cmd_args.pop(0) +tx_file = infiles.pop(0) tx_data = get_lines_from_file(tx_file,"transaction data") metadata,tx_hex,sig_data,inputs_data = parse_tx_data(tx_data,tx_file) @@ -120,60 +115,50 @@ for i in inputs_data: if mmgen_addrs and not 'force_wallet_dat' in opts: # Check that all the seed IDs are the same: seed_ids = list(set([i['account'][:8] for i in mmgen_addrs])) + ext_data = ( + (wallet_ext, {}), + (mn_ext, {"from_mnemonic":True}), + (seed_ext, {"from_seed": True}), + (brain_ext, opts) + ) while seed_ids: infile = False - if cmd_args: - infile = cmd_args.pop() + if infiles: + infile = infiles.pop() ext = infile.split(".")[-1] - found = False - for e,o in ( - (wallet_ext, {}), - (mn_ext, {"from_mnemonic":True}), - (seed_ext, {"from_seed": True}), - (brain_ext, {}) - ): + for e,o in ext_data: if e == ext: - found = True if e == brain_ext: - if "from_brain" in opts: o = opts - else: - msg("'--from-brain' option must be specified for brainwallet file") + if "from_brain" not in opts: + msg( + "'--from-brain' option must be specified for brainwallet file") sys.exit(2) - silent = False - while True: - seed = get_seed(infile,o,silent=silent) - silent = True - if seed: break - - if not found: - msg("%s: invalid file extension" % ext) + seed = get_seed_retry(infile,o); break + else: + msg("Invalid file extension: '.%s'\nValid extensions: '.%s'" % + (ext,"' '.".join([i[0] for i in ext_data]))) sys.exit(2) elif "from_brain" in opts or "from_mnemonic" in opts or "from_seed" in opts: msg("Need data for seed ID %s" % seed_ids[0]) - silent = False - while True: - seed = get_seed("",opts,silent=silent) - silent = True - if seed: break + seed = get_seed_retry("",opts) else: - msg("One of '-b', '-m' or '-s' must be specified for seed IDs: %s" % - " ".join(seed_ids)) + b,p,v = ("A seed","","is") if len(seed_ids) == 1 else ("Seed","s","are") + msg("ERROR: %s source%s %s required for the following seed ID%s: %s" % + (b,p,v,p," ".join(seed_ids))) sys.exit(2) seed_id = make_chksum_8(seed) if seed_id in seed_ids: seed_ids.remove(seed_id) - seed_id_addrs = [] - for i in mmgen_addrs: - if i['account'][:8] == seed_id: - seed_id_addrs.append(int(i['account'].split()[0][9:])) + seed_id_addrs = [ + int(i['account'].split()[0][9:]) for i in mmgen_addrs + if i['account'][:8] == seed_id] - from mmgen.addr import generate_addrs - o = {'no_addresses': True, 'gen_what': "keys"} - keys += [i['wif'] for i in generate_addrs(seed, seed_id_addrs, o)] + from mmgen.addr import generate_keys + keys += [i['wif'] for i in generate_keys(seed, seed_id_addrs)] else: - msg("Supplied seed ID (%s) is incorrect" % seed_id) + msg("Seed source produced an invalid seed ID (%s)" % seed_id) if infile: msg("Invalid input file: %s" % infile) sys.exit(2) diff --git a/mmgen-walletgen b/mmgen-walletgen index 7e491cad..8e9ceb17 100755 --- a/mmgen-walletgen +++ b/mmgen-walletgen @@ -151,12 +151,9 @@ if debug: display_user_random_data(usr_keys,key_timings) usr_rand_data = sha256(usr_keys).digest() + \ sha256("".join(key_timings)).digest() -if 'from_mnemonic' in opts or 'from_brain' in opts or 'from_seed' in opts: - silent = False - while True: - seed = get_seed(infile,opts,silent=silent) - silent = True - if seed: break +for i in 'from_mnemonic','from_brain','from_seed': + if i in opts: + seed = get_seed_retry(infile,opts); break else: # Truncate random data for smaller seed lengths seed = os_rand_data[0] + usr_rand_data diff --git a/mmgen/addr.py b/mmgen/addr.py index 508ad126..fd7518a8 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -116,6 +116,10 @@ def generate_addrs(seed, addrnums, opts): return out +def generate_keys(seed, addrnums): + o = {'no_addresses': True, 'gen_what': "keys"} + return generate_addrs(seed, addrnums, o) + def format_addr_data(addrlist, seed_chksum, opts): """ diff --git a/mmgen/tx.py b/mmgen/tx.py index 9f36a8ac..ac160110 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -162,9 +162,9 @@ def sort_and_view(unspent): def s_age(a,b): return cmp(b.confirmations,a.confirmations) def s_mmgen(a,b): return cmp(a.account,b.account) - fs = " %-4s %-11s %-2s %-34s %13s %-s" - fs_hdr = " %-4s %-11s %-4s %-35s %-9s %-s" - sort,group,mmaddr,reverse = "",False,False,False + fs = " %-4s %-11s %-2s %-34s %-13s %-s" + sort,group,show_mmaddr,reverse = "",False,False,False + total = trim_exponent(sum([i.amount for i in unspent])) from copy import deepcopy msg("") @@ -179,32 +179,35 @@ def sort_and_view(unspent): elif sort == "txid" and a.txid == b.txid: out[n+1].skip = "t" - output = [] - output.append("UNSPENT OUTPUTS (sort order: %s%s%s)" % ( + output = ["UNSPENT OUTPUTS (sort order: %s%s%s) Total BTC: %s" % ( "reverse " if reverse else "", sort if sort else "None", - " (grouped)" if group and (sort == "address" or sort == "txid") else "" - )) - output.append(fs_hdr % ("Num","TX id","Vout","Address","Amount", + " (grouped)" if group and (sort == "address" or sort == "txid") else "", + total + )] + output.append(fs % ("Num","TX id Vout","","Address","Amount (BTC)", "Age (days)")) - for n,i in enumerate(out): + for i in out: amt = str(trim_exponent(i.amount)) - fill = 8 - len(amt.split(".")[-1]) if "." in amt else 9 + lfill = 3 - len(amt.split(".")[0]) if "." in amt else 3 - len(amt) + i.amt = " "*lfill + amt + i.days = int(i.confirmations * mins_per_block / (60*24)) + + for n,i in enumerate(out): if i.skip == "d": addr = " |" + "-"*32 else: - if mmaddr: - if i.account and verify_mmgen_label(i.account): + if show_mmaddr: + if verify_mmgen_label(i.account): addr = "%s.. %s" % (i.address[:4],i.account) else: addr = i.address else: addr = i.address txid = " |---" if i.skip == "t" else i.txid[:8]+"..." - days = int(i.confirmations * mins_per_block / (60*24)) - output.append(fs % (str(n+1)+")", txid,i.vout,addr,amt+(" "*fill),days)) + output.append(fs % (str(n+1)+")",txid,i.vout,addr,i.amt,i.days)) while True: reply = get_char("\n".join(output) + @@ -216,21 +219,33 @@ View options: [g]roup, show [m]mgen addr elif reply == 't': unspent.sort(s_txid); sort = "txid"; break elif reply == 'd': unspent.sort(s_addr); sort = "address"; break elif reply == 'A': unspent.sort(s_age); sort = "age"; break - elif reply == 'M': unspent.sort(s_mmgen); mmaddr,sort=True,"mmgen"; break + elif reply == 'M': unspent.sort(s_mmgen); show_mmaddr,sort=True,"mmgen"; break elif reply == 'r': reverse = False if reverse else True unspent.reverse() break elif reply == 'g': group = False if group else True; break - elif reply == 'm': mmaddr = False if mmaddr else True; break + elif reply == 'm': show_mmaddr = False if show_mmaddr else True; break elif reply == 'p': - outfile = "listunspent.out" - o = "Date: {} UTC\n\n{}\n\nTotal BTC: {}\n".format( - make_timestr(), - "\n".join(output), - trim_exponent(sum([i.amount for i in unspent])) + pfs = " %-4s %-67s %-34s %-12s %-13s %-10s %s" + pout = [pfs % ("Num","TX id,Vout","Address","MMgen ID", + "Amount (BTC)","Age (days)", "Comment")] + + for n,i in enumerate(out): + if verify_mmgen_label(i.account): + s = i.account.split(None,1) + mmid,cmt = s[0],(s[1] if len(s) == 2 else "") + else: + mmid,cmt = "",i.account + os = pfs % (str(n+1)+")", str(i.txid)+","+str(i.vout), + i.address,mmid,i.amt,i.days,cmt) + pout.append(os.rstrip()) + + outdata = "Unspent outputs ({} UTC)\n\n{}\n\nTotal BTC: {}\n".format( + make_timestr(), "\n".join(pout), total ) - write_to_file(outfile, o) + outfile = "listunspent.out" + write_to_file(outfile, outdata) msg("\nData written to '%s'" % outfile) sys.exit(1) elif reply == 'q': break diff --git a/mmgen/utils.py b/mmgen/utils.py index 153bc421..7ac72124 100755 --- a/mmgen/utils.py +++ b/mmgen/utils.py @@ -593,7 +593,10 @@ def _check_mmseed_format(words): valid = False what = "%s data" % seed_ext - chklen = len(words[0]) + try: + chklen = len(words[0]) + except: + return False if len(words) < 3 or len(words) > 12: msg("Invalid data length (%s) in %s" % (len(words),what)) @@ -840,6 +843,14 @@ def get_seed(infile,opts,silent=False): return seed +# Repeat if data entry is incorrect +def get_seed_retry(infile,opts): + silent = False + while True: + seed = get_seed(infile,opts,silent=silent) + silent = True + if seed: return seed + def remove_blanks_comments(lines): import re diff --git a/scripts/deinstall.sh b/scripts/deinstall.sh index dae4b507..88ee8a15 100755 --- a/scripts/deinstall.sh +++ b/scripts/deinstall.sh @@ -1,3 +1,7 @@ #!/bin/bash + +CMD='rm -r /usr/local/bin/mmgen-* /usr/local/lib/python2.7/dist-packages/mmgen*' + set -x -sudo rm -r /usr/local/bin/mmgen-* /usr/local/lib/python2.7/dist-packages/mmgen* + +if [ "$EUID" = 0 ]; then $CMD; else sudo $CMD; fi