AddrListID: support initialization from string + proto

This commit is contained in:
The MMGen Project 2022-11-16 17:56:05 +00:00
commit 7311b2d1cf
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 17 additions and 7 deletions

View file

@ -94,11 +94,19 @@ class AddrListID(str,Hilite,InitErrors,MMGenObject):
width = 10
trunc_ok = False
color = 'yellow'
def __new__(cls,sid,mmtype):
def __new__(cls,sid=None,mmtype=None,proto=None,id_str=None):
try:
assert type(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')
if id_str:
a,b = id_str.split(':')
sid = SeedID(sid=a)
try:
mmtype = MMGenAddrType( proto=proto, id_str=b )
except:
mmtype = MMGenPasswordType( proto=proto, id_str=b )
else:
assert type(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.sid = sid
me.mmtype = mmtype
@ -106,8 +114,8 @@ class AddrListID(str,Hilite,InitErrors,MMGenObject):
except Exception as e:
return cls.init_fail(e, f'sid={sid}, mmtype={mmtype}')
def is_addrlist_id(s):
return get_obj( AddrListID, sid=s, silent=True, 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(str,Hilite,InitErrors,MMGenObject):
color = 'orange'

View file

@ -263,12 +263,14 @@ tests = {
{'proto':proto, 's':r32,'compressed':True,'pubkey_type':'std','ret':r32}
)
},
'AddrListID': { # a rather pointless test, but do it anyway
'AddrListID': {
'arg1': 'sid',
'bad': (
{'sid':SeedID(sid='F00BAA12'),'mmtype':'Z','ret':'F00BAA12:Z'},
{'id_str':'G00BAA12:B','proto':proto},
),
'good': (
{'id_str':'F00BAA12:B','proto':proto,'ret':'F00BAA12:B'},
{'sid':SeedID(sid='F00BAA12'),'mmtype':proto.addr_type(id_str='S'),'ret':'F00BAA12:S'},
{'sid':SeedID(sid='F00BAA12'),'mmtype':proto.addr_type(id_str='L'),'ret':'F00BAA12:L'},
)