mmgen-txcreate interface improvements

utils.py -> util.py rename
This commit is contained in:
The MMGen Project 2014-04-03 14:02:00 +04:00
commit 1c9b0f8816
29 changed files with 162 additions and 140 deletions

View file

@ -27,7 +27,7 @@ import sys
from mmgen.Opts import *
import mmgen.config as g
from mmgen.license import *
from mmgen.utils import *
from mmgen.util import *
from mmgen.addr import *
invoked_as = sys.argv[0].split("-")[-1]

View file

@ -23,7 +23,7 @@ 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,msg,msg_r,secs_to_hms,get_lines_from_file
from mmgen.util 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

View file

@ -22,7 +22,7 @@ mmgen-passchg: Change a mmgen deterministic wallet's passphrase, label or
import sys
from mmgen.Opts import *
from mmgen.utils import *
from mmgen.util import *
import mmgen.config as g
help_data = {

View file

@ -44,7 +44,7 @@ mmgen-pywallet: Dump contents of a bitcoind wallet to file
# Ported from C code written by Laurent Haan (http://www.progressive-coding.com)
from mmgen.Opts import *
from mmgen.utils import msg
from mmgen.util import msg
from bsddb.db import *
import sys, time
import json
@ -92,7 +92,7 @@ long_opts = "help","outdir=","echo_passphrase","json","keys","addrs",\
opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
from mmgen.utils import check_infile
from mmgen.util import check_infile
check_opts(opts,long_opts)
@ -1575,7 +1575,7 @@ def read_wallet(json_db, db_env, db_file, print_wallet, print_wallet_transaction
if password == None and \
('json' in opts or 'keysforaddrs' in opts or 'keys' in opts):
from mmgen.utils import get_bitcoind_passphrase
from mmgen.util import get_bitcoind_passphrase
password = get_bitcoind_passphrase("Enter password: ",opts)
if password != None:
@ -1682,7 +1682,7 @@ elif 'addrs' in opts:
ext,what = "addrs","addresses"
elif 'keysforaddrs' in opts:
from mmgen.utils import get_lines_from_file
from mmgen.util import get_lines_from_file
usr_addrs = get_lines_from_file(opts['keysforaddrs'],"addresses")
for addr in usr_addrs:
try:
@ -1696,7 +1696,7 @@ len_arg = "%s" % len(wallet_addrs) \
if len(data) == len(wallet_addrs) or ext == "json" \
else "%s:%s" % (len(data),len(wallet_addrs))
from mmgen.utils import make_chksum_8,write_walletdat_dump_to_file,write_to_stdout
from mmgen.util import make_chksum_8,write_walletdat_dump_to_file,write_to_stdout
wallet_id = make_chksum_8(str(sorted(wallet_addrs)))
data = "\n".join(data) + "\n"

View file

@ -26,7 +26,7 @@ from mmgen.Opts import *
from mmgen.license import *
import mmgen.config as g
from mmgen.tx import *
from mmgen.utils import msg, msg_r, user_confirm
from mmgen.util import msg, msg_r, user_confirm
from decimal import Decimal
prog_name = sys.argv[0].split("/")[-1]

View file

@ -25,7 +25,7 @@ from mmgen.Opts import *
from mmgen.license import *
import mmgen.config as g
from mmgen.tx import *
from mmgen.utils import msg,check_infile,get_lines_from_file,confirm_or_exit
from mmgen.util import msg,check_infile,get_lines_from_file,confirm_or_exit
prog_name = sys.argv[0].split("/")[-1]

View file

@ -25,7 +25,7 @@ from mmgen.Opts import *
from mmgen.license import *
import mmgen.config as g
from mmgen.tx import *
from mmgen.utils import msg
from mmgen.util import msg
help_data = {
'prog_name': sys.argv[0].split("/")[-1],

View file

@ -23,7 +23,7 @@ mmgen-walletchk: Check integrity of a mmgen deterministic wallet, display
import sys
from mmgen.Opts import *
from mmgen.utils import *
from mmgen.util import *
help_data = {
'prog_name': sys.argv[0].split("/")[-1],

View file

@ -26,7 +26,7 @@ from mmgen.Opts import *
from mmgen.license import *
import mmgen.config as g
from mmgen.walletgen import *
from mmgen.utils import *
from mmgen.util import *
prog_name = sys.argv[0].split("/")[-1]
help_data = {

View file

@ -18,7 +18,7 @@
import sys, getopt
import mmgen.config as g
from mmgen.utils import msg
from mmgen.util import msg
def usage(hd):
print "USAGE: %s %s" % (hd['prog_name'], hd['usage'])

View file

@ -29,7 +29,7 @@ __all__ = [
'mn_tirosh.py',
'Opts.py',
'tx.py',
'utils.py',
'util.py',
'walletgen.py'
]

View file

@ -210,7 +210,7 @@ def write_addr_data_to_file(seed, data, addr_list, opts):
else: ext = "akeys"
if 'b16' in opts: ext = ext.replace("keys","xkeys")
from mmgen.utils import write_to_file, make_chksum_8, msg
from mmgen.util import write_to_file, make_chksum_8, msg
outfile = "{}[{}].{}".format(
make_chksum_8(seed),
fmt_addr_list(addr_list),

View file

@ -20,7 +20,7 @@ license.py: Show the license
"""
import sys
from mmgen.utils import msg, msg_r, get_char
from mmgen.util import msg, msg_r, get_char
gpl = {
'warning': """
@ -591,7 +591,7 @@ def do_license_msg(immed=False):
while True:
reply = get_char(prompt, immed_chars="wc" if immed else "")
if reply == 'w':
from mmgen.utils import do_pager
from mmgen.util import do_pager
do_pager(gpl['conditions'])
elif reply == 'c':
msg(""); break

View file

@ -31,7 +31,7 @@ def mn_len(hexnum): return len(hexnum) * 3 / 8
import sys
from mmgen.utils import msg,make_chksum_8
from mmgen.util import msg,make_chksum_8
import mmgen.config as g
# These universal base-conversion routines work for any base

View file

@ -66,7 +66,7 @@ class BitcoinConnection(object):
return self.proxy.importaddress(address,label)
except JSONRPCException as e:
if e.error['message'] == "Method not found":
from mmgen.utils import msg_r
from mmgen.util import msg_r
msg_r("""
ERROR: 'importaddress' method not found. Is your bitcoind enabled for watch-only addresses?""")
raise _wrap_exception(e.error)

View file

@ -104,7 +104,7 @@ class AuthServiceProxy(object):
'Authorization' : self.__authhdr,
'Content-type' : 'application/json' })
except:
from mmgen.utils import msg
from mmgen.util import msg
import sys
msg("\nUnable to connect to bitcoind.")
sys.exit(2)

View file

@ -2,17 +2,17 @@
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C) 2013 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/>.
"""

View file

@ -2,17 +2,17 @@
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C) 2013 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/>.
"""
@ -20,7 +20,7 @@ bitcoin.py: Test suite for mmgen.bitcoin module
"""
from mmgen.bitcoin import *
from mmgen.utils import msg
from mmgen.util import msg
from test import *
import sys

View file

@ -2,17 +2,17 @@
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C) 2013 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/>.
"""

View file

@ -2,17 +2,17 @@
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C) 2013 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/>.
"""

View file

@ -2,17 +2,17 @@
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C) 2013 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/>.
"""
@ -66,7 +66,9 @@ def random192(): do_random_tests(24)
def random256(): do_random_tests(32)
def random512(): do_random_tests(64)
def electrum(): check_wordlist(el,"electrum")
def electrum_print(): print "%s" % el.strip()
def tirosh(): check_wordlist(tl,"tirosh")
def tirosh_print(): print "%s" % tl.strip()
def base10tohex(num,quiet=False):
enc,dec = baseNtohex_test(num,10,"0123456789",quiet)
@ -99,6 +101,8 @@ tests = {
"random512": [],
"electrum": [],
"tirosh": [],
"electrum_print": [],
"tirosh_print": [],
"base10tohex": ['base10num [int]','quiet [bool=False]'],
"hextobase10": ['hexnum [str]', 'quiet [bool=False]'],
"base8tohex": ['base8num [int]', 'quiet [bool=False]'],

View file

@ -2,17 +2,17 @@
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C) 2013 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/>.
"""
@ -20,7 +20,7 @@ test.py: Shared routines for mmgen test suite
"""
import sys
from mmgen.utils import msg
from mmgen.util import msg
def nomsg(s): pass
@ -55,7 +55,7 @@ def process_test_args(argv, tests):
print "'%s': no such test" % argv[1]
sys.exit(2)
else:
cargs = tests[argv[1]]
cargs = tests[argv[1]]
uargs = argv[2:]
if len(uargs) > len(cargs):
print "Too many arguments\nUsage: %s(%s)" % \

View file

@ -2,24 +2,24 @@
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C) 2013 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/>.
"""
utils.py: Test suite for mmgen.utils module
util.py: Test suite for mmgen.util module
"""
from mmgen.utils import *
from mmgen.util import *
tests = "none",

View file

@ -2,17 +2,17 @@
#
# mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
# Copyright (C) 2013 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/>.
"""

View file

@ -20,7 +20,7 @@ tx.py: Bitcoin transaction routines
"""
from binascii import unhexlify
from mmgen.utils import *
from mmgen.util import *
import sys, os
from decimal import Decimal
import mmgen.config as g
@ -169,6 +169,32 @@ def print_sent_tx_to_file(tx,metadata,opts):
msg("Transaction ID saved to file '%s'" % outfile)
def format_unspent_outputs_for_printing(out,sort_info,total):
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
addr = "=" if i.skip == "addr" and "grouped" in sort_info else i.address
tx = " " * 63 + "=" \
if i.skip == "txid" and "grouped" in sort_info else str(i.txid)
s = pfs % (str(n+1)+")", tx+","+str(i.vout),addr,mmid,i.amt,i.days,cmt)
pout.append(s.rstrip())
return \
"Unspent outputs ({} UTC)\nSort order: {}\n\n{}\n\nTotal BTC: {}\n".format(
make_timestr(), " ".join(sort_info), "\n".join(pout), total
)
def sort_and_view(unspent):
def s_amt(a,b): return cmp(a.amount,b.amount)
@ -182,27 +208,28 @@ def sort_and_view(unspent):
sort,group,show_mmaddr,reverse = "",False,False,False
total = trim_exponent(sum([i.amount for i in unspent]))
hdr_fmt = "UNSPENT OUTPUTS (sort order: %s) Total BTC: %s"
table_hdr = fs % ("Num","TX id Vout","","Address","Amount (BTC)", "Age(days)")
options_msg = """
Sort options: [t]xid, [a]mount, a[d]dress, [A]ge, [r]everse, [M]mgen addr
Format options: [g]roup, show [m]mgen addr
""".strip()
prompt = \
"('q' = quit sorting, 'p' = print to file, 'v' = pager view, 'w' = wide view): "
from copy import deepcopy
print_to_file_msg = ""
msg("")
while True:
out = deepcopy(unspent)
for i in out: i.skip = ""
for n in range(len(out)):
if group and n < len(out)-1:
if group and (sort == "address" or sort == "txid"):
for n in range(len(out)-1):
a,b = out[n],out[n+1]
if sort == "address" and a.address == b.address:
out[n+1].skip = "d"
elif sort == "txid" and a.txid == b.txid:
out[n+1].skip = "t"
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 "",
total
)]
output.append(fs % ("Num","TX id Vout","","Address","Amount (BTC)",
"Age(days)"))
if sort == "address" and a.address == b.address: b.skip = "addr"
elif sort == "txid" and a.txid == b.txid: b.skip = "txid"
for i in out:
amt = str(trim_exponent(i.amount))
@ -210,81 +237,72 @@ def sort_and_view(unspent):
i.amt = " "*lfill + amt
i.days = int(i.confirmations * g.mins_per_block / (60*24))
for n,i in enumerate(out):
if i.skip == "d":
addr = "|" + "." * 33
if i.skip == "addr":
i.addr = "|" + "." * 33
else:
if show_mmaddr:
if verify_mmgen_label(i.account):
addr = "%s.. %s" % (i.address[:4],i.account)
i.addr = "%s.. %s" % (i.address[:4],i.account)
else:
addr = i.address
i.addr = i.address
else:
addr = i.address
txid = " |..." if i.skip == "t" else i.txid[:8]+"..."
i.addr = i.address
output.append(fs % (str(n+1)+")",txid,i.vout,addr,i.amt,i.days))
i.tx = " |..." if i.skip == "txid" else i.txid[:8]+"..."
sort_info = ["reverse"] if reverse else []
sort_info.append(sort if sort else "unsorted")
if group and (sort == "address" or sort == "txid"):
sort_info.append("grouped")
output = [hdr_fmt % (" ".join(sort_info), total), table_hdr]
for n,i in enumerate(out):
output.append(fs % (str(n+1)+")",i.tx,i.vout,i.addr,i.amt,i.days))
msg("\n".join(output) +"\n\n" + print_to_file_msg + options_msg)
print_to_file_msg = ""
immed_chars = "qpPtadArMgm"
skip_prompt = False
skip_body = False
while True:
if skip_body:
skip_body = False
immed_chars = "qpP"
else:
msg("\n".join(output))
msg("""
Sort options: [t]xid, [a]mount, a[d]dress, [A]ge, [r]everse, [M]mgen addr
View options: [g]roup, show [m]mgen addr""")
immed_chars = "qpPtadArMgm"
reply = get_char(prompt, immed_chars=immed_chars)
reply = get_char(
"(Type 'q' to quit sorting, 'p' to print to file, 'v' to view in pager): ",
immed_chars=immed_chars)
if reply == 'a': unspent.sort(s_amt); sort = "amount"; break
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); show_mmaddr,sort=True,"mmgen"; break
if reply == 'a': unspent.sort(s_amt); sort = "amount"
elif reply == 't': unspent.sort(s_txid); sort = "txid"
elif reply == 'd': unspent.sort(s_addr); sort = "address"
elif reply == 'A': unspent.sort(s_age); sort = "age"
elif reply == 'M':
unspent.sort(s_mmgen)
sort = "mmgen"
show_mmaddr = True
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': show_mmaddr = False if show_mmaddr else True; break
reverse = False if reverse else True
elif reply == 'g': group = False if group else True
elif reply == 'm': show_mmaddr = False if show_mmaddr else True
elif reply == 'q': pass
elif reply == 'p':
pfs = " %-4s %-67s %-34s %-12s %-13s %-10s %s"
pout = [pfs % ("Num","TX id,Vout","Address","MMgen ID",
"Amount (BTC)","Age (days)", "Comment")]
data = format_unspent_outputs_for_printing(out,sort_info,total)
outfile = "listunspent[%s].out" % ",".join(sort_info)
write_to_file(outfile, data)
print_to_file_msg = "Data written to '%s'\n\n" % outfile
elif reply == 'v':
do_pager("\n".join(output))
continue
elif reply == 'w':
data = format_unspent_outputs_for_printing(out,sort_info,total)
do_pager(data)
continue
else:
msg("\nInvalid input")
continue
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())
sort_info = (
("reverse," if reverse else "") +
(sort if sort else "unsorted")
)
outdata = \
"Unspent outputs ({} UTC)\nSort order: {}\n\n{}\n\nTotal BTC: {}\n".format(
make_timestr(), sort_info, "\n".join(pout), total
)
outfile = "listunspent[%s].out" % sort_info
write_to_file(outfile, outdata)
skip_body = True
msg("\nData written to '%s'" % outfile)
elif reply == 'v': do_pager("\n".join(output))
elif reply == 'q': break
else: msg("Invalid input")
break
msg("\n")
if reply in 'q': break
if reply == 'q': break
return tuple(unspent)
@ -401,7 +419,7 @@ def select_outputs(unspent,prompt):
if not reply: continue
from mmgen.utils import parse_address_list
from mmgen.util import parse_address_list
selected = parse_address_list(reply,sep=None)
if not selected: continue

View file

@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
utils.py: Shared routines for the mmgen suite
util.py: Shared routines for the mmgen suite
"""
import sys
@ -964,4 +964,4 @@ def do_pager(text):
else: print text+end
if __name__ == "__main__":
print "utils.py"
print "util.py"

View file

@ -20,7 +20,7 @@ walletgen.py: Routines used for seed generation and wallet creation
"""
import sys
from mmgen.utils import msg, msg_r, get_char, prompt_and_get_char
from mmgen.util import msg, msg_r, get_char, prompt_and_get_char
from binascii import hexlify
def get_random_data_from_user(opts):

View file

@ -31,15 +31,15 @@ setup(
'mmgen.rpc.proxy',
'mmgen.rpc.util',
'tests.__init__',
'tests.addr',
'tests.bitcoin',
'tests.mn_electrum',
'tests.mnemonic',
'tests.mn_tirosh',
'tests.test',
'tests.utils',
'tests.walletgen'
'mmgen.tests.__init__',
'mmgen.tests.addr',
'mmgen.tests.bitcoin',
'mmgen.tests.mn_electrum',
'mmgen.tests.mnemonic',
'mmgen.tests.mn_tirosh',
'mmgen.tests.test',
'mmgen.tests.utils',
'mmgen.tests.walletgen'
],
scripts=[
'mmgen-addrgen',