158158class Session :
159159 """A connect to a domain"""
160160
161- domain : Domain
162- parent : Commands
163- key : bytes
164- counter : int
165- epoch : bytes
166- delta : int
167- sharedKey : bytes
168- hmac : bytes
169- publicKey : bytes
170- lock : Lock
171- ready : bool
172-
173161 def __init__ (self , parent : Commands , domain : Domain ):
174- self .parent = parent
175- self .domain = domain
162+ self .parent : Commands = parent
163+ self .domain : Domain = domain
164+ self .counter : int = 0
165+ self .epoch : bytes | None = None
166+ self .delta : int = 0
167+ self .sharedKey : bytes | None = None
168+ self .hmac : bytes | None = None
169+ self .publicKey : bytes | None = None
176170 self .lock = Lock ()
177- self .counter = 0
178- self .ready = False
171+
172+ @property
173+ def ready (self ) -> bool :
174+ return self .epoch is not None and self .hmac is not None and self .delta > 0
179175
180176 def update (self , sessionInfo : SessionInfo ):
181177 """Update the session with new information"""
@@ -187,14 +183,14 @@ def update(self, sessionInfo: SessionInfo):
187183 self .publicKey = sessionInfo .publicKey
188184 self .sharedKey = self .parent .shared_key (sessionInfo .publicKey )
189185 self .hmac = hmac .new (self .sharedKey , "authenticated command" .encode (), hashlib .sha256 ).digest ()
190- self .ready = True
191186
192187 def hmac_personalized (self ) -> HMAC_Personalized_Signature_Data :
193188 """Sign a command and return session metadata"""
194189 self .counter += 1
195190 return HMAC_Personalized_Signature_Data (
196191 epoch = self .epoch ,
197192 counter = self .counter ,
193+ # Expire command in 10 seconds
198194 expires_at = int (time .time ()) - self .delta + 10 ,
199195 )
200196
@@ -205,7 +201,8 @@ def aes_gcm_personalized(self) -> AES_GCM_Personalized_Signature_Data:
205201 epoch = self .epoch ,
206202 nonce = randbytes (12 ),
207203 counter = self .counter ,
208- expires_at = int (time .time ()) - self .delta + 10 ,
204+ # Expire command in 30 seconds (BLE can be slow)
205+ expires_at = int (time .time ()) - self .delta + 30 ,
209206 )
210207
211208
0 commit comments