2626from solana .publickey import PublicKey
2727from nacl .signing import VerifyKey
2828from nacl .exceptions import BadSignatureError
29+ from redis import Redis
2930
3031import logging
3132
@@ -47,12 +48,12 @@ class VaccountAuthProvider:
4748 def __init__ (self , config , account_handler : ModuleApi ):
4849 self .account_handler = account_handler
4950 self .store : DataStore = account_handler ._hs .get_datastore ()
50- self .last_tonce = int ( account_handler . _hs . get_clock (). time () )
51+ self .redis = Redis ( )
5152
5253 @staticmethod
5354 def get_supported_login_types ():
5455 supported_login_types = {
55- 'm .login.vaccount' : (
56+ 'com.bitorbit .login.vaccount' : (
5657 'vaccount_address' ,
5758 'signature' ,
5859 'signer' ,
@@ -69,7 +70,7 @@ async def check_auth(self, evm_vaccount_address, login_type, login_dict):
6970 Args:
7071 evm_vaccount_address: ethereum based interpretation of the Vaccount address
7172 login_type: type of authentication
72- login_dict: authentication parameters `supported_login_types
73+ login_dict: authentication parameters `supported_login_types`
7374
7475 Returns:
7576 Canonical user ID if authentication was successful
@@ -84,9 +85,6 @@ async def check_auth(self, evm_vaccount_address, login_type, login_dict):
8485 if not signature or not signer_key or not signed_timestamp or not vaccount_address or not signer_type :
8586 return False
8687
87- if not self ._is_valid_sign_timestamp (signed_timestamp ):
88- return False
89-
9088 if evm_vaccount_address .startswith ("@" ) and ":" in evm_vaccount_address :
9189 # username is of the form @V4Bw2..:bar.com
9290 evm_vaccount_address = evm_vaccount_address .split (":" , 1 )[0 ][1 :]
@@ -115,6 +113,9 @@ async def check_auth(self, evm_vaccount_address, login_type, login_dict):
115113 if not is_valid_signature or not is_active_vaccount or not is_valid_evm_address :
116114 return False
117115
116+ if not self ._is_valid_sign_timestamp (evm_vaccount_address , signed_timestamp ):
117+ return False
118+
118119 user_id = self .account_handler .get_qualified_user_id (username = evm_vaccount_address )
119120
120121 if await self .account_handler .check_user_exists (user_id ):
@@ -151,7 +152,7 @@ async def check_auth(self, evm_vaccount_address, login_type, login_dict):
151152 # await self.store.set_e2e_cross_signing_key(
152153 # user_id, "master", vaccount_signing_key
153154 # )
154- self .last_tonce = signed_timestamp
155+ self ._commit_last_sign_timestamp ( evm_vaccount_address , signed_timestamp )
155156
156157 return user_id
157158
@@ -167,16 +168,19 @@ def _is_valid_signature(signature, signer_key, signed_msg) -> bool:
167168
168169 return True
169170
170- def _is_valid_sign_timestamp (self , signed_timestamp : int ):
171+ def _is_valid_sign_timestamp (self , evm_vaccount_address : str , signed_timestamp : int ):
171172 """Check if signed timestamp is valid
172173 Args:
173- signed_tonce: signed timestamp
174+ evm_vaccount_address: ethereum representing of the VA address
175+ signed_timestamp: last signed timestamp by VA key
174176 Returns:
175177 True if timestamp is greater than last signed timestamp
176178 """
177179 current_timestamp = int (self .account_handler ._hs .get_clock ().time ())
178- ts_window = current_timestamp - signed_timestamp
179- if signed_timestamp >= self .last_tonce and ts_window <= SIGN_TIMESTAMP_TOLERANCE :
180+ ts_window = current_timestamp - signed_timestamp
181+ last_signed_timestamp = self .redis .get (evm_vaccount_address ) or signed_timestamp
182+
183+ if signed_timestamp >= int (last_signed_timestamp ) and ts_window <= SIGN_TIMESTAMP_TOLERANCE :
180184 return True
181185
182186 return False
@@ -201,7 +205,7 @@ async def register_user(self, localpart, displayname):
201205 displayname = displayname ,
202206 )
203207
204- logger .info (f"Registration was successful: { user_id } , timestamp: { self . last_tonce } " )
208+ logger .info (f"Registration was successful: { user_id } " )
205209 return user_id
206210
207211 async def _is_active_vaccount (self , vaccount_address : PublicKey , signer : PublicKey , signer_type : str ) -> bool :
@@ -251,6 +255,15 @@ async def _get_parsed_account_info(self, account_address):
251255
252256 return account_data
253257
258+ def _commit_last_sign_timestamp (self , evm_vaccount_address , last_timestamp ):
259+ is_commit = self .redis .set (
260+ name = evm_vaccount_address ,
261+ value = last_timestamp ,
262+ # ex=SIGN_TIMESTAMP_TOLERANCE,
263+ )
264+
265+ return is_commit
266+
254267 @staticmethod
255268 def parse_config (config ):
256269 return config
0 commit comments