@@ -112,19 +112,26 @@ def validate_share(share: Dict[str, str], index: int) -> Tuple[str, str]:
112112 key_str = key_str [2 :]
113113 if index_str .startswith ('0x' ):
114114 index_str = index_str [2 :]
115-
116115
117116 if not key_str :
118117 raise ValueError (f"Empty key in share at index { index } " )
119- if not all ( c in '0123456789abcdef' for c in key_str ) :
120- raise ValueError (f"Invalid key format in share at index { index } : must be a valid hex string " )
118+ if not index_str :
119+ raise ValueError (f"Empty index in share at index { index } " )
121120
122121 if len (key_str ) % 2 != 0 :
123122 key_str = '0' + key_str
124-
125- if not index_str :
126- raise ValueError (f"Empty index in share at index { index } " )
127- if not all (c in '0123456789abcdef' for c in index_str ):
123+
124+ if len (index_str ) % 2 != 0 :
125+ index_str = '0' + index_str
126+
127+ try :
128+ bytes .fromhex (key_str )
129+ except ValueError :
130+ raise ValueError (f"Invalid key format in share at index { index } : must be a valid hex string" )
131+
132+ try :
133+ bytes .fromhex (index_str )
134+ except ValueError :
128135 raise ValueError (f"Invalid index format in share at index { index } : must be a valid hex string" )
129136
130137 index_int = int (index_str , 16 )
@@ -133,7 +140,6 @@ def validate_share(share: Dict[str, str], index: int) -> Tuple[str, str]:
133140
134141 return key_str , index_str
135142
136-
137143async def recover_key (keyShards : List [Dict [str , str ]]) -> Dict [str , Any ]:
138144 """
139145 Recover the master key from a subset of key shares using Lagrange interpolation.
@@ -168,4 +174,4 @@ async def recover_key(keyShards: List[Dict[str, str]]) -> Dict[str, Any]:
168174 return {
169175 "masterKey" : None ,
170176 "error" : f"Recovery error: { str (e )} "
171- }
177+ }
0 commit comments