Browse Source

AddrListID: support initialization from string + proto

The MMGen Project 2 years ago
parent
commit
7311b2d1cf
2 changed files with 17 additions and 7 deletions
  1. 14 6
      mmgen/addr.py
  2. 3 1
      test/objtest_py_d/ot_btc_mainnet.py

+ 14 - 6
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'

+ 3 - 1
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'},
 		)