whitespace: top-level modules
This commit is contained in:
parent
96b6b38c01
commit
b5600fc563
43 changed files with 1962 additions and 1982 deletions
101
mmgen/addr.py
101
mmgen/addr.py
|
|
@ -22,29 +22,29 @@ addr: MMGen address-related types
|
|||
|
||||
from collections import namedtuple
|
||||
|
||||
from .objmethods import HiliteStr,InitErrors,MMGenObject
|
||||
from .obj import ImmutableAttr,MMGenIdx,get_obj
|
||||
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'])
|
||||
['name', 'pubkey_type', 'compressed', 'gen_method', 'addr_fmt', 'wif_label', 'extra_attrs', 'desc'])
|
||||
|
||||
class MMGenAddrType(HiliteStr,InitErrors,MMGenObject):
|
||||
class MMGenAddrType(HiliteStr, InitErrors, MMGenObject):
|
||||
width = 1
|
||||
trunc_ok = False
|
||||
color = 'blue'
|
||||
|
||||
name = ImmutableAttr(str)
|
||||
pubkey_type = ImmutableAttr(str)
|
||||
compressed = ImmutableAttr(bool,set_none_ok=True)
|
||||
gen_method = ImmutableAttr(str,set_none_ok=True)
|
||||
addr_fmt = ImmutableAttr(str,set_none_ok=True)
|
||||
wif_label = ImmutableAttr(str,set_none_ok=True)
|
||||
extra_attrs = ImmutableAttr(tuple,set_none_ok=True)
|
||||
compressed = ImmutableAttr(bool, set_none_ok=True)
|
||||
gen_method = ImmutableAttr(str, set_none_ok=True)
|
||||
addr_fmt = ImmutableAttr(str, set_none_ok=True)
|
||||
wif_label = ImmutableAttr(str, set_none_ok=True)
|
||||
extra_attrs = ImmutableAttr(tuple, set_none_ok=True)
|
||||
desc = ImmutableAttr(str)
|
||||
|
||||
pkh_fmts = ('p2pkh','bech32','ethereum')
|
||||
pkh_fmts = ('p2pkh', 'bech32', 'ethereum')
|
||||
mmtypes = {
|
||||
'L': ati('legacy', 'std', False,'p2pkh', 'p2pkh', 'wif', (), 'Legacy uncompressed address'),
|
||||
'C': ati('compressed','std', True, 'p2pkh', 'p2pkh', 'wif', (), 'Compressed P2PKH address'),
|
||||
|
|
@ -54,34 +54,35 @@ class MMGenAddrType(HiliteStr,InitErrors,MMGenObject):
|
|||
'Z': ati('zcash_z','zcash_z',False,'zcash_z', 'zcash_z', 'wif', ('viewkey',), 'Zcash z-address'),
|
||||
'M': ati('monero', 'monero', False,'monero', 'monero', 'spendkey',('viewkey','wallet_passwd'),'Monero address'),
|
||||
}
|
||||
def __new__(cls,proto,id_str,errmsg=None):
|
||||
if isinstance(id_str,cls):
|
||||
def __new__(cls, proto, id_str, errmsg=None):
|
||||
if isinstance(id_str, cls):
|
||||
return id_str
|
||||
try:
|
||||
id_str = id_str.replace('-','_')
|
||||
for k,v in cls.mmtypes.items():
|
||||
if id_str in (k,v.name):
|
||||
id_str = id_str.replace('-', '_')
|
||||
for k, v in cls.mmtypes.items():
|
||||
if id_str in (k, v.name):
|
||||
if id_str == v.name:
|
||||
id_str = k
|
||||
me = str.__new__(cls,id_str)
|
||||
me = str.__new__(cls, id_str)
|
||||
for k in v._fields:
|
||||
setattr(me,k,getattr(v,k))
|
||||
setattr(me, k, getattr(v, k))
|
||||
if me not in proto.mmtypes + ('P',):
|
||||
raise ValueError(f'{me.name!r}: invalid address type for {proto.name} protocol')
|
||||
me.proto = proto
|
||||
return me
|
||||
raise ValueError(f'{id_str}: unrecognized address type for protocol {proto.name}')
|
||||
except Exception as e:
|
||||
return cls.init_fail( e,
|
||||
return cls.init_fail(
|
||||
e,
|
||||
f"{errmsg or ''}{id_str!r}: invalid value for {cls.__name__} ({e!s})",
|
||||
preformat = True )
|
||||
preformat = True)
|
||||
|
||||
@classmethod
|
||||
def get_names(cls):
|
||||
return [v.name for v in cls.mmtypes.values()]
|
||||
|
||||
def is_mmgen_addrtype(proto,id_str):
|
||||
return get_obj( MMGenAddrType, proto=proto, id_str=id_str, silent=True, return_bool=True )
|
||||
def is_mmgen_addrtype(proto, id_str):
|
||||
return get_obj(MMGenAddrType, proto=proto, id_str=id_str, silent=True, return_bool=True)
|
||||
|
||||
class MMGenPasswordType(MMGenAddrType):
|
||||
mmtypes = {
|
||||
|
|
@ -92,58 +93,58 @@ class AddrIdx(MMGenIdx):
|
|||
max_digits = 7
|
||||
|
||||
def is_addr_idx(s):
|
||||
return get_obj( AddrIdx, n=s, silent=True, return_bool=True )
|
||||
return get_obj(AddrIdx, n=s, silent=True, return_bool=True)
|
||||
|
||||
class AddrListID(HiliteStr,InitErrors,MMGenObject):
|
||||
class AddrListID(HiliteStr, InitErrors, MMGenObject):
|
||||
width = 10
|
||||
trunc_ok = False
|
||||
color = 'yellow'
|
||||
def __new__(cls,sid=None,mmtype=None,proto=None,id_str=None):
|
||||
def __new__(cls, sid=None, mmtype=None, proto=None, id_str=None):
|
||||
try:
|
||||
if id_str:
|
||||
a,b = id_str.split(':')
|
||||
a, b = id_str.split(':')
|
||||
sid = SeedID(sid=a)
|
||||
try:
|
||||
mmtype = MMGenAddrType( proto=proto, id_str=b )
|
||||
mmtype = MMGenAddrType(proto=proto, id_str=b)
|
||||
except:
|
||||
mmtype = MMGenPasswordType( proto=proto, id_str=b )
|
||||
mmtype = MMGenPasswordType(proto=proto, id_str=b)
|
||||
else:
|
||||
assert isinstance(sid,SeedID), f'{sid!r} not a SeedID instance'
|
||||
if not isinstance(mmtype,(MMGenAddrType,MMGenPasswordType)):
|
||||
assert isinstance(sid, SeedID), f'{sid!r} not a SeedID instance'
|
||||
if not isinstance(mmtype, (MMGenAddrType, MMGenPasswordType)):
|
||||
raise ValueError(f'{mmtype!r}: not an instance of MMGenAddrType or MMGenPasswordType')
|
||||
me = str.__new__(cls,sid+':'+mmtype)
|
||||
me = str.__new__(cls, sid+':'+mmtype)
|
||||
me.sid = sid
|
||||
me.mmtype = mmtype
|
||||
return me
|
||||
except Exception as e:
|
||||
return cls.init_fail(e, f'sid={sid}, mmtype={mmtype}')
|
||||
|
||||
def is_addrlist_id(proto,s):
|
||||
return get_obj( AddrListID, proto=proto, id_str=s, silent=False, return_bool=True )
|
||||
def is_addrlist_id(proto, s):
|
||||
return get_obj(AddrListID, proto=proto, id_str=s, silent=False, return_bool=True)
|
||||
|
||||
class MMGenID(HiliteStr,InitErrors,MMGenObject):
|
||||
class MMGenID(HiliteStr, InitErrors, MMGenObject):
|
||||
color = 'orange'
|
||||
width = 0
|
||||
trunc_ok = False
|
||||
def __new__(cls,proto,id_str):
|
||||
def __new__(cls, proto, id_str):
|
||||
try:
|
||||
ss = str(id_str).split(':')
|
||||
assert len(ss) in (2,3),'not 2 or 3 colon-separated items'
|
||||
t = proto.addr_type((ss[1],proto.dfl_mmtype)[len(ss)==2])
|
||||
me = str.__new__(cls,f'{ss[0]}:{t}:{ss[-1]}')
|
||||
assert len(ss) in (2, 3), 'not 2 or 3 colon-separated items'
|
||||
t = proto.addr_type((ss[1], proto.dfl_mmtype)[len(ss)==2])
|
||||
me = str.__new__(cls, f'{ss[0]}:{t}:{ss[-1]}')
|
||||
me.sid = SeedID(sid=ss[0])
|
||||
me.idx = AddrIdx(ss[-1])
|
||||
me.mmtype = t
|
||||
assert t in proto.mmtypes, f'{t}: invalid address type for {proto.cls_name}'
|
||||
me.al_id = str.__new__(AddrListID,me.sid+':'+me.mmtype) # checks already done
|
||||
me.al_id = str.__new__(AddrListID, me.sid+':'+me.mmtype) # checks already done
|
||||
me.sort_key = f'{me.sid}:{me.mmtype}:{me.idx:0{me.idx.max_digits}}'
|
||||
me.proto = proto
|
||||
return me
|
||||
except Exception as e:
|
||||
return cls.init_fail(e,id_str)
|
||||
return cls.init_fail(e, id_str)
|
||||
|
||||
def is_mmgen_id(proto,s):
|
||||
return get_obj( MMGenID, proto=proto, id_str=s, silent=True, return_bool=True )
|
||||
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):
|
||||
color = 'cyan'
|
||||
|
|
@ -152,7 +153,7 @@ class CoinAddr(HiliteStr, InitErrors, MMGenObject):
|
|||
trunc_ok = False
|
||||
|
||||
def __new__(cls, proto, addr):
|
||||
if isinstance(addr,cls):
|
||||
if isinstance(addr, cls):
|
||||
return addr
|
||||
try:
|
||||
ap = proto.decode_addr(addr)
|
||||
|
|
@ -171,18 +172,18 @@ class CoinAddr(HiliteStr, InitErrors, MMGenObject):
|
|||
me.proto = proto
|
||||
return me
|
||||
except Exception as e:
|
||||
return cls.init_fail(e,addr,objname=f'{proto.cls_name} address')
|
||||
return cls.init_fail(e, addr, objname=f'{proto.cls_name} address')
|
||||
|
||||
@property
|
||||
def parsed(self):
|
||||
if not hasattr(self,'_parsed'):
|
||||
self._parsed = self.proto.parse_addr(self.ver_bytes,self.bytes,self.addr_fmt)
|
||||
if not hasattr(self, '_parsed'):
|
||||
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 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, view_pref, width, color=False):
|
||||
s = self.views[view_pref]
|
||||
|
|
@ -191,11 +192,11 @@ class CoinAddr(HiliteStr, InitErrors, MMGenObject):
|
|||
def hl(self, view_pref, color=True):
|
||||
return getattr(color_mod, self.color)(self.views[view_pref]) if color else self.views[view_pref]
|
||||
|
||||
def is_coin_addr(proto,s):
|
||||
return get_obj( CoinAddr, proto=proto, addr=s, silent=True, return_bool=True )
|
||||
def is_coin_addr(proto, s):
|
||||
return get_obj(CoinAddr, proto=proto, addr=s, silent=True, return_bool=True)
|
||||
|
||||
class TokenAddr(CoinAddr):
|
||||
color = 'blue'
|
||||
|
||||
def ViewKey(proto,viewkey_str):
|
||||
def ViewKey(proto, viewkey_str):
|
||||
return proto.viewkey(viewkey_str)
|
||||
|
|
|
|||
|
|
@ -21,15 +21,15 @@ addrdata: MMGen AddrData and related classes
|
|||
"""
|
||||
|
||||
from .cfg import gc
|
||||
from .util import fmt,die
|
||||
from .util import fmt, die
|
||||
from .base_obj import AsyncInit
|
||||
from .obj import MMGenObject,MMGenDict,get_obj
|
||||
from .addr import MMGenID,AddrListID
|
||||
from .addrlist import AddrListEntry,AddrListData,AddrList
|
||||
from .obj import MMGenObject, MMGenDict, get_obj
|
||||
from .addr import MMGenID, AddrListID
|
||||
from .addrlist import AddrListEntry, AddrListData, AddrList
|
||||
|
||||
class AddrData(MMGenObject):
|
||||
|
||||
def __init__(self,proto,*args,**kwargs):
|
||||
def __init__(self, proto, *args, **kwargs):
|
||||
self.al_ids = {}
|
||||
self.proto = proto
|
||||
self.rpc = None
|
||||
|
|
@ -37,63 +37,63 @@ class AddrData(MMGenObject):
|
|||
def seed_ids(self):
|
||||
return list(self.al_ids.keys())
|
||||
|
||||
def addrlist(self,al_id):
|
||||
def addrlist(self, al_id):
|
||||
# TODO: Validate al_id
|
||||
if al_id in self.al_ids:
|
||||
return self.al_ids[al_id]
|
||||
|
||||
def mmaddr2coinaddr(self,mmaddr):
|
||||
al_id,idx = MMGenID(self.proto,mmaddr).rsplit(':',1)
|
||||
def mmaddr2coinaddr(self, mmaddr):
|
||||
al_id, idx = MMGenID(self.proto, mmaddr).rsplit(':', 1)
|
||||
coinaddr = ''
|
||||
if al_id in self.al_ids:
|
||||
coinaddr = self.addrlist(al_id).coinaddr(int(idx))
|
||||
return coinaddr or None
|
||||
|
||||
def coinaddr2mmaddr(self,coinaddr):
|
||||
def coinaddr2mmaddr(self, coinaddr):
|
||||
d = self.make_reverse_dict([coinaddr])
|
||||
return (list(d.values())[0][0]) if d else None
|
||||
|
||||
def add(self,addrlist):
|
||||
if isinstance(addrlist,AddrList):
|
||||
def add(self, addrlist):
|
||||
if isinstance(addrlist, AddrList):
|
||||
self.al_ids[addrlist.al_id] = addrlist
|
||||
return True
|
||||
else:
|
||||
raise TypeError(f'Error: object {addrlist!r} is not an instance of AddrList')
|
||||
|
||||
def make_reverse_dict(self,coinaddrs):
|
||||
def make_reverse_dict(self, coinaddrs):
|
||||
d = MMGenDict()
|
||||
for al_id in self.al_ids:
|
||||
d.update(self.al_ids[al_id].make_reverse_dict_addrlist(coinaddrs))
|
||||
return d
|
||||
|
||||
class TwAddrData(AddrData,metaclass=AsyncInit):
|
||||
class TwAddrData(AddrData, metaclass=AsyncInit):
|
||||
|
||||
def __new__(cls,cfg,proto,*args,**kwargs):
|
||||
return MMGenObject.__new__(proto.base_proto_subclass(cls,'addrdata'))
|
||||
def __new__(cls, cfg, proto, *args, **kwargs):
|
||||
return MMGenObject.__new__(proto.base_proto_subclass(cls, 'addrdata'))
|
||||
|
||||
async def __init__(self,cfg,proto,twctl=None):
|
||||
async def __init__(self, cfg, proto, twctl=None):
|
||||
from .rpc import rpc_init
|
||||
from .tw.shared import TwLabel
|
||||
from .seed import SeedID
|
||||
self.cfg = cfg
|
||||
self.proto = proto
|
||||
self.rpc = await rpc_init(cfg,proto)
|
||||
self.rpc = await rpc_init(cfg, proto)
|
||||
self.al_ids = {}
|
||||
twd = await self.get_tw_data(twctl)
|
||||
out,i = {},0
|
||||
for acct,addr_array in twd:
|
||||
l = get_obj(TwLabel,proto=self.proto,text=acct,silent=True)
|
||||
out, i = {}, 0
|
||||
for acct, addr_array in twd:
|
||||
l = get_obj(TwLabel, proto=self.proto, text=acct, silent=True)
|
||||
if l and l.mmid.type == 'mmgen':
|
||||
obj = l.mmid.obj
|
||||
if len(addr_array) != 1:
|
||||
message = self.msgs['multiple_acct_addrs'].strip().format( acct=acct, proj=gc.proj_name )
|
||||
die(3, fmt( message, indent=' ' ))
|
||||
message = self.msgs['multiple_acct_addrs'].strip().format(acct=acct, proj=gc.proj_name)
|
||||
die(3, fmt(message, indent=' '))
|
||||
al_id = AddrListID(
|
||||
sid = SeedID(sid=obj.sid),
|
||||
mmtype = self.proto.addr_type(obj.mmtype) )
|
||||
mmtype = self.proto.addr_type(obj.mmtype))
|
||||
if al_id not in out:
|
||||
out[al_id] = []
|
||||
out[al_id].append(AddrListEntry(self.proto,idx=obj.idx,addr=addr_array[0],comment=l.comment))
|
||||
out[al_id].append(AddrListEntry(self.proto, idx=obj.idx, addr=addr_array[0], comment=l.comment))
|
||||
i += 1
|
||||
|
||||
self.cfg._util.vmsg(f'{i} {gc.proj_name} addresses found, {len(twd)} accounts total')
|
||||
|
|
@ -103,5 +103,5 @@ class TwAddrData(AddrData,metaclass=AsyncInit):
|
|||
self.cfg,
|
||||
self.proto,
|
||||
al_id = al_id,
|
||||
adata = AddrListData(sorted( out[al_id], key=lambda a: a.idx ))
|
||||
adata = AddrListData(sorted(out[al_id], key=lambda a: a.idx))
|
||||
))
|
||||
|
|
|
|||
|
|
@ -21,13 +21,13 @@ addrfile: Address and password file classes for the MMGen suite
|
|||
"""
|
||||
|
||||
from .cfg import gc
|
||||
from .util import msg,die,capfirst
|
||||
from .util import msg, die, capfirst
|
||||
from .protocol import init_proto
|
||||
from .obj import MMGenObject,TwComment,WalletPassword,MMGenPWIDString
|
||||
from .seed import SeedID,is_seed_id
|
||||
from .obj import MMGenObject, TwComment, WalletPassword, MMGenPWIDString
|
||||
from .seed import SeedID, is_seed_id
|
||||
from .key import PrivKey
|
||||
from .addr import ViewKey,AddrListID,MMGenAddrType,MMGenPasswordType,is_addr_idx
|
||||
from .addrlist import KeyList,AddrListData
|
||||
from .addr import ViewKey, AddrListID, MMGenAddrType, MMGenPasswordType, is_addr_idx
|
||||
from .addrlist import KeyList, AddrListData
|
||||
|
||||
class AddrFile(MMGenObject):
|
||||
desc = 'addresses'
|
||||
|
|
@ -43,7 +43,7 @@ class AddrFile(MMGenObject):
|
|||
# The label may contain any printable ASCII symbol.
|
||||
"""
|
||||
|
||||
def __init__(self,parent):
|
||||
def __init__(self, parent):
|
||||
self.parent = parent
|
||||
self.cfg = parent.cfg
|
||||
self.infile = None
|
||||
|
|
@ -53,7 +53,7 @@ class AddrFile(MMGenObject):
|
|||
from .crypto import Crypto
|
||||
self.fmt_data = Crypto(self.cfg).mmgen_encrypt(
|
||||
data = self.fmt_data.encode(),
|
||||
desc = f'new {self.parent.desc} list' )
|
||||
desc = f'new {self.parent.desc} list')
|
||||
self.ext += f'.{Crypto.mmenc_ext}'
|
||||
|
||||
@property
|
||||
|
|
@ -61,7 +61,7 @@ class AddrFile(MMGenObject):
|
|||
return '{}{}.{}'.format(
|
||||
self.parent.id_str,
|
||||
('.' + self.parent.proto.network) if self.parent.proto.testnet else '',
|
||||
self.ext )
|
||||
self.ext)
|
||||
|
||||
def write(
|
||||
self,
|
||||
|
|
@ -92,9 +92,9 @@ class AddrFile(MMGenObject):
|
|||
)
|
||||
return self.parent.al_id.sid + (' ' if lbl_p2 else '') + lbl_p2
|
||||
|
||||
def format(self,add_comments=False):
|
||||
def format(self, add_comments=False):
|
||||
p = self.parent
|
||||
if p.gen_passwds and p.pw_fmt in ('bip39','xmrseed'):
|
||||
if p.gen_passwds and p.pw_fmt in ('bip39', 'xmrseed'):
|
||||
desc_pfx = f'{p.pw_fmt.upper()} '
|
||||
hdr2 = ''
|
||||
else:
|
||||
|
|
@ -102,9 +102,9 @@ class AddrFile(MMGenObject):
|
|||
hdr2 = self.text_label_header
|
||||
out = [
|
||||
f'# {gc.proj_name} {desc_pfx}{p.desc} file\n#\n'
|
||||
+ self.header.strip().format( pnm=gc.proj_name )
|
||||
+ self.header.strip().format(pnm=gc.proj_name)
|
||||
+ '\n'
|
||||
+ hdr2.lstrip().format( n=TwComment.max_screen_width )
|
||||
+ hdr2.lstrip().format(n=TwComment.max_screen_width)
|
||||
+ '#\n'
|
||||
]
|
||||
|
||||
|
|
@ -113,41 +113,41 @@ class AddrFile(MMGenObject):
|
|||
out.append('# Record this value to a secure location.\n')
|
||||
|
||||
lbl = self.make_label()
|
||||
self.parent.dmsg_sc('lbl',lbl[9:])
|
||||
self.parent.dmsg_sc('lbl', lbl[9:])
|
||||
out.append(f'{lbl} {{')
|
||||
|
||||
fs = ' {:<%s} {:<34}{}' % len(str(p.data[-1].idx))
|
||||
for e in p.data:
|
||||
c = ' ' + e.comment if add_comments and e.comment else ''
|
||||
if type(p) is KeyList:
|
||||
out.append(fs.format( e.idx, f'{p.al_id.mmtype.wif_label}: {e.sec.wif}', c ))
|
||||
out.append(fs.format(e.idx, f'{p.al_id.mmtype.wif_label}: {e.sec.wif}', c))
|
||||
elif type(p).__name__ == 'PasswordList':
|
||||
out.append(fs.format(e.idx,e.passwd,c))
|
||||
out.append(fs.format(e.idx, e.passwd, c))
|
||||
else: # First line with idx
|
||||
out.append(fs.format(e.idx, e.addr.views[e.addr.view_pref], c))
|
||||
if p.has_keys:
|
||||
if self.cfg.b16:
|
||||
out.append(fs.format( '', f'orig_hex: {e.sec.orig_bytes.hex()}', c ))
|
||||
out.append(fs.format('', f'orig_hex: {e.sec.orig_bytes.hex()}', c))
|
||||
if type(self) is not ViewKeyAddrFile:
|
||||
out.append(fs.format( '', f'{p.al_id.mmtype.wif_label}: {e.sec.wif}', c ))
|
||||
for k in ('viewkey','wallet_passwd'):
|
||||
v = getattr(e,k)
|
||||
out.append(fs.format('', f'{p.al_id.mmtype.wif_label}: {e.sec.wif}', c))
|
||||
for k in ('viewkey', 'wallet_passwd'):
|
||||
v = getattr(e, k)
|
||||
if v:
|
||||
out.append(fs.format( '', f'{k}: {v}', c ))
|
||||
out.append(fs.format('', f'{k}: {v}', c))
|
||||
|
||||
out.append('}')
|
||||
self.fmt_data = '\n'.join([l.rstrip() for l in out]) + '\n'
|
||||
return self.fmt_data
|
||||
|
||||
def get_line(self,lines):
|
||||
ret = lines.pop(0).split(None,2)
|
||||
def get_line(self, lines):
|
||||
ret = lines.pop(0).split(None, 2)
|
||||
self.line_ctr += 1
|
||||
if ret[0] == 'orig_hex:': # hacky
|
||||
ret = lines.pop(0).split(None,2)
|
||||
ret = lines.pop(0).split(None, 2)
|
||||
self.line_ctr += 1
|
||||
return ret if len(ret) == 3 else ret + ['']
|
||||
|
||||
def parse_file_body(self,lines):
|
||||
def parse_file_body(self, lines):
|
||||
|
||||
p = self.parent
|
||||
ret = AddrListData()
|
||||
|
|
@ -155,39 +155,39 @@ class AddrFile(MMGenObject):
|
|||
iifs = "{!r}: invalid identifier [expected '{}:']"
|
||||
|
||||
while lines:
|
||||
idx,addr,comment = self.get_line(lines)
|
||||
idx, addr, comment = self.get_line(lines)
|
||||
|
||||
assert is_addr_idx(idx), f'invalid address index {idx!r}'
|
||||
p.check_format(addr)
|
||||
|
||||
a = le(**{ 'proto': p.proto, 'idx':int(idx), p.main_attr:addr, 'comment':comment })
|
||||
a = le(**{'proto': p.proto, 'idx':int(idx), p.main_attr:addr, 'comment':comment})
|
||||
|
||||
if p.has_keys: # order: wif,(orig_hex),viewkey,wallet_passwd
|
||||
if p.has_keys: # order: wif, (orig_hex), viewkey, wallet_passwd
|
||||
if type(self) is not ViewKeyAddrFile:
|
||||
d = self.get_line(lines)
|
||||
assert d[0] == p.al_id.mmtype.wif_label+':', iifs.format(d[0],p.al_id.mmtype.wif_label)
|
||||
a.sec = PrivKey(proto=p.proto,wif=d[1])
|
||||
for k,dtype,add_proto in (
|
||||
('viewkey',ViewKey,True),
|
||||
('wallet_passwd',WalletPassword,False) ):
|
||||
assert d[0] == p.al_id.mmtype.wif_label+':', iifs.format(d[0], p.al_id.mmtype.wif_label)
|
||||
a.sec = PrivKey(proto=p.proto, wif=d[1])
|
||||
for k, dtype, add_proto in (
|
||||
('viewkey', ViewKey, True),
|
||||
('wallet_passwd', WalletPassword, False)):
|
||||
if k in p.al_id.mmtype.extra_attrs:
|
||||
d = self.get_line(lines)
|
||||
assert d[0] == k+':', iifs.format(d[0],k)
|
||||
setattr(a,k,dtype( *((p.proto,d[1]) if add_proto else (d[1],)) ) )
|
||||
assert d[0] == k+':', iifs.format(d[0], k)
|
||||
setattr(a, k, dtype(*((p.proto, d[1]) if add_proto else (d[1],))))
|
||||
|
||||
ret.append(a)
|
||||
|
||||
if type(self) is not ViewKeyAddrFile and p.has_keys and p.ka_validity_chk is not False:
|
||||
|
||||
def verify_keys():
|
||||
from .addrgen import KeyGenerator,AddrGenerator
|
||||
kg = KeyGenerator( self.cfg, p.proto, p.al_id.mmtype.pubkey_type )
|
||||
ag = AddrGenerator( self.cfg, p.proto, p.al_id.mmtype )
|
||||
from .addrgen import KeyGenerator, AddrGenerator
|
||||
kg = KeyGenerator(self.cfg, p.proto, p.al_id.mmtype.pubkey_type)
|
||||
ag = AddrGenerator(self.cfg, p.proto, p.al_id.mmtype)
|
||||
llen = len(ret)
|
||||
qmsg_r = p.cfg._util.qmsg_r
|
||||
for n,e in enumerate(ret):
|
||||
for n, e in enumerate(ret):
|
||||
qmsg_r(f'\rVerifying keys {n+1}/{llen}')
|
||||
assert e.addr == ag.to_addr(kg.gen_data(e.sec)),(
|
||||
assert e.addr == ag.to_addr(kg.gen_data(e.sec)), (
|
||||
f'Key doesn’t match address!\n {e.sec.wif}\n {e.addr}')
|
||||
p.cfg._util.qmsg(' - done')
|
||||
|
||||
|
|
@ -195,12 +195,12 @@ class AddrFile(MMGenObject):
|
|||
verify_keys()
|
||||
else:
|
||||
from .ui import keypress_confirm
|
||||
if keypress_confirm( p.cfg, 'Check key-to-address validity?' ):
|
||||
if keypress_confirm(p.cfg, 'Check key-to-address validity?'):
|
||||
verify_keys()
|
||||
|
||||
return ret
|
||||
|
||||
def parse_file(self,fn,buf=[],exit_on_error=True):
|
||||
def parse_file(self, fn, buf=[], exit_on_error=True):
|
||||
|
||||
def parse_addrfile_label(lbl):
|
||||
"""
|
||||
|
|
@ -232,7 +232,7 @@ class AddrFile(MMGenObject):
|
|||
network = 'mainnet'
|
||||
|
||||
from .proto.btc.params import mainnet
|
||||
if lbl in [MMGenAddrType(mainnet,key).name for key in mainnet.mmtypes]:
|
||||
if lbl in [MMGenAddrType(mainnet, key).name for key in mainnet.mmtypes]:
|
||||
coin, mmtype_key = ('BTC', lbl)
|
||||
elif ':' in lbl: # first component is coin, second is mmtype_key
|
||||
coin, mmtype_key = lbl.split(':')
|
||||
|
|
@ -249,7 +249,7 @@ class AddrFile(MMGenObject):
|
|||
p = self.parent
|
||||
|
||||
from .fileutil import get_lines_from_file
|
||||
lines = get_lines_from_file( p.cfg, fn, p.desc+' data', trim_comments=True )
|
||||
lines = get_lines_from_file(p.cfg, fn, p.desc+' data', trim_comments=True)
|
||||
|
||||
try:
|
||||
assert len(lines) >= 3, f'Too few lines in address file ({len(lines)})'
|
||||
|
|
@ -267,15 +267,15 @@ class AddrFile(MMGenObject):
|
|||
p.set_pw_fmt(ss[0])
|
||||
p.set_pw_len(ss[1])
|
||||
p.pw_id_str = MMGenPWIDString(ls.pop())
|
||||
modname,funcname = p.pw_info[p.pw_fmt].chk_func.split('.')
|
||||
modname, funcname = p.pw_info[p.pw_fmt].chk_func.split('.')
|
||||
import importlib
|
||||
p.chk_func = getattr(importlib.import_module('mmgen.'+modname),funcname)
|
||||
proto = init_proto( p.cfg, 'btc' ) # FIXME: dummy protocol
|
||||
mmtype = MMGenPasswordType(proto,'P')
|
||||
p.chk_func = getattr(importlib.import_module('mmgen.'+modname), funcname)
|
||||
proto = init_proto(p.cfg, 'btc') # FIXME: dummy protocol
|
||||
mmtype = MMGenPasswordType(proto, 'P')
|
||||
elif len(ls) == 1:
|
||||
proto,mmtype = parse_addrfile_label(ls[0])
|
||||
proto, mmtype = parse_addrfile_label(ls[0])
|
||||
elif len(ls) == 0:
|
||||
proto = init_proto( p.cfg, 'btc' )
|
||||
proto = init_proto(p.cfg, 'btc')
|
||||
mmtype = proto.addr_type('L')
|
||||
else:
|
||||
raise ValueError(f'{lines[0]}: Invalid first line for {p.gen_desc} file {fn!r}')
|
||||
|
|
@ -289,22 +289,22 @@ class AddrFile(MMGenObject):
|
|||
raise ValueError(
|
||||
f'{p.desc} file is '
|
||||
+ f'{proto.base_coin} {proto.network} but protocol is '
|
||||
+ f'{p.proto.base_coin} {p.proto.network}' )
|
||||
+ f'{p.proto.base_coin} {p.proto.network}')
|
||||
|
||||
p.base_coin = proto.base_coin
|
||||
p.network = proto.network
|
||||
p.al_id = AddrListID( sid=SeedID(sid=sid), mmtype=mmtype )
|
||||
p.al_id = AddrListID(sid=SeedID(sid=sid), mmtype=mmtype)
|
||||
|
||||
data = self.parse_file_body(lines[1:-1])
|
||||
assert isinstance(data,list),'Invalid file body data'
|
||||
assert isinstance(data, list), 'Invalid file body data'
|
||||
except Exception as e:
|
||||
m = 'Invalid data in {} list file {!r}{} ({!s})'.format(
|
||||
p.desc,
|
||||
self.infile,
|
||||
(f', content line {self.line_ctr}' if self.line_ctr else ''),
|
||||
e )
|
||||
e)
|
||||
if exit_on_error:
|
||||
die(3,m)
|
||||
die(3, m)
|
||||
else:
|
||||
msg(m)
|
||||
return False
|
||||
|
|
@ -339,13 +339,13 @@ class PasswordFile(AddrFile):
|
|||
# password. The label may contain any printable ASCII symbol.
|
||||
"""
|
||||
|
||||
def get_line(self,lines):
|
||||
def get_line(self, lines):
|
||||
|
||||
self.line_ctr += 1
|
||||
p = self.parent
|
||||
|
||||
if p.pw_fmt in ('bip39','xmrseed'):
|
||||
ret = lines.pop(0).split(None,p.pw_len+1)
|
||||
if p.pw_fmt in ('bip39', 'xmrseed'):
|
||||
ret = lines.pop(0).split(None, p.pw_len+1)
|
||||
if len(ret) > p.pw_len+1:
|
||||
m1 = f'extraneous text {ret[p.pw_len+1]!r} found after password'
|
||||
m2 = '[bare comments not allowed in BIP39 password files]'
|
||||
|
|
@ -353,10 +353,10 @@ class PasswordFile(AddrFile):
|
|||
elif len(ret) < p.pw_len+1:
|
||||
m = f'invalid password length {len(ret)-1}'
|
||||
else:
|
||||
return (ret[0],' '.join(ret[1:p.pw_len+1]),'')
|
||||
return (ret[0], ' '.join(ret[1:p.pw_len+1]), '')
|
||||
raise ValueError(m)
|
||||
else:
|
||||
ret = lines.pop(0).split(None,2)
|
||||
ret = lines.pop(0).split(None, 2)
|
||||
return ret if len(ret) == 3 else ret + ['']
|
||||
|
||||
def make_label(self):
|
||||
|
|
|
|||
|
|
@ -24,19 +24,19 @@ from .keygen import KeyGenerator # convenience import
|
|||
|
||||
# decorator for to_addr() and to_viewkey()
|
||||
def check_data(orig_func):
|
||||
def f(self,data):
|
||||
def f(self, data):
|
||||
assert data.pubkey_type == self.pubkey_type, 'addrgen.py:check_data() pubkey_type mismatch'
|
||||
assert data.compressed == self.compressed,(
|
||||
assert data.compressed == self.compressed, (
|
||||
f'addrgen.py:check_data() expected compressed={self.compressed} but got compressed={data.compressed}'
|
||||
)
|
||||
return orig_func(self,data)
|
||||
return orig_func(self, data)
|
||||
return f
|
||||
|
||||
class addr_generator:
|
||||
|
||||
class base:
|
||||
|
||||
def __init__(self,cfg,proto,addr_type):
|
||||
def __init__(self, cfg, proto, addr_type):
|
||||
self.proto = proto
|
||||
self.pubkey_type = addr_type.pubkey_type
|
||||
self.compressed = addr_type.compressed
|
||||
|
|
@ -44,12 +44,12 @@ class addr_generator:
|
|||
|
||||
class keccak(base):
|
||||
|
||||
def __init__(self,cfg,proto,addr_type):
|
||||
super().__init__(cfg,proto,addr_type)
|
||||
def __init__(self, cfg, proto, addr_type):
|
||||
super().__init__(cfg, proto, addr_type)
|
||||
from .util2 import get_keccak
|
||||
self.keccak_256 = get_keccak(cfg)
|
||||
|
||||
def AddrGenerator(cfg,proto,addr_type):
|
||||
def AddrGenerator(cfg, proto, addr_type):
|
||||
"""
|
||||
factory function returning an address generator for the specified address type
|
||||
"""
|
||||
|
|
@ -67,7 +67,7 @@ def AddrGenerator(cfg,proto,addr_type):
|
|||
from .addr import MMGenAddrType
|
||||
|
||||
if type(addr_type) is str:
|
||||
addr_type = MMGenAddrType(proto=proto,id_str=addr_type)
|
||||
addr_type = MMGenAddrType(proto=proto, id_str=addr_type)
|
||||
elif type(addr_type) is MMGenAddrType:
|
||||
assert addr_type in proto.mmtypes, f'{addr_type}: invalid address type for coin {proto.coin}'
|
||||
else:
|
||||
|
|
@ -76,4 +76,4 @@ def AddrGenerator(cfg,proto,addr_type):
|
|||
import importlib
|
||||
return getattr(
|
||||
importlib.import_module(f'mmgen.proto.{package_map[addr_type.name]}.addrgen'),
|
||||
addr_type.name )(cfg,proto,addr_type)
|
||||
addr_type.name)(cfg, proto, addr_type)
|
||||
|
|
|
|||
|
|
@ -20,17 +20,17 @@
|
|||
addrlist: Address list classes for the MMGen suite
|
||||
"""
|
||||
|
||||
from .util import suf,make_chksum_N,Msg,die
|
||||
from .objmethods import MMGenObject,HiliteStr,InitErrors
|
||||
from .obj import MMGenListItem,ListItemAttr,MMGenDict,TwComment,WalletPassword
|
||||
from .util import suf, make_chksum_N, Msg, die
|
||||
from .objmethods import MMGenObject, HiliteStr, InitErrors
|
||||
from .obj import MMGenListItem, ListItemAttr, MMGenDict, TwComment, WalletPassword
|
||||
from .key import PrivKey
|
||||
from .addr import MMGenID,MMGenAddrType,CoinAddr,AddrIdx,AddrListID,ViewKey
|
||||
from .addr import MMGenID, MMGenAddrType, CoinAddr, AddrIdx, AddrListID, ViewKey
|
||||
|
||||
class AddrIdxList(tuple,InitErrors,MMGenObject):
|
||||
class AddrIdxList(tuple, InitErrors, MMGenObject):
|
||||
|
||||
max_len = 1000000
|
||||
|
||||
def __new__(cls,fmt_str=None,idx_list=None,sep=','):
|
||||
def __new__(cls, fmt_str=None, idx_list=None, sep=','):
|
||||
try:
|
||||
if fmt_str:
|
||||
def gen():
|
||||
|
|
@ -45,9 +45,9 @@ class AddrIdxList(tuple,InitErrors,MMGenObject):
|
|||
else:
|
||||
raise ValueError(f'{i}: invalid range')
|
||||
idx_list = tuple(gen())
|
||||
return tuple.__new__(cls,sorted({AddrIdx(i) for i in (idx_list or [])}))
|
||||
return tuple.__new__(cls, sorted({AddrIdx(i) for i in (idx_list or [])}))
|
||||
except Exception as e:
|
||||
return cls.init_fail(e,idx_list or fmt_str)
|
||||
return cls.init_fail(e, idx_list or fmt_str)
|
||||
|
||||
@property
|
||||
def id_str(self):
|
||||
|
|
@ -74,36 +74,36 @@ class AddrIdxList(tuple,InitErrors,MMGenObject):
|
|||
|
||||
class AddrListEntryBase(MMGenListItem):
|
||||
invalid_attrs = {'proto'}
|
||||
def __init__(self,proto,**kwargs):
|
||||
def __init__(self, proto, **kwargs):
|
||||
self.__dict__['proto'] = proto
|
||||
MMGenListItem.__init__(self,**kwargs)
|
||||
MMGenListItem.__init__(self, **kwargs)
|
||||
|
||||
class AddrListEntry(AddrListEntryBase):
|
||||
addr = ListItemAttr(CoinAddr,include_proto=True)
|
||||
addr_p2pkh = ListItemAttr(CoinAddr,include_proto=True)
|
||||
addr = ListItemAttr(CoinAddr, include_proto=True)
|
||||
addr_p2pkh = ListItemAttr(CoinAddr, include_proto=True)
|
||||
idx = ListItemAttr(AddrIdx) # not present in flat addrlists
|
||||
comment = ListItemAttr(TwComment,reassign_ok=True)
|
||||
sec = ListItemAttr(PrivKey,include_proto=True)
|
||||
viewkey = ListItemAttr(ViewKey,include_proto=True)
|
||||
comment = ListItemAttr(TwComment, reassign_ok=True)
|
||||
sec = ListItemAttr(PrivKey, include_proto=True)
|
||||
viewkey = ListItemAttr(ViewKey, include_proto=True)
|
||||
wallet_passwd = ListItemAttr(WalletPassword)
|
||||
|
||||
class AddrListChksum(HiliteStr):
|
||||
color = 'pink'
|
||||
trunc_ok = False
|
||||
|
||||
def __new__(cls,addrlist):
|
||||
def __new__(cls, addrlist):
|
||||
ea = addrlist.al_id.mmtype.extra_attrs or () # add viewkey and passwd to the mix, if present
|
||||
lines = [' '.join(
|
||||
addrlist.chksum_rec_f(e) +
|
||||
tuple(getattr(e,a) for a in ea if getattr(e,a))
|
||||
tuple(getattr(e, a) for a in ea if getattr(e, a))
|
||||
) for e in addrlist.data]
|
||||
return str.__new__(cls,make_chksum_N(' '.join(lines), nchars=16, sep=True))
|
||||
return str.__new__(cls, make_chksum_N(' '.join(lines), nchars=16, sep=True))
|
||||
|
||||
class AddrListIDStr(HiliteStr):
|
||||
color = 'green'
|
||||
trunc_ok = False
|
||||
|
||||
def __new__(cls,addrlist,fmt_str=None):
|
||||
def __new__(cls, addrlist, fmt_str=None):
|
||||
idxs = [e.idx for e in addrlist.data]
|
||||
prev = idxs[0]
|
||||
ret = [prev]
|
||||
|
|
@ -116,7 +116,7 @@ class AddrListIDStr(HiliteStr):
|
|||
ret.extend(['-', prev])
|
||||
ret.extend([',', i])
|
||||
prev = i
|
||||
s = ''.join(map(str,ret))
|
||||
s = ''.join(map(str, ret))
|
||||
|
||||
if fmt_str:
|
||||
ret = fmt_str.format(s)
|
||||
|
|
@ -127,14 +127,14 @@ class AddrListIDStr(HiliteStr):
|
|||
ret = '{}{}{}[{}]'.format(
|
||||
addrlist.al_id.sid,
|
||||
(f'-{coin}', '')[coin == 'BTC'],
|
||||
(f'-{mmtype}', '')[mmtype in ('L','E')],
|
||||
(f'-{mmtype}', '')[mmtype in ('L', 'E')],
|
||||
s)
|
||||
|
||||
addrlist.dmsg_sc('id_str',ret[8:].split('[')[0])
|
||||
addrlist.dmsg_sc('id_str', ret[8:].split('[')[0])
|
||||
|
||||
return str.__new__(cls,ret)
|
||||
return str.__new__(cls, ret)
|
||||
|
||||
class AddrListData(list,MMGenObject):
|
||||
class AddrListData(list, MMGenObject):
|
||||
pass
|
||||
|
||||
class AddrList(MMGenObject): # Address info for a single seed ID
|
||||
|
|
@ -149,10 +149,10 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
has_keys = False
|
||||
chksum_rec_f = lambda foo, e: (str(e.idx), e.addr.views[e.addr.view_pref])
|
||||
|
||||
def dmsg_sc(self,desc,data):
|
||||
def dmsg_sc(self, desc, data):
|
||||
Msg(f'sc_debug_{desc}: {data}')
|
||||
|
||||
def noop(self,desc,data):
|
||||
def noop(self, desc, data):
|
||||
pass
|
||||
|
||||
def __init__(
|
||||
|
|
@ -171,7 +171,7 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
key_address_validity_check = None, # None=prompt user, True=check without prompt, False=skip check
|
||||
skip_chksum = False,
|
||||
skip_chksum_msg = False,
|
||||
add_p2pkh = False ):
|
||||
add_p2pkh = False):
|
||||
|
||||
self.cfg = cfg
|
||||
self.ka_validity_chk = key_address_validity_check
|
||||
|
|
@ -183,9 +183,9 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
self.dmsg_sc = self.noop
|
||||
|
||||
if seed and addr_idxs: # data from seed + idxs
|
||||
self.al_id = AddrListID( sid=seed.sid, mmtype=MMGenAddrType(proto, mmtype or proto.dfl_mmtype) )
|
||||
self.al_id = AddrListID(sid=seed.sid, mmtype=MMGenAddrType(proto, mmtype or proto.dfl_mmtype))
|
||||
src = 'gen'
|
||||
adata = self.generate(seed, addr_idxs if isinstance(addr_idxs,AddrIdxList) else AddrIdxList(addr_idxs))
|
||||
adata = self.generate(seed, addr_idxs if isinstance(addr_idxs, AddrIdxList) else AddrIdxList(addr_idxs))
|
||||
do_chksum = True
|
||||
elif addrfile: # data from MMGen address file
|
||||
self.infile = addrfile
|
||||
|
|
@ -196,21 +196,21 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
elif addrlist: # data from flat address list
|
||||
self.al_id = None
|
||||
from .util import remove_dups
|
||||
addrlist = remove_dups(addrlist,edesc='address',desc='address list')
|
||||
adata = AddrListData([AddrListEntry(proto=proto,addr=a) for a in addrlist])
|
||||
addrlist = remove_dups(addrlist, edesc='address', desc='address list')
|
||||
adata = AddrListData([AddrListEntry(proto=proto, addr=a) for a in addrlist])
|
||||
elif keylist: # data from flat key list
|
||||
self.al_id = None
|
||||
from .util import remove_dups
|
||||
keylist = remove_dups(keylist,edesc='key',desc='key list',hide=True)
|
||||
adata = AddrListData([AddrListEntry(proto=proto,sec=PrivKey(proto=proto,wif=k)) for k in keylist])
|
||||
keylist = remove_dups(keylist, edesc='key', desc='key list', hide=True)
|
||||
adata = AddrListData([AddrListEntry(proto=proto, sec=PrivKey(proto=proto, wif=k)) for k in keylist])
|
||||
elif seed or addr_idxs:
|
||||
die(3,'Must specify both seed and addr indexes')
|
||||
die(3, 'Must specify both seed and addr indexes')
|
||||
elif al_id or adata:
|
||||
die(3,'Must specify both al_id and adata')
|
||||
die(3, 'Must specify both al_id and adata')
|
||||
else:
|
||||
die(3,f'Incorrect arguments for {type(self).__name__}')
|
||||
die(3, f'Incorrect arguments for {type(self).__name__}')
|
||||
|
||||
# al_id,adata now set
|
||||
# al_id, adata now set
|
||||
self.data = adata
|
||||
self.num_addrs = len(adata)
|
||||
self.fmt_data = ''
|
||||
|
|
@ -221,7 +221,7 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
|
||||
if type(self) is ViewKeyAddrList:
|
||||
if not 'viewkey' in self.al_id.mmtype.extra_attrs:
|
||||
die(1,f'viewkeys not supported for address type {self.al_id.mmtype.desc!r}')
|
||||
die(1, f'viewkeys not supported for address type {self.al_id.mmtype.desc!r}')
|
||||
|
||||
self.id_str = AddrListIDStr(self)
|
||||
|
||||
|
|
@ -233,30 +233,30 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
if not skip_chksum_msg:
|
||||
self.do_chksum_msg(record=src=='gen')
|
||||
|
||||
def do_chksum_msg(self,record):
|
||||
def do_chksum_msg(self, record):
|
||||
chk = 'Check this value against your records'
|
||||
rec = f'Record this checksum: it will be used to verify the {self.desc} file in the future'
|
||||
self.cfg._util.qmsg(
|
||||
f'Checksum for {self.desc} data {self.id_str.hl()}: {self.chksum.hl()}\n' +
|
||||
(chk,rec)[record] )
|
||||
(chk, rec)[record])
|
||||
|
||||
def generate(self,seed,addr_idxs):
|
||||
def generate(self, seed, addr_idxs):
|
||||
|
||||
seed = self.scramble_seed(seed.data)
|
||||
self.dmsg_sc('seed',seed[:8].hex())
|
||||
self.dmsg_sc('seed', seed[:8].hex())
|
||||
|
||||
mmtype = self.al_id.mmtype
|
||||
|
||||
gen_wallet_passwd = type(self) in (KeyAddrList,ViewKeyAddrList) and 'wallet_passwd' in mmtype.extra_attrs
|
||||
gen_viewkey = type(self) in (KeyAddrList,ViewKeyAddrList) and 'viewkey' in mmtype.extra_attrs
|
||||
gen_wallet_passwd = type(self) in (KeyAddrList, ViewKeyAddrList) and 'wallet_passwd' in mmtype.extra_attrs
|
||||
gen_viewkey = type(self) in (KeyAddrList, ViewKeyAddrList) and 'viewkey' in mmtype.extra_attrs
|
||||
|
||||
if self.gen_addrs:
|
||||
from .keygen import KeyGenerator
|
||||
from .addrgen import AddrGenerator
|
||||
kg = KeyGenerator( self.cfg, self.proto, mmtype.pubkey_type )
|
||||
ag = AddrGenerator( self.cfg, self.proto, mmtype )
|
||||
kg = KeyGenerator(self.cfg, self.proto, mmtype.pubkey_type)
|
||||
ag = AddrGenerator(self.cfg, self.proto, mmtype)
|
||||
if self.add_p2pkh:
|
||||
ag2 = AddrGenerator( self.cfg, self.proto, 'compressed' )
|
||||
ag2 = AddrGenerator(self.cfg, self.proto, 'compressed')
|
||||
|
||||
from .derive import derive_coin_privkey_bytes
|
||||
|
||||
|
|
@ -265,19 +265,19 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
out = AddrListData()
|
||||
CR = '\n' if self.cfg.debug_addrlist else '\r'
|
||||
|
||||
for pk_bytes in derive_coin_privkey_bytes(seed,addr_idxs):
|
||||
for pk_bytes in derive_coin_privkey_bytes(seed, addr_idxs):
|
||||
|
||||
if not self.cfg.debug:
|
||||
self.cfg._util.qmsg_r(
|
||||
f'{CR}Generating {self.gen_desc} #{pk_bytes.idx} ({pk_bytes.pos} of {t_addrs})' )
|
||||
f'{CR}Generating {self.gen_desc} #{pk_bytes.idx} ({pk_bytes.pos} of {t_addrs})')
|
||||
|
||||
e = le( proto=self.proto, idx=pk_bytes.idx )
|
||||
e = le(proto=self.proto, idx=pk_bytes.idx)
|
||||
|
||||
e.sec = PrivKey(
|
||||
self.proto,
|
||||
pk_bytes.data,
|
||||
compressed = mmtype.compressed,
|
||||
pubkey_type = mmtype.pubkey_type )
|
||||
pubkey_type = mmtype.pubkey_type)
|
||||
|
||||
if self.gen_addrs:
|
||||
data = kg.gen_data(e.sec)
|
||||
|
|
@ -288,7 +288,7 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
e.viewkey = ag.to_viewkey(data)
|
||||
if gen_wallet_passwd:
|
||||
e.wallet_passwd = self.gen_wallet_passwd(
|
||||
e.viewkey.encode() if type(self) is ViewKeyAddrList else e.sec )
|
||||
e.viewkey.encode() if type(self) is ViewKeyAddrList else e.sec)
|
||||
elif self.gen_passwds:
|
||||
e.passwd = self.gen_passwd(e.sec) # TODO - own type
|
||||
|
||||
|
|
@ -299,32 +299,32 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
self.al_id.hl(),
|
||||
t_addrs,
|
||||
self.gen_desc,
|
||||
suf(t_addrs,self.gen_desc_pl),
|
||||
' ' * 15 ))
|
||||
suf(t_addrs, self.gen_desc_pl),
|
||||
' ' * 15))
|
||||
|
||||
return out
|
||||
|
||||
def gen_wallet_passwd(self,privbytes):
|
||||
def gen_wallet_passwd(self, privbytes):
|
||||
from .proto.btc.common import hash256
|
||||
return WalletPassword( hash256(privbytes)[:16].hex() )
|
||||
return WalletPassword(hash256(privbytes)[:16].hex())
|
||||
|
||||
def check_format(self,addr):
|
||||
def check_format(self, addr):
|
||||
return True # format is checked when added to list entry object
|
||||
|
||||
def scramble_seed(self,seed):
|
||||
def scramble_seed(self, seed):
|
||||
is_btcfork = self.proto.base_coin == 'BTC'
|
||||
if is_btcfork and self.al_id.mmtype == 'L' and not self.proto.testnet:
|
||||
self.dmsg_sc('str','(none)')
|
||||
self.dmsg_sc('str', '(none)')
|
||||
return seed
|
||||
if self.proto.base_coin == 'ETH':
|
||||
scramble_key = self.proto.coin.lower()
|
||||
else:
|
||||
scramble_key = (self.proto.coin.lower()+':','')[is_btcfork] + self.al_id.mmtype.name
|
||||
scramble_key = (self.proto.coin.lower()+':', '')[is_btcfork] + self.al_id.mmtype.name
|
||||
from .crypto import Crypto
|
||||
if self.proto.testnet:
|
||||
scramble_key += ':' + self.proto.network
|
||||
self.dmsg_sc('str',scramble_key)
|
||||
return Crypto(self.cfg).scramble_seed(seed,scramble_key.encode())
|
||||
self.dmsg_sc('str', scramble_key)
|
||||
return Crypto(self.cfg).scramble_seed(seed, scramble_key.encode())
|
||||
|
||||
def idxs(self):
|
||||
return [e.idx for e in self.data]
|
||||
|
|
@ -333,7 +333,7 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
return [f'{self.al_id.sid}:{e.idx}' for e in self.data]
|
||||
|
||||
def addrpairs(self):
|
||||
return [(e.idx,e.addr) for e in self.data]
|
||||
return [(e.idx, e.addr) for e in self.data]
|
||||
|
||||
def coinaddrs(self):
|
||||
return [e.addr for e in self.data]
|
||||
|
|
@ -341,57 +341,57 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
def comments(self):
|
||||
return [e.comment for e in self.data]
|
||||
|
||||
def entry(self,idx):
|
||||
def entry(self, idx):
|
||||
for e in self.data:
|
||||
if idx == e.idx:
|
||||
return e
|
||||
|
||||
def coinaddr(self,idx):
|
||||
def coinaddr(self, idx):
|
||||
for e in self.data:
|
||||
if idx == e.idx:
|
||||
return e.addr
|
||||
|
||||
def comment(self,idx):
|
||||
def comment(self, idx):
|
||||
for e in self.data:
|
||||
if idx == e.idx:
|
||||
return e.comment
|
||||
|
||||
def set_comment(self,idx,comment):
|
||||
def set_comment(self, idx, comment):
|
||||
for e in self.data:
|
||||
if idx == e.idx:
|
||||
e.comment = comment
|
||||
|
||||
def make_reverse_dict_addrlist(self,coinaddrs):
|
||||
def make_reverse_dict_addrlist(self, coinaddrs):
|
||||
d = MMGenDict()
|
||||
b = coinaddrs
|
||||
for e in self.data:
|
||||
try:
|
||||
d[b[b.index(e.addr)]] = ( MMGenID(self.proto, f'{self.al_id}:{e.idx}'), e.comment )
|
||||
d[b[b.index(e.addr)]] = (MMGenID(self.proto, f'{self.al_id}:{e.idx}'), e.comment)
|
||||
except ValueError:
|
||||
pass
|
||||
return d
|
||||
|
||||
def add_wifs(self,key_list):
|
||||
def add_wifs(self, key_list):
|
||||
"""
|
||||
Match WIF keys in a flat list to addresses in self by generating all
|
||||
possible addresses for each key.
|
||||
"""
|
||||
def gen_addr(pk,t):
|
||||
def gen_addr(pk, t):
|
||||
at = self.proto.addr_type(t)
|
||||
from .keygen import KeyGenerator
|
||||
from .addrgen import AddrGenerator
|
||||
kg = KeyGenerator( self.cfg, self.proto, at.pubkey_type )
|
||||
ag = AddrGenerator( self.cfg, self.proto, at )
|
||||
kg = KeyGenerator(self.cfg, self.proto, at.pubkey_type)
|
||||
ag = AddrGenerator(self.cfg, self.proto, at)
|
||||
return ag.to_addr(kg.gen_data(pk))
|
||||
|
||||
compressed_types = set(self.proto.mmtypes) - {'L','E'}
|
||||
uncompressed_types = set(self.proto.mmtypes) & {'L','E'}
|
||||
compressed_types = set(self.proto.mmtypes) - {'L', 'E'}
|
||||
uncompressed_types = set(self.proto.mmtypes) & {'L', 'E'}
|
||||
|
||||
def gen():
|
||||
for wif in key_list:
|
||||
pk = PrivKey(proto=self.proto,wif=wif)
|
||||
pk = PrivKey(proto=self.proto, wif=wif)
|
||||
for t in (compressed_types if pk.compressed else uncompressed_types):
|
||||
yield ( gen_addr(pk,t), pk )
|
||||
yield (gen_addr(pk, t), pk)
|
||||
|
||||
addrs4keys = dict(gen())
|
||||
|
||||
|
|
@ -399,14 +399,14 @@ class AddrList(MMGenObject): # Address info for a single seed ID
|
|||
if d.addr in addrs4keys:
|
||||
d.sec = addrs4keys[d.addr]
|
||||
|
||||
def list_missing(self,attr):
|
||||
return [d.addr for d in self.data if not getattr(d,attr)]
|
||||
def list_missing(self, attr):
|
||||
return [d.addr for d in self.data if not getattr(d, attr)]
|
||||
|
||||
@property
|
||||
def file(self):
|
||||
if not hasattr(self,'_file'):
|
||||
if not hasattr(self, '_file'):
|
||||
from . import addrfile
|
||||
self._file = getattr( addrfile, type(self).__name__.replace('List','File') )(self)
|
||||
self._file = getattr(addrfile, type(self).__name__.replace('List', 'File'))(self)
|
||||
return self._file
|
||||
|
||||
class KeyAddrList(AddrList):
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ from subprocess import run, PIPE, DEVNULL
|
|||
from .cfg import Config
|
||||
from .util import msg, msg_r, ymsg, rmsg, gmsg, bmsg, die, suf, fmt, fmt_list, is_int, have_sudo, capfirst
|
||||
from .color import yellow, red, orange, brown, blue
|
||||
from .wallet import Wallet,get_wallet_cls
|
||||
from .wallet import Wallet, get_wallet_cls
|
||||
from .addrlist import AddrIdxList
|
||||
from .filename import find_file_in_dir
|
||||
from .ui import keypress_confirm
|
||||
|
|
@ -135,10 +135,10 @@ class Signable:
|
|||
multiple_ok = True
|
||||
action_desc = 'signed'
|
||||
|
||||
def __init__(self,parent):
|
||||
def __init__(self, parent):
|
||||
self.parent = parent
|
||||
self.cfg = parent.cfg
|
||||
self.dir = getattr(parent,self.dir_name)
|
||||
self.dir = getattr(parent, self.dir_name)
|
||||
self.name = type(self).__name__
|
||||
|
||||
@property
|
||||
|
|
@ -152,21 +152,21 @@ class Signable:
|
|||
|
||||
@property
|
||||
def unsigned(self):
|
||||
return self._unprocessed( '_unsigned', self.rawext, self.sigext )
|
||||
return self._unprocessed('_unsigned', self.rawext, self.sigext)
|
||||
|
||||
@property
|
||||
def unsubmitted(self):
|
||||
return self._unprocessed( '_unsubmitted', self.sigext, self.subext )
|
||||
return self._unprocessed('_unsubmitted', self.sigext, self.subext)
|
||||
|
||||
@property
|
||||
def unsubmitted_raw(self):
|
||||
return self._unprocessed( '_unsubmitted_raw', self.rawext, self.subext )
|
||||
return self._unprocessed('_unsubmitted_raw', self.rawext, self.subext)
|
||||
|
||||
unsent = unsubmitted
|
||||
unsent_raw = unsubmitted_raw
|
||||
|
||||
def _unprocessed(self,attrname,rawext,sigext):
|
||||
if not hasattr(self,attrname):
|
||||
def _unprocessed(self, attrname, rawext, sigext):
|
||||
if not hasattr(self, attrname):
|
||||
dirlist = sorted(self.dir.iterdir())
|
||||
names = {f.name for f in dirlist}
|
||||
setattr(
|
||||
|
|
@ -174,13 +174,13 @@ class Signable:
|
|||
attrname,
|
||||
tuple(f for f in dirlist
|
||||
if f.name.endswith('.' + rawext)
|
||||
and f.name[:-len(rawext)] + sigext not in names) )
|
||||
return getattr(self,attrname)
|
||||
and f.name[:-len(rawext)] + sigext not in names))
|
||||
return getattr(self, attrname)
|
||||
|
||||
def print_bad_list(self,bad_files):
|
||||
def print_bad_list(self, bad_files):
|
||||
msg('\n{a}\n{b}'.format(
|
||||
a = red(f'Failed {self.desc}s:'),
|
||||
b = ' {}\n'.format('\n '.join(self.gen_bad_list(sorted(bad_files,key=lambda f: f.name))))
|
||||
b = ' {}\n'.format('\n '.join(self.gen_bad_list(sorted(bad_files, key=lambda f: f.name))))
|
||||
))
|
||||
|
||||
def die_wrong_num_txs(self, tx_type, msg=None, desc=None, show_dir=False):
|
||||
|
|
@ -255,7 +255,7 @@ class Signable:
|
|||
dir_name = 'tx_dir'
|
||||
fail_msg = 'failed to sign'
|
||||
|
||||
async def sign(self,f):
|
||||
async def sign(self, f):
|
||||
from .tx import UnsignedTX
|
||||
tx1 = UnsignedTX(
|
||||
cfg = self.cfg,
|
||||
|
|
@ -263,7 +263,7 @@ class Signable:
|
|||
automount = self.name=='automount_transaction')
|
||||
if tx1.proto.sign_mode == 'daemon':
|
||||
from .rpc import rpc_init
|
||||
tx1.rpc = await rpc_init( self.cfg, tx1.proto, ignore_wallet=True )
|
||||
tx1.rpc = await rpc_init(self.cfg, tx1.proto, ignore_wallet=True)
|
||||
from .tx.sign import txsign
|
||||
tx2 = await txsign(
|
||||
cfg_parm = self.cfg,
|
||||
|
|
@ -278,7 +278,7 @@ class Signable:
|
|||
else:
|
||||
return False
|
||||
|
||||
def print_summary(self,signables):
|
||||
def print_summary(self, signables):
|
||||
|
||||
if self.cfg.full_summary:
|
||||
bmsg('\nAutosign summary:\n')
|
||||
|
|
@ -289,22 +289,22 @@ class Signable:
|
|||
for tx in signables:
|
||||
non_mmgen = [o for o in tx.outputs if not o.mmid]
|
||||
if non_mmgen:
|
||||
yield (tx,non_mmgen)
|
||||
yield (tx, non_mmgen)
|
||||
|
||||
body = list(gen())
|
||||
|
||||
if body:
|
||||
bmsg('\nAutosign summary:')
|
||||
fs = '{} {} {}'
|
||||
t_wid,a_wid = 6,44
|
||||
t_wid, a_wid = 6, 44
|
||||
|
||||
def gen():
|
||||
yield fs.format('TX ID ','Non-MMGen outputs'+' '*(a_wid-17),'Amount')
|
||||
yield fs.format('TX ID ', 'Non-MMGen outputs'+' '*(a_wid-17), 'Amount')
|
||||
yield fs.format('-'*t_wid, '-'*a_wid, '-'*7)
|
||||
for tx,non_mmgen in body:
|
||||
for tx, non_mmgen in body:
|
||||
for nm in non_mmgen:
|
||||
yield fs.format(
|
||||
tx.txid.fmt( width=t_wid, color=True ) if nm is non_mmgen[0] else ' '*t_wid,
|
||||
tx.txid.fmt(width=t_wid, color=True) if nm is non_mmgen[0] else ' '*t_wid,
|
||||
nm.addr.fmt(nm.addr.view_pref, width=a_wid, color=True),
|
||||
nm.amt.hl() + ' ' + yellow(tx.coin))
|
||||
|
||||
|
|
@ -312,7 +312,7 @@ class Signable:
|
|||
else:
|
||||
msg('\nNo non-MMGen outputs')
|
||||
|
||||
def gen_bad_list(self,bad_files):
|
||||
def gen_bad_list(self, bad_files):
|
||||
for f in bad_files:
|
||||
yield red(f.name)
|
||||
|
||||
|
|
@ -326,12 +326,12 @@ class Signable:
|
|||
|
||||
class xmr_signable(transaction): # mixin class
|
||||
|
||||
def need_daemon_restart(self,m,new_idx):
|
||||
def need_daemon_restart(self, m, new_idx):
|
||||
old_idx = self.parent.xmr_cur_wallet_idx
|
||||
self.parent.xmr_cur_wallet_idx = new_idx
|
||||
return old_idx != new_idx or m.wd.state != 'ready'
|
||||
|
||||
def print_summary(self,signables):
|
||||
def print_summary(self, signables):
|
||||
bmsg('\nAutosign summary:')
|
||||
msg('\n'.join(s.get_info(indent=' ') for s in signables) + self.summary_footer)
|
||||
|
||||
|
|
@ -342,7 +342,7 @@ class Signable:
|
|||
multiple_ok = False
|
||||
summary_footer = ''
|
||||
|
||||
async def sign(self,f):
|
||||
async def sign(self, f):
|
||||
from . import xmrwallet
|
||||
from .xmrwallet.file.tx import MoneroMMGenTX
|
||||
tx1 = MoneroMMGenTX.Completed(self.parent.xmrwallet_cfg, f)
|
||||
|
|
@ -351,7 +351,7 @@ class Signable:
|
|||
self.parent.xmrwallet_cfg,
|
||||
infile = str(self.parent.wallet_files[0]), # MMGen wallet file
|
||||
wallets = str(tx1.src_wallet_idx))
|
||||
tx2 = await m.main( f, restart_daemon=self.need_daemon_restart(m,tx1.src_wallet_idx) )
|
||||
tx2 = await m.main(f, restart_daemon=self.need_daemon_restart(m, tx1.src_wallet_idx))
|
||||
tx2.write(ask_write=False)
|
||||
return tx2
|
||||
|
||||
|
|
@ -370,7 +370,7 @@ class Signable:
|
|||
f for f in super().unsigned
|
||||
if not json.loads(f.read_text())['MoneroMMGenWalletOutputsFile']['data']['imported'])
|
||||
|
||||
async def sign(self,f):
|
||||
async def sign(self, f):
|
||||
from . import xmrwallet
|
||||
wallet_idx = xmrwallet.op_cls('wallet').get_idx_from_fn(f)
|
||||
m = xmrwallet.op(
|
||||
|
|
@ -378,7 +378,7 @@ class Signable:
|
|||
self.parent.xmrwallet_cfg,
|
||||
infile = str(self.parent.wallet_files[0]), # MMGen wallet file
|
||||
wallets = str(wallet_idx))
|
||||
obj = await m.main(f, wallet_idx, restart_daemon=self.need_daemon_restart(m,wallet_idx))
|
||||
obj = await m.main(f, wallet_idx, restart_daemon=self.need_daemon_restart(m, wallet_idx))
|
||||
obj.write(quiet=not obj.data.sign)
|
||||
self.action_desc = 'imported and signed' if obj.data.sign else 'imported'
|
||||
return obj
|
||||
|
|
@ -390,26 +390,26 @@ class Signable:
|
|||
dir_name = 'msg_dir'
|
||||
fail_msg = 'failed to sign or signed incompletely'
|
||||
|
||||
async def sign(self,f):
|
||||
from .msg import UnsignedMsg,SignedMsg
|
||||
m = UnsignedMsg( self.cfg, infile=f )
|
||||
async def sign(self, f):
|
||||
from .msg import UnsignedMsg, SignedMsg
|
||||
m = UnsignedMsg(self.cfg, infile=f)
|
||||
await m.sign(wallet_files=self.parent.wallet_files[:], passwd_file=str(self.parent.keyfile))
|
||||
m = SignedMsg( self.cfg, data=m.__dict__ )
|
||||
m = SignedMsg(self.cfg, data=m.__dict__)
|
||||
m.write_to_file(
|
||||
outdir = self.dir.resolve(),
|
||||
ask_overwrite = False )
|
||||
ask_overwrite = False)
|
||||
if m.data.get('failed_sids'):
|
||||
die('MsgFileFailedSID',f'Failed Seed IDs: {fmt_list(m.data["failed_sids"],fmt="bare")}')
|
||||
die('MsgFileFailedSID', f'Failed Seed IDs: {fmt_list(m.data["failed_sids"], fmt="bare")}')
|
||||
return m
|
||||
|
||||
def print_summary(self,signables):
|
||||
def print_summary(self, signables):
|
||||
gmsg('\nSigned message files:')
|
||||
for message in signables:
|
||||
gmsg(' ' + message.signed_filename)
|
||||
|
||||
def gen_bad_list(self,bad_files):
|
||||
def gen_bad_list(self, bad_files):
|
||||
for f in bad_files:
|
||||
sigfile = f.parent / ( f.name[:-len(self.rawext)] + self.sigext )
|
||||
sigfile = f.parent / (f.name[:-len(self.rawext)] + self.sigext)
|
||||
yield orange(sigfile.name) if sigfile.exists() else red(f.name)
|
||||
|
||||
class Autosign:
|
||||
|
|
@ -453,13 +453,13 @@ class Autosign:
|
|||
def init_fixup(self): # see test/overlay/fakemods/mmgen/autosign.py
|
||||
pass
|
||||
|
||||
def __init__(self,cfg,cmd=None):
|
||||
def __init__(self, cfg, cmd=None):
|
||||
|
||||
if cfg.mnemonic_fmt:
|
||||
if cfg.mnemonic_fmt not in self.mn_fmts:
|
||||
die(1,'{!r}: invalid mnemonic format (must be one of: {})'.format(
|
||||
die(1, '{!r}: invalid mnemonic format (must be one of: {})'.format(
|
||||
cfg.mnemonic_fmt,
|
||||
fmt_list( self.mn_fmts, fmt='no_spc' ) ))
|
||||
fmt_list(self.mn_fmts, fmt='no_spc')))
|
||||
|
||||
if sys.platform == 'linux':
|
||||
self.dfl_mountpoint = f'/mnt/{self.linux_mount_subdir}'
|
||||
|
|
@ -507,11 +507,11 @@ class Autosign:
|
|||
|
||||
self.keyfile = self.mountpoint / 'autosign.key'
|
||||
|
||||
if any(k in cfg._uopts for k in ('help','longhelp')):
|
||||
if any(k in cfg._uopts for k in ('help', 'longhelp')):
|
||||
return
|
||||
|
||||
if 'coin' in cfg._uopts:
|
||||
die(1,'--coin option not supported with this command. Use --coins instead')
|
||||
die(1, '--coin option not supported with this command. Use --coins instead')
|
||||
|
||||
self.coins = cfg.coins.upper().split(',') if cfg.coins else []
|
||||
|
||||
|
|
@ -539,7 +539,7 @@ class Autosign:
|
|||
self.dirs |= self.xmr_dirs
|
||||
self.signables += Signable.xmr_signables
|
||||
|
||||
for name,path in self.dirs.items():
|
||||
for name, path in self.dirs.items():
|
||||
setattr(self, name, self.mountpoint / path)
|
||||
|
||||
self.swap = SwapMgr(self.cfg, ignore_zram=True)
|
||||
|
|
@ -553,28 +553,28 @@ class Autosign:
|
|||
from .rpc import rpc_init
|
||||
from .exception import SocketError
|
||||
try:
|
||||
await rpc_init( self.cfg, proto, ignore_wallet=True )
|
||||
await rpc_init(self.cfg, proto, ignore_wallet=True)
|
||||
except SocketError as e:
|
||||
from .daemon import CoinDaemon
|
||||
d = CoinDaemon( self.cfg, proto=proto, test_suite=self.cfg.test_suite )
|
||||
d = CoinDaemon(self.cfg, proto=proto, test_suite=self.cfg.test_suite)
|
||||
die(2,
|
||||
f'\n{e}\nIs the {d.coind_name} daemon ({d.exec_fn}) running '
|
||||
+ 'and listening on the correct port?' )
|
||||
+ 'and listening on the correct port?')
|
||||
|
||||
@property
|
||||
def wallet_files(self):
|
||||
|
||||
if not hasattr(self,'_wallet_files'):
|
||||
if not hasattr(self, '_wallet_files'):
|
||||
|
||||
try:
|
||||
dirlist = self.wallet_dir.iterdir()
|
||||
except:
|
||||
die(1,f"Cannot open wallet directory '{self.wallet_dir}'. Did you run ‘mmgen-autosign setup’?")
|
||||
die(1, f"Cannot open wallet directory '{self.wallet_dir}'. Did you run ‘mmgen-autosign setup’?")
|
||||
|
||||
self._wallet_files = [f for f in dirlist if f.suffix == '.mmdat']
|
||||
|
||||
if not self._wallet_files:
|
||||
die(1,'No wallet files present!')
|
||||
die(1, 'No wallet files present!')
|
||||
|
||||
return self._wallet_files
|
||||
|
||||
|
|
@ -595,7 +595,7 @@ class Autosign:
|
|||
|
||||
if sys.platform == 'linux' and not self.mountpoint.is_dir():
|
||||
def do_die(m):
|
||||
die(1,'\n' + yellow(fmt(m.strip(),indent=' ')))
|
||||
die(1, '\n' + yellow(fmt(m.strip(), indent=' ')))
|
||||
if Path(self.old_dfl_mountpoint).is_dir():
|
||||
do_die(self.old_dfl_mountpoint_errmsg)
|
||||
else:
|
||||
|
|
@ -607,14 +607,14 @@ class Autosign:
|
|||
if not silent:
|
||||
msg(f"Mounting '{self.mountpoint}'")
|
||||
else:
|
||||
die(1,f'Unable to mount device {self.dev_label} at {self.mountpoint}')
|
||||
die(1, f'Unable to mount device {self.dev_label} at {self.mountpoint}')
|
||||
|
||||
for dirname in self.dirs:
|
||||
check_or_create(dirname)
|
||||
|
||||
def do_umount(self, silent=False, verbose=False):
|
||||
if self.mountpoint.is_mount():
|
||||
run( ['sync'], check=True )
|
||||
run(['sync'], check=True)
|
||||
if not silent:
|
||||
msg(f"Unmounting '{self.mountpoint}'")
|
||||
redir = None if verbose else DEVNULL
|
||||
|
|
@ -634,8 +634,8 @@ class Autosign:
|
|||
|
||||
return not fails
|
||||
|
||||
async def sign_all(self,target_name):
|
||||
target = getattr(Signable,target_name)(self)
|
||||
async def sign_all(self, target_name):
|
||||
target = getattr(Signable, target_name)(self)
|
||||
if target.unsigned:
|
||||
good = []
|
||||
bad = []
|
||||
|
|
@ -675,7 +675,7 @@ class Autosign:
|
|||
self.led.set('busy')
|
||||
ret = [await self.sign_all(signable) for signable in self.signables]
|
||||
for val in ret:
|
||||
if isinstance(val,str):
|
||||
if isinstance(val, str):
|
||||
msg(val)
|
||||
if self.cfg.test_suite_autosign_threaded:
|
||||
await asyncio.sleep(0.3)
|
||||
|
|
@ -701,15 +701,15 @@ class Autosign:
|
|||
desc = f"key file '{self.keyfile}'"
|
||||
msg('Creating ' + desc)
|
||||
try:
|
||||
self.keyfile.write_text( os.urandom(32).hex() )
|
||||
self.keyfile.write_text(os.urandom(32).hex())
|
||||
self.keyfile.chmod(0o400)
|
||||
except:
|
||||
die(2,'Unable to write ' + desc)
|
||||
die(2, 'Unable to write ' + desc)
|
||||
msg('Wrote ' + desc)
|
||||
|
||||
def gen_key(self,no_unmount=False):
|
||||
def gen_key(self, no_unmount=False):
|
||||
if not self.device_inserted:
|
||||
die(1,'Removable device not present!')
|
||||
die(1, 'Removable device not present!')
|
||||
self.do_mount()
|
||||
self.wipe_encryption_key()
|
||||
self.create_key()
|
||||
|
|
@ -756,7 +756,7 @@ class Autosign:
|
|||
try:
|
||||
self.wallet_dir.stat()
|
||||
except:
|
||||
die(2,f"Unable to create wallet directory '{self.wallet_dir}'")
|
||||
die(2, f"Unable to create wallet directory '{self.wallet_dir}'")
|
||||
|
||||
self.gen_key(no_unmount=True)
|
||||
|
||||
|
|
@ -768,20 +768,20 @@ class Autosign:
|
|||
remove_wallet_dir()
|
||||
create_wallet_dir()
|
||||
|
||||
wf = find_file_in_dir( get_wallet_cls('mmgen'), self.cfg.data_dir )
|
||||
wf = find_file_in_dir(get_wallet_cls('mmgen'), self.cfg.data_dir)
|
||||
if wf and keypress_confirm(
|
||||
cfg = self.cfg,
|
||||
prompt = f"Default wallet '{wf}' found.\nUse default wallet for autosigning?",
|
||||
default_yes = True ):
|
||||
ss_in = Wallet( Config(), wf )
|
||||
default_yes = True):
|
||||
ss_in = Wallet(Config(), wf)
|
||||
else:
|
||||
ss_in = Wallet( self.cfg, in_fmt=self.mn_fmts[self.cfg.mnemonic_fmt or self.dfl_mn_fmt] )
|
||||
ss_out = Wallet( self.cfg, ss=ss_in, passwd_file=str(self.keyfile) )
|
||||
ss_out.write_to_file( desc='autosign wallet', outdir=self.wallet_dir )
|
||||
ss_in = Wallet(self.cfg, in_fmt=self.mn_fmts[self.cfg.mnemonic_fmt or self.dfl_mn_fmt])
|
||||
ss_out = Wallet(self.cfg, ss=ss_in, passwd_file=str(self.keyfile))
|
||||
ss_out.write_to_file(desc='autosign wallet', outdir=self.wallet_dir)
|
||||
|
||||
@property
|
||||
def xmrwallet_cfg(self):
|
||||
if not hasattr(self,'_xmrwallet_cfg'):
|
||||
if not hasattr(self, '_xmrwallet_cfg'):
|
||||
self._xmrwallet_cfg = Config({
|
||||
'_clone': self.cfg,
|
||||
'coin': 'xmr',
|
||||
|
|
@ -820,29 +820,29 @@ class Autosign:
|
|||
nonlocal count
|
||||
msg_r('.')
|
||||
from .fileutil import shred_file
|
||||
shred_file( f, verbose=self.cfg.verbose )
|
||||
shred_file(f, verbose=self.cfg.verbose)
|
||||
count += 1
|
||||
|
||||
def clean_dir(s_name):
|
||||
|
||||
def clean_files(rawext,sigext):
|
||||
def clean_files(rawext, sigext):
|
||||
for f in s.dir.iterdir():
|
||||
if s.clean_all and (f.name.endswith(f'.{rawext}') or f.name.endswith(f'.{sigext}')):
|
||||
do_shred(f)
|
||||
elif f.name.endswith(f'.{sigext}'):
|
||||
raw = f.parent / ( f.name[:-len(sigext)] + rawext )
|
||||
raw = f.parent / (f.name[:-len(sigext)] + rawext)
|
||||
if raw.is_file():
|
||||
do_shred(raw)
|
||||
|
||||
s = getattr(Signable,s_name)(self)
|
||||
s = getattr(Signable, s_name)(self)
|
||||
|
||||
msg_r(f"Cleaning directory '{s.dir}'..")
|
||||
|
||||
if s.dir.is_dir():
|
||||
clean_files( s.rawext, s.sigext )
|
||||
if hasattr(s,'subext'):
|
||||
clean_files( s.rawext, s.subext )
|
||||
clean_files( s.sigext, s.subext )
|
||||
clean_files(s.rawext, s.sigext)
|
||||
if hasattr(s, 'subext'):
|
||||
clean_files(s.rawext, s.subext)
|
||||
clean_files(s.sigext, s.subext)
|
||||
|
||||
msg('done' if s.dir.is_dir() else 'skipped (no dir)')
|
||||
|
||||
|
|
@ -887,7 +887,7 @@ class Autosign:
|
|||
msg_r('.')
|
||||
n += 1
|
||||
|
||||
def at_exit(self,exit_val,message=None):
|
||||
def at_exit(self, exit_val, message=None):
|
||||
if message:
|
||||
msg(message)
|
||||
self.led.stop()
|
||||
|
|
@ -895,16 +895,16 @@ class Autosign:
|
|||
|
||||
def init_exit_handler(self):
|
||||
|
||||
def handler(arg1,arg2):
|
||||
self.at_exit(1,'\nCleaning up...')
|
||||
def handler(arg1, arg2):
|
||||
self.at_exit(1, '\nCleaning up...')
|
||||
|
||||
import signal
|
||||
signal.signal( signal.SIGTERM, handler )
|
||||
signal.signal( signal.SIGINT, handler )
|
||||
signal.signal(signal.SIGTERM, handler)
|
||||
signal.signal(signal.SIGINT, handler)
|
||||
|
||||
def init_led(self):
|
||||
from .led import LEDControl
|
||||
self.led = LEDControl(
|
||||
enabled = self.cfg.led,
|
||||
simulate = self.cfg.test_suite_autosign_led_simulate )
|
||||
simulate = self.cfg.test_suite_autosign_led_simulate)
|
||||
self.led.set('off')
|
||||
|
|
|
|||
|
|
@ -21,14 +21,14 @@ base_obj: base objects with no internal imports for the MMGen suite
|
|||
"""
|
||||
|
||||
class AsyncInit(type):
|
||||
async def __call__(cls,*args,**kwargs):
|
||||
instance = cls.__new__(cls,*args,**kwargs)
|
||||
await type(instance).__init__(instance,*args,**kwargs)
|
||||
async def __call__(cls, *args, **kwargs):
|
||||
instance = cls.__new__(cls, *args, **kwargs)
|
||||
await type(instance).__init__(instance, *args, **kwargs)
|
||||
return instance
|
||||
|
||||
class AttrCtrlMeta(type):
|
||||
def __call__(cls,*args,**kwargs):
|
||||
instance = super().__call__(*args,**kwargs)
|
||||
def __call__(cls, *args, **kwargs):
|
||||
instance = super().__call__(*args, **kwargs)
|
||||
if instance._autolock:
|
||||
instance._lock()
|
||||
return instance
|
||||
|
|
@ -55,38 +55,38 @@ class AttrCtrl(metaclass=AttrCtrlMeta):
|
|||
def _lock(self):
|
||||
self._locked = True
|
||||
|
||||
def __getattr__(self,name):
|
||||
def __getattr__(self, name):
|
||||
if self._locked and self._default_to_none:
|
||||
return None
|
||||
else:
|
||||
raise AttributeError(f'{type(self).__name__} object has no attribute {name!r}')
|
||||
|
||||
def __setattr__(self,name,value):
|
||||
def __setattr__(self, name, value):
|
||||
|
||||
if self._locked:
|
||||
assert name != '_locked', 'lock can be set only once'
|
||||
|
||||
def do_error(name,value,ref_val):
|
||||
def do_error(name, value, ref_val):
|
||||
raise AttributeError(
|
||||
f'{value!r}: invalid value for attribute {name!r}'
|
||||
+ ' of {} object (must be of type {}, not {})'.format(
|
||||
type(self).__name__,
|
||||
type(ref_val).__name__,
|
||||
type(value).__name__ ) )
|
||||
type(value).__name__))
|
||||
|
||||
if not (name in self.__dict__ or hasattr(type(self),name)):
|
||||
if not (name in self.__dict__ or hasattr(type(self), name)):
|
||||
raise AttributeError(f'{type(self).__name__} object has no attribute {name!r}')
|
||||
|
||||
ref_val = getattr(type(self),name) if self._use_class_attr else getattr(self,name)
|
||||
ref_val = getattr(type(self), name) if self._use_class_attr else getattr(self, name)
|
||||
|
||||
if (
|
||||
(name not in self._skip_type_check)
|
||||
and (ref_val is not None)
|
||||
and not isinstance(value,type(ref_val))
|
||||
and not isinstance(value, type(ref_val))
|
||||
):
|
||||
do_error(name,value,ref_val)
|
||||
do_error(name, value, ref_val)
|
||||
|
||||
return object.__setattr__(self,name,value)
|
||||
return object.__setattr__(self, name, value)
|
||||
|
||||
def __delattr__(self, name):
|
||||
if self._locked and not name in self._delete_ok:
|
||||
|
|
@ -107,25 +107,23 @@ class Lockable(AttrCtrl):
|
|||
_reset_ok = ()
|
||||
|
||||
def _lock(self):
|
||||
for name in ('_set_ok','_reset_ok'):
|
||||
for attr in getattr(self,name):
|
||||
assert hasattr(self,attr), (
|
||||
f'attribute {attr!r} in {name!r} not found in {type(self).__name__} object {id(self)}' )
|
||||
for name in ('_set_ok', '_reset_ok'):
|
||||
for attr in getattr(self, name):
|
||||
assert hasattr(self, attr), (
|
||||
f'attribute {attr!r} in {name!r} not found in {type(self).__name__} object {id(self)}')
|
||||
super()._lock()
|
||||
|
||||
def __setattr__(self,name,value):
|
||||
if self._locked and (name in self.__dict__ or hasattr(type(self),name)):
|
||||
val = getattr(self,name)
|
||||
def __setattr__(self, name, value):
|
||||
if self._locked and (name in self.__dict__ or hasattr(type(self), name)):
|
||||
val = getattr(self, name)
|
||||
if name not in (self._set_ok + self._reset_ok):
|
||||
raise AttributeError(f'attribute {name!r} of {type(self).__name__} object is read-only')
|
||||
elif name not in self._reset_ok:
|
||||
#print(self.__dict__)
|
||||
if not (
|
||||
(val != 0 and not val) or
|
||||
(self._use_class_attr and name not in self.__dict__) ):
|
||||
(self._use_class_attr and name not in self.__dict__)):
|
||||
raise AttributeError(
|
||||
f'attribute {name!r} of {type(self).__name__} object is already set,'
|
||||
+ ' and resetting is forbidden' )
|
||||
# else name is in (_set_ok + _reset_ok) -- allow name to be in both lists
|
||||
+ ' and resetting is forbidden')
|
||||
|
||||
return AttrCtrl.__setattr__(self,name,value)
|
||||
return AttrCtrl.__setattr__(self, name, value)
|
||||
|
|
|
|||
|
|
@ -32,23 +32,22 @@ def is_b32_str(s):
|
|||
|
||||
def is_mmgen_mnemonic(s):
|
||||
try:
|
||||
baseconv('mmgen').tobytes(s.split(),pad='seed')
|
||||
baseconv('mmgen').tobytes(s.split(), pad='seed')
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
class baseconv:
|
||||
mn_base = 1626
|
||||
dt = namedtuple('desc_tuple',['short','long'])
|
||||
dt = namedtuple('desc_tuple', ['short', 'long'])
|
||||
constants = {
|
||||
'desc': {
|
||||
'b58': dt('base58', 'base58-encoded data'),
|
||||
'b32': dt('MMGen base32', 'MMGen base32-encoded data created using simple base conversion'),
|
||||
'b16': dt('hexadecimal string','base16 (hexadecimal) string data'),
|
||||
'b10': dt('base10 string', 'base10 (decimal) string data'),
|
||||
'b8': dt('base8 string', 'base8 (octal) string data'),
|
||||
'b6d': dt('base6d (die roll)', 'base6 data using the digits from one to six'),
|
||||
# 'tirosh':('Tirosh mnemonic', 'base1626 mnemonic using truncated Tirosh wordlist'), # not used by wallet
|
||||
'b58': dt('base58', 'base58-encoded data'),
|
||||
'b32': dt('MMGen base32', 'MMGen base32-encoded data created using simple base conversion'),
|
||||
'b16': dt('hexadecimal string', 'base16 (hexadecimal) string data'),
|
||||
'b10': dt('base10 string', 'base10 (decimal) string data'),
|
||||
'b8': dt('base8 string', 'base8 (octal) string data'),
|
||||
'b6d': dt('base6d (die roll)', 'base6 data using the digits from one to six'),
|
||||
'mmgen': dt('MMGen native mnemonic',
|
||||
'MMGen native mnemonic seed phrase created using old Electrum wordlist and simple base conversion'),
|
||||
},
|
||||
|
|
@ -68,18 +67,18 @@ class baseconv:
|
|||
# 'tirosh1633': '1a5faeff' # tirosh list is 1633 words long!
|
||||
},
|
||||
'seedlen_map': {
|
||||
'b58': { 16:22, 24:33, 32:44 },
|
||||
'b6d': { 16:50, 24:75, 32:100 },
|
||||
'mmgen': { 16:12, 24:18, 32:24 },
|
||||
'b58': {16:22, 24:33, 32:44},
|
||||
'b6d': {16:50, 24:75, 32:100},
|
||||
'mmgen': {16:12, 24:18, 32:24},
|
||||
},
|
||||
'seedlen_map_rev': {
|
||||
'b58': { 22:16, 33:24, 44:32 },
|
||||
'b6d': { 50:16, 75:24, 100:32 },
|
||||
'mmgen': { 12:16, 18:24, 24:32 },
|
||||
'b58': {22:16, 33:24, 44:32},
|
||||
'b6d': {50:16, 75:24, 100:32},
|
||||
'mmgen': {12:16, 18:24, 24:32},
|
||||
}
|
||||
}
|
||||
|
||||
def __init__(self,wl_id):
|
||||
def __init__(self, wl_id):
|
||||
|
||||
if wl_id == 'mmgen':
|
||||
from .wordlist.electrum import words
|
||||
|
|
@ -87,9 +86,9 @@ class baseconv:
|
|||
elif wl_id not in self.constants['digits']:
|
||||
raise ValueError(f'{wl_id}: unrecognized mnemonic ID')
|
||||
|
||||
for k,v in self.constants.items():
|
||||
for k, v in self.constants.items():
|
||||
if wl_id in v:
|
||||
setattr(self,k,v[wl_id])
|
||||
setattr(self, k, v[wl_id])
|
||||
|
||||
self.wl_id = wl_id
|
||||
|
||||
|
|
@ -98,23 +97,23 @@ class baseconv:
|
|||
|
||||
def get_wordlist_chksum(self):
|
||||
from hashlib import sha256
|
||||
return sha256( ' '.join(self.digits).encode() ).hexdigest()[:8]
|
||||
return sha256(' '.join(self.digits).encode()).hexdigest()[:8]
|
||||
|
||||
def check_wordlist(self,cfg):
|
||||
def check_wordlist(self, cfg):
|
||||
|
||||
wl = self.digits
|
||||
ret = f'Wordlist: {self.wl_id}\nLength: {len(wl)} words'
|
||||
new_chksum = self.get_wordlist_chksum()
|
||||
|
||||
cfg._util.compare_chksums( new_chksum, 'generated', self.wl_chksum, 'saved', die_on_fail=True )
|
||||
cfg._util.compare_chksums(new_chksum, 'generated', self.wl_chksum, 'saved', die_on_fail=True)
|
||||
|
||||
if tuple(sorted(wl)) == wl:
|
||||
return ret + '\nList is sorted'
|
||||
else:
|
||||
die(3,'ERROR: List is not sorted!')
|
||||
die(3, 'ERROR: List is not sorted!')
|
||||
|
||||
@staticmethod
|
||||
def get_pad(pad,seed_pad_func):
|
||||
def get_pad(pad, seed_pad_func):
|
||||
"""
|
||||
'pad' argument to baseconv conversion methods must be either None, 'seed' or an integer.
|
||||
If None, output of minimum (but never zero) length will be produced.
|
||||
|
|
@ -128,72 +127,72 @@ class baseconv:
|
|||
elif pad == 'seed':
|
||||
return seed_pad_func()
|
||||
else:
|
||||
die('BaseConversionPadError',f"{pad!r}: illegal value for 'pad' (must be None,'seed' or int)")
|
||||
die('BaseConversionPadError', f"{pad!r}: illegal value for 'pad' (must be None, 'seed' or int)")
|
||||
|
||||
def tohex(self,words_arg,pad=None):
|
||||
def tohex(self, words_arg, pad=None):
|
||||
"convert string or list data of instance base to a hexadecimal string"
|
||||
return self.tobytes(words_arg, pad//2 if type(pad) is int else pad).hex()
|
||||
|
||||
def tobytes(self,words_arg,pad=None):
|
||||
def tobytes(self, words_arg, pad=None):
|
||||
"convert string or list data of instance base to byte string"
|
||||
|
||||
words = words_arg if isinstance(words_arg,(list,tuple)) else tuple(words_arg.strip())
|
||||
words = words_arg if isinstance(words_arg, (list, tuple)) else tuple(words_arg.strip())
|
||||
desc = self.desc.short
|
||||
|
||||
if len(words) == 0:
|
||||
die('BaseConversionError',f'empty {desc} data')
|
||||
die('BaseConversionError', f'empty {desc} data')
|
||||
|
||||
def get_seed_pad():
|
||||
assert hasattr(self,'seedlen_map_rev'), f'seed padding not supported for base {self.wl_id!r}'
|
||||
assert hasattr(self, 'seedlen_map_rev'), f'seed padding not supported for base {self.wl_id!r}'
|
||||
d = self.seedlen_map_rev
|
||||
if not len(words) in d:
|
||||
die( 'BaseConversionError',
|
||||
f'{len(words)}: invalid length for seed-padded {desc} data in base conversion' )
|
||||
die('BaseConversionError',
|
||||
f'{len(words)}: invalid length for seed-padded {desc} data in base conversion')
|
||||
return d[len(words)]
|
||||
|
||||
pad_val = max(self.get_pad(pad,get_seed_pad),1)
|
||||
pad_val = max(self.get_pad(pad, get_seed_pad), 1)
|
||||
wl = self.digits
|
||||
base = len(wl)
|
||||
|
||||
if not set(words) <= set(wl):
|
||||
die( 'BaseConversionError',
|
||||
( 'seed data' if pad == 'seed' else f'{words_arg!r}:' ) +
|
||||
f' not in {desc} format' )
|
||||
die('BaseConversionError',
|
||||
('seed data' if pad == 'seed' else f'{words_arg!r}:') +
|
||||
f' not in {desc} format')
|
||||
|
||||
ret = sum(wl.index(words[::-1][i])*(base**i) for i in range(len(words)))
|
||||
bl = ret.bit_length()
|
||||
return ret.to_bytes(max(pad_val,bl//8+bool(bl%8)),'big')
|
||||
return ret.to_bytes(max(pad_val, bl//8+bool(bl%8)), 'big')
|
||||
|
||||
def fromhex(self,hexstr,pad=None,tostr=False):
|
||||
def fromhex(self, hexstr, pad=None, tostr=False):
|
||||
"convert a hexadecimal string to a list or string data of instance base"
|
||||
|
||||
from .util import is_hex_str
|
||||
if not is_hex_str(hexstr):
|
||||
die( 'HexadecimalStringError',
|
||||
( 'seed data' if pad == 'seed' else f'{hexstr!r}:' ) +
|
||||
' not a hexadecimal string' )
|
||||
die('HexadecimalStringError',
|
||||
('seed data' if pad == 'seed' else f'{hexstr!r}:') +
|
||||
' not a hexadecimal string')
|
||||
|
||||
return self.frombytes( bytes.fromhex(hexstr), pad, tostr )
|
||||
return self.frombytes(bytes.fromhex(hexstr), pad, tostr)
|
||||
|
||||
def frombytes(self,bytestr,pad=None,tostr=False):
|
||||
def frombytes(self, bytestr, pad=None, tostr=False):
|
||||
"convert byte string to list or string data of instance base"
|
||||
|
||||
if not bytestr:
|
||||
die( 'BaseConversionError', 'empty data not allowed in base conversion' )
|
||||
die('BaseConversionError', 'empty data not allowed in base conversion')
|
||||
|
||||
def get_seed_pad():
|
||||
assert hasattr(self,'seedlen_map'), f'seed padding not supported for base {self.wl_id!r}'
|
||||
assert hasattr(self, 'seedlen_map'), f'seed padding not supported for base {self.wl_id!r}'
|
||||
d = self.seedlen_map
|
||||
if not len(bytestr) in d:
|
||||
die( 'SeedLengthError',
|
||||
f'{len(bytestr)}: invalid byte length for seed data in seed-padded base conversion' )
|
||||
die('SeedLengthError',
|
||||
f'{len(bytestr)}: invalid byte length for seed data in seed-padded base conversion')
|
||||
return d[len(bytestr)]
|
||||
|
||||
pad = max(self.get_pad(pad,get_seed_pad),1)
|
||||
pad = max(self.get_pad(pad, get_seed_pad), 1)
|
||||
wl = self.digits
|
||||
|
||||
def gen():
|
||||
num = int.from_bytes(bytestr,'big')
|
||||
num = int.from_bytes(bytestr, 'big')
|
||||
base = len(wl)
|
||||
while num:
|
||||
yield num % base
|
||||
|
|
|
|||
|
|
@ -23,21 +23,21 @@ bip39.py - Data and routines for BIP39 mnemonic seed phrases
|
|||
from hashlib import sha256
|
||||
|
||||
from .baseconv import baseconv
|
||||
from .util import is_hex_str,die
|
||||
from .util import is_hex_str, die
|
||||
|
||||
def is_bip39_mnemonic(s):
|
||||
return bool( bip39().tohex(s.split()) )
|
||||
return bool(bip39().tohex(s.split()))
|
||||
|
||||
# implements a subset of the baseconv API
|
||||
class bip39(baseconv):
|
||||
|
||||
desc = baseconv.dt('BIP39 mnemonic', 'BIP39 mnemonic seed phrase')
|
||||
wl_chksum = 'f18b9a84'
|
||||
seedlen_map = { 16:12, 24:18, 32:24 }
|
||||
seedlen_map_rev = { 12:16, 18:24, 24:32 }
|
||||
seedlen_map = {16:12, 24:18, 32:24}
|
||||
seedlen_map_rev = {12:16, 18:24, 24:32}
|
||||
|
||||
from collections import namedtuple
|
||||
bc = namedtuple('bip39_constants',['chk_len','mn_len'])
|
||||
bc = namedtuple('bip39_constants', ['chk_len', 'mn_len'])
|
||||
# ENT CS MS
|
||||
constants = {
|
||||
128: bc(4, 12),
|
||||
|
|
@ -47,21 +47,21 @@ class bip39(baseconv):
|
|||
256: bc(8, 24),
|
||||
}
|
||||
|
||||
def __init__(self,wl_id='bip39'):
|
||||
def __init__(self, wl_id='bip39'):
|
||||
assert wl_id == 'bip39', "initialize with 'bip39' for compatibility with baseconv API"
|
||||
from .wordlist.bip39 import words
|
||||
self.digits = words
|
||||
self.wl_id = 'bip39'
|
||||
|
||||
@classmethod
|
||||
def nwords2seedlen(cls,nwords,in_bytes=False,in_hex=False):
|
||||
for k,v in cls.constants.items():
|
||||
def nwords2seedlen(cls, nwords, in_bytes=False, in_hex=False):
|
||||
for k, v in cls.constants.items():
|
||||
if v.mn_len == nwords:
|
||||
return k//8 if in_bytes else k//4 if in_hex else k
|
||||
die( 'MnemonicError', f'{nwords!r}: invalid word length for BIP39 mnemonic' )
|
||||
die('MnemonicError', f'{nwords!r}: invalid word length for BIP39 mnemonic')
|
||||
|
||||
@classmethod
|
||||
def seedlen2nwords(cls,seed_len,in_bytes=False,in_hex=False):
|
||||
def seedlen2nwords(cls, seed_len, in_bytes=False, in_hex=False):
|
||||
seed_bits = seed_len * 8 if in_bytes else seed_len * 4 if in_hex else seed_len
|
||||
try:
|
||||
return cls.constants[seed_bits].mn_len
|
||||
|
|
@ -71,47 +71,47 @@ class bip39(baseconv):
|
|||
def tohex(self, words_arg, pad=None):
|
||||
return self.tobytes(words_arg, pad=pad).hex()
|
||||
|
||||
def tobytes(self,words_arg,pad=None):
|
||||
assert isinstance(words_arg,(list,tuple)),'words_arg must be list or tuple'
|
||||
assert pad in (None,'seed'), f"{pad}: invalid 'pad' argument (must be None or 'seed')"
|
||||
def tobytes(self, words_arg, pad=None):
|
||||
assert isinstance(words_arg, (list, tuple)), 'words_arg must be list or tuple'
|
||||
assert pad in (None, 'seed'), f"{pad}: invalid 'pad' argument (must be None or 'seed')"
|
||||
|
||||
wl = self.digits
|
||||
|
||||
for n,w in enumerate(words_arg):
|
||||
for n, w in enumerate(words_arg):
|
||||
if w not in wl:
|
||||
die( 'MnemonicError', f'word #{n+1} is not in the BIP39 word list' )
|
||||
die('MnemonicError', f'word #{n+1} is not in the BIP39 word list')
|
||||
|
||||
res = ''.join(f'{wl.index(w):011b}' for w in words_arg)
|
||||
|
||||
for k,v in self.constants.items():
|
||||
for k, v in self.constants.items():
|
||||
if len(words_arg) == v.mn_len:
|
||||
bitlen = k
|
||||
break
|
||||
else:
|
||||
die( 'MnemonicError', f'{len(words_arg)}: invalid BIP39 seed phrase length' )
|
||||
die('MnemonicError', f'{len(words_arg)}: invalid BIP39 seed phrase length')
|
||||
|
||||
seed_bin = res[:bitlen]
|
||||
chk_bin = res[bitlen:]
|
||||
|
||||
seed_hex = f'{int(seed_bin,2):0{bitlen//4}x}'
|
||||
seed_hex = f'{int(seed_bin, 2):0{bitlen//4}x}'
|
||||
seed_bytes = bytes.fromhex(seed_hex)
|
||||
|
||||
chk_len = self.constants[bitlen].chk_len
|
||||
chk_hex_chk = sha256(seed_bytes).hexdigest()
|
||||
chk_bin_chk = f'{int(chk_hex_chk,16):0256b}'[:chk_len]
|
||||
chk_bin_chk = f'{int(chk_hex_chk, 16):0256b}'[:chk_len]
|
||||
|
||||
if chk_bin != chk_bin_chk:
|
||||
die( 'MnemonicError', f'invalid BIP39 seed phrase checksum ({chk_bin} != {chk_bin_chk})' )
|
||||
die('MnemonicError', f'invalid BIP39 seed phrase checksum ({chk_bin} != {chk_bin_chk})')
|
||||
|
||||
return seed_bytes
|
||||
|
||||
def fromhex(self, hexstr, pad=None, tostr=False):
|
||||
assert is_hex_str(hexstr),'seed data not a hexadecimal string'
|
||||
assert is_hex_str(hexstr), 'seed data not a hexadecimal string'
|
||||
return self.frombytes(bytes.fromhex(hexstr), pad=pad, tostr=tostr)
|
||||
|
||||
def frombytes(self, seed_bytes, pad=None, tostr=False):
|
||||
assert tostr is False,"'tostr' must be False for 'bip39'"
|
||||
assert pad in (None,'seed'), f"{pad}: invalid 'pad' argument (must be None or 'seed')"
|
||||
assert tostr is False, "'tostr' must be False for 'bip39'"
|
||||
assert pad in (None, 'seed'), f"{pad}: invalid 'pad' argument (must be None or 'seed')"
|
||||
|
||||
wl = self.digits
|
||||
bitlen = len(seed_bytes) * 8
|
||||
|
|
@ -121,14 +121,14 @@ class bip39(baseconv):
|
|||
|
||||
chk_hex = sha256(seed_bytes).hexdigest()
|
||||
|
||||
seed_bin = f'{int(seed_bytes.hex(),16):0{bitlen}b}'
|
||||
chk_bin = f'{int(chk_hex,16):0256b}'
|
||||
seed_bin = f'{int(seed_bytes.hex(), 16):0{bitlen}b}'
|
||||
chk_bin = f'{int(chk_hex, 16):0256b}'
|
||||
|
||||
res = seed_bin + chk_bin
|
||||
|
||||
return tuple(wl[int(res[i*11:(i+1)*11],2)] for i in range(c.mn_len))
|
||||
return tuple(wl[int(res[i*11:(i+1)*11], 2)] for i in range(c.mn_len))
|
||||
|
||||
def generate_seed(self,words_arg,passwd=''):
|
||||
def generate_seed(self, words_arg, passwd=''):
|
||||
|
||||
self.tohex(words_arg) # validate
|
||||
|
||||
|
|
|
|||
261
mmgen/cfg.py
261
mmgen/cfg.py
|
|
@ -20,15 +20,15 @@
|
|||
cfg: Configuration classes for the MMGen suite
|
||||
"""
|
||||
|
||||
import sys,os
|
||||
import sys, os
|
||||
from collections import namedtuple
|
||||
from .base_obj import Lockable
|
||||
|
||||
def die(*args,**kwargs):
|
||||
def die(*args, **kwargs):
|
||||
from .util import die
|
||||
die(*args,**kwargs)
|
||||
die(*args, **kwargs)
|
||||
|
||||
def die2(exit_val,s):
|
||||
def die2(exit_val, s):
|
||||
sys.stderr.write(s+'\n')
|
||||
sys.exit(exit_val)
|
||||
|
||||
|
|
@ -86,16 +86,16 @@ class GlobalConstants(Lockable):
|
|||
cmd_caps = cmd_caps_data.get(prog_id)
|
||||
|
||||
if sys.platform not in ('linux', 'win32', 'darwin'):
|
||||
die2(1,f'{sys.platform!r}: platform not supported by {proj_name}')
|
||||
die2(1, f'{sys.platform!r}: platform not supported by {proj_name}')
|
||||
|
||||
if os.getenv('HOME'): # Linux, MSYS2, or macOS
|
||||
home_dir = os.getenv('HOME')
|
||||
elif sys.platform == 'win32': # Windows without MSYS2 - not supported
|
||||
die2(1,f'$HOME not set! {proj_name} for Windows must be run in MSYS2 environment')
|
||||
die2(1, f'$HOME not set! {proj_name} for Windows must be run in MSYS2 environment')
|
||||
else:
|
||||
die2(2,'$HOME is not set! Unable to determine home directory')
|
||||
die2(2, '$HOME is not set! Unable to determine home directory')
|
||||
|
||||
def get_mmgen_data_file(self,filename,package='mmgen'):
|
||||
def get_mmgen_data_file(self, filename, package='mmgen'):
|
||||
"""
|
||||
this is an expensive import, so do only when required
|
||||
"""
|
||||
|
|
@ -111,7 +111,7 @@ class GlobalConstants(Lockable):
|
|||
from importlib.resources import files # Python 3.9 and above
|
||||
except ImportError:
|
||||
from importlib_resources import files
|
||||
return files(package).joinpath('data',filename).read_text()
|
||||
return files(package).joinpath('data', filename).read_text()
|
||||
|
||||
@property
|
||||
def version(self):
|
||||
|
|
@ -144,7 +144,7 @@ class Config(Lockable):
|
|||
3 - config file
|
||||
"""
|
||||
_autolock = False
|
||||
_set_ok = ('usr_randchars','_proto')
|
||||
_set_ok = ('usr_randchars', '_proto')
|
||||
_reset_ok = ('accept_defaults',)
|
||||
_delete_ok = ('_opts',)
|
||||
_use_class_attr = True
|
||||
|
|
@ -210,7 +210,7 @@ class Config(Lockable):
|
|||
pager = False
|
||||
columns = 0
|
||||
color = bool(
|
||||
( sys.stdout.isatty() and not os.getenv('MMGEN_TEST_SUITE_PEXPECT') ) or
|
||||
(sys.stdout.isatty() and not os.getenv('MMGEN_TEST_SUITE_PEXPECT')) or
|
||||
os.getenv('MMGEN_TEST_SUITE_ENABLE_COLOR')
|
||||
)
|
||||
|
||||
|
|
@ -256,7 +256,7 @@ class Config(Lockable):
|
|||
exit_after = ''
|
||||
resuming = False
|
||||
skipping_deps = False
|
||||
test_datadir = os.path.join('test','data_dir' + ('','-α')[bool(os.getenv('MMGEN_DEBUG_UTF8'))])
|
||||
test_datadir = os.path.join('test', 'data_dir' + ('', '-α')[bool(os.getenv('MMGEN_DEBUG_UTF8'))])
|
||||
|
||||
mnemonic_entry_modes = {}
|
||||
|
||||
|
|
@ -273,12 +273,12 @@ class Config(Lockable):
|
|||
)
|
||||
|
||||
_incompatible_opts = (
|
||||
('help','longhelp'),
|
||||
('bob','alice','carol'),
|
||||
('label','keep_label'),
|
||||
('tx_id','info'),
|
||||
('tx_id','terse_info'),
|
||||
('autosign','outdir'),
|
||||
('help', 'longhelp'),
|
||||
('bob', 'alice', 'carol'),
|
||||
('label', 'keep_label'),
|
||||
('tx_id', 'info'),
|
||||
('tx_id', 'terse_info'),
|
||||
('autosign', 'outdir'),
|
||||
)
|
||||
|
||||
_cfg_file_opts = (
|
||||
|
|
@ -319,7 +319,7 @@ class Config(Lockable):
|
|||
'ltc_ignore_daemon_version',
|
||||
'xmr_ignore_daemon_version',
|
||||
'eth_mainnet_chain_names',
|
||||
'eth_testnet_chain_names' )
|
||||
'eth_testnet_chain_names')
|
||||
|
||||
# Supported environmental vars
|
||||
# The corresponding attributes (lowercase, without 'mmgen_') must exist in the class.
|
||||
|
|
@ -377,10 +377,10 @@ class Config(Lockable):
|
|||
'contract_data',
|
||||
)
|
||||
# Auto-typechecked and auto-set opts - first value in list is the default
|
||||
_ov = namedtuple('autoset_opt_info',['type','choices'])
|
||||
_ov = namedtuple('autoset_opt_info', ['type', 'choices'])
|
||||
_autoset_opts = {
|
||||
'fee_estimate_mode': _ov('nocase_pfx', ['conservative','economical']),
|
||||
'rpc_backend': _ov('nocase_pfx', ['auto','httplib','curl','aiohttp','requests']),
|
||||
'fee_estimate_mode': _ov('nocase_pfx', ['conservative', 'economical']),
|
||||
'rpc_backend': _ov('nocase_pfx', ['auto', 'httplib', 'curl', 'aiohttp', 'requests']),
|
||||
}
|
||||
|
||||
_auto_typeset_opts = {
|
||||
|
|
@ -414,13 +414,13 @@ class Config(Lockable):
|
|||
"""
|
||||
location of mmgen.cfg
|
||||
"""
|
||||
if not hasattr(self,'_data_dir_root'):
|
||||
if not hasattr(self, '_data_dir_root'):
|
||||
if self._data_dir_root_override:
|
||||
self._data_dir_root = os.path.normpath(os.path.abspath(self._data_dir_root_override))
|
||||
elif self.test_suite:
|
||||
self._data_dir_root = self.test_datadir
|
||||
else:
|
||||
self._data_dir_root = os.path.join(gc.home_dir,'.'+gc.proj_name.lower())
|
||||
self._data_dir_root = os.path.join(gc.home_dir, '.'+gc.proj_name.lower())
|
||||
return self._data_dir_root
|
||||
|
||||
@property
|
||||
|
|
@ -428,12 +428,12 @@ class Config(Lockable):
|
|||
"""
|
||||
location of wallet and other data - same as data_dir_root for mainnet
|
||||
"""
|
||||
if not hasattr(self,'_data_dir'):
|
||||
if not hasattr(self, '_data_dir'):
|
||||
self._data_dir = os.path.normpath(os.path.join(*{
|
||||
'regtest': (self.data_dir_root, 'regtest', (self.regtest_user or 'none')),
|
||||
'testnet': (self.data_dir_root, 'testnet'),
|
||||
'mainnet': (self.data_dir_root,),
|
||||
}[self.network] ))
|
||||
}[self.network]))
|
||||
return self._data_dir
|
||||
|
||||
def __init__(
|
||||
|
|
@ -457,7 +457,7 @@ class Config(Lockable):
|
|||
if opts_data or parsed_opts or process_opts:
|
||||
assert cfg is None, (
|
||||
'Config(): ‘cfg’ cannot be used simultaneously with ' +
|
||||
'‘opts_data’, ‘parsed_opts’ or ‘process_opts’' )
|
||||
'‘opts_data’, ‘parsed_opts’ or ‘process_opts’')
|
||||
from .opts import UserOpts
|
||||
UserOpts(
|
||||
cfg = self,
|
||||
|
|
@ -472,26 +472,26 @@ class Config(Lockable):
|
|||
self._uopts = {}
|
||||
else:
|
||||
if '_clone' in cfg:
|
||||
assert isinstance( cfg['_clone'], Config )
|
||||
assert isinstance(cfg['_clone'], Config)
|
||||
self._cloned = cfg['_clone'].__dict__
|
||||
for k,v in self._cloned.items():
|
||||
for k, v in self._cloned.items():
|
||||
if not k.startswith('_'):
|
||||
setattr(self,k,v)
|
||||
setattr(self, k, v)
|
||||
del cfg['_clone']
|
||||
self._uopts = cfg
|
||||
self._uopt_desc = 'configuration option'
|
||||
|
||||
self._data_dir_root_override = self._cloned.pop(
|
||||
'_data_dir_root_override',
|
||||
self._uopts.pop('data_dir',None))
|
||||
self._uopts.pop('data_dir', None))
|
||||
|
||||
if parse_only and not any(k in self._uopts for k in ['help','longhelp']):
|
||||
if parse_only and not any(k in self._uopts for k in ['help', 'longhelp']):
|
||||
return
|
||||
|
||||
# Step 2: set cfg from user-supplied data, skipping auto opts; set type from corresponding
|
||||
# class attribute, if it exists:
|
||||
auto_opts = tuple(self._autoset_opts) + tuple(self._auto_typeset_opts)
|
||||
for key,val in self._uopts.items():
|
||||
for key, val in self._uopts.items():
|
||||
assert key.isascii() and key.isidentifier() and key[0] != '_', (
|
||||
f'{key!r}: malformed configuration option')
|
||||
assert key not in self._forbidden_opts, (
|
||||
|
|
@ -524,13 +524,13 @@ class Config(Lockable):
|
|||
# Step 4: set cfg from cfgfile, skipping already-set opts and auto opts; save set opts and auto
|
||||
# opts to be set:
|
||||
# requires ‘data_dir_root’, ‘test_suite_cfgtest’
|
||||
self._cfgfile_opts = self._set_cfg_from_cfg_file( self._envopts, need_proto )
|
||||
self._cfgfile_opts = self._set_cfg_from_cfg_file(self._envopts, need_proto)
|
||||
|
||||
# Step 5: set autoset opts from user-supplied data, cfgfile data, or default values, in that order:
|
||||
self._set_autoset_opts( self._cfgfile_opts.autoset )
|
||||
self._set_autoset_opts(self._cfgfile_opts.autoset)
|
||||
|
||||
# Step 6: set auto typeset opts from user-supplied data or cfgfile data, in that order:
|
||||
self._set_auto_typeset_opts( self._cfgfile_opts.auto_typeset )
|
||||
self._set_auto_typeset_opts(self._cfgfile_opts.auto_typeset)
|
||||
|
||||
if self.regtest or self.bob or self.alice or self.carol or gc.prog_name == f'{gc.proj_id}-regtest':
|
||||
self.network = 'regtest'
|
||||
|
|
@ -567,7 +567,7 @@ class Config(Lockable):
|
|||
if need_proto:
|
||||
from .protocol import init_proto_from_cfg, warn_trustlevel
|
||||
# requires the default-to-none behavior, so do after the lock:
|
||||
self._proto = init_proto_from_cfg(self,need_amt=need_amt)
|
||||
self._proto = init_proto_from_cfg(self, need_amt=need_amt)
|
||||
|
||||
if self._opts and not caller_post_init:
|
||||
self._post_init()
|
||||
|
|
@ -590,41 +590,38 @@ class Config(Lockable):
|
|||
sys.exit(1) # called only on bad invocation
|
||||
|
||||
def _set_cfg_from_env(self):
|
||||
for name,val in ((k,v) for k,v in os.environ.items() if k.startswith('MMGEN_')):
|
||||
for name, val in ((k, v) for k, v in os.environ.items() if k.startswith('MMGEN_')):
|
||||
if name == 'MMGEN_DEBUG_ALL':
|
||||
continue
|
||||
if name in self._env_opts:
|
||||
if val: # ignore empty string values; string value of '0' or 'false' sets variable to False
|
||||
disable = name.startswith('MMGEN_DISABLE_')
|
||||
gname = name[(6,14)[disable]:].lower()
|
||||
gname = name[(6, 14)[disable]:].lower()
|
||||
if gname in self._uopts: # don’t touch attr if already set by user
|
||||
continue
|
||||
if hasattr(self,gname):
|
||||
if hasattr(self, gname):
|
||||
setattr(
|
||||
self,
|
||||
gname,
|
||||
conv_type( name, val, getattr(self,gname), 'environment var', invert_bool=disable ))
|
||||
conv_type(name, val, getattr(self, gname), 'environment var', invert_bool=disable))
|
||||
yield gname
|
||||
else:
|
||||
raise ValueError(f'Name {gname!r} not present in globals')
|
||||
else:
|
||||
raise ValueError(f'{name!r} is not a valid MMGen environment variable')
|
||||
|
||||
def _set_cfg_from_cfg_file(
|
||||
self,
|
||||
env_cfg,
|
||||
need_proto ):
|
||||
def _set_cfg_from_cfg_file(self, env_cfg, need_proto):
|
||||
|
||||
_ret = namedtuple('cfgfile_opts',['non_auto','autoset','auto_typeset'])
|
||||
_ret = namedtuple('cfgfile_opts', ['non_auto', 'autoset', 'auto_typeset'])
|
||||
|
||||
if not self._use_cfg_file:
|
||||
return _ret( (), {}, {} )
|
||||
return _ret((), {}, {})
|
||||
|
||||
# check for changes in system template file (term must be initialized)
|
||||
from .cfgfile import mmgen_cfg_file
|
||||
mmgen_cfg_file(self,'sample')
|
||||
mmgen_cfg_file(self, 'sample')
|
||||
|
||||
ucfg = mmgen_cfg_file(self,'usr')
|
||||
ucfg = mmgen_cfg_file(self, 'usr')
|
||||
|
||||
self._cfgfile_fn = ucfg.fn
|
||||
|
||||
|
|
@ -642,9 +639,9 @@ class Config(Lockable):
|
|||
if ns[0] in gc.core_coins:
|
||||
if not need_proto:
|
||||
continue
|
||||
nse,tn = (
|
||||
(ns[2:],ns[1]=='testnet') if len(ns) > 2 and ns[1] in ('mainnet','testnet') else
|
||||
(ns[1:],False)
|
||||
nse, tn = (
|
||||
(ns[2:], ns[1]=='testnet') if len(ns) > 2 and ns[1] in ('mainnet', 'testnet') else
|
||||
(ns[1:], False)
|
||||
)
|
||||
# no instance yet, so override _class_ attr:
|
||||
cls = init_proto(self, ns[0], tn, need_amt=True, return_cls=True)
|
||||
|
|
@ -652,26 +649,26 @@ class Config(Lockable):
|
|||
else:
|
||||
cls = self
|
||||
attr = d.name
|
||||
refval = getattr(cls,attr)
|
||||
val = ucfg.parse_value(d.value,refval)
|
||||
refval = getattr(cls, attr)
|
||||
val = ucfg.parse_value(d.value, refval)
|
||||
if not val:
|
||||
die( 'CfgFileParseError', f'Parse error in file {ucfg.fn!r}, line {d.lineno}' )
|
||||
val_conv = conv_type( attr, val, refval, 'configuration file option', src=ucfg.fn )
|
||||
die('CfgFileParseError', f'Parse error in file {ucfg.fn!r}, line {d.lineno}')
|
||||
val_conv = conv_type(attr, val, refval, 'configuration file option', src=ucfg.fn)
|
||||
if not attr in already_set:
|
||||
setattr(cls,attr,val_conv)
|
||||
setattr(cls, attr, val_conv)
|
||||
non_auto_opts.append(attr)
|
||||
elif d.name in self._autoset_opts:
|
||||
autoset_opts[d.name] = d.value
|
||||
elif d.name in self._auto_typeset_opts:
|
||||
auto_typeset_opts[d.name] = d.value
|
||||
else:
|
||||
die( 'CfgFileParseError', f'{d.name!r}: unrecognized option in {ucfg.fn!r}, line {d.lineno}' )
|
||||
die('CfgFileParseError', f'{d.name!r}: unrecognized option in {ucfg.fn!r}, line {d.lineno}')
|
||||
|
||||
return _ret( tuple(non_auto_opts), autoset_opts, auto_typeset_opts )
|
||||
return _ret(tuple(non_auto_opts), autoset_opts, auto_typeset_opts)
|
||||
|
||||
def _set_autoset_opts(self,cfgfile_autoset_opts):
|
||||
def _set_autoset_opts(self, cfgfile_autoset_opts):
|
||||
|
||||
def get_autoset_opt(key,val,src):
|
||||
def get_autoset_opt(key, val, src):
|
||||
|
||||
def die_on_err(desc):
|
||||
from .util import fmt_list
|
||||
|
|
@ -680,11 +677,11 @@ class Config(Lockable):
|
|||
'{a!r}: invalid {b} (not {c}: {d})'.format(
|
||||
a = val,
|
||||
b = {
|
||||
'cmdline': f'parameter for option --{key.replace("_","-")}',
|
||||
'cmdline': f'parameter for option --{key.replace("_", "-")}',
|
||||
'cfgfile': f'value for cfg file option {key!r}'
|
||||
}[src],
|
||||
c = desc,
|
||||
d = fmt_list(data.choices) ))
|
||||
d = fmt_list(data.choices)))
|
||||
|
||||
class opt_type:
|
||||
|
||||
|
|
@ -703,7 +700,7 @@ class Config(Lockable):
|
|||
|
||||
data = self._autoset_opts[key]
|
||||
|
||||
return getattr(opt_type,data.type)()
|
||||
return getattr(opt_type, data.type)()
|
||||
|
||||
# Check autoset opts, setting if unset
|
||||
for key in self._autoset_opts:
|
||||
|
|
@ -711,27 +708,27 @@ class Config(Lockable):
|
|||
if key in self._cloned:
|
||||
continue
|
||||
|
||||
assert not hasattr(self,key), f'autoset opt {key!r} is already set, but it shouldn’t be!'
|
||||
assert not hasattr(self, key), f'autoset opt {key!r} is already set, but it shouldn’t be!'
|
||||
|
||||
if key in self._uopts:
|
||||
val,src = (self._uopts[key],'cmdline')
|
||||
val, src = (self._uopts[key], 'cmdline')
|
||||
elif key in cfgfile_autoset_opts:
|
||||
val,src = (cfgfile_autoset_opts[key],'cfgfile')
|
||||
val, src = (cfgfile_autoset_opts[key], 'cfgfile')
|
||||
else:
|
||||
val = None
|
||||
|
||||
if val is None:
|
||||
setattr(self, key, self._autoset_opts[key].choices[0])
|
||||
else:
|
||||
setattr(self, key, get_autoset_opt(key,val,src=src))
|
||||
setattr(self, key, get_autoset_opt(key, val, src=src))
|
||||
|
||||
def _set_auto_typeset_opts(self,cfgfile_auto_typeset_opts):
|
||||
def _set_auto_typeset_opts(self, cfgfile_auto_typeset_opts):
|
||||
|
||||
def do_set(key,val,ref_type):
|
||||
assert not hasattr(self,key), f'{key!r} is in cfg!'
|
||||
setattr(self,key,None if val is None else ref_type(val))
|
||||
def do_set(key, val, ref_type):
|
||||
assert not hasattr(self, key), f'{key!r} is in cfg!'
|
||||
setattr(self, key, None if val is None else ref_type(val))
|
||||
|
||||
for key,ref_type in self._auto_typeset_opts.items():
|
||||
for key, ref_type in self._auto_typeset_opts.items():
|
||||
if key in self._uopts:
|
||||
do_set(key, self._uopts[key], ref_type)
|
||||
elif key in cfgfile_auto_typeset_opts:
|
||||
|
|
@ -739,18 +736,18 @@ class Config(Lockable):
|
|||
|
||||
def _die_on_incompatible_opts(self):
|
||||
for group in self._incompatible_opts:
|
||||
bad = [k for k in self.__dict__ if k in group and getattr(self,k) is not None]
|
||||
bad = [k for k in self.__dict__ if k in group and getattr(self, k) is not None]
|
||||
if len(bad) > 1:
|
||||
die(1,'Conflicting options: {}'.format(', '.join(map(fmt_opt,bad))))
|
||||
die(1, 'Conflicting options: {}'.format(', '.join(map(fmt_opt, bad))))
|
||||
|
||||
def _set_quiet(self,val):
|
||||
def _set_quiet(self, val):
|
||||
from .util import Util
|
||||
self.__dict__['quiet'] = val
|
||||
self.__dict__['_util'] = Util(self) # qmsg, qmsg_r
|
||||
|
||||
def check_opts(cfg): # Raises exception if any check fails
|
||||
|
||||
from .util import is_int,Msg
|
||||
from .util import is_int, Msg
|
||||
|
||||
def get_desc(desc_pfx=''):
|
||||
return (
|
||||
|
|
@ -760,18 +757,18 @@ def check_opts(cfg): # Raises exception if any check fails
|
|||
if name in cfg._uopts and 'command-line' in cfg._uopt_desc else
|
||||
f'value for configuration option {name!r}'
|
||||
)
|
||||
+ ( ' from environment' if name in cfg._envopts else '')
|
||||
+ (' from environment' if name in cfg._envopts else '')
|
||||
+ (f' in {cfg._cfgfile_fn!r}' if name in cfg._cfgfile_opts.non_auto else '')
|
||||
)
|
||||
|
||||
def display_opt(name,val='',beg='For selected',end=':\n'):
|
||||
def display_opt(name, val='', beg='For selected', end=':\n'):
|
||||
from .util import msg_r
|
||||
msg_r('{} option {!r}{}'.format(
|
||||
beg,
|
||||
f'{fmt_opt(name)}={val}' if val else fmt_opt(name),
|
||||
end ))
|
||||
end))
|
||||
|
||||
def opt_compares(val,op_str,target):
|
||||
def opt_compares(val, op_str, target):
|
||||
import operator
|
||||
if not {
|
||||
'<': operator.lt,
|
||||
|
|
@ -779,24 +776,24 @@ def check_opts(cfg): # Raises exception if any check fails
|
|||
'>': operator.gt,
|
||||
'>=': operator.ge,
|
||||
'=': operator.eq,
|
||||
}[op_str](val,target):
|
||||
die( 'UserOptError', f'{val}: invalid {get_desc()} (not {op_str} {target})' )
|
||||
}[op_str](val, target):
|
||||
die('UserOptError', f'{val}: invalid {get_desc()} (not {op_str} {target})')
|
||||
|
||||
def opt_is_int(val,desc_pfx=''):
|
||||
def opt_is_int(val, desc_pfx=''):
|
||||
if not is_int(val):
|
||||
die( 'UserOptError', f'{val!r}: invalid {get_desc(desc_pfx)} (not an integer)' )
|
||||
die('UserOptError', f'{val!r}: invalid {get_desc(desc_pfx)} (not an integer)')
|
||||
|
||||
def opt_is_in_list(val,tlist,desc_pfx=''):
|
||||
def opt_is_in_list(val, tlist, desc_pfx=''):
|
||||
if val not in tlist:
|
||||
q,sep = (('',','),("'","','"))[isinstance(tlist[0],str)]
|
||||
die( 'UserOptError', '{q}{v}{q}: invalid {w}\nValid choices: {q}{o}{q}'.format(
|
||||
q, sep = (('', ','), ("'", "','"))[isinstance(tlist[0], str)]
|
||||
die('UserOptError', '{q}{v}{q}: invalid {w}\nValid choices: {q}{o}{q}'.format(
|
||||
v = val,
|
||||
w = get_desc(desc_pfx),
|
||||
q = q,
|
||||
o = sep.join(map(str,sorted(tlist))) ))
|
||||
o = sep.join(map(str, sorted(tlist)))))
|
||||
|
||||
def opt_unrecognized():
|
||||
die( 'UserOptError', f'{val!r}: unrecognized {get_desc()}' )
|
||||
die('UserOptError', f'{val!r}: unrecognized {get_desc()}')
|
||||
|
||||
class check_funcs:
|
||||
|
||||
|
|
@ -808,32 +805,32 @@ def check_opts(cfg): # Raises exception if any check fails
|
|||
if name == 'out_fmt':
|
||||
p = 'hidden_incog_output_params'
|
||||
|
||||
if wd.type == 'incog_hidden' and not getattr(cfg,p):
|
||||
die( 'UserOptError',
|
||||
if wd.type == 'incog_hidden' and not getattr(cfg, p):
|
||||
die('UserOptError',
|
||||
'Hidden incog format output requested. ' +
|
||||
f'You must supply a file and offset with the {fmt_opt(p)!r} option' )
|
||||
f'You must supply a file and offset with the {fmt_opt(p)!r} option')
|
||||
|
||||
if wd.base_type == 'incog_base' and cfg.old_incog_fmt:
|
||||
display_opt(name,val,beg='Selected',end=' ')
|
||||
display_opt('old_incog_fmt',beg='conflicts with',end=':\n')
|
||||
die( 'UserOptError', 'Export to old incog wallet format unsupported' )
|
||||
display_opt(name, val, beg='Selected', end=' ')
|
||||
display_opt('old_incog_fmt', beg='conflicts with', end=':\n')
|
||||
die('UserOptError', 'Export to old incog wallet format unsupported')
|
||||
elif wd.type == 'brain':
|
||||
die( 'UserOptError', 'Output to brainwallet format unsupported' )
|
||||
die('UserOptError', 'Output to brainwallet format unsupported')
|
||||
|
||||
out_fmt = in_fmt
|
||||
|
||||
def hidden_incog_input_params():
|
||||
a = val.rsplit(',',1) # permit comma in filename
|
||||
a = val.rsplit(',', 1) # permit comma in filename
|
||||
if len(a) != 2:
|
||||
display_opt(name,val)
|
||||
die( 'UserOptError', 'Option requires two comma-separated arguments' )
|
||||
display_opt(name, val)
|
||||
die('UserOptError', 'Option requires two comma-separated arguments')
|
||||
|
||||
fn,offset = a
|
||||
fn, offset = a
|
||||
opt_is_int(offset)
|
||||
|
||||
from .fileutil import check_infile,check_outdir,check_outfile
|
||||
from .fileutil import check_infile, check_outdir, check_outfile
|
||||
if name == 'hidden_incog_input_params':
|
||||
check_infile(fn,blkdev_ok=True)
|
||||
check_infile(fn, blkdev_ok=True)
|
||||
key2 = 'in_fmt'
|
||||
else:
|
||||
try:
|
||||
|
|
@ -843,52 +840,52 @@ def check_opts(cfg): # Raises exception if any check fails
|
|||
if b:
|
||||
check_outdir(b)
|
||||
else:
|
||||
check_outfile(fn,blkdev_ok=True)
|
||||
check_outfile(fn, blkdev_ok=True)
|
||||
key2 = 'out_fmt'
|
||||
|
||||
if hasattr(cfg,key2):
|
||||
val2 = getattr(cfg,key2)
|
||||
if hasattr(cfg, key2):
|
||||
val2 = getattr(cfg, key2)
|
||||
from .wallet import get_wallet_data
|
||||
wd = get_wallet_data('incog_hidden')
|
||||
if val2 and val2 not in wd.fmt_codes:
|
||||
die( 'UserOptError', f'Option conflict:\n {fmt_opt(name)}, with\n {fmt_opt(key2)}={val2}' )
|
||||
die('UserOptError', f'Option conflict:\n {fmt_opt(name)}, with\n {fmt_opt(key2)}={val2}')
|
||||
|
||||
hidden_incog_output_params = hidden_incog_input_params
|
||||
|
||||
def subseeds():
|
||||
from .subseed import SubSeedIdxRange
|
||||
opt_compares(val,'>=',SubSeedIdxRange.min_idx)
|
||||
opt_compares(val,'<=',SubSeedIdxRange.max_idx)
|
||||
opt_compares(val, '>=', SubSeedIdxRange.min_idx)
|
||||
opt_compares(val, '<=', SubSeedIdxRange.max_idx)
|
||||
|
||||
def seed_len():
|
||||
from .seed import Seed
|
||||
opt_is_in_list(int(val),Seed.lens)
|
||||
opt_is_in_list(int(val), Seed.lens)
|
||||
|
||||
def hash_preset():
|
||||
from .crypto import Crypto
|
||||
opt_is_in_list(val,list(Crypto.hash_presets.keys()))
|
||||
opt_is_in_list(val, list(Crypto.hash_presets.keys()))
|
||||
|
||||
def brain_params():
|
||||
a = val.split(',')
|
||||
if len(a) != 2:
|
||||
display_opt(name,val)
|
||||
die( 'UserOptError', 'Option requires two comma-separated arguments' )
|
||||
display_opt(name, val)
|
||||
die('UserOptError', 'Option requires two comma-separated arguments')
|
||||
|
||||
opt_is_int( a[0], desc_pfx='seed length' )
|
||||
opt_is_int(a[0], desc_pfx='seed length')
|
||||
from .seed import Seed
|
||||
opt_is_in_list( int(a[0]), Seed.lens, desc_pfx='seed length' )
|
||||
opt_is_in_list(int(a[0]), Seed.lens, desc_pfx='seed length')
|
||||
|
||||
from .crypto import Crypto
|
||||
opt_is_in_list( a[1], list(Crypto.hash_presets.keys()), desc_pfx='hash preset' )
|
||||
opt_is_in_list(a[1], list(Crypto.hash_presets.keys()), desc_pfx='hash preset')
|
||||
|
||||
def usr_randchars():
|
||||
if val != 0:
|
||||
opt_compares(val,'>=',cfg.min_urandchars)
|
||||
opt_compares(val,'<=',cfg.max_urandchars)
|
||||
opt_compares(val, '>=', cfg.min_urandchars)
|
||||
opt_compares(val, '<=', cfg.max_urandchars)
|
||||
|
||||
def tx_confs():
|
||||
opt_is_int(val)
|
||||
opt_compares(int(val),'>=',1)
|
||||
opt_compares(int(val), '>=', 1)
|
||||
|
||||
def vsize_adj():
|
||||
from .util import ymsg
|
||||
|
|
@ -896,19 +893,19 @@ def check_opts(cfg): # Raises exception if any check fails
|
|||
|
||||
def daemon_id():
|
||||
from .daemon import CoinDaemon
|
||||
opt_is_in_list(val,CoinDaemon.all_daemon_ids())
|
||||
opt_is_in_list(val, CoinDaemon.all_daemon_ids())
|
||||
|
||||
def locktime():
|
||||
opt_is_int(val)
|
||||
opt_compares(int(val),'>',0)
|
||||
opt_compares(int(val), '>', 0)
|
||||
|
||||
def columns():
|
||||
opt_compares(val,'>',10)
|
||||
opt_compares(val, '>', 10)
|
||||
|
||||
# TODO: add checks for token, rbf, tx_fee
|
||||
check_funcs_names = tuple(check_funcs.__dict__)
|
||||
for name in tuple(cfg._uopts) + cfg._envopts + cfg._cfgfile_opts.non_auto:
|
||||
val = getattr(cfg,name)
|
||||
val = getattr(cfg, name)
|
||||
if name in cfg._infile_opts:
|
||||
from .fileutil import check_infile
|
||||
check_infile(val) # file exists and is readable - dies on error
|
||||
|
|
@ -916,19 +913,19 @@ def check_opts(cfg): # Raises exception if any check fails
|
|||
from .fileutil import check_outdir
|
||||
check_outdir(val) # dies on error
|
||||
elif name in check_funcs_names:
|
||||
getattr(check_funcs,name)()
|
||||
getattr(check_funcs, name)()
|
||||
elif cfg.debug:
|
||||
Msg(f'check_opts(): No test for config opt {name!r}')
|
||||
|
||||
def fmt_opt(o):
|
||||
return '--' + o.replace('_','-')
|
||||
return '--' + o.replace('_', '-')
|
||||
|
||||
def opt_postproc_debug(cfg):
|
||||
none_opts = [k for k in dir(cfg) if k[:2] != '__' and getattr(cfg,k) is None]
|
||||
none_opts = [k for k in dir(cfg) if k[:2] != '__' and getattr(cfg, k) is None]
|
||||
from .util import Msg
|
||||
Msg('\n Configuration opts:')
|
||||
for e in [d for d in dir(cfg) if d[:2] != '__']:
|
||||
Msg(f' {e:<20}: {getattr(cfg,e)}')
|
||||
Msg(f' {e:<20}: {getattr(cfg, e)}')
|
||||
Msg(" Configuration opts set to 'None':")
|
||||
Msg(' {}\n'.format('\n '.join(none_opts)))
|
||||
Msg('\n=== end opts.py debug ===\n')
|
||||
|
|
@ -939,21 +936,21 @@ def conv_type(
|
|||
refval,
|
||||
desc,
|
||||
invert_bool = False,
|
||||
src = None ):
|
||||
src = None):
|
||||
|
||||
def do_fail():
|
||||
die(1,'{a!r}: invalid value for {b} {c!r}{d} (must be of type {e!r})'.format(
|
||||
die(1, '{a!r}: invalid value for {b} {c!r}{d} (must be of type {e!r})'.format(
|
||||
a = val,
|
||||
b = desc,
|
||||
c = fmt_opt(name) if 'command-line' in desc else name,
|
||||
d = f' in {src!r}' if src else '',
|
||||
e = type(refval).__name__ ))
|
||||
e = type(refval).__name__))
|
||||
|
||||
if type(refval) is bool:
|
||||
v = str(val).lower()
|
||||
ret = (
|
||||
True if v in ('true','yes','1','on') else
|
||||
False if v in ('false','no','none','0','off','') else
|
||||
True if v in ('true', 'yes', '1', 'on') else
|
||||
False if v in ('false', 'no', 'none', '0', 'off', '') else
|
||||
None
|
||||
)
|
||||
return do_fail() if ret is None else (not ret) if invert_bool else ret
|
||||
|
|
|
|||
110
mmgen/cfgfile.py
110
mmgen/cfgfile.py
|
|
@ -20,13 +20,13 @@
|
|||
cfgfile: API for the MMGen runtime configuration file and related files
|
||||
"""
|
||||
|
||||
import os,re
|
||||
import os, re
|
||||
from collections import namedtuple
|
||||
|
||||
from .cfg import gc
|
||||
from .util import msg,ymsg,suf,fmt,fmt_list,oneshot_warning,strip_comment,capfirst,die
|
||||
from .util import msg, ymsg, suf, fmt, fmt_list, oneshot_warning, strip_comment, capfirst, die
|
||||
|
||||
def mmgen_cfg_file(cfg,id_str):
|
||||
def mmgen_cfg_file(cfg, id_str):
|
||||
return cfg_file.get_cls_by_id(id_str)(cfg)
|
||||
|
||||
class cfg_file:
|
||||
|
|
@ -35,62 +35,62 @@ class cfg_file:
|
|||
write_ok = False
|
||||
warn_missing = True
|
||||
write_metadata = False
|
||||
line_data = namedtuple('cfgfile_line',['name','value','lineno','chunk'])
|
||||
line_data = namedtuple('cfgfile_line', ['name', 'value', 'lineno', 'chunk'])
|
||||
fn_base = 'mmgen.cfg'
|
||||
|
||||
class warn_missing_file(oneshot_warning):
|
||||
color = 'yellow' # has no effect, as color not initialized yet
|
||||
message = '{} not found at {!r}'
|
||||
|
||||
def get_data(self,fn):
|
||||
def get_data(self, fn):
|
||||
try:
|
||||
with open(fn) as fp:
|
||||
return fp.read().splitlines()
|
||||
except:
|
||||
if self.warn_missing:
|
||||
self.warn_missing_file( div=fn, fmt_args=(self.desc,fn) )
|
||||
self.warn_missing_file(div=fn, fmt_args=(self.desc, fn))
|
||||
return ''
|
||||
|
||||
def copy_system_data(self,fn):
|
||||
def copy_system_data(self, fn):
|
||||
assert self.write_ok, f'writing to file {fn!r} not allowed!'
|
||||
src = mmgen_cfg_file(self.cfg,'sys')
|
||||
src = mmgen_cfg_file(self.cfg, 'sys')
|
||||
if src.data:
|
||||
data = src.data + src.make_metadata() if self.write_metadata else src.data
|
||||
try:
|
||||
with open(fn,'w') as fp:
|
||||
with open(fn, 'w') as fp:
|
||||
fp.write('\n'.join(data)+'\n')
|
||||
os.chmod(fn,0o600)
|
||||
os.chmod(fn, 0o600)
|
||||
except:
|
||||
die(2,f'ERROR: unable to write to {fn!r}')
|
||||
die(2, f'ERROR: unable to write to {fn!r}')
|
||||
|
||||
def parse_value(self,value,refval):
|
||||
if isinstance(refval,dict):
|
||||
m = re.fullmatch(r'((\s+\w+:\S+)+)',' '+value) # expect one or more colon-separated values
|
||||
def parse_value(self, value, refval):
|
||||
if isinstance(refval, dict):
|
||||
m = re.fullmatch(r'((\s+\w+:\S+)+)', ' '+value) # expect one or more colon-separated values
|
||||
if m:
|
||||
return dict([i.split(':') for i in m[1].split()])
|
||||
elif isinstance(refval,(list,tuple)):
|
||||
m = re.fullmatch(r'((\s+\S+)+)',' '+value) # expect single value or list
|
||||
elif isinstance(refval, (list, tuple)):
|
||||
m = re.fullmatch(r'((\s+\S+)+)', ' '+value) # expect single value or list
|
||||
if m:
|
||||
ret = m[1].split()
|
||||
return ret if isinstance(refval,list) else tuple(ret)
|
||||
return ret if isinstance(refval, list) else tuple(ret)
|
||||
else:
|
||||
return value
|
||||
|
||||
def get_lines(self):
|
||||
def gen_lines():
|
||||
for lineno,line in enumerate(self.data,1):
|
||||
for lineno, line in enumerate(self.data, 1):
|
||||
line = strip_comment(line)
|
||||
if line == '':
|
||||
continue
|
||||
m = re.fullmatch(r'(\w+)(\s+)(.*)',line)
|
||||
m = re.fullmatch(r'(\w+)(\s+)(.*)', line)
|
||||
if m:
|
||||
yield self.line_data(m[1],m[3],lineno,None)
|
||||
yield self.line_data(m[1], m[3], lineno, None)
|
||||
else:
|
||||
die( 'CfgFileParseError', f'Parse error in file {self.fn!r}, line {lineno}' )
|
||||
die('CfgFileParseError', f'Parse error in file {self.fn!r}, line {lineno}')
|
||||
return gen_lines()
|
||||
|
||||
@classmethod
|
||||
def get_cls_by_id(cls,id_str):
|
||||
def get_cls_by_id(cls, id_str):
|
||||
d = {
|
||||
'usr': CfgFileUsr,
|
||||
'sys': CfgFileSampleSys,
|
||||
|
|
@ -101,13 +101,13 @@ class cfg_file:
|
|||
class cfg_file_sample(cfg_file):
|
||||
|
||||
@classmethod
|
||||
def cls_make_metadata(cls,data):
|
||||
def cls_make_metadata(cls, data):
|
||||
return [f'# Version {cls.cur_ver} {cls.compute_chksum(data)}']
|
||||
|
||||
@staticmethod
|
||||
def compute_chksum(data):
|
||||
import hashlib
|
||||
return hashlib.new('ripemd160','\n'.join(data).encode()).hexdigest()
|
||||
return hashlib.new('ripemd160', '\n'.join(data).encode()).hexdigest()
|
||||
|
||||
@property
|
||||
def computed_chksum(self):
|
||||
|
|
@ -124,19 +124,19 @@ class cfg_file_sample(cfg_file):
|
|||
- last line is metadata line of the form '# Version VER_NUM HASH'
|
||||
"""
|
||||
|
||||
def process_chunk(chunk,lineno):
|
||||
m = re.fullmatch(r'(#\s*)(\w+)(\s+)(.*)',chunk[-1])
|
||||
def process_chunk(chunk, lineno):
|
||||
m = re.fullmatch(r'(#\s*)(\w+)(\s+)(.*)', chunk[-1])
|
||||
if m:
|
||||
return self.line_data(m[2],m[4],lineno,chunk)
|
||||
return self.line_data(m[2], m[4], lineno, chunk)
|
||||
else:
|
||||
die( 'CfgFileParseError', f'Parse error in file {self.fn!r}, line {lineno}' )
|
||||
die('CfgFileParseError', f'Parse error in file {self.fn!r}, line {lineno}')
|
||||
|
||||
def gen_chunks(lines):
|
||||
hdr = True
|
||||
chunk = []
|
||||
in_chunk = False
|
||||
|
||||
for lineno,line in enumerate(lines,1):
|
||||
for lineno, line in enumerate(lines, 1):
|
||||
|
||||
if line.startswith('##'):
|
||||
hdr = False
|
||||
|
|
@ -150,17 +150,17 @@ class cfg_file_sample(cfg_file):
|
|||
elif line.startswith('#'):
|
||||
if in_chunk is False:
|
||||
if chunk:
|
||||
yield process_chunk(chunk,last_nonblank)
|
||||
yield process_chunk(chunk, last_nonblank)
|
||||
chunk = [line]
|
||||
in_chunk = True
|
||||
else:
|
||||
chunk.append(line)
|
||||
last_nonblank = lineno
|
||||
else:
|
||||
die( 'CfgFileParseError', f'Parse error in file {self.fn!r}, line {lineno}' )
|
||||
die('CfgFileParseError', f'Parse error in file {self.fn!r}, line {lineno}')
|
||||
|
||||
if chunk:
|
||||
yield process_chunk(chunk,last_nonblank)
|
||||
yield process_chunk(chunk, last_nonblank)
|
||||
|
||||
return list(gen_chunks(self.data))
|
||||
|
||||
|
|
@ -169,9 +169,9 @@ class CfgFileUsr(cfg_file):
|
|||
warn_missing = False
|
||||
write_ok = True
|
||||
|
||||
def __init__(self,cfg):
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.fn = os.path.join(cfg.data_dir_root,self.fn_base)
|
||||
self.fn = os.path.join(cfg.data_dir_root, self.fn_base)
|
||||
self.data = self.get_data(self.fn)
|
||||
if not self.data:
|
||||
self.copy_system_data(self.fn)
|
||||
|
|
@ -180,15 +180,15 @@ class CfgFileSampleSys(cfg_file_sample):
|
|||
desc = 'system sample configuration file'
|
||||
test_fn_subdir = 'usr.local.share'
|
||||
|
||||
def __init__(self,cfg):
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
if self.cfg.test_suite_cfgtest:
|
||||
self.fn = os.path.join(cfg.data_dir_root,self.test_fn_subdir,self.fn_base)
|
||||
self.fn = os.path.join(cfg.data_dir_root, self.test_fn_subdir, self.fn_base)
|
||||
with open(self.fn) as fp:
|
||||
self.data = fp.read().splitlines()
|
||||
else:
|
||||
# self.fn is used for error msgs only, so file need not exist on filesystem
|
||||
self.fn = os.path.join(os.path.dirname(__file__),'data',self.fn_base)
|
||||
self.fn = os.path.join(os.path.dirname(__file__), 'data', self.fn_base)
|
||||
self.data = gc.get_mmgen_data_file(self.fn_base).splitlines()
|
||||
|
||||
def make_metadata(self):
|
||||
|
|
@ -204,12 +204,12 @@ class CfgFileSampleUsr(cfg_file_sample):
|
|||
out_of_date_fs = 'File {!r} is out of date - replacing'
|
||||
altered_by_user_fs = 'File {!r} was altered by user - replacing'
|
||||
|
||||
def __init__(self,cfg):
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.fn = os.path.join(cfg.data_dir_root,f'{self.fn_base}.sample')
|
||||
self.fn = os.path.join(cfg.data_dir_root, f'{self.fn_base}.sample')
|
||||
self.data = self.get_data(self.fn)
|
||||
|
||||
src = mmgen_cfg_file(cfg,'sys')
|
||||
src = mmgen_cfg_file(cfg, 'sys')
|
||||
|
||||
if not src.data:
|
||||
return
|
||||
|
|
@ -217,7 +217,7 @@ class CfgFileSampleUsr(cfg_file_sample):
|
|||
if self.data:
|
||||
if self.parse_metadata():
|
||||
if self.chksum == self.computed_chksum:
|
||||
diff = self.diff(self.get_lines(),src.get_lines())
|
||||
diff = self.diff(self.get_lines(), src.get_lines())
|
||||
if not diff:
|
||||
return
|
||||
self.show_changes(diff)
|
||||
|
|
@ -230,14 +230,14 @@ class CfgFileSampleUsr(cfg_file_sample):
|
|||
|
||||
def parse_metadata(self):
|
||||
if self.data:
|
||||
m = re.match(r'# Version (\d+) ([a-f0-9]{40})$',self.data[-1])
|
||||
m = re.match(r'# Version (\d+) ([a-f0-9]{40})$', self.data[-1])
|
||||
if m:
|
||||
self.ver = m[1]
|
||||
self.chksum = m[2]
|
||||
self.data = self.data[:-1] # remove metadata line
|
||||
return True
|
||||
|
||||
def diff(self,a_tup,b_tup): # a=user, b=system
|
||||
def diff(self, a_tup, b_tup): # a=user, b=system
|
||||
a = [i.name for i in a_tup]#[3:] # Debug
|
||||
b = [i.name for i in b_tup]#[:-2] # Debug
|
||||
removed = set(a) - set(b)
|
||||
|
|
@ -250,34 +250,34 @@ class CfgFileSampleUsr(cfg_file_sample):
|
|||
else:
|
||||
return None
|
||||
|
||||
def show_changes(self,diff):
|
||||
def show_changes(self, diff):
|
||||
ymsg('Warning: configuration file options have changed!\n')
|
||||
for desc in ('added','removed'):
|
||||
for desc in ('added', 'removed'):
|
||||
data = diff[desc]
|
||||
if data:
|
||||
opts = fmt_list([i.name for i in data],fmt='bare')
|
||||
msg(f' The following option{suf(data,verb="has")} been {desc}:\n {opts}\n')
|
||||
opts = fmt_list([i.name for i in data], fmt='bare')
|
||||
msg(f' The following option{suf(data, verb="has")} been {desc}:\n {opts}\n')
|
||||
if desc == 'removed' and data:
|
||||
uc = mmgen_cfg_file(self.cfg,'usr')
|
||||
uc = mmgen_cfg_file(self.cfg, 'usr')
|
||||
usr_names = [i.name for i in uc.get_lines()]
|
||||
rm_names = [i.name for i in data]
|
||||
bad = sorted(set(usr_names).intersection(rm_names))
|
||||
if bad:
|
||||
m = f"""
|
||||
The following removed option{suf(bad,verb='is')} set in {uc.fn!r}
|
||||
The following removed option{suf(bad, verb='is')} set in {uc.fn!r}
|
||||
and must be deleted or commented out:
|
||||
{' ' + fmt_list(bad,fmt='bare')}
|
||||
{' ' + fmt_list(bad, fmt='bare')}
|
||||
"""
|
||||
ymsg(fmt(m,indent=' ',strip_char='\t'))
|
||||
ymsg(fmt(m, indent=' ', strip_char='\t'))
|
||||
|
||||
from .ui import keypress_confirm,do_pager
|
||||
from .ui import keypress_confirm, do_pager
|
||||
while True:
|
||||
if not keypress_confirm( self.cfg, self.details_confirm_prompt, no_nl=True ):
|
||||
if not keypress_confirm(self.cfg, self.details_confirm_prompt, no_nl=True):
|
||||
return
|
||||
|
||||
def get_details():
|
||||
for desc,data in diff.items():
|
||||
sep,sep2 = ('\n ','\n\n ')
|
||||
for desc, data in diff.items():
|
||||
sep, sep2 = ('\n ', '\n\n ')
|
||||
if data:
|
||||
yield (
|
||||
f'{capfirst(desc)} section{suf(data)}:'
|
||||
|
|
|
|||
|
|
@ -21,27 +21,27 @@ color: color handling for the MMGen suite
|
|||
"""
|
||||
|
||||
_colors = {
|
||||
'black': ( 232, (30,0) ),
|
||||
'red': ( 210, (31,1) ),
|
||||
'green': ( 121, (32,1) ),
|
||||
'yellow': ( 229, (33,1) ),
|
||||
'blue': ( 75, (34,1) ),
|
||||
'magenta': ( 205, (35,1) ),
|
||||
'cyan': ( 122, (36,1) ),
|
||||
'black': (232, (30, 0)),
|
||||
'red': (210, (31, 1)),
|
||||
'green': (121, (32, 1)),
|
||||
'yellow': (229, (33, 1)),
|
||||
'blue': (75, (34, 1)),
|
||||
'magenta': (205, (35, 1)),
|
||||
'cyan': (122, (36, 1)),
|
||||
|
||||
'gray': ( 246, (30,1) ),
|
||||
'orange': ( 216, (31,1) ),
|
||||
'purple': ( 141, (35,1) ),
|
||||
'pink': ( 218, (35,1) ),
|
||||
'gray': (246, (30, 1)),
|
||||
'orange': (216, (31, 1)),
|
||||
'purple': (141, (35, 1)),
|
||||
'pink': (218, (35, 1)),
|
||||
|
||||
'melon': ( 222, (33,1) ),
|
||||
'brown': ( 173, (33,0) ),
|
||||
'grndim': ( 108, (32,0) ),
|
||||
'melon': (222, (33, 1)),
|
||||
'brown': (173, (33, 0)),
|
||||
'grndim': (108, (32, 0)),
|
||||
|
||||
'redbg': ( (232,210), (30,101) ),
|
||||
'grnbg': ( (232,121), (30,102) ),
|
||||
'yelbg': ( (232,229), (30,103) ),
|
||||
'blubg': ( (232,75), (30,104) ),
|
||||
'redbg': ((232, 210), (30, 101)),
|
||||
'grnbg': ((232, 121), (30, 102)),
|
||||
'yelbg': ((232, 229), (30, 103)),
|
||||
'blubg': ((232, 75), (30, 104)),
|
||||
}
|
||||
|
||||
def nocolor(s):
|
||||
|
|
@ -52,16 +52,16 @@ def set_vt100():
|
|||
import sys
|
||||
if sys.platform == 'win32':
|
||||
from subprocess import run
|
||||
run([],shell=True)
|
||||
run([], shell=True)
|
||||
|
||||
def get_terminfo_colors(term=None):
|
||||
from subprocess import run,PIPE
|
||||
cmd = ['infocmp','-0']
|
||||
from subprocess import run, PIPE
|
||||
cmd = ['infocmp', '-0']
|
||||
if term:
|
||||
cmd.append(term)
|
||||
|
||||
try:
|
||||
cmdout = run(cmd,stdout=PIPE,check=True).stdout.decode()
|
||||
cmdout = run(cmd, stdout=PIPE, check=True).stdout.decode()
|
||||
except:
|
||||
set_vt100()
|
||||
return None
|
||||
|
|
@ -72,12 +72,12 @@ def get_terminfo_colors(term=None):
|
|||
if s.isdecimal():
|
||||
return int(s)
|
||||
elif s.startswith('0x') and is_hex_str(s[2:]):
|
||||
return int(s[2:],16)
|
||||
return int(s[2:], 16)
|
||||
else:
|
||||
return None
|
||||
|
||||
def init_color(num_colors='auto'):
|
||||
assert num_colors in ('auto',8,16,256,0)
|
||||
assert num_colors in ('auto', 8, 16, 256, 0)
|
||||
|
||||
import sys
|
||||
self = sys.modules[__name__]
|
||||
|
|
@ -98,19 +98,19 @@ def init_color(num_colors='auto'):
|
|||
if num_colors == 0:
|
||||
ncc = (lambda s: s).__code__
|
||||
for c in _colors:
|
||||
getattr(self,c).__code__ = ncc
|
||||
getattr(self, c).__code__ = ncc
|
||||
elif num_colors == 256:
|
||||
for c,e in _colors.items():
|
||||
for c, e in _colors.items():
|
||||
start = (
|
||||
'\033[38;5;{};1m'.format(e[0]) if type(e[0]) == int else
|
||||
'\033[38;5;{};48;5;{};1m'.format(*e[0]) )
|
||||
getattr(self,c).__code__ = eval(f'(lambda s: "{start}" + s + "{reset}").__code__')
|
||||
elif num_colors in (8,16):
|
||||
for c,e in _colors.items():
|
||||
'\033[38;5;{};48;5;{};1m'.format(*e[0]))
|
||||
getattr(self, c).__code__ = eval(f'(lambda s: "{start}" + s + "{reset}").__code__')
|
||||
elif num_colors in (8, 16):
|
||||
for c, e in _colors.items():
|
||||
start = (
|
||||
'\033[{}m'.format(e[1][0]) if e[1][1] == 0 else
|
||||
'\033[{};{}m'.format(*e[1]) )
|
||||
getattr(self,c).__code__ = eval(f'(lambda s: "{start}" + s + "{reset}").__code__')
|
||||
'\033[{};{}m'.format(*e[1]))
|
||||
getattr(self, c).__code__ = eval(f'(lambda s: "{start}" + s + "{reset}").__code__')
|
||||
|
||||
set_vt100()
|
||||
|
||||
|
|
|
|||
162
mmgen/crypto.py
162
mmgen/crypto.py
|
|
@ -24,15 +24,7 @@ import os
|
|||
from collections import namedtuple
|
||||
|
||||
from .cfg import gc
|
||||
from .util import (
|
||||
msg,
|
||||
msg_r,
|
||||
ymsg,
|
||||
fmt,
|
||||
die,
|
||||
make_chksum_8,
|
||||
oneshot_warning,
|
||||
)
|
||||
from .util import msg, msg_r, ymsg, fmt, die, make_chksum_8, oneshot_warning
|
||||
|
||||
class Crypto:
|
||||
|
||||
|
|
@ -41,7 +33,7 @@ class Crypto:
|
|||
|
||||
salt_len = 16
|
||||
aesctr_iv_len = 16
|
||||
aesctr_dfl_iv = int.to_bytes(1,aesctr_iv_len,'big')
|
||||
aesctr_dfl_iv = int.to_bytes(1, aesctr_iv_len, 'big')
|
||||
hincog_chk_len = 8
|
||||
|
||||
mmenc_salt_len = 32
|
||||
|
|
@ -50,7 +42,7 @@ class Crypto:
|
|||
# Scrypt params: 'id_num': [N, r, p] (N is an exponent of two)
|
||||
# NB: hashlib.scrypt in Python (>=v3.6) supports max N value of 14. This means that
|
||||
# for hash presets > 3 the standalone scrypt library must be used!
|
||||
_hp = namedtuple('scrypt_preset',['N','r','p'])
|
||||
_hp = namedtuple('scrypt_preset', ['N', 'r', 'p'])
|
||||
hash_presets = {
|
||||
'1': _hp(12, 8, 1),
|
||||
'2': _hp(13, 8, 4),
|
||||
|
|
@ -63,60 +55,60 @@ class Crypto:
|
|||
|
||||
class pwfile_reuse_warning(oneshot_warning):
|
||||
message = 'Reusing passphrase from file {!r} at user request'
|
||||
def __init__(self,fn):
|
||||
oneshot_warning.__init__(self,div=fn,fmt_args=[fn],reverse=True)
|
||||
def __init__(self, fn):
|
||||
oneshot_warning.__init__(self, div=fn, fmt_args=[fn], reverse=True)
|
||||
|
||||
def pwfile_used(self,passwd_file):
|
||||
if hasattr(self,'_pwfile_used'):
|
||||
def pwfile_used(self, passwd_file):
|
||||
if hasattr(self, '_pwfile_used'):
|
||||
self.pwfile_reuse_warning(passwd_file)
|
||||
return True
|
||||
else:
|
||||
self._pwfile_used = True
|
||||
return False
|
||||
|
||||
def __init__(self,cfg):
|
||||
def __init__(self, cfg):
|
||||
self.cfg = cfg
|
||||
self.util = cfg._util
|
||||
|
||||
def get_hash_params(self,hash_preset):
|
||||
def get_hash_params(self, hash_preset):
|
||||
if hash_preset in self.hash_presets:
|
||||
return self.hash_presets[hash_preset] # N,r,p
|
||||
return self.hash_presets[hash_preset] # N, r, p
|
||||
else: # Shouldn't be here
|
||||
die(3,f"{hash_preset}: invalid 'hash_preset' value")
|
||||
die(3, f"{hash_preset}: invalid 'hash_preset' value")
|
||||
|
||||
def sha256_rounds(self,s):
|
||||
def sha256_rounds(self, s):
|
||||
from hashlib import sha256
|
||||
for _ in range(self.scramble_hash_rounds):
|
||||
s = sha256(s).digest()
|
||||
return s
|
||||
|
||||
def scramble_seed(self,seed,scramble_key):
|
||||
def scramble_seed(self, seed, scramble_key):
|
||||
import hmac
|
||||
step1 = hmac.digest(seed,scramble_key,'sha256')
|
||||
step1 = hmac.digest(seed, scramble_key, 'sha256')
|
||||
if self.cfg.debug:
|
||||
msg(f'Seed: {seed.hex()!r}\nScramble key: {scramble_key}\nScrambled seed: {step1.hex()}\n')
|
||||
return self.sha256_rounds(step1)
|
||||
|
||||
def encrypt_seed(self,data,key,desc='seed'):
|
||||
return self.encrypt_data(data,key,desc=desc)
|
||||
def encrypt_seed(self, data, key, desc='seed'):
|
||||
return self.encrypt_data(data, key, desc=desc)
|
||||
|
||||
def decrypt_seed(self,enc_seed,key,seed_id,key_id):
|
||||
def decrypt_seed(self, enc_seed, key, seed_id, key_id):
|
||||
self.util.vmsg_r('Checking key...')
|
||||
chk1 = make_chksum_8(key)
|
||||
if key_id:
|
||||
if not self.util.compare_chksums(key_id,'key ID',chk1,'computed'):
|
||||
if not self.util.compare_chksums(key_id, 'key ID', chk1, 'computed'):
|
||||
msg('Incorrect passphrase or hash preset')
|
||||
return False
|
||||
|
||||
dec_seed = self.decrypt_data(enc_seed,key,desc='seed')
|
||||
dec_seed = self.decrypt_data(enc_seed, key, desc='seed')
|
||||
chk2 = make_chksum_8(dec_seed)
|
||||
if seed_id:
|
||||
if self.util.compare_chksums(seed_id,'Seed ID',chk2,'decrypted seed'):
|
||||
if self.util.compare_chksums(seed_id, 'Seed ID', chk2, 'decrypted seed'):
|
||||
self.util.qmsg('Passphrase is OK')
|
||||
else:
|
||||
if not self.cfg.debug:
|
||||
msg_r('Checking key ID...')
|
||||
if self.util.compare_chksums(key_id,'key ID',chk1,'computed'):
|
||||
if self.util.compare_chksums(key_id, 'key ID', chk1, 'computed'):
|
||||
msg('Key ID is correct but decryption of seed failed')
|
||||
else:
|
||||
msg('Incorrect passphrase or hash preset')
|
||||
|
|
@ -130,26 +122,26 @@ class Crypto:
|
|||
self,
|
||||
data,
|
||||
key,
|
||||
iv = aesctr_dfl_iv,
|
||||
desc = 'data',
|
||||
iv = aesctr_dfl_iv,
|
||||
desc = 'data',
|
||||
verify = True,
|
||||
silent = False ):
|
||||
silent = False):
|
||||
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher,algorithms,modes
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
if not silent:
|
||||
self.util.vmsg(f'Encrypting {desc}')
|
||||
c = Cipher(algorithms.AES(key),modes.CTR(iv),backend=default_backend())
|
||||
c = Cipher(algorithms.AES(key), modes.CTR(iv), backend=default_backend())
|
||||
encryptor = c.encryptor()
|
||||
enc_data = encryptor.update(data) + encryptor.finalize()
|
||||
|
||||
if verify:
|
||||
self.util.vmsg_r(f'Performing a test decryption of the {desc}...')
|
||||
c = Cipher(algorithms.AES(key),modes.CTR(iv),backend=default_backend())
|
||||
c = Cipher(algorithms.AES(key), modes.CTR(iv), backend=default_backend())
|
||||
encryptor = c.encryptor()
|
||||
dec_data = encryptor.update(enc_data) + encryptor.finalize()
|
||||
if dec_data != data:
|
||||
die(2,f'ERROR.\nDecrypted {desc} doesn’t match original {desc}')
|
||||
die(2, f'ERROR.\nDecrypted {desc} doesn’t match original {desc}')
|
||||
if not silent:
|
||||
self.util.vmsg('done')
|
||||
|
||||
|
|
@ -159,13 +151,13 @@ class Crypto:
|
|||
self,
|
||||
enc_data,
|
||||
key,
|
||||
iv = aesctr_dfl_iv,
|
||||
desc = 'data' ):
|
||||
iv = aesctr_dfl_iv,
|
||||
desc = 'data'):
|
||||
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher,algorithms,modes
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
self.util.vmsg_r(f'Decrypting {desc} with key...')
|
||||
c = Cipher(algorithms.AES(key),modes.CTR(iv),backend=default_backend())
|
||||
c = Cipher(algorithms.AES(key), modes.CTR(iv), backend=default_backend())
|
||||
encryptor = c.encryptor()
|
||||
return encryptor.update(enc_data) + encryptor.finalize()
|
||||
|
||||
|
|
@ -174,13 +166,13 @@ class Crypto:
|
|||
passwd,
|
||||
salt,
|
||||
hash_preset,
|
||||
buflen = 32 ):
|
||||
buflen = 32):
|
||||
|
||||
# Buflen arg is for brainwallets only, which use this function to generate
|
||||
# the seed directly.
|
||||
ps = self.get_hash_params(hash_preset)
|
||||
|
||||
if isinstance(passwd,str):
|
||||
if isinstance(passwd, str):
|
||||
passwd = passwd.encode()
|
||||
|
||||
def do_hashlib_scrypt():
|
||||
|
|
@ -192,7 +184,7 @@ class Crypto:
|
|||
r = ps.r,
|
||||
p = ps.p,
|
||||
maxmem = 0,
|
||||
dklen = buflen )
|
||||
dklen = buflen)
|
||||
|
||||
def do_standalone_scrypt():
|
||||
import scrypt
|
||||
|
|
@ -202,7 +194,7 @@ class Crypto:
|
|||
N = 2**ps.N,
|
||||
r = ps.r,
|
||||
p = ps.p,
|
||||
buflen = buflen )
|
||||
buflen = buflen)
|
||||
|
||||
if int(hash_preset) > 3:
|
||||
msg_r('Hashing passphrase, please wait...')
|
||||
|
|
@ -210,7 +202,7 @@ class Crypto:
|
|||
# hashlib.scrypt doesn't support N > 14 (hash preset > 3)
|
||||
ret = (
|
||||
do_standalone_scrypt() if ps.N > 14 or self.cfg.force_standalone_scrypt_module else
|
||||
do_hashlib_scrypt() )
|
||||
do_hashlib_scrypt())
|
||||
|
||||
if int(hash_preset) > 3:
|
||||
msg_r('\b'*34 + ' '*34 + '\b'*34)
|
||||
|
|
@ -222,19 +214,19 @@ class Crypto:
|
|||
passwd,
|
||||
salt,
|
||||
hash_preset,
|
||||
desc = 'encryption key',
|
||||
desc = 'encryption key',
|
||||
from_what = 'passphrase',
|
||||
verbose = False ):
|
||||
verbose = False):
|
||||
|
||||
if self.cfg.verbose or verbose:
|
||||
msg_r(f"Generating {desc}{' from ' + from_what if from_what else ''}...")
|
||||
key = self.scrypt_hash_passphrase(passwd,salt,hash_preset)
|
||||
key = self.scrypt_hash_passphrase(passwd, salt, hash_preset)
|
||||
if self.cfg.verbose or verbose:
|
||||
msg('done')
|
||||
self.util.dmsg(f'Key: {key.hex()}')
|
||||
return key
|
||||
|
||||
def _get_random_data_from_user(self,uchars=None,desc='data'):
|
||||
def _get_random_data_from_user(self, uchars=None, desc='data'):
|
||||
|
||||
if uchars is None:
|
||||
uchars = self.cfg.usr_randchars
|
||||
|
|
@ -260,8 +252,8 @@ class Crypto:
|
|||
"""
|
||||
|
||||
msg(f'Enter {uchars} random symbols' if self.cfg.quiet else
|
||||
'\n' + fmt(info1,indent=' ') +
|
||||
'\n' + fmt(info2) )
|
||||
'\n' + fmt(info1, indent=' ') +
|
||||
'\n' + fmt(info2))
|
||||
|
||||
import time
|
||||
from .term import get_char_raw
|
||||
|
|
@ -272,7 +264,7 @@ class Crypto:
|
|||
key_data += get_char_raw(f'\rYou may begin typing. {uchars-i} symbols left: ')
|
||||
time_data.append(time.time())
|
||||
|
||||
msg_r( '\r' if self.cfg.quiet else f'\rThank you. That’s enough.{" "*18}\n\n' )
|
||||
msg_r('\r' if self.cfg.quiet else f'\rThank you. That’s enough.{" "*18}\n\n')
|
||||
|
||||
time_data = [f'{t:.22f}'.rstrip('0') for t in time_data]
|
||||
|
||||
|
|
@ -287,24 +279,24 @@ class Crypto:
|
|||
msg(f'USER ENTROPY (user input + keystroke timings):\n{ret}')
|
||||
|
||||
from .ui import line_input
|
||||
line_input( self.cfg, 'User random data successfully acquired. Press ENTER to continue: ' )
|
||||
line_input(self.cfg, 'User random data successfully acquired. Press ENTER to continue: ')
|
||||
|
||||
return ret.encode()
|
||||
|
||||
def get_random(self,length):
|
||||
def get_random(self, length):
|
||||
|
||||
os_rand = os.urandom(length)
|
||||
assert len(os_rand) == length, f'OS random number generator returned {len(os_rand)} (!= {length}) bytes!'
|
||||
|
||||
return self.add_user_random(
|
||||
rand_bytes = os_rand,
|
||||
desc = 'from your operating system' )
|
||||
desc = 'from your operating system')
|
||||
|
||||
def add_user_random(
|
||||
self,
|
||||
rand_bytes,
|
||||
desc,
|
||||
urand = {'data':b'', 'counter':0} ):
|
||||
urand = {'data':b'', 'counter':0}):
|
||||
|
||||
assert type(rand_bytes) is bytes, 'add_user_random_chk1'
|
||||
|
||||
|
|
@ -323,12 +315,12 @@ class Crypto:
|
|||
import hmac
|
||||
key = hmac.digest(
|
||||
urand['data'],
|
||||
os_rand + int.to_bytes(urand['counter'],8,'big'),
|
||||
'sha256' )
|
||||
os_rand + int.to_bytes(urand['counter'], 8, 'big'),
|
||||
'sha256')
|
||||
|
||||
msg(f'Encrypting random data {desc} with ephemeral key #{urand["counter"]}')
|
||||
|
||||
return self.encrypt_data( data=rand_bytes, key=key, desc=desc, verify=False, silent=True )
|
||||
return self.encrypt_data(data=rand_bytes, key=key, desc=desc, verify=False, silent=True)
|
||||
else:
|
||||
return rand_bytes
|
||||
|
||||
|
|
@ -336,15 +328,15 @@ class Crypto:
|
|||
self,
|
||||
old_preset = gc.dfl_hash_preset,
|
||||
data_desc = 'data',
|
||||
prompt = None ):
|
||||
prompt = None):
|
||||
|
||||
prompt = prompt or (
|
||||
f'Enter hash preset for {data_desc},\n' +
|
||||
f'or hit ENTER to accept the default value ({old_preset!r}): ' )
|
||||
f'Enter hash preset for {data_desc}, \n' +
|
||||
f'or hit ENTER to accept the default value ({old_preset!r}): ')
|
||||
|
||||
from .ui import line_input
|
||||
while True:
|
||||
ret = line_input( self.cfg, prompt )
|
||||
ret = line_input(self.cfg, prompt)
|
||||
if ret:
|
||||
if ret in self.hash_presets:
|
||||
return ret
|
||||
|
|
@ -353,7 +345,7 @@ class Crypto:
|
|||
else:
|
||||
return old_preset
|
||||
|
||||
def get_new_passphrase(self,data_desc,hash_preset,passwd_file,pw_desc='passphrase'):
|
||||
def get_new_passphrase(self, data_desc, hash_preset, passwd_file, pw_desc='passphrase'):
|
||||
message = f"""
|
||||
You must choose a passphrase to encrypt your {data_desc} with.
|
||||
A key will be generated from your passphrase using a hash preset of '{hash_preset}'.
|
||||
|
|
@ -366,72 +358,72 @@ class Crypto:
|
|||
cfg = self.cfg,
|
||||
infile = passwd_file,
|
||||
desc = f'{pw_desc} for {data_desc}',
|
||||
quiet = self.pwfile_used(passwd_file) ))
|
||||
quiet = self.pwfile_used(passwd_file)))
|
||||
else:
|
||||
self.util.qmsg('\n'+fmt(message,indent=' '))
|
||||
self.util.qmsg('\n'+fmt(message, indent=' '))
|
||||
from .ui import get_words_from_user
|
||||
if self.cfg.echo_passphrase:
|
||||
pw = ' '.join(get_words_from_user( self.cfg, f'Enter {pw_desc} for {data_desc}: ' ))
|
||||
pw = ' '.join(get_words_from_user(self.cfg, f'Enter {pw_desc} for {data_desc}: '))
|
||||
else:
|
||||
for _ in range(gc.passwd_max_tries):
|
||||
pw = ' '.join(get_words_from_user( self.cfg, f'Enter {pw_desc} for {data_desc}: ' ))
|
||||
pw_chk = ' '.join(get_words_from_user( self.cfg, f'Repeat {pw_desc}: ' ))
|
||||
pw = ' '.join(get_words_from_user(self.cfg, f'Enter {pw_desc} for {data_desc}: '))
|
||||
pw_chk = ' '.join(get_words_from_user(self.cfg, f'Repeat {pw_desc}: '))
|
||||
self.util.dmsg(f'Passphrases: [{pw}] [{pw_chk}]')
|
||||
if pw == pw_chk:
|
||||
self.util.vmsg('Passphrases match')
|
||||
break
|
||||
msg('Passphrases do not match. Try again.')
|
||||
else:
|
||||
die(2,f'User failed to duplicate passphrase in {gc.passwd_max_tries} attempts')
|
||||
die(2, f'User failed to duplicate passphrase in {gc.passwd_max_tries} attempts')
|
||||
|
||||
if pw == '':
|
||||
self.util.qmsg('WARNING: Empty passphrase')
|
||||
|
||||
return pw
|
||||
|
||||
def get_passphrase(self,data_desc,passwd_file,pw_desc='passphrase'):
|
||||
def get_passphrase(self, data_desc, passwd_file, pw_desc='passphrase'):
|
||||
if passwd_file:
|
||||
from .fileutil import get_words_from_file
|
||||
return ' '.join(get_words_from_file(
|
||||
cfg = self.cfg,
|
||||
infile = passwd_file,
|
||||
desc = f'{pw_desc} for {data_desc}',
|
||||
quiet = self.pwfile_used(passwd_file) ))
|
||||
quiet = self.pwfile_used(passwd_file)))
|
||||
else:
|
||||
from .ui import get_words_from_user
|
||||
return ' '.join(get_words_from_user( self.cfg, f'Enter {pw_desc} for {data_desc}: ' ))
|
||||
return ' '.join(get_words_from_user(self.cfg, f'Enter {pw_desc} for {data_desc}: '))
|
||||
|
||||
def mmgen_encrypt(self,data,desc='data',hash_preset=None):
|
||||
def mmgen_encrypt(self, data, desc='data', hash_preset=None):
|
||||
salt = self.get_random(self.mmenc_salt_len)
|
||||
iv = self.get_random(self.aesctr_iv_len)
|
||||
nonce = self.get_random(self.mmenc_nonce_len)
|
||||
hp = hash_preset or self.cfg.hash_preset or self.get_hash_preset_from_user(data_desc=desc)
|
||||
m = ('user-requested','default')[hp=='3']
|
||||
m = ('user-requested', 'default')[hp=='3']
|
||||
self.util.vmsg(f'Encrypting {desc}')
|
||||
self.util.qmsg(f'Using {m} hash preset of {hp!r}')
|
||||
passwd = self.get_new_passphrase(
|
||||
data_desc = desc,
|
||||
hash_preset = hp,
|
||||
passwd_file = self.cfg.passwd_file )
|
||||
key = self.make_key(passwd,salt,hp)
|
||||
passwd_file = self.cfg.passwd_file)
|
||||
key = self.make_key(passwd, salt, hp)
|
||||
from hashlib import sha256
|
||||
enc_d = self.encrypt_data( sha256(nonce+data).digest() + nonce + data, key, iv, desc=desc )
|
||||
enc_d = self.encrypt_data(sha256(nonce+data).digest() + nonce + data, key, iv, desc=desc)
|
||||
return salt+iv+enc_d
|
||||
|
||||
def mmgen_decrypt(self,data,desc='data',hash_preset=None):
|
||||
def mmgen_decrypt(self, data, desc='data', hash_preset=None):
|
||||
self.util.vmsg(f'Preparing to decrypt {desc}')
|
||||
dstart = self.mmenc_salt_len + self.aesctr_iv_len
|
||||
salt = data[:self.mmenc_salt_len]
|
||||
iv = data[self.mmenc_salt_len:dstart]
|
||||
enc_d = data[dstart:]
|
||||
hp = hash_preset or self.cfg.hash_preset or self.get_hash_preset_from_user(data_desc=desc)
|
||||
m = ('user-requested','default')[hp=='3']
|
||||
m = ('user-requested', 'default')[hp=='3']
|
||||
self.util.qmsg(f'Using {m} hash preset of {hp!r}')
|
||||
passwd = self.get_passphrase(
|
||||
data_desc = desc,
|
||||
passwd_file = self.cfg.passwd_file )
|
||||
key = self.make_key(passwd,salt,hp)
|
||||
dec_d = self.decrypt_data( enc_d, key, iv, desc )
|
||||
passwd_file = self.cfg.passwd_file)
|
||||
key = self.make_key(passwd, salt, hp)
|
||||
dec_d = self.decrypt_data(enc_d, key, iv, desc)
|
||||
sha256_len = 32
|
||||
from hashlib import sha256
|
||||
if dec_d[:sha256_len] == sha256(dec_d[sha256_len:]).digest():
|
||||
|
|
@ -441,9 +433,9 @@ class Crypto:
|
|||
msg('Incorrect passphrase or hash preset')
|
||||
return False
|
||||
|
||||
def mmgen_decrypt_retry(self,d,desc='data'):
|
||||
def mmgen_decrypt_retry(self, d, desc='data'):
|
||||
while True:
|
||||
d_dec = self.mmgen_decrypt(d,desc)
|
||||
d_dec = self.mmgen_decrypt(d, desc)
|
||||
if d_dec:
|
||||
return d_dec
|
||||
msg('Trying again...')
|
||||
|
|
|
|||
202
mmgen/daemon.py
202
mmgen/daemon.py
|
|
@ -20,17 +20,17 @@
|
|||
daemon: Daemon control interface for the MMGen suite
|
||||
"""
|
||||
|
||||
import sys,os,time,importlib
|
||||
from subprocess import run,PIPE,CompletedProcess
|
||||
import sys, os, time, importlib
|
||||
from subprocess import run, PIPE, CompletedProcess
|
||||
from collections import namedtuple
|
||||
|
||||
from .base_obj import Lockable
|
||||
from .color import set_vt100
|
||||
from .util import msg,Msg_r,die,remove_dups,oneshot_warning,fmt_list
|
||||
from .flags import ClassFlags,ClassOpts
|
||||
from .util import msg, Msg_r, die, remove_dups, oneshot_warning, fmt_list
|
||||
from .flags import ClassFlags, ClassOpts
|
||||
|
||||
_dd = namedtuple('daemon_data',['coind_name','coind_version','coind_version_str']) # latest tested version
|
||||
_nw = namedtuple('coin_networks',['mainnet','testnet','regtest'])
|
||||
_dd = namedtuple('daemon_data', ['coind_name', 'coind_version', 'coind_version_str']) # latest tested version
|
||||
_nw = namedtuple('coin_networks', ['mainnet', 'testnet', 'regtest'])
|
||||
|
||||
class Daemon(Lockable):
|
||||
|
||||
|
|
@ -47,10 +47,10 @@ class Daemon(Lockable):
|
|||
private_port = None
|
||||
avail_opts = ()
|
||||
avail_flags = () # like opts, but can be set or unset after instantiation
|
||||
_reset_ok = ('debug','wait','pids')
|
||||
_reset_ok = ('debug', 'wait', 'pids')
|
||||
version_info_arg = '--version'
|
||||
|
||||
def __init__(self,cfg,opts=None,flags=None):
|
||||
def __init__(self, cfg, opts=None, flags=None):
|
||||
|
||||
self.cfg = cfg
|
||||
self.platform = sys.platform
|
||||
|
|
@ -58,40 +58,40 @@ class Daemon(Lockable):
|
|||
self.use_pidfile = False
|
||||
self.use_threads = True
|
||||
|
||||
self.opt = ClassOpts(self,opts)
|
||||
self.flag = ClassFlags(self,flags)
|
||||
self.opt = ClassOpts(self, opts)
|
||||
self.flag = ClassFlags(self, flags)
|
||||
self.debug = self.debug or cfg.debug_daemon
|
||||
|
||||
def exec_cmd_thread(self,cmd):
|
||||
def exec_cmd_thread(self, cmd):
|
||||
import threading
|
||||
tname = ('exec_cmd','exec_cmd_win_console')[self.platform == 'win32' and self.new_console_mswin]
|
||||
t = threading.Thread(target=getattr(self,tname),args=(cmd,))
|
||||
tname = ('exec_cmd', 'exec_cmd_win_console')[self.platform == 'win32' and self.new_console_mswin]
|
||||
t = threading.Thread(target=getattr(self, tname), args=(cmd,))
|
||||
t.daemon = True
|
||||
t.start()
|
||||
if self.platform == 'win32':
|
||||
Msg_r(' \b') # blocks w/o this...crazy
|
||||
return True
|
||||
|
||||
def exec_cmd_win_console(self,cmd):
|
||||
from subprocess import Popen,CREATE_NEW_CONSOLE,STARTUPINFO,STARTF_USESHOWWINDOW,SW_HIDE
|
||||
si = STARTUPINFO(dwFlags=STARTF_USESHOWWINDOW,wShowWindow=SW_HIDE)
|
||||
p = Popen(cmd,creationflags=CREATE_NEW_CONSOLE,startupinfo=si)
|
||||
def exec_cmd_win_console(self, cmd):
|
||||
from subprocess import Popen, CREATE_NEW_CONSOLE, STARTUPINFO, STARTF_USESHOWWINDOW, SW_HIDE
|
||||
si = STARTUPINFO(dwFlags=STARTF_USESHOWWINDOW, wShowWindow=SW_HIDE)
|
||||
p = Popen(cmd, creationflags=CREATE_NEW_CONSOLE, startupinfo=si)
|
||||
p.wait()
|
||||
|
||||
def exec_cmd(self,cmd,is_daemon=False,check_retcode=False):
|
||||
out = (PIPE,None)[is_daemon and self.opt.no_daemonize]
|
||||
def exec_cmd(self, cmd, is_daemon=False, check_retcode=False):
|
||||
out = (PIPE, None)[is_daemon and self.opt.no_daemonize]
|
||||
try:
|
||||
cp = run(cmd,check=False,stdout=out,stderr=out)
|
||||
cp = run(cmd, check=False, stdout=out, stderr=out)
|
||||
except OSError as e:
|
||||
die( 'MMGenCalledProcessError', f'Error starting executable: {type(e).__name__} [Errno {e.errno}]' )
|
||||
die('MMGenCalledProcessError', f'Error starting executable: {type(e).__name__} [Errno {e.errno}]')
|
||||
set_vt100()
|
||||
if check_retcode and cp.returncode:
|
||||
die(1,str(cp))
|
||||
die(1, str(cp))
|
||||
if self.debug:
|
||||
print(cp)
|
||||
return cp
|
||||
|
||||
def run_cmd(self,cmd,silent=False,is_daemon=False,check_retcode=False):
|
||||
def run_cmd(self, cmd, silent=False, is_daemon=False, check_retcode=False):
|
||||
|
||||
if self.debug:
|
||||
msg('\n\n')
|
||||
|
|
@ -100,14 +100,14 @@ class Daemon(Lockable):
|
|||
msg(f'Starting {self.desc} on port {self.bind_port}')
|
||||
|
||||
if self.debug:
|
||||
msg(f'\nExecuting:\n{fmt_list(cmd,fmt="col",indent=" ")}\n')
|
||||
msg(f'\nExecuting:\n{fmt_list(cmd, fmt="col", indent=" ")}\n')
|
||||
|
||||
if self.use_threads and is_daemon and not self.opt.no_daemonize:
|
||||
ret = self.exec_cmd_thread(cmd)
|
||||
else:
|
||||
ret = self.exec_cmd(cmd,is_daemon,check_retcode)
|
||||
ret = self.exec_cmd(cmd, is_daemon, check_retcode)
|
||||
|
||||
if isinstance(ret,CompletedProcess):
|
||||
if isinstance(ret, CompletedProcess):
|
||||
if ret.stdout and (self.debug or not silent):
|
||||
msg(ret.stdout.decode().rstrip())
|
||||
if ret.stderr and (self.debug or (ret.returncode and not silent)):
|
||||
|
|
@ -124,7 +124,7 @@ class Daemon(Lockable):
|
|||
# Assumes only one running instance of given daemon. If multiple daemons are running,
|
||||
# the first PID in the list is returned and self.pids is set to the PID list.
|
||||
ss = f'{self.exec_fn}.exe'
|
||||
cp = self.run_cmd(['ps','-Wl'],silent=True)
|
||||
cp = self.run_cmd(['ps', '-Wl'], silent=True)
|
||||
self.pids = ()
|
||||
# use Windows, not Cygwin, PID
|
||||
pids = tuple(line.split()[3] for line in cp.stdout.decode().splitlines() if ss in line)
|
||||
|
|
@ -134,10 +134,10 @@ class Daemon(Lockable):
|
|||
return pids[0]
|
||||
elif self.platform in ('linux', 'darwin'):
|
||||
ss = ' '.join(self.start_cmd)
|
||||
cp = self.run_cmd(['pgrep','-f',ss],silent=True)
|
||||
cp = self.run_cmd(['pgrep', '-f', ss], silent=True)
|
||||
if cp.stdout:
|
||||
return cp.stdout.strip().decode()
|
||||
die(2,f'{ss!r} not found in process list, cannot determine PID')
|
||||
die(2, f'{ss!r} not found in process list, cannot determine PID')
|
||||
|
||||
@property
|
||||
def bind_port(self):
|
||||
|
|
@ -147,7 +147,7 @@ class Daemon(Lockable):
|
|||
def state(self):
|
||||
if self.debug:
|
||||
msg(f'Testing port {self.bind_port}')
|
||||
return 'ready' if self.test_socket('localhost',self.bind_port) else 'stopped'
|
||||
return 'ready' if self.test_socket('localhost', self.bind_port) else 'stopped'
|
||||
|
||||
@property
|
||||
def start_cmds(self):
|
||||
|
|
@ -156,17 +156,17 @@ class Daemon(Lockable):
|
|||
@property
|
||||
def stop_cmd(self):
|
||||
return (
|
||||
['kill','-Wf',self.pid] if self.platform == 'win32' else
|
||||
['kill','-9',self.pid] if self.force_kill else
|
||||
['kill',self.pid] )
|
||||
['kill', '-Wf', self.pid] if self.platform == 'win32' else
|
||||
['kill', '-9', self.pid] if self.force_kill else
|
||||
['kill', self.pid])
|
||||
|
||||
def cmd(self,action,*args,**kwargs):
|
||||
return getattr(self,action)(*args,**kwargs)
|
||||
def cmd(self, action, *args, **kwargs):
|
||||
return getattr(self, action)(*args, **kwargs)
|
||||
|
||||
def cli(self,*cmds,silent=False):
|
||||
return self.run_cmd(self.cli_cmd(*cmds),silent=silent)
|
||||
def cli(self, *cmds, silent=False):
|
||||
return self.run_cmd(self.cli_cmd(*cmds), silent=silent)
|
||||
|
||||
def state_msg(self,extra_text=None):
|
||||
def state_msg(self, extra_text=None):
|
||||
try:
|
||||
pid = self.pid
|
||||
except:
|
||||
|
|
@ -176,12 +176,12 @@ class Daemon(Lockable):
|
|||
f'{self.desc} {extra_text}running',
|
||||
'pid N/A' if pid is None or self.pids or self.state == 'stopped' else f'pid {pid}',
|
||||
f'port {self.bind_port}',
|
||||
w = 60 )
|
||||
w = 60)
|
||||
|
||||
def pre_start(self):
|
||||
pass
|
||||
|
||||
def start(self,quiet=False,silent=False):
|
||||
def start(self, quiet=False, silent=False):
|
||||
if self.state == 'ready':
|
||||
if not (quiet or silent):
|
||||
msg(self.state_msg(extra_text='already'))
|
||||
|
|
@ -193,24 +193,24 @@ class Daemon(Lockable):
|
|||
|
||||
if not silent:
|
||||
msg(f'Starting {self.desc} on port {self.bind_port}')
|
||||
ret = self.run_cmd(self.start_cmd,silent=True,is_daemon=True,check_retcode=True)
|
||||
ret = self.run_cmd(self.start_cmd, silent=True, is_daemon=True, check_retcode=True)
|
||||
|
||||
if self.wait:
|
||||
self.wait_for_state('ready')
|
||||
|
||||
return ret
|
||||
|
||||
def stop(self,quiet=False,silent=False):
|
||||
def stop(self, quiet=False, silent=False):
|
||||
if self.state == 'ready':
|
||||
if not silent:
|
||||
msg(f'Stopping {self.desc} on port {self.bind_port}')
|
||||
if self.force_kill:
|
||||
run(['sync'])
|
||||
ret = self.run_cmd(self.stop_cmd,silent=True)
|
||||
ret = self.run_cmd(self.stop_cmd, silent=True)
|
||||
|
||||
if self.pids:
|
||||
msg('Warning: multiple PIDs [{}] -- we may be stopping the wrong instance'.format(
|
||||
fmt_list(self.pids,fmt='bare')
|
||||
fmt_list(self.pids, fmt='bare')
|
||||
))
|
||||
if self.wait:
|
||||
self.wait_for_state('stopped')
|
||||
|
|
@ -221,49 +221,49 @@ class Daemon(Lockable):
|
|||
msg(f'{self.desc} on port {self.bind_port} not running')
|
||||
return True
|
||||
|
||||
def restart(self,silent=False):
|
||||
def restart(self, silent=False):
|
||||
self.stop(silent=silent)
|
||||
return self.start(silent=silent)
|
||||
|
||||
def test_socket(self,host,port,timeout=10):
|
||||
def test_socket(self, host, port, timeout=10):
|
||||
import socket
|
||||
try:
|
||||
socket.create_connection((host,port),timeout=timeout).close()
|
||||
socket.create_connection((host, port), timeout=timeout).close()
|
||||
except:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def wait_for_state(self,req_state):
|
||||
def wait_for_state(self, req_state):
|
||||
for _ in range(300):
|
||||
if self.state == req_state:
|
||||
return True
|
||||
time.sleep(0.2)
|
||||
die(2,f'Wait for state {req_state!r} timeout exceeded for {self.desc} (port {self.bind_port})')
|
||||
die(2, f'Wait for state {req_state!r} timeout exceeded for {self.desc} (port {self.bind_port})')
|
||||
|
||||
@classmethod
|
||||
def get_exec_version_str(cls):
|
||||
try:
|
||||
cp = run([cls.exec_fn,cls.version_info_arg],stdout=PIPE,stderr=PIPE,check=True)
|
||||
cp = run([cls.exec_fn, cls.version_info_arg], stdout=PIPE, stderr=PIPE, check=True)
|
||||
except Exception as e:
|
||||
die(2,f'{e}\nUnable to execute {cls.exec_fn}')
|
||||
die(2, f'{e}\nUnable to execute {cls.exec_fn}')
|
||||
|
||||
if cp.returncode:
|
||||
die(2,f'Unable to execute {cls.exec_fn}')
|
||||
die(2, f'Unable to execute {cls.exec_fn}')
|
||||
else:
|
||||
res = cp.stdout.decode().splitlines()
|
||||
return ( res[0] if len(res) == 1 else [s for s in res if 'ersion' in s][0] ).strip()
|
||||
return (res[0] if len(res) == 1 else [s for s in res if 'ersion' in s][0]).strip()
|
||||
|
||||
class RPCDaemon(Daemon):
|
||||
|
||||
avail_opts = ('no_daemonize',)
|
||||
|
||||
def __init__(self,cfg,opts=None,flags=None):
|
||||
super().__init__(cfg,opts=opts,flags=flags)
|
||||
def __init__(self, cfg, opts=None, flags=None):
|
||||
super().__init__(cfg, opts=opts, flags=flags)
|
||||
self.desc = '{} {} {}RPC daemon'.format(
|
||||
self.rpc_type,
|
||||
getattr(self.proto.network_names,self.proto.network),
|
||||
'test suite ' if self.test_suite else '' )
|
||||
getattr(self.proto.network_names, self.proto.network),
|
||||
'test suite ' if self.test_suite else '')
|
||||
self._set_ok += ('usr_daemon_args',)
|
||||
self.usr_daemon_args = []
|
||||
|
||||
|
|
@ -272,22 +272,22 @@ class RPCDaemon(Daemon):
|
|||
return [self.exec_fn] + self.daemon_args + self.usr_daemon_args
|
||||
|
||||
class CoinDaemon(Daemon):
|
||||
networks = ('mainnet','testnet','regtest')
|
||||
networks = ('mainnet', 'testnet', 'regtest')
|
||||
cfg_file_hdr = ''
|
||||
avail_flags = ('keep_cfg_file',)
|
||||
avail_opts = ('no_daemonize','online')
|
||||
avail_opts = ('no_daemonize', 'online')
|
||||
testnet_dir = None
|
||||
test_suite_port_shift = 1237
|
||||
rpc_user = None
|
||||
rpc_password = None
|
||||
|
||||
_cd = namedtuple('coins_data',['daemon_ids'])
|
||||
_cd = namedtuple('coins_data', ['daemon_ids'])
|
||||
coins = {
|
||||
'BTC': _cd(['bitcoin_core']),
|
||||
'BCH': _cd(['bitcoin_cash_node']),
|
||||
'LTC': _cd(['litecoin_core']),
|
||||
'XMR': _cd(['monero']),
|
||||
'ETH': _cd(['geth','erigon','openethereum']),
|
||||
'ETH': _cd(['geth', 'erigon', 'openethereum']),
|
||||
'ETC': _cd(['parity']),
|
||||
}
|
||||
|
||||
|
|
@ -300,7 +300,7 @@ class CoinDaemon(Daemon):
|
|||
message = 'blacklisted daemon: {!r}'
|
||||
|
||||
@classmethod
|
||||
def get_daemon_ids(cls,cfg,coin):
|
||||
def get_daemon_ids(cls, cfg, coin):
|
||||
|
||||
ret = cls.coins[coin].daemon_ids
|
||||
if 'erigon' in ret and not cfg.enable_erigon:
|
||||
|
|
@ -310,14 +310,14 @@ class CoinDaemon(Daemon):
|
|||
def gen():
|
||||
for daemon_id in ret:
|
||||
if daemon_id in blacklist:
|
||||
cls.warn_blacklisted(div=daemon_id,fmt_args=[daemon_id])
|
||||
cls.warn_blacklisted(div=daemon_id, fmt_args=[daemon_id])
|
||||
else:
|
||||
yield daemon_id
|
||||
ret = list(gen())
|
||||
return ret
|
||||
|
||||
@classmethod
|
||||
def get_daemon(cls,cfg,coin,daemon_id,proto=None):
|
||||
def get_daemon(cls, cfg, coin, daemon_id, proto=None):
|
||||
if proto:
|
||||
proto_cls = type(proto)
|
||||
else:
|
||||
|
|
@ -325,17 +325,17 @@ class CoinDaemon(Daemon):
|
|||
proto_cls = init_proto(cfg, coin, return_cls=True)
|
||||
return getattr(
|
||||
importlib.import_module(f'mmgen.proto.{proto_cls.base_proto_coin.lower()}.daemon'),
|
||||
daemon_id+'_daemon' )
|
||||
daemon_id+'_daemon')
|
||||
|
||||
@classmethod
|
||||
def get_network_ids(cls,cfg):
|
||||
def get_network_ids(cls, cfg):
|
||||
from .protocol import CoinProtocol
|
||||
def gen():
|
||||
for coin in cls.coins:
|
||||
for daemon_id in cls.get_daemon_ids(cfg,coin):
|
||||
for network in cls.get_daemon( cfg, coin, daemon_id ).networks:
|
||||
yield CoinProtocol.Base.create_network_id(coin,network)
|
||||
return remove_dups(list(gen()),quiet=True)
|
||||
for daemon_id in cls.get_daemon_ids(cfg, coin):
|
||||
for network in cls.get_daemon(cfg, coin, daemon_id).networks:
|
||||
yield CoinProtocol.Base.create_network_id(coin, network)
|
||||
return remove_dups(list(gen()), quiet=True)
|
||||
|
||||
def __new__(cls,
|
||||
cfg,
|
||||
|
|
@ -347,7 +347,7 @@ class CoinDaemon(Daemon):
|
|||
port_shift = None,
|
||||
p2p_port = None,
|
||||
datadir = None,
|
||||
daemon_id = None ):
|
||||
daemon_id = None):
|
||||
|
||||
assert network_id or proto, 'CoinDaemon_chk1'
|
||||
assert not (network_id and proto), 'CoinDaemon_chk2'
|
||||
|
|
@ -358,20 +358,20 @@ class CoinDaemon(Daemon):
|
|||
coin = proto.coin
|
||||
else:
|
||||
network_id = network_id.lower()
|
||||
from .protocol import CoinProtocol,init_proto
|
||||
proto = init_proto( cfg, network_id=network_id )
|
||||
coin,network = CoinProtocol.Base.parse_network_id(network_id)
|
||||
from .protocol import CoinProtocol, init_proto
|
||||
proto = init_proto(cfg, network_id=network_id)
|
||||
coin, network = CoinProtocol.Base.parse_network_id(network_id)
|
||||
coin = coin.upper()
|
||||
|
||||
daemon_ids = cls.get_daemon_ids(cfg,coin)
|
||||
daemon_ids = cls.get_daemon_ids(cfg, coin)
|
||||
if not daemon_ids:
|
||||
die(1,f'No configured daemons for coin {coin}!')
|
||||
die(1, f'No configured daemons for coin {coin}!')
|
||||
daemon_id = daemon_id or cfg.daemon_id or daemon_ids[0]
|
||||
|
||||
if daemon_id not in daemon_ids:
|
||||
die(1,f'{daemon_id!r}: invalid daemon_id - valid choices: {fmt_list(daemon_ids)}')
|
||||
die(1, f'{daemon_id!r}: invalid daemon_id - valid choices: {fmt_list(daemon_ids)}')
|
||||
|
||||
me = Daemon.__new__(cls.get_daemon( cfg, None, daemon_id, proto=proto ))
|
||||
me = Daemon.__new__(cls.get_daemon(cfg, None, daemon_id, proto=proto))
|
||||
|
||||
assert network in me.networks, f'{network!r}: unsupported network for daemon {daemon_id}'
|
||||
me.network_id = network_id
|
||||
|
|
@ -392,30 +392,30 @@ class CoinDaemon(Daemon):
|
|||
port_shift = None,
|
||||
p2p_port = None,
|
||||
datadir = None,
|
||||
daemon_id = None ):
|
||||
daemon_id = None):
|
||||
|
||||
self.test_suite = test_suite
|
||||
|
||||
super().__init__(cfg=cfg,opts=opts,flags=flags)
|
||||
super().__init__(cfg=cfg, opts=opts, flags=flags)
|
||||
|
||||
self._set_ok += ('shared_args','usr_coind_args')
|
||||
self._set_ok += ('shared_args', 'usr_coind_args')
|
||||
self.shared_args = []
|
||||
self.usr_coind_args = []
|
||||
|
||||
for k,v in self.daemon_data._asdict().items():
|
||||
setattr(self,k,v)
|
||||
for k, v in self.daemon_data._asdict().items():
|
||||
setattr(self, k, v)
|
||||
|
||||
self.desc = '{} {} {}daemon'.format(
|
||||
self.coind_name,
|
||||
getattr(self.proto.network_names,self.network),
|
||||
'test suite ' if test_suite else '' )
|
||||
getattr(self.proto.network_names, self.network),
|
||||
'test suite ' if test_suite else '')
|
||||
|
||||
# user-set values take precedence
|
||||
self.datadir = os.path.abspath(datadir or cfg.daemon_data_dir or self.init_datadir())
|
||||
self.non_dfl_datadir = bool(datadir or cfg.daemon_data_dir or test_suite or self.network == 'regtest')
|
||||
|
||||
# init_datadir() may have already initialized logdir
|
||||
self.logdir = os.path.abspath(getattr(self,'logdir',self.datadir))
|
||||
self.logdir = os.path.abspath(getattr(self, 'logdir', self.datadir))
|
||||
|
||||
ps_adj = (port_shift or 0) + (self.test_suite_port_shift if test_suite else 0)
|
||||
|
||||
|
|
@ -424,10 +424,10 @@ class CoinDaemon(Daemon):
|
|||
self.p2p_port = (
|
||||
p2p_port or (
|
||||
self.get_p2p_port() + ps_adj if self.get_p2p_port() and (test_suite or ps_adj) else None
|
||||
) if self.network != 'regtest' else None )
|
||||
) if self.network != 'regtest' else None)
|
||||
|
||||
if hasattr(self,'private_ports'):
|
||||
self.private_port = getattr(self.private_ports,self.network)
|
||||
if hasattr(self, 'private_ports'):
|
||||
self.private_port = getattr(self.private_ports, self.network)
|
||||
|
||||
# bind_port == self.private_port or self.rpc_port
|
||||
self.pidfile = f'{self.logdir}/{self.id}-{self.network}-daemon-{self.bind_port}.pid'
|
||||
|
|
@ -437,7 +437,7 @@ class CoinDaemon(Daemon):
|
|||
|
||||
def init_datadir(self):
|
||||
if self.test_suite:
|
||||
return os.path.join('test','daemons',self.network_id)
|
||||
return os.path.join('test', 'daemons', self.network_id)
|
||||
else:
|
||||
return os.path.join(*self.datadirs[self.platform])
|
||||
|
||||
|
|
@ -446,7 +446,7 @@ class CoinDaemon(Daemon):
|
|||
return self.datadir
|
||||
|
||||
def get_rpc_port(self):
|
||||
return getattr(self.rpc_ports,self.network)
|
||||
return getattr(self.rpc_ports, self.network)
|
||||
|
||||
def get_p2p_port(self):
|
||||
return None
|
||||
|
|
@ -456,27 +456,27 @@ class CoinDaemon(Daemon):
|
|||
return ([self.exec_fn]
|
||||
+ self.coind_args
|
||||
+ self.shared_args
|
||||
+ self.usr_coind_args )
|
||||
+ self.usr_coind_args)
|
||||
|
||||
def cli_cmd(self,*cmds):
|
||||
def cli_cmd(self, *cmds):
|
||||
return ([self.cli_fn]
|
||||
+ self.shared_args
|
||||
+ list(cmds) )
|
||||
+ list(cmds))
|
||||
|
||||
def start(self,*args,**kwargs):
|
||||
def start(self, *args, **kwargs):
|
||||
assert self.test_suite or self.network == 'regtest', 'start() restricted to test suite and regtest'
|
||||
return super().start(*args,**kwargs)
|
||||
return super().start(*args, **kwargs)
|
||||
|
||||
def stop(self,*args,**kwargs):
|
||||
def stop(self, *args, **kwargs):
|
||||
assert self.test_suite or self.network == 'regtest', 'stop() restricted to test suite and regtest'
|
||||
return super().stop(*args,**kwargs)
|
||||
return super().stop(*args, **kwargs)
|
||||
|
||||
def pre_start(self):
|
||||
os.makedirs(self.datadir,exist_ok=True)
|
||||
os.makedirs(self.datadir, exist_ok=True)
|
||||
|
||||
if self.test_suite or self.network == 'regtest':
|
||||
if self.cfg_file and not self.flag.keep_cfg_file:
|
||||
with open(f'{self.datadir}/{self.cfg_file}','w') as fp:
|
||||
with open(f'{self.datadir}/{self.cfg_file}', 'w') as fp:
|
||||
fp.write(self.cfg_file_hdr)
|
||||
|
||||
if self.use_pidfile and os.path.exists(self.pidfile):
|
||||
|
|
@ -491,7 +491,7 @@ class CoinDaemon(Daemon):
|
|||
run([
|
||||
('rm' if self.platform == 'win32' else '/bin/rm'),
|
||||
'-rf',
|
||||
self.datadir ])
|
||||
self.datadir])
|
||||
set_vt100()
|
||||
else:
|
||||
msg(f'Cannot remove {self.network_datadir!r} - daemon is not stopped')
|
||||
|
|
|
|||
|
|
@ -21,19 +21,19 @@ derive: coin private key secret derivation for the MMGen suite
|
|||
"""
|
||||
|
||||
from collections import namedtuple
|
||||
from hashlib import sha512,sha256
|
||||
from hashlib import sha512, sha256
|
||||
from .addrlist import AddrIdxList
|
||||
|
||||
pk_bytes = namedtuple('coin_privkey_bytes',['idx','pos','data'])
|
||||
pk_bytes = namedtuple('coin_privkey_bytes', ['idx', 'pos', 'data'])
|
||||
|
||||
def derive_coin_privkey_bytes(seed,idxs):
|
||||
def derive_coin_privkey_bytes(seed, idxs):
|
||||
|
||||
assert isinstance(idxs,AddrIdxList), f'{type(idxs)}: idx list not of type AddrIdxList'
|
||||
assert isinstance(idxs, AddrIdxList), f'{type(idxs)}: idx list not of type AddrIdxList'
|
||||
|
||||
t_keys = len(idxs)
|
||||
pos = 0
|
||||
|
||||
for idx in range( 1, AddrIdxList.max_len+1 ): # key/addr indexes begin from one
|
||||
for idx in range(1, AddrIdxList.max_len + 1): # key/addr indexes begin from one
|
||||
|
||||
seed = sha512(seed).digest()
|
||||
|
||||
|
|
@ -42,7 +42,7 @@ def derive_coin_privkey_bytes(seed,idxs):
|
|||
pos += 1
|
||||
|
||||
# secret is double sha256 of seed hash round /idx/
|
||||
yield pk_bytes( idx, pos, sha256(sha256(seed).digest()).digest() )
|
||||
yield pk_bytes(idx, pos, sha256(sha256(seed).digest()).digest())
|
||||
|
||||
if pos == t_keys:
|
||||
break
|
||||
|
|
|
|||
|
|
@ -13,35 +13,35 @@ devinit: Developer tools initialization for the MMGen suite
|
|||
"""
|
||||
|
||||
devtools_funcs = {
|
||||
'pfmt': lambda *args,**kwargs: devtools_call('pfmt',*args,**kwargs),
|
||||
'pmsg': lambda *args,**kwargs: devtools_call('pmsg',*args,**kwargs),
|
||||
'pmsg_r': lambda *args,**kwargs: devtools_call('pmsg_r',*args,**kwargs),
|
||||
'pdie': lambda *args,**kwargs: devtools_call('pdie',*args,**kwargs),
|
||||
'pexit': lambda *args,**kwargs: devtools_call('pexit',*args,**kwargs),
|
||||
'Pmsg': lambda *args,**kwargs: devtools_call('Pmsg',*args,**kwargs),
|
||||
'Pdie': lambda *args,**kwargs: devtools_call('Pdie',*args,**kwargs),
|
||||
'Pexit': lambda *args,**kwargs: devtools_call('Pexit',*args,**kwargs),
|
||||
'print_stack_trace': lambda *args,**kwargs: devtools_call('print_stack_trace',*args,**kwargs),
|
||||
'get_diff': lambda *args,**kwargs: devtools_call('get_diff',*args,**kwargs),
|
||||
'print_diff': lambda *args,**kwargs: devtools_call('print_diff',*args,**kwargs),
|
||||
'get_ndiff': lambda *args,**kwargs: devtools_call('get_ndiff',*args,**kwargs),
|
||||
'print_ndiff': lambda *args,**kwargs: devtools_call('print_ndiff',*args,**kwargs),
|
||||
'pfmt': lambda *args, **kwargs: devtools_call('pfmt', *args, **kwargs),
|
||||
'pmsg': lambda *args, **kwargs: devtools_call('pmsg', *args, **kwargs),
|
||||
'pmsg_r': lambda *args, **kwargs: devtools_call('pmsg_r', *args, **kwargs),
|
||||
'pdie': lambda *args, **kwargs: devtools_call('pdie', *args, **kwargs),
|
||||
'pexit': lambda *args, **kwargs: devtools_call('pexit', *args, **kwargs),
|
||||
'Pmsg': lambda *args, **kwargs: devtools_call('Pmsg', *args, **kwargs),
|
||||
'Pdie': lambda *args, **kwargs: devtools_call('Pdie', *args, **kwargs),
|
||||
'Pexit': lambda *args, **kwargs: devtools_call('Pexit', *args, **kwargs),
|
||||
'print_stack_trace': lambda *args, **kwargs: devtools_call('print_stack_trace', *args, **kwargs),
|
||||
'get_diff': lambda *args, **kwargs: devtools_call('get_diff', *args, **kwargs),
|
||||
'print_diff': lambda *args, **kwargs: devtools_call('print_diff', *args, **kwargs),
|
||||
'get_ndiff': lambda *args, **kwargs: devtools_call('get_ndiff', *args, **kwargs),
|
||||
'print_ndiff': lambda *args, **kwargs: devtools_call('print_ndiff', *args, **kwargs),
|
||||
}
|
||||
|
||||
def devtools_call(funcname,*args,**kwargs):
|
||||
def devtools_call(funcname, *args, **kwargs):
|
||||
from . import devtools
|
||||
return getattr(devtools,funcname)(*args,**kwargs)
|
||||
return getattr(devtools, funcname)(*args, **kwargs)
|
||||
|
||||
def MMGenObject_call(methodname,*args,**kwargs):
|
||||
def MMGenObject_call(methodname, *args, **kwargs):
|
||||
from .devtools import MMGenObjectMethods
|
||||
return getattr(MMGenObjectMethods,methodname)(*args,**kwargs)
|
||||
return getattr(MMGenObjectMethods, methodname)(*args, **kwargs)
|
||||
|
||||
class MMGenObjectDevTools:
|
||||
|
||||
pmsg = lambda *args,**kwargs: MMGenObject_call('pmsg',*args,**kwargs)
|
||||
pdie = lambda *args,**kwargs: MMGenObject_call('pdie',*args,**kwargs)
|
||||
pexit = lambda *args,**kwargs: MMGenObject_call('pexit',*args,**kwargs)
|
||||
pfmt = lambda *args,**kwargs: MMGenObject_call('pfmt',*args,**kwargs)
|
||||
pmsg = lambda *args, **kwargs: MMGenObject_call('pmsg', *args, **kwargs)
|
||||
pdie = lambda *args, **kwargs: MMGenObject_call('pdie', *args, **kwargs)
|
||||
pexit = lambda *args, **kwargs: MMGenObject_call('pexit', *args, **kwargs)
|
||||
pfmt = lambda *args, **kwargs: MMGenObject_call('pfmt', *args, **kwargs)
|
||||
|
||||
# Check that all immutables have been initialized. Expensive, so do only when testing.
|
||||
def immutable_attr_init_check(self):
|
||||
|
|
@ -50,22 +50,22 @@ class MMGenObjectDevTools:
|
|||
|
||||
for attrname in self.valid_attrs:
|
||||
|
||||
for o in (cls,cls.__bases__[0]): # assume there's only one base class
|
||||
for o in (cls, cls.__bases__[0]): # assume there's only one base class
|
||||
if attrname in o.__dict__:
|
||||
attr = o.__dict__[attrname]
|
||||
break
|
||||
else:
|
||||
from .util import die
|
||||
die(4,f'unable to find descriptor {cls.__name__}.{attrname}')
|
||||
die(4, f'unable to find descriptor {cls.__name__}.{attrname}')
|
||||
|
||||
if type(attr).__name__ == 'ImmutableAttr' and attrname not in self.__dict__:
|
||||
from .util import die
|
||||
die(4,f'attribute {attrname!r} of {cls.__name__} has not been initialized in constructor!')
|
||||
die(4, f'attribute {attrname!r} of {cls.__name__} has not been initialized in constructor!')
|
||||
|
||||
def init_dev():
|
||||
import builtins
|
||||
# MMGenObject is added to the namespace by objmethods.py, so we must name the builtin differently
|
||||
# to avoid inadvertently adding MMGenObject to the global namespace here:
|
||||
setattr(builtins,'MMGenObjectDevTools',MMGenObjectDevTools)
|
||||
for funcname,func in devtools_funcs.items():
|
||||
setattr(builtins,funcname,func)
|
||||
setattr(builtins, 'MMGenObjectDevTools', MMGenObjectDevTools)
|
||||
for funcname, func in devtools_funcs.items():
|
||||
setattr(builtins, funcname, func)
|
||||
|
|
|
|||
|
|
@ -16,77 +16,77 @@ import sys
|
|||
|
||||
color_funcs = {
|
||||
name: lambda s, n=n: f'\033[{n};1m{s}\033[0m'
|
||||
for name,n in (
|
||||
('red', 31),
|
||||
('green', 32),
|
||||
('yellow',33),
|
||||
('blue', 34),
|
||||
('purple',35))
|
||||
for name, n in (
|
||||
('red', 31),
|
||||
('green', 32),
|
||||
('yellow', 33),
|
||||
('blue', 34),
|
||||
('purple', 35))
|
||||
}
|
||||
|
||||
def pfmt(*args,color=None):
|
||||
def pfmt(*args, color=None):
|
||||
import pprint
|
||||
ret = pprint.PrettyPrinter(indent=4,width=116).pformat(
|
||||
args if len(args) > 1 else '' if not args else args[0] )
|
||||
ret = pprint.PrettyPrinter(indent=4, width=116).pformat(
|
||||
args if len(args) > 1 else '' if not args else args[0])
|
||||
return color_funcs[color](ret) if color else ret
|
||||
|
||||
def pmsg(*args,color=None):
|
||||
sys.stderr.write(pfmt(*args,color=color) + '\n')
|
||||
def pmsg(*args, color=None):
|
||||
sys.stderr.write(pfmt(*args, color=color) + '\n')
|
||||
|
||||
def pmsg_r(*args,color=None):
|
||||
sys.stderr.write(pfmt(*args,color=color))
|
||||
def pmsg_r(*args, color=None):
|
||||
sys.stderr.write(pfmt(*args, color=color))
|
||||
|
||||
def pdie(*args,exit_val=1):
|
||||
pmsg(*args,color='red' if exit_val else None)
|
||||
def pdie(*args, exit_val=1):
|
||||
pmsg(*args, color='red' if exit_val else None)
|
||||
sys.exit(exit_val)
|
||||
|
||||
def pexit(*args):
|
||||
pdie(*args,exit_val=0)
|
||||
pdie(*args, exit_val=0)
|
||||
|
||||
def Pmsg(*args,color=None):
|
||||
sys.stdout.write(pfmt(*args,color=color) + '\n')
|
||||
def Pmsg(*args, color=None):
|
||||
sys.stdout.write(pfmt(*args, color=color) + '\n')
|
||||
|
||||
def Pdie(*args,exit_val=1):
|
||||
Pmsg(*args,color=('yellow' if exit_val == 1 else 'red' if exit_val else None))
|
||||
def Pdie(*args, exit_val=1):
|
||||
Pmsg(*args, color=('yellow' if exit_val == 1 else 'red' if exit_val else None))
|
||||
sys.exit(exit_val)
|
||||
|
||||
def Pexit(*args):
|
||||
Pdie(*args,exit_val=0)
|
||||
Pdie(*args, exit_val=0)
|
||||
|
||||
def print_stack_trace(message=None,fh_list=[],nl='\n',sep='\n ',trim=4):
|
||||
def print_stack_trace(message=None, fh_list=[], nl='\n', sep='\n ', trim=4):
|
||||
if not fh_list:
|
||||
import os
|
||||
fh_list.append(open(f'devtools.trace.{os.getpid()}','w'))
|
||||
fh_list.append(open(f'devtools.trace.{os.getpid()}', 'w'))
|
||||
nl = ''
|
||||
res = get_stack_trace(message,nl,sep,trim)
|
||||
res = get_stack_trace(message, nl, sep, trim)
|
||||
sys.stderr.write(res)
|
||||
fh_list[0].write(res)
|
||||
|
||||
def get_stack_trace(message=None,nl='\n',sep='\n ',trim=3):
|
||||
def get_stack_trace(message=None, nl='\n', sep='\n ', trim=3):
|
||||
|
||||
import os,re,traceback
|
||||
import os, re, traceback
|
||||
|
||||
tb = [t for t in traceback.extract_stack() if t.filename[:1] != '<']
|
||||
fs = '{}:{}: in {}:\n {}'
|
||||
out = [
|
||||
fs.format(
|
||||
re.sub(r'^\./','',os.path.relpath(t.filename)),
|
||||
re.sub(r'^\./', '', os.path.relpath(t.filename)),
|
||||
t.lineno,
|
||||
(t.name+'()' if t.name[-1] != '>' else t.name),
|
||||
t.line or '(none)')
|
||||
for t in (tb[:-trim] if trim else tb) ]
|
||||
for t in (tb[:-trim] if trim else tb)]
|
||||
|
||||
return f'{nl}STACK TRACE {message or "[unnamed]"}:{sep}{sep.join(out)}\n'
|
||||
|
||||
def print_diff(*args,**kwargs):
|
||||
sys.stderr.write(get_diff(*args,**kwargs))
|
||||
def print_diff(*args, **kwargs):
|
||||
sys.stderr.write(get_diff(*args, **kwargs))
|
||||
|
||||
def get_diff(a,b,a_fn='',b_fn='',from_json=True):
|
||||
def get_diff(a, b, a_fn='', b_fn='', from_json=True):
|
||||
|
||||
if from_json:
|
||||
import json
|
||||
a = json.dumps(json.loads(a),indent=4)
|
||||
b = json.dumps(json.loads(b),indent=4)
|
||||
a = json.dumps(json.loads(a), indent=4)
|
||||
b = json.dumps(json.loads(b), indent=4)
|
||||
|
||||
from difflib import unified_diff
|
||||
# chunk headers have trailing newlines, hence the rstrip()
|
||||
|
|
@ -95,70 +95,70 @@ def get_diff(a,b,a_fn='',b_fn='',from_json=True):
|
|||
a.split('\n'),
|
||||
b.split('\n'),
|
||||
a_fn,
|
||||
b_fn )))
|
||||
b_fn)))
|
||||
|
||||
def print_ndiff(*args,**kwargs):
|
||||
sys.stderr.write(get_ndiff(*args,**kwargs))
|
||||
def print_ndiff(*args, **kwargs):
|
||||
sys.stderr.write(get_ndiff(*args, **kwargs))
|
||||
|
||||
def get_ndiff(a,b):
|
||||
def get_ndiff(a, b):
|
||||
from difflib import ndiff
|
||||
return list(ndiff(
|
||||
a.split('\n'),
|
||||
b.split('\n') ))
|
||||
b.split('\n')))
|
||||
|
||||
class MMGenObjectMethods: # mixin class for MMGenObject
|
||||
|
||||
# Pretty-print an MMGenObject instance, recursing into sub-objects - WIP
|
||||
def pmsg(self,color=None):
|
||||
def pmsg(self, color=None):
|
||||
sys.stdout.write('\n'+self.pfmt(color=color))
|
||||
|
||||
def pdie(self,exit_val=1):
|
||||
def pdie(self, exit_val=1):
|
||||
self.pmsg(color='red' if exit_val else None)
|
||||
sys.exit(exit_val)
|
||||
|
||||
def pexit(self):
|
||||
self.pdie(exit_val=0)
|
||||
|
||||
def pfmt(self,lvl=0,id_list=[],color=None):
|
||||
def pfmt(self, lvl=0, id_list=[], color=None):
|
||||
|
||||
from decimal import Decimal
|
||||
scalars = (str,int,float,Decimal)
|
||||
scalars = (str, int, float, Decimal)
|
||||
|
||||
def isDict(obj):
|
||||
return isinstance(obj,dict)
|
||||
return isinstance(obj, dict)
|
||||
def isList(obj):
|
||||
return isinstance(obj,list)
|
||||
return isinstance(obj, list)
|
||||
def isScalar(obj):
|
||||
return isinstance(obj,scalars)
|
||||
return isinstance(obj, scalars)
|
||||
|
||||
def do_list(out,e,lvl=0,is_dict=False):
|
||||
def do_list(out, e, lvl=0, is_dict=False):
|
||||
out.append('\n')
|
||||
for i in e:
|
||||
el = i if not is_dict else e[i]
|
||||
if is_dict:
|
||||
# out.append('{s}{:<{l}}'.format(i,s=' '*(4*lvl+8),l=10,l2=8*(lvl+1)+8))
|
||||
# out.append('{s}{:<{l}}'.format(i, s=' '*(4*lvl+8), l=10, l2=8*(lvl+1)+8))
|
||||
out.append('{s1}{i}{s2}'.format(
|
||||
i = i,
|
||||
s1 = ' ' * (4*lvl+8),
|
||||
s2 = ' ' * 10 ))
|
||||
if hasattr(el,'pfmt'):
|
||||
s2 = ' ' * 10))
|
||||
if hasattr(el, 'pfmt'):
|
||||
out.append('{:>{l}}{}'.format(
|
||||
'',
|
||||
el.pfmt( lvl=lvl+1, id_list=id_list+[id(self)] ),
|
||||
l = (lvl+1)*8 ))
|
||||
elif isinstance(el,scalars):
|
||||
el.pfmt(lvl=lvl+1, id_list=id_list+[id(self)]),
|
||||
l = (lvl+1)*8))
|
||||
elif isinstance(el, scalars):
|
||||
if isList(e):
|
||||
out.append( '{:>{l}}{!r:16}\n'.format( '', el, l=lvl*8 ))
|
||||
out.append('{:>{l}}{!r:16}\n'.format('', el, l=lvl*8))
|
||||
else:
|
||||
out.append(f' {el!r}')
|
||||
elif isList(el) or isDict(el):
|
||||
indent = 1 if is_dict else lvl*8+4
|
||||
out.append('{:>{l}}{:16}'.format( '', f'<{type(el).__name__}>', l=indent ))
|
||||
if isList(el) and isinstance(el[0],scalars):
|
||||
out.append('{:>{l}}{:16}'.format('', f'<{type(el).__name__}>', l=indent))
|
||||
if isList(el) and isinstance(el[0], scalars):
|
||||
out.append('\n')
|
||||
do_list(out,el,lvl=lvl+1,is_dict=isDict(el))
|
||||
do_list(out, el, lvl=lvl+1, is_dict=isDict(el))
|
||||
else:
|
||||
out.append('{:>{l}}{:16} {!r}\n'.format( '', f'<{type(el).__name__}>', el, l=(lvl*8)+8 ))
|
||||
out.append('{:>{l}}{:16} {!r}\n'.format('', f'<{type(el).__name__}>', el, l=(lvl*8)+8))
|
||||
out.append('\n')
|
||||
|
||||
if not e:
|
||||
|
|
@ -169,27 +169,27 @@ class MMGenObjectMethods: # mixin class for MMGenObject
|
|||
if id(self) in id_list:
|
||||
return out[-1].rstrip() + ' [RECURSION]\n'
|
||||
if isList(self) or isDict(self):
|
||||
do_list(out,self,lvl=lvl,is_dict=isDict(self))
|
||||
do_list(out, self, lvl=lvl, is_dict=isDict(self))
|
||||
|
||||
for k in self.__dict__:
|
||||
e = getattr(self,k)
|
||||
e = getattr(self, k)
|
||||
if isList(e) or isDict(e):
|
||||
out.append('{:>{l}}{:<10} {:16}'.format( '', k, f'<{type(e).__name__}>', l=(lvl*8)+4 ))
|
||||
do_list(out,e,lvl=lvl,is_dict=isDict(e))
|
||||
elif hasattr(e,'pfmt') and callable(e.pfmt) and not isinstance(e,type):
|
||||
out.append('{:>{l}}{:<10} {:16}'.format('', k, f'<{type(e).__name__}>', l=(lvl*8)+4))
|
||||
do_list(out, e, lvl=lvl, is_dict=isDict(e))
|
||||
elif hasattr(e, 'pfmt') and callable(e.pfmt) and not isinstance(e, type):
|
||||
out.append('{:>{l}}{:10} {}'.format(
|
||||
'',
|
||||
k,
|
||||
e.pfmt( lvl=lvl+1, id_list=id_list+[id(self)] ),
|
||||
l = (lvl*8)+4 ))
|
||||
e.pfmt(lvl=lvl+1, id_list=id_list+[id(self)]),
|
||||
l = (lvl*8)+4))
|
||||
else:
|
||||
out.append('{:>{l}}{:<10} {:16} {}\n'.format(
|
||||
'',
|
||||
k,
|
||||
f'<{type(e).__name__}>',
|
||||
repr(e),
|
||||
l=(lvl*8)+4 ))
|
||||
l=(lvl*8)+4))
|
||||
|
||||
import re
|
||||
ret = re.sub('\n+','\n',''.join(out))
|
||||
ret = re.sub('\n+', '\n', ''.join(out))
|
||||
return color_funcs[color](ret) if color else ret
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ exception: Exception classes for the MMGen suite
|
|||
|
||||
class MMGenError(Exception):
|
||||
|
||||
def __init__(self,errno,strerror,stdout):
|
||||
def __init__(self, errno, strerror, stdout):
|
||||
self.mmcode = errno
|
||||
self.stdout = stdout
|
||||
super().__init__(strerror)
|
||||
|
|
|
|||
|
|
@ -20,12 +20,12 @@
|
|||
filename: File and MMGenFile classes and methods for the MMGen suite
|
||||
"""
|
||||
|
||||
import sys,os
|
||||
from .util import die,get_extension
|
||||
import sys, os
|
||||
from .util import die, get_extension
|
||||
|
||||
class File:
|
||||
|
||||
def __init__(self,fn,write=False):
|
||||
def __init__(self, fn, write=False):
|
||||
|
||||
self.name = fn
|
||||
self.dirname = os.path.dirname(fn)
|
||||
|
|
@ -38,18 +38,18 @@ class File:
|
|||
try:
|
||||
st = os.stat(fn)
|
||||
except:
|
||||
die( 'FileNotFound', f'{fn!r}: file not found' )
|
||||
die('FileNotFound', f'{fn!r}: file not found')
|
||||
|
||||
import stat
|
||||
if stat.S_ISBLK(st.st_mode):
|
||||
if sys.platform in ('win32',):
|
||||
die(2, 'Access to raw block devices not supported on platform {sys.platform!r}')
|
||||
mode = (os.O_RDONLY,os.O_RDWR)[bool(write)]
|
||||
mode = (os.O_RDONLY, os.O_RDWR)[bool(write)]
|
||||
try:
|
||||
fd = os.open(fn, mode)
|
||||
except OSError as e:
|
||||
if e.errno == 13:
|
||||
die(2,f'{fn!r}: permission denied')
|
||||
die(2, f'{fn!r}: permission denied')
|
||||
# if e.errno != 17: raise
|
||||
else:
|
||||
if sys.platform == 'linux':
|
||||
|
|
@ -66,21 +66,21 @@ class File:
|
|||
|
||||
class FileList(list):
|
||||
|
||||
def __init__(self,fns,write=False):
|
||||
def __init__(self, fns, write=False):
|
||||
list.__init__(
|
||||
self,
|
||||
[File(fn,write) for fn in fns] )
|
||||
[File(fn, write) for fn in fns])
|
||||
|
||||
def names(self):
|
||||
return [f.name for f in self]
|
||||
|
||||
def sort_by_age(self,key='mtime',reverse=False):
|
||||
assert key in ('atime','ctime','mtime'), f'{key!r}: invalid sort key'
|
||||
self.sort( key=lambda a: getattr(a,key), reverse=reverse )
|
||||
def sort_by_age(self, key='mtime', reverse=False):
|
||||
assert key in ('atime', 'ctime', 'mtime'), f'{key!r}: invalid sort key'
|
||||
self.sort(key=lambda a: getattr(a, key), reverse=reverse)
|
||||
|
||||
class MMGenFile(File):
|
||||
|
||||
def __init__(self,fn,base_class=None,subclass=None,proto=None,write=False):
|
||||
def __init__(self, fn, base_class=None, subclass=None, proto=None, write=False):
|
||||
"""
|
||||
'base_class' - a base class with an 'ext_to_cls' method
|
||||
'subclass' - a subclass with an 'ext' attribute
|
||||
|
|
@ -91,45 +91,45 @@ class MMGenFile(File):
|
|||
attribute to True.
|
||||
"""
|
||||
|
||||
super().__init__(fn,write)
|
||||
super().__init__(fn, write)
|
||||
|
||||
assert (subclass or base_class) and not (subclass and base_class), 'MMGenFile chk1'
|
||||
|
||||
if not getattr(subclass or base_class,'filename_api',False):
|
||||
die(3,f'Class {(subclass or base_class).__name__!r} does not support the MMGenFile API')
|
||||
if not getattr(subclass or base_class, 'filename_api', False):
|
||||
die(3, f'Class {(subclass or base_class).__name__!r} does not support the MMGenFile API')
|
||||
|
||||
if base_class:
|
||||
subclass = base_class.ext_to_cls( self.ext, proto )
|
||||
subclass = base_class.ext_to_cls(self.ext, proto)
|
||||
if not subclass:
|
||||
die( 'BadFileExtension', f'{self.ext!r}: not a recognized file extension for {base_class}' )
|
||||
die('BadFileExtension', f'{self.ext!r}: not a recognized file extension for {base_class}')
|
||||
|
||||
self.subclass = subclass
|
||||
|
||||
class MMGenFileList(FileList):
|
||||
|
||||
def __init__(self,fns,base_class,proto=None,write=False):
|
||||
def __init__(self, fns, base_class, proto=None, write=False):
|
||||
list.__init__(
|
||||
self,
|
||||
[MMGenFile( fn, base_class=base_class, proto=proto, write=write ) for fn in fns] )
|
||||
[MMGenFile(fn, base_class=base_class, proto=proto, write=write) for fn in fns])
|
||||
|
||||
def find_files_in_dir(subclass,fdir,no_dups=False):
|
||||
def find_files_in_dir(subclass, fdir, no_dups=False):
|
||||
|
||||
assert isinstance(subclass,type), f'{subclass}: not a class'
|
||||
assert isinstance(subclass, type), f'{subclass}: not a class'
|
||||
|
||||
if not getattr(subclass,'filename_api',False):
|
||||
die(3,f'Class {subclass.__name__!r} does not support the MMGenFile API')
|
||||
if not getattr(subclass, 'filename_api', False):
|
||||
die(3, f'Class {subclass.__name__!r} does not support the MMGenFile API')
|
||||
|
||||
matches = [l for l in os.listdir(fdir) if l.endswith('.'+subclass.ext)]
|
||||
|
||||
if no_dups:
|
||||
if len(matches) == 1:
|
||||
return os.path.join(fdir,matches[0])
|
||||
return os.path.join(fdir, matches[0])
|
||||
elif matches:
|
||||
die(1,f'ERROR: more than one {subclass.__name__} file in directory {fdir!r}')
|
||||
die(1, f'ERROR: more than one {subclass.__name__} file in directory {fdir!r}')
|
||||
else:
|
||||
return None
|
||||
else:
|
||||
return [os.path.join(fdir,m) for m in matches]
|
||||
return [os.path.join(fdir, m) for m in matches]
|
||||
|
||||
def find_file_in_dir(subclass,fdir):
|
||||
return find_files_in_dir(subclass,fdir,no_dups=True)
|
||||
def find_file_in_dir(subclass, fdir):
|
||||
return find_files_in_dir(subclass, fdir, no_dups=True)
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ fileutil: Routines that read, write, execute or stat files
|
|||
|
||||
# 09 Mar 2024: make all utils accept Path instances as arguments
|
||||
|
||||
import sys,os
|
||||
import sys, os
|
||||
|
||||
from .color import set_vt100
|
||||
from .util import (
|
||||
|
|
@ -48,42 +48,42 @@ def check_or_create_dir(path):
|
|||
str(path)])
|
||||
set_vt100()
|
||||
try:
|
||||
os.makedirs(path,0o700)
|
||||
os.makedirs(path, 0o700)
|
||||
except:
|
||||
die(2, f'ERROR: unable to read or create path ‘{path}’')
|
||||
|
||||
def check_binary(args):
|
||||
from subprocess import run,DEVNULL
|
||||
from subprocess import run, DEVNULL
|
||||
try:
|
||||
run(args,stdout=DEVNULL,stderr=DEVNULL,check=True)
|
||||
run(args, stdout=DEVNULL, stderr=DEVNULL, check=True)
|
||||
except:
|
||||
die(2,f'{args[0]!r} binary missing, not in path, or not executable')
|
||||
die(2, f'{args[0]!r} binary missing, not in path, or not executable')
|
||||
set_vt100()
|
||||
|
||||
def shred_file(fn,verbose=False):
|
||||
check_binary(['shred','--version'])
|
||||
def shred_file(fn, verbose=False):
|
||||
check_binary(['shred', '--version'])
|
||||
from subprocess import run
|
||||
run(
|
||||
['shred','--force','--iterations=30','--zero','--remove=wipesync']
|
||||
['shred', '--force', '--iterations=30', '--zero', '--remove=wipesync']
|
||||
+ (['--verbose'] if verbose else [])
|
||||
+ [str(fn)],
|
||||
check=True )
|
||||
check=True)
|
||||
set_vt100()
|
||||
|
||||
def _check_file_type_and_access(fname,ftype,blkdev_ok=False):
|
||||
def _check_file_type_and_access(fname, ftype, blkdev_ok=False):
|
||||
|
||||
import stat
|
||||
|
||||
access,op_desc = (
|
||||
(os.W_OK,'writ') if ftype in ('output file','output directory') else
|
||||
(os.R_OK,'read') )
|
||||
access, op_desc = (
|
||||
(os.W_OK, 'writ') if ftype in ('output file', 'output directory') else
|
||||
(os.R_OK, 'read'))
|
||||
|
||||
if ftype == 'output directory':
|
||||
ok_types = [(stat.S_ISDIR, 'output directory')]
|
||||
else:
|
||||
ok_types = [
|
||||
(stat.S_ISREG,'regular file'),
|
||||
(stat.S_ISLNK,'symbolic link')
|
||||
(stat.S_ISREG, 'regular file'),
|
||||
(stat.S_ISLNK, 'symbolic link')
|
||||
]
|
||||
if blkdev_ok:
|
||||
if not sys.platform in ('win32',):
|
||||
|
|
@ -98,31 +98,31 @@ def _check_file_type_and_access(fname,ftype,blkdev_ok=False):
|
|||
if t[0](mode):
|
||||
break
|
||||
else:
|
||||
ok_list = ' or '.join( t[1] for t in ok_types )
|
||||
ok_list = ' or '.join(t[1] for t in ok_types)
|
||||
die(1, f'Requested {ftype} ‘{fname}’ is not a {ok_list}')
|
||||
|
||||
if not os.access(fname,access):
|
||||
if not os.access(fname, access):
|
||||
die(1, f'Requested {ftype} ‘{fname}’ is not {op_desc}able by you')
|
||||
|
||||
return True
|
||||
|
||||
def check_infile(f,blkdev_ok=False):
|
||||
return _check_file_type_and_access(f,'input file',blkdev_ok=blkdev_ok)
|
||||
def check_infile(f, blkdev_ok=False):
|
||||
return _check_file_type_and_access(f, 'input file', blkdev_ok=blkdev_ok)
|
||||
|
||||
def check_outfile(f,blkdev_ok=False):
|
||||
return _check_file_type_and_access(f,'output file',blkdev_ok=blkdev_ok)
|
||||
def check_outfile(f, blkdev_ok=False):
|
||||
return _check_file_type_and_access(f, 'output file', blkdev_ok=blkdev_ok)
|
||||
|
||||
def check_outdir(f):
|
||||
return _check_file_type_and_access(f,'output directory')
|
||||
return _check_file_type_and_access(f, 'output directory')
|
||||
|
||||
def get_seed_file(cfg,nargs,wallets=None,invoked_as=None):
|
||||
def get_seed_file(cfg, nargs, wallets=None, invoked_as=None):
|
||||
|
||||
wallets = wallets or cfg._args
|
||||
|
||||
from .filename import find_file_in_dir
|
||||
from .wallet.mmgen import wallet
|
||||
|
||||
wf = find_file_in_dir(wallet,cfg.data_dir)
|
||||
wf = find_file_in_dir(wallet, cfg.data_dir)
|
||||
|
||||
wd_from_opt = bool(cfg.hidden_incog_input_params or cfg.in_fmt) # have wallet data from opt?
|
||||
|
||||
|
|
@ -138,16 +138,16 @@ def get_seed_file(cfg,nargs,wallets=None,invoked_as=None):
|
|||
if wallets or wf:
|
||||
check_infile(wallets[0] if wallets else wf)
|
||||
|
||||
return str(wallets[0]) if wallets else (wf,None)[wd_from_opt] # could be a Path instance
|
||||
return str(wallets[0]) if wallets else (wf, None)[wd_from_opt] # could be a Path instance
|
||||
|
||||
def _open_or_die(filename,mode,silent=False):
|
||||
def _open_or_die(filename, mode, silent=False):
|
||||
try:
|
||||
return open(filename,mode)
|
||||
return open(filename, mode)
|
||||
except:
|
||||
if silent:
|
||||
die(2,'')
|
||||
die(2, '')
|
||||
else:
|
||||
fn = {0:'STDIN',1:'STDOUT',2:'STDERR'}[filename] if isinstance(filename,int) else f'‘{filename}’'
|
||||
fn = {0:'STDIN', 1:'STDOUT', 2:'STDERR'}[filename] if isinstance(filename, int) else f'‘{filename}’'
|
||||
desc = 'reading' if 'r' in mode else 'writing'
|
||||
die(2, f'Unable to open file {fn} for {desc}')
|
||||
|
||||
|
|
@ -184,13 +184,13 @@ def write_data_to_file(
|
|||
cfg._util.qmsg('Output to STDOUT requested')
|
||||
if cfg.stdin_tty:
|
||||
if no_tty:
|
||||
die(2,f'Printing {desc} to screen is not allowed')
|
||||
die(2, f'Printing {desc} to screen is not allowed')
|
||||
if (ask_tty and not cfg.quiet) or binary:
|
||||
from .ui import confirm_or_raise
|
||||
confirm_or_raise(
|
||||
cfg,
|
||||
message = '',
|
||||
action = f'output {desc} to screen' )
|
||||
action = f'output {desc} to screen')
|
||||
else:
|
||||
try:
|
||||
of = os.readlink(f'/proc/{os.getpid()}/fd/1') # Linux
|
||||
|
|
@ -200,15 +200,15 @@ def write_data_to_file(
|
|||
if of:
|
||||
if of[:5] == 'pipe:':
|
||||
if no_tty:
|
||||
die(2,f'Writing {desc} to pipe is not allowed')
|
||||
die(2, f'Writing {desc} to pipe is not allowed')
|
||||
if ask_tty and not cfg.quiet:
|
||||
from .ui import confirm_or_raise
|
||||
confirm_or_raise(
|
||||
cfg,
|
||||
message = '',
|
||||
action = f'output {desc} to pipe' )
|
||||
action = f'output {desc} to pipe')
|
||||
msg('')
|
||||
of2,pd = os.path.relpath(of),os.path.pardir
|
||||
of2, pd = os.path.relpath(of), os.path.pardir
|
||||
msg('Redirecting output to file {!r}'.format(of if of2[:len(pd)] == pd else of2))
|
||||
else:
|
||||
msg('Redirecting output to file')
|
||||
|
|
@ -216,15 +216,15 @@ def write_data_to_file(
|
|||
if binary:
|
||||
if sys.platform == 'win32': # condition on separate line for pylint
|
||||
import msvcrt
|
||||
msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY)
|
||||
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
|
||||
|
||||
# MSWin workaround. See msg_r()
|
||||
try:
|
||||
sys.stdout.write(data.decode() if isinstance(data,bytes) else data)
|
||||
sys.stdout.write(data.decode() if isinstance(data, bytes) else data)
|
||||
except:
|
||||
os.write(1,data if isinstance(data,bytes) else data.encode())
|
||||
os.write(1, data if isinstance(data, bytes) else data.encode())
|
||||
|
||||
def do_file(outfile,ask_write_prompt):
|
||||
def do_file(outfile, ask_write_prompt):
|
||||
if (outdir or (cfg.outdir and not ignore_opt_outdir)) and not os.path.isabs(outfile):
|
||||
outfile = make_full_path(str(outdir or cfg.outdir), outfile) # outdir could be Path instance
|
||||
|
||||
|
|
@ -235,8 +235,8 @@ def write_data_to_file(
|
|||
if not keypress_confirm(
|
||||
cfg,
|
||||
ask_write_prompt,
|
||||
default_yes = ask_write_default_yes ):
|
||||
die(1,f'{capfirst(desc)} not saved')
|
||||
default_yes = ask_write_default_yes):
|
||||
die(1, f'{capfirst(desc)} not saved')
|
||||
|
||||
hush = False
|
||||
if os.path.lexists(outfile) and ask_overwrite:
|
||||
|
|
@ -244,7 +244,7 @@ def write_data_to_file(
|
|||
confirm_or_raise(
|
||||
cfg,
|
||||
message = '',
|
||||
action = f'File {outfile!r} already exists\nOverwrite?' )
|
||||
action = f'File {outfile!r} already exists\nOverwrite?')
|
||||
msg(f'Overwriting file {outfile!r}')
|
||||
hush = True
|
||||
|
||||
|
|
@ -253,34 +253,34 @@ def write_data_to_file(
|
|||
if check_data:
|
||||
d = ''
|
||||
try:
|
||||
with open(outfile,('r','rb')[bool(binary)]) as fp:
|
||||
with open(outfile, ('r', 'rb')[bool(binary)]) as fp:
|
||||
d = fp.read()
|
||||
except:
|
||||
pass
|
||||
if d != cmp_data:
|
||||
die(3,f'{desc} in file {outfile!r} has been altered by some other program! Aborting file write')
|
||||
die(3, f'{desc} in file {outfile!r} has been altered by some other program! Aborting file write')
|
||||
|
||||
# To maintain portability, always open files in binary mode
|
||||
# If 'binary' option not set, encode/decode data before writing and after reading
|
||||
try:
|
||||
with _open_or_die(outfile,'wb') as fp:
|
||||
with _open_or_die(outfile, 'wb') as fp:
|
||||
fp.write(data if binary else data.encode())
|
||||
except:
|
||||
die(2,f'Failed to write {desc} to file {outfile!r}')
|
||||
die(2, f'Failed to write {desc} to file {outfile!r}')
|
||||
|
||||
if not (hush or quiet):
|
||||
msg(f'{capfirst(desc)} written to file {outfile!r}')
|
||||
|
||||
return True
|
||||
|
||||
if cfg.stdout or outfile in ('','-'):
|
||||
if cfg.stdout or outfile in ('', '-'):
|
||||
do_stdout()
|
||||
elif sys.stdin.isatty() and not sys.stdout.isatty():
|
||||
do_stdout()
|
||||
else:
|
||||
do_file(outfile,ask_write_prompt)
|
||||
do_file(outfile, ask_write_prompt)
|
||||
|
||||
def get_words_from_file(cfg,infile,desc,quiet=False):
|
||||
def get_words_from_file(cfg, infile, desc, quiet=False):
|
||||
|
||||
if not quiet:
|
||||
cfg._util.qmsg(f'Getting {desc} from file ‘{infile}’')
|
||||
|
|
@ -291,7 +291,7 @@ def get_words_from_file(cfg,infile,desc,quiet=False):
|
|||
try:
|
||||
words = data.decode().split()
|
||||
except:
|
||||
die(1,f'{capfirst(desc)} data must be UTF-8 encoded.')
|
||||
die(1, f'{capfirst(desc)} data must be UTF-8 encoded.')
|
||||
|
||||
cfg._util.dmsg('Sanitized input: [{}]'.format(' '.join(words)))
|
||||
|
||||
|
|
@ -304,7 +304,7 @@ def get_data_from_file(
|
|||
dash = False,
|
||||
silent = False,
|
||||
binary = False,
|
||||
quiet = False ):
|
||||
quiet = False):
|
||||
|
||||
if not (cfg.quiet or silent or quiet):
|
||||
cfg._util.qmsg(f'Getting {desc} from file ‘{infile}’')
|
||||
|
|
@ -319,8 +319,8 @@ def get_data_from_file(
|
|||
data = data.decode()
|
||||
|
||||
if len(data) == cfg.max_input_size + 1:
|
||||
die( 'MaxInputSizeExceeded',
|
||||
f'Too much input data! Max input data size: {cfg.max_input_size} bytes' )
|
||||
die('MaxInputSizeExceeded',
|
||||
f'Too much input data! Max input data size: {cfg.max_input_size} bytes')
|
||||
|
||||
return data
|
||||
|
||||
|
|
@ -330,16 +330,16 @@ def get_lines_from_file(
|
|||
desc = 'data',
|
||||
trim_comments = False,
|
||||
quiet = False,
|
||||
silent = False ):
|
||||
silent = False):
|
||||
|
||||
def decrypt_file_maybe():
|
||||
data = get_data_from_file( cfg, fn, desc=desc, binary=True, quiet=quiet, silent=silent )
|
||||
data = get_data_from_file(cfg, fn, desc=desc, binary=True, quiet=quiet, silent=silent)
|
||||
from .crypto import Crypto
|
||||
have_enc_ext = get_extension(fn) == Crypto.mmenc_ext
|
||||
if have_enc_ext or not is_utf8(data):
|
||||
m = ('Attempting to decrypt','Decrypting')[have_enc_ext]
|
||||
m = ('Attempting to decrypt', 'Decrypting')[have_enc_ext]
|
||||
cfg._util.qmsg(f'{m} {desc} ‘{fn}’')
|
||||
data = Crypto(cfg).mmgen_decrypt_retry(data,desc)
|
||||
data = Crypto(cfg).mmgen_decrypt_retry(data, desc)
|
||||
return data
|
||||
|
||||
lines = decrypt_file_maybe().decode().splitlines()
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue