From 7311b2d1cfbc0ba644ed6afba3660b3e83ac3491 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 16 Nov 2022 17:56:05 +0000 Subject: [PATCH] AddrListID: support initialization from string + proto --- mmgen/addr.py | 20 ++++++++++++++------ test/objtest_py_d/ot_btc_mainnet.py | 4 +++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/mmgen/addr.py b/mmgen/addr.py index 38779de3..965f3e65 100755 --- a/mmgen/addr.py +++ b/mmgen/addr.py @@ -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' diff --git a/test/objtest_py_d/ot_btc_mainnet.py b/test/objtest_py_d/ot_btc_mainnet.py index 36776616..930bc32a 100755 --- a/test/objtest_py_d/ot_btc_mainnet.py +++ b/test/objtest_py_d/ot_btc_mainnet.py @@ -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'}, )