@@ -112,19 +112,26 @@ def validate_share(share: Dict[str, str], index: int) -> Tuple[str, str]:
112
112
key_str = key_str [2 :]
113
113
if index_str .startswith ('0x' ):
114
114
index_str = index_str [2 :]
115
-
116
115
117
116
if not key_str :
118
117
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 } " )
121
120
122
121
if len (key_str ) % 2 != 0 :
123
122
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 :
128
135
raise ValueError (f"Invalid index format in share at index { index } : must be a valid hex string" )
129
136
130
137
index_int = int (index_str , 16 )
@@ -133,7 +140,6 @@ def validate_share(share: Dict[str, str], index: int) -> Tuple[str, str]:
133
140
134
141
return key_str , index_str
135
142
136
-
137
143
async def recover_key (keyShards : List [Dict [str , str ]]) -> Dict [str , Any ]:
138
144
"""
139
145
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]:
168
174
return {
169
175
"masterKey" : None ,
170
176
"error" : f"Recovery error: { str (e )} "
171
- }
177
+ }
0 commit comments