move SeedID class to seed.py

This commit is contained in:
The MMGen Project 2022-01-15 14:00:06 +00:00
commit 4bb1b1533a
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 28 additions and 24 deletions

View file

@ -25,6 +25,7 @@ from .common import *
from .obj import *
from .baseconv import *
from .protocol import init_proto,hash160
from .seed import SeedID,is_seed_id
pnm = g.proj_name
@ -765,7 +766,7 @@ Removed {{}} duplicate WIF key{{}} from keylist (also in {pnm} key-address file
assert ls.pop() == '{', f'{ls!r}: invalid first line'
assert lines[-1] == '}', f'{lines[-1]!r}: invalid last line'
sid = ls.pop(0)
assert is_mmgen_seed_id(sid), f'{sid!r}: invalid Seed ID'
assert is_seed_id(sid), f'{sid!r}: invalid Seed ID'
if type(self) == PasswordList and len(ls) == 2:
ss = ls.pop().split(':')

View file

@ -62,7 +62,6 @@ def get_obj(objname,*args,**kwargs):
else:
return True if return_bool else ret
def is_mmgen_seed_id(s): return get_obj(SeedID, sid=s, silent=True,return_bool=True)
def is_mmgen_idx(s): return get_obj(AddrIdx, n=s, silent=True,return_bool=True)
def is_addrlist_id(s): return get_obj(AddrListID, sid=s, silent=True,return_bool=True)
@ -634,32 +633,12 @@ class ViewKey(object):
class ZcashViewKey(CoinAddr): hex_width = 128
class SeedID(str,Hilite,InitErrors):
color = 'blue'
width = 8
trunc_ok = False
def __new__(cls,seed=None,sid=None):
if type(sid) == cls:
return sid
try:
if seed:
from .seed import SeedBase
assert isinstance(seed,SeedBase),'not a subclass of SeedBase'
from .util import make_chksum_8
return str.__new__(cls,make_chksum_8(seed.data))
elif sid:
assert set(sid) <= set(hexdigits.upper()),'not uppercase hex digits'
assert len(sid) == cls.width, f'not {cls.width} characters wide'
return str.__new__(cls,sid)
raise ValueError('no arguments provided')
except Exception as e:
return cls.init_fail(e,seed or sid)
class MMGenID(str,Hilite,InitErrors,MMGenObject):
color = 'orange'
width = 0
trunc_ok = False
def __new__(cls,proto,id_str):
from .seed import SeedID
try:
ss = str(id_str).split(':')
assert len(ss) in (2,3),'not 2 or 3 colon-separated items'
@ -837,6 +816,7 @@ class AddrListID(str,Hilite,InitErrors,MMGenObject):
trunc_ok = False
color = 'yellow'
def __new__(cls,sid,mmtype):
from .seed import SeedID
try:
assert type(sid) == SeedID, f'{sid!r} not a SeedID instance'
if not isinstance(mmtype,(MMGenAddrType,MMGenPasswordType)):

View file

@ -24,6 +24,28 @@ from .common import *
from .obj import *
from .crypto import get_random,scramble_seed
class SeedID(str,Hilite,InitErrors):
color = 'blue'
width = 8
trunc_ok = False
def __new__(cls,seed=None,sid=None):
if type(sid) == cls:
return sid
try:
if seed:
assert isinstance(seed,SeedBase),'not a subclass of SeedBase'
return str.__new__(cls,make_chksum_8(seed.data))
elif sid:
assert set(sid) <= set(hexdigits.upper()),'not uppercase hex digits'
assert len(sid) == cls.width, f'not {cls.width} characters wide'
return str.__new__(cls,sid)
raise ValueError('no arguments provided')
except Exception as e:
return cls.init_fail(e,seed or sid)
def is_seed_id(s):
return get_obj( SeedID, sid=s, silent=True, return_bool=True )
class SeedBase(MMGenObject):
data = ImmutableAttr(bytes,typeconv=False)

View file

@ -25,9 +25,10 @@ from collections import namedtuple
from .common import *
from .addr import KeyAddrList,AddrIdxList
from .rpc import MoneroRPCClientRaw,MoneroWalletRPCClient,json_encoder
from .seed import SeedID
from .daemon import MoneroWalletDaemon
from .protocol import _b58a,init_proto
from .obj import CoinAddr,CoinTxID,SeedID,AddrIdx,Hilite,InitErrors
from .obj import CoinAddr,CoinTxID,AddrIdx,Hilite,InitErrors
xmrwallet_uarg_info = (
lambda e,hp: {