From 24ae3b237c1992a5d5ea4d27854f447e50c6a322 Mon Sep 17 00:00:00 2001 From: philemon Date: Sat, 9 Aug 2014 10:18:32 +0400 Subject: [PATCH] Save tx user comment field in base58 rather than raw string --- mmgen/tool.py | 21 ++++++++++++++++++--- mmgen/tx.py | 18 ++++++++++++------ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/mmgen/tool.py b/mmgen/tool.py index dc42c0d6..bfb093ff 100755 --- a/mmgen/tool.py +++ b/mmgen/tool.py @@ -41,6 +41,7 @@ commands = { "strtob58": [' [str]'], "hextob58": [' [str]'], "b58tohex": [' [str]'], + "b58tostr": [' [str]'], "b58randenc": [], "randhex": ['nbytes [int=32]'], "randwif": ['compressed [bool=False]'], @@ -60,7 +61,7 @@ commands = { "str2id6": [' [str]'], "listaddresses":['minconf [int=1]', 'showempty [bool=False]'], "getbalance": ['minconf [int=1]'], - "txview": [' [str]','pager [bool=False]'], + "viewtx": [' [str]','pager [bool=False]'], "check_addrfile": [' [str]'], "find_incog_data": [' [str]',' [str]','keep_searching [bool=False]'], "hexreverse": [' [str]'], @@ -83,6 +84,7 @@ command_help = """ addr2hexaddr - convert Bitcoin address from base58 to hex format b58randenc - generate a random 32-byte number and convert it to base 58 b58tohex - convert a base 58 number to hexadecimal + b58tostr - convert a base 58 number to a string hex2wif - convert a private key from hex to WIF format hexaddr2addr - convert Bitcoin address from hex to base58 format hextob58 - convert a hexadecimal number to base 58 @@ -99,7 +101,7 @@ command_help = """ getbalance - like 'bitcoind getbalance' but shows confirmed/unconfirmed, spendable/unspendable balances for individual {pnm} wallets listaddresses - list {pnm} addresses and their balances - txview - show raw/signed {pnm} transaction in human-readable form + viewtx - show raw/signed {pnm} transaction in human-readable form General utilities: bytespec - convert a byte specifier such as '1GB' into a plain integer @@ -236,6 +238,12 @@ def b58tohex(s,f_enc=bitcoin.b58decode, f_dec=bitcoin.b58encode): dec = f_dec(ba.unhexlify(enc)) print_convert_results(s,enc,dec) +def b58tostr(s,f_enc=bitcoin.b58decode, f_dec=bitcoin.b58encode): + enc = f_enc(s) + if enc == False: sys.exit(1) + dec = f_dec(enc) + print_convert_results(s,enc,dec) + def b58randenc(): r = get_random(32,opts) enc = bitcoin.b58encode(r) @@ -331,6 +339,13 @@ def listaddresses(minconf=1,showempty=False): key = "_".join(ma.split(":")) if key not in addrs: addrs[key] = [0,comment] + if not addrs: + if showempty: + msg("No tracked addresses!") + else: + msg("No addresses with balances!") + sys.exit(1) + fs = "%-{}s %-{}s %s".format( max([len(k) for k in addrs.keys()]), max([len(str(addrs[k][1])) for k in addrs.keys()]) @@ -372,7 +387,7 @@ def getbalance(minconf=1): for key in sorted(accts.keys()): print fs.format(key+":", *[str(trim_exponent(a))+" BTC" for a in accts[key]]) -def txview(infile,pager=False): +def viewtx(infile,pager=False): c = connect_to_bitcoind() tx_data = get_lines_from_file(infile,"transaction data") diff --git a/mmgen/tx.py b/mmgen/tx.py index a1a0c640..261eb393 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -406,10 +406,15 @@ def parse_tx_data(tx_data,infile): try: outputs_data = eval(outputs_data) except: err_str = "mmgen-to-btc address map data" else: - if is_valid_tx_comment(comment,True): - comment = comment.decode("utf8") + from mmgen.bitcoin import b58decode + comment = b58decode(comment) + if comment == False: + err_str = "encoded comment (not base58)" else: - err_str = "comment" + if is_valid_tx_comment(comment,True): + comment = comment.decode("utf8") + else: + err_str = "comment" if err_str: msg(err_fmt % err_str) @@ -800,6 +805,7 @@ def get_tx_comment_from_user(comment=""): def make_tx_data(metadata_fmt, tx_hex, inputs_data, b2m_map, comment): - lines = (metadata_fmt, tx_hex, repr(inputs_data), repr(b2m_map)) + \ - ((comment,) if comment else ()) - return "\n".join(lines).encode("utf8")+"\n" + from mmgen.bitcoin import b58encode + c = (b58encode(comment.encode("utf8")),) if comment else () + lines = (metadata_fmt, tx_hex, repr(inputs_data), repr(b2m_map)) + c + return "\n".join(lines)+"\n"