From 037bbda14aa2019edd4dc214f78a50472fd250db Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Sun, 29 Sep 2024 11:59:53 +0000 Subject: [PATCH] whitespace, minor cleanups --- mmgen/addr.py | 16 ++++++++-------- mmgen/opts.py | 10 +++++----- mmgen/proto/btc/params.py | 6 +++--- mmgen/proto/btc/tx/info.py | 6 ++++-- mmgen/proto/eth/params.py | 4 ++-- mmgen/proto/eth/tx/info.py | 2 +- mmgen/proto/zec/params.py | 4 ++-- mmgen/protocol.py | 6 +++--- mmgen/tw/shared.py | 1 - mmgen/tw/view.py | 4 ++-- mmgen/tx/base.py | 2 +- mmgen/tx/info.py | 20 ++++++++++++-------- setup.cfg | 2 +- test/unit_tests.py | 2 +- 14 files changed, 45 insertions(+), 40 deletions(-) diff --git a/mmgen/addr.py b/mmgen/addr.py index 6ce9cbc9..0b5864e3 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -25,6 +25,7 @@ from collections import namedtuple from .objmethods import HiliteStr,InitErrors,MMGenObject from .obj import ImmutableAttr,MMGenIdx,get_obj from .seed import SeedID +from . import color as color_mod ati = namedtuple('addrtype_info', ['name','pubkey_type','compressed','gen_method','addr_fmt','wif_label','extra_attrs','desc']) @@ -144,19 +145,19 @@ class MMGenID(HiliteStr,InitErrors,MMGenObject): def is_mmgen_id(proto,s): return get_obj( MMGenID, proto=proto, id_str=s, silent=True, return_bool=True ) -class CoinAddr(HiliteStr,InitErrors,MMGenObject): +class CoinAddr(HiliteStr, InitErrors, MMGenObject): color = 'cyan' hex_width = 40 width = 1 trunc_ok = False + def __new__(cls,proto,addr): if isinstance(addr,cls): return addr try: - assert addr.isascii() and addr.isalnum(), 'not an ASCII alphanumeric string' - me = str.__new__(cls,addr) ap = proto.decode_addr(addr) assert ap, f'coin address {addr!r} could not be parsed' + me = str.__new__(cls, addr) me.addr_fmt = ap.fmt me.bytes = ap.bytes me.ver_bytes = ap.ver_bytes @@ -171,15 +172,14 @@ class CoinAddr(HiliteStr,InitErrors,MMGenObject): self._parsed = self.proto.parse_addr(self.ver_bytes,self.bytes,self.addr_fmt) return self._parsed + # reimplement some HiliteStr methods: @classmethod def fmtc(cls,s,width,color=False): return super().fmtc( s=s[:width-2]+'..' if len(s) > width else s, width=width, color=color ) - def fmt(self,width,color=False): - return ( - super().fmtc( s=self[:width-2]+'..', width=width, color=color ) if len(self) > width else - super().fmt( width=width, color=color ) - ) + def fmt(self, width, color=False): + s = self + return super().fmtc(f'{s[:width-2]}..' if len(s) > width else s, width=width, color=color) def is_coin_addr(proto,s): return get_obj( CoinAddr, proto=proto, addr=s, silent=True, return_bool=True ) diff --git a/mmgen/opts.py b/mmgen/opts.py index 318a9d2f..bc02aa82 100755 --- a/mmgen/opts.py +++ b/mmgen/opts.py @@ -93,11 +93,11 @@ class UserOpts: def __init__( self, cfg, - opts_data = None, - init_opts = None, # dict containing opts to pre-initialize - opt_filter = None, # whitelist of opt letters; all others are skipped - parse_only = False, - parsed_opts = None ): + opts_data, + init_opts, # dict containing opts to pre-initialize + opt_filter, # whitelist of opt letters; all others are skipped + parse_only, + parsed_opts): self.opts_data = od = opts_data or opts_data_dfl self.opt_filter = opt_filter diff --git a/mmgen/proto/btc/params.py b/mmgen/proto/btc/params.py index 32855558..9e620f3f 100755 --- a/mmgen/proto/btc/params.py +++ b/mmgen/proto/btc/params.py @@ -75,7 +75,7 @@ class mainnet(CoinProtocol.Secp256k1): # chainparams.cpp pubkey_type = self.wif_ver_bytes_to_pubkey_type[key_data[:vlen]], compressed = len(key) == self.privkey_len + 1 ) - def decode_addr(self,addr): + def decode_addr(self, addr): if 'B' in self.mmtypes and addr[:len(self.bech32_hrp)] == self.bech32_hrp: from ...contrib import bech32 @@ -86,12 +86,12 @@ class mainnet(CoinProtocol.Secp256k1): # chainparams.cpp msg(f'{ret[0]}: Invalid witness version number') return False - return decoded_addr( bytes(ret[1]), None, 'bech32' ) if ret[1] else False + return decoded_addr(bytes(ret[1]), None, 'bech32') if ret[1] else False return self.decode_addr_bytes(b58chk_decode(addr)) def pubhash2addr(self,pubhash,p2sh): - assert len(pubhash) == 20, f'{len(pubhash)}: invalid length for pubkey hash' + assert len(pubhash) == self.addr_len, f'{len(pubhash)}: invalid length for pubkey hash' return b58chk_encode( self.addr_fmt_to_ver_bytes[('p2pkh','p2sh')[p2sh]] + pubhash ) diff --git a/mmgen/proto/btc/tx/info.py b/mmgen/proto/btc/tx/info.py index 2cece536..42da2cd5 100755 --- a/mmgen/proto/btc/tx/info.py +++ b/mmgen/proto/btc/tx/info.py @@ -48,8 +48,7 @@ class TxInfo(TxInfo): out += f', Base {tsize-wsize}, Witness {wsize}' return out + '\n' - def format_body(self,blockcount,nonmm_str,max_mmwid,enl,terse,sort): - tx = self.tx + def format_body(self, blockcount, nonmm_str, max_mmwid, enl, terse, sort): if sort not in self.sort_orders: die(1,'{!r}: invalid transaction view sort order. Valid options: {}'.format( @@ -61,12 +60,14 @@ class TxInfo(TxInfo): is_input = desc == 'inputs' yield desc.capitalize() + ':\n' + enl confs_per_day = 60*60*24 // tx.proto.avg_bdi + io_sorted = { 'addr': lambda: sorted( io, # prepend '+' (sorts before '0') to ensure non-MMGen addrs are displayed first key = lambda o: (o.mmid.sort_key if o.mmid else f'+{o.addr}') + f'{o.amt:040.20f}' ), 'raw': lambda: io }[sort] + if terse: iwidth = max(len(str(int(e.amt))) for e in io) else: @@ -107,6 +108,7 @@ class TxInfo(TxInfo): yield ('', 'change:', green('True')) yield '\n'.join('{:>{w}} {:<8} {}'.format(*d,w=col1_w) for d in gen()) + '\n\n' + tx = self.tx addr_w = max(len(e.addr) for f in (tx.inputs,tx.outputs) for e in f) return ( diff --git a/mmgen/proto/eth/params.py b/mmgen/proto/eth/params.py index ef3edae9..60afd387 100755 --- a/mmgen/proto/eth/params.py +++ b/mmgen/proto/eth/params.py @@ -54,9 +54,9 @@ class mainnet(CoinProtocol.DummyWIF,CoinProtocol.Secp256k1): def dcoin(self): return self.tokensym or self.coin - def decode_addr(self,addr): + def decode_addr(self, addr): if is_hex_str_lc(addr) and len(addr) == self.addr_len * 2: - return decoded_addr( bytes.fromhex(addr), None, 'ethereum' ) + return decoded_addr(bytes.fromhex(addr), None, 'ethereum') if self.cfg.debug: Msg(f'Invalid address: {addr}') return False diff --git a/mmgen/proto/eth/tx/info.py b/mmgen/proto/eth/tx/info.py index 6cd59413..7e54f8d3 100755 --- a/mmgen/proto/eth/tx/info.py +++ b/mmgen/proto/eth/tx/info.py @@ -29,7 +29,7 @@ class TxInfo(TxInfo): """) fmt_keys = ('from','to','amt','nonce') - def format_body(self,blockcount,nonmm_str,max_mmwid,enl,terse,sort): + def format_body(self, blockcount, nonmm_str, max_mmwid, enl, terse, sort): tx = self.tx m = {} for k in ('inputs','outputs'): diff --git a/mmgen/proto/zec/params.py b/mmgen/proto/zec/params.py index e85306a9..c37f65ee 100755 --- a/mmgen/proto/zec/params.py +++ b/mmgen/proto/zec/params.py @@ -45,7 +45,7 @@ class mainnet(mainnet): def get_addr_len(self,addr_fmt): return (20,64)[addr_fmt in ('zcash_z','viewkey')] - def decode_addr_bytes(self,addr_bytes): + def decode_addr_bytes(self, addr_bytes): """ vlen must be set dynamically since Zcash has variable-length version bytes """ @@ -53,7 +53,7 @@ class mainnet(mainnet): vlen = len(ver_bytes) if addr_bytes[:vlen] == ver_bytes: if len(addr_bytes[vlen:]) == self.get_addr_len(addr_fmt): - return decoded_addr( addr_bytes[vlen:], ver_bytes, addr_fmt ) + return decoded_addr(addr_bytes[vlen:], ver_bytes, addr_fmt) return False diff --git a/mmgen/protocol.py b/mmgen/protocol.py index 15dd09b4..cbb2c74c 100755 --- a/mmgen/protocol.py +++ b/mmgen/protocol.py @@ -26,7 +26,7 @@ from .cfg import gc from .objmethods import MMGenObject decoded_wif = namedtuple('decoded_wif',['sec','pubkey_type','compressed']) -decoded_addr = namedtuple('decoded_addr',['bytes','ver_bytes','fmt']) +decoded_addr = namedtuple('decoded_addr', ['bytes', 'ver_bytes', 'fmt']) parsed_addr = namedtuple('parsed_addr',['ver_bytes','data']) _finfo = namedtuple('fork_info',['height','hash','name','replayable']) @@ -149,12 +149,12 @@ class CoinProtocol(MMGenObject): def get_addr_len(self,addr_fmt): return self.addr_len - def decode_addr_bytes(self,addr_bytes): + def decode_addr_bytes(self, addr_bytes): vlen = self.addr_ver_bytes_len return decoded_addr( addr_bytes[vlen:], addr_bytes[:vlen], - self.addr_ver_bytes[addr_bytes[:vlen]] ) + self.addr_ver_bytes[addr_bytes[:vlen]]) def coin_addr(self,addr): from .addr import CoinAddr diff --git a/mmgen/tw/shared.py b/mmgen/tw/shared.py index 3ac7f907..55312f68 100755 --- a/mmgen/tw/shared.py +++ b/mmgen/tw/shared.py @@ -31,7 +31,6 @@ class TwMMGenID(HiliteStr,InitErrors,MMGenObject): coin,addr = id_str.split(':',1) assert coin == proto.base_coin.lower(),( f'not a string beginning with the prefix {proto.base_coin.lower()!r}:' ) - assert addr.isascii() and addr.isalnum(), 'not an ASCII alphanumeric string' ret,sort_key,idtype,disp = (id_str,'z_'+id_str,'non-mmgen','non-MMGen') addr = proto.coin_addr(addr) except Exception as e2: diff --git a/mmgen/tw/view.py b/mmgen/tw/view.py index 3cb2cef4..401b788a 100755 --- a/mmgen/tw/view.py +++ b/mmgen/tw/view.py @@ -418,7 +418,7 @@ class TwView(MMGenObject,metaclass=AsyncInit): yield from getattr(self,dt.subhdr_fmt_method)(cw,color) - yield '' + yield ' ' * self.term_width if data and dt.colhdr_fmt_method: col_hdr = getattr(self,dt.colhdr_fmt_method)(cw,hdr_fs,color) @@ -580,7 +580,7 @@ class TwView(MMGenObject,metaclass=AsyncInit): msg('') if self.scroll: self.term.set('echo') - return self.disp_data + return else: if not scroll: msg_r('\ninvalid keypress ') diff --git a/mmgen/tx/base.py b/mmgen/tx/base.py index 06677104..2a15d3f8 100755 --- a/mmgen/tx/base.py +++ b/mmgen/tx/base.py @@ -126,7 +126,7 @@ class Base(MMGenObject): @property def info(self): from .info import init_info - return init_info(self) + return init_info(self.cfg, self) def check_correct_chain(self): if hasattr(self,'rpc'): diff --git a/mmgen/tx/info.py b/mmgen/tx/info.py index 46bc1b98..cb411fbf 100755 --- a/mmgen/tx/info.py +++ b/mmgen/tx/info.py @@ -21,10 +21,11 @@ from ..util2 import format_elapsed_hr class TxInfo: - def __init__(self,tx): + def __init__(self, cfg, tx): + self.cfg = cfg self.tx = tx - def format(self,terse=False,sort='addr'): + def format(self, terse=False, sort='addr'): tx = self.tx @@ -61,9 +62,6 @@ class TxInfo: _ = decode_timestamp(val) yield f'{label:8} {make_timestr(_)} ({format_elapsed_hr(_)})\n' - if not terse: - yield '\n' - if tx.chain != 'mainnet': # if mainnet has a coin-specific name, display it yield green(f'Chain: {tx.chain.upper()}') + '\n' @@ -76,7 +74,13 @@ class TxInfo: if tx.comment: yield f'Comment: {tx.comment.hl()}\n{enl}' - yield self.format_body(blockcount,nonmm_str,max_mmwid,enl,terse=terse,sort=sort) + yield self.format_body( + blockcount, + nonmm_str, + max_mmwid, + enl, + terse = terse, + sort = sort) iwidth = len(str(int(tx.sum_inputs()))) @@ -123,7 +127,7 @@ class TxInfo: get_char('Press any key to continue: ') msg('') -def init_info(tx): +def init_info(cfg, tx): return getattr( importlib.import_module(f'mmgen.proto.{tx.proto.base_proto_coin.lower()}.tx.info'), - ('Token' if tx.proto.tokensym else '') + 'TxInfo' )(tx) + ('Token' if tx.proto.tokensym else '') + 'TxInfo' )(cfg, tx) diff --git a/setup.cfg b/setup.cfg index e57ecfcd..aaa6ebea 100644 --- a/setup.cfg +++ b/setup.cfg @@ -16,7 +16,7 @@ author = The MMGen Project author_email = mmgen@tuta.io url = https://github.com/mmgen/mmgen-wallet license = GNU GPL v3 -platforms = Linux, Armbian, Raspbian, MS Windows +platforms = Linux, Armbian, Raspbian, MS Windows, MacOS keywords = file: mmgen/data/keywords project_urls = Website = https://mmgen-wallet.cc diff --git a/test/unit_tests.py b/test/unit_tests.py index fe60ee15..92a9bce0 100755 --- a/test/unit_tests.py +++ b/test/unit_tests.py @@ -35,7 +35,7 @@ if not os.getenv('MMGEN_DEVTOOLS'): from mmgen.cfg import Config,gc from mmgen.color import gray, brown, orange, yellow, red -from mmgen.util import msg,gmsg,ymsg,Msg +from mmgen.util import msg, msg_r, gmsg, ymsg, Msg from test.include.common import set_globals,end_msg