77from eth_account .messages import encode_defunct
88from jwcrypto import jwk
99from jwcrypto .jwa import JWA
10+ from eth_account import Account
1011
1112logger = logging .getLogger (__name__ )
1213
14+
1315class VmClient :
1416 def __init__ (
15- self , account , domain : str = ""
17+ self , account : Account , domain : str = ""
1618 ):
17- self .account = account
19+ self .account : Account = account
1820 self .ephemeral_key = jwk .JWK .generate (kty = "EC" , crv = "P-256" )
1921 self .expected_domain = domain
2022 self .pubkey_payload = self ._generate_pubkey_payload ()
21- self .pubkey_signature_header = self . _generate_pubkey_signature_header ()
23+ self .pubkey_signature_header = None
2224 self .session = aiohttp .ClientSession ()
2325
2426 def _generate_pubkey_payload (self ):
2527 return {
2628 "pubkey" : json .loads (self .ephemeral_key .export_public ()),
2729 "alg" : "ECDSA" ,
2830 "domain" : self .expected_domain ,
29- "address" : self .account .get_address () ,
31+ "address" : self .account .address ,
3032 "expires" : (
31- datetime .datetime .utcnow () + datetime .timedelta (days = 1 )
32- ).isoformat ()
33- + "Z" ,
33+ datetime .datetime .utcnow () + datetime .timedelta (days = 1 )
34+ ).isoformat ()
35+ + "Z" ,
3436 }
3537
3638 def _generate_pubkey_signature_header (self ):
@@ -40,6 +42,7 @@ def _generate_pubkey_signature_header(self):
4042 pubkey_signature = self .to_0x_hex (signed_message .signature )
4143 return json .dumps (
4244 {
45+ "sender" : self .account .address ,
4346 "payload" : pubkey_payload ,
4447 "signature" : pubkey_signature ,
4548 "content" : {"domain" : self .expected_domain },
@@ -60,6 +63,9 @@ def on_message(content):
6063 logger .error (f"Unable to parse content: { content } , Error: { str (e )} " )
6164
6265 async def perform_operation (self , vm_id , operation ):
66+ if self .pubkey_signature_header is None :
67+ self .pubkey_signature_header = self ._generate_pubkey_signature_header ()
68+
6369 hostname = f"https://{ self .expected_domain } "
6470 path = f"/control/machine/{ vm_id } /{ operation } "
6571
@@ -91,6 +97,9 @@ async def perform_operation(self, vm_id, operation):
9197 return None , str (e )
9298
9399 async def get_logs (self , vm_id ):
100+ if self .pubkey_signature_header is None :
101+ self .pubkey_signature_header = self ._generate_pubkey_signature_header ()
102+
94103 ws_url = f"https://{ self .expected_domain } /control/machine/{ vm_id } /logs"
95104
96105 payload = {
@@ -165,7 +174,7 @@ async def expire_instance(self, vm_id):
165174 async def notify_allocation (self , vm_id ) -> Tuple [Any , str ]:
166175 json_data = {"instance" : vm_id }
167176 async with self .session .post (
168- f"https://{ self .expected_domain } /control/allocation/notify" , json = json_data
177+ f"https://{ self .expected_domain } /control/allocation/notify" , json = json_data
169178 ) as s :
170179 form_response_text = await s .text ()
171180 return s .status , form_response_text
0 commit comments