@@ -318,44 +318,75 @@ export class MetamaskAuthProvider implements AuthProvider<MetamaskAuthCredential
318318 salt,
319319 } ;
320320
321- let newSessionKey ;
322- if ( registerSessionKey ) {
323- const nodeService = NodeService . getInstance ( ) ;
324- const secp256k1Blob : Secp256k1Blob = {
325- identity,
326- data : digest ,
327- public_key : publicKey ,
328- signature : compactSignature ,
329- } ;
330- const secp_blob = {
331- contract_name : "secp256k1" ,
332- data : serializeSecp256k1Blob ( secp256k1Blob ) ,
333- } ;
334-
335- const wallet_blob = verifyIdentityBlob ( username , nonce ) ;
336-
337- const { duration, whitelist } = registerSessionKey ;
338- const expiration = Date . now ( ) + duration ;
339- newSessionKey = sessionKeyService . generateSessionKey ( expiration , whitelist ) ;
340-
341- const newPKblob = addSessionKeyBlob ( username , newSessionKey . publicKey , expiration , nonce , whitelist )
342- const blobTx : BlobTransaction = {
343- identity,
344- // warning: secp_blob need to be at index 1
345- blobs : [ wallet_blob , secp_blob , newPKblob ] ,
346- } ;
347-
348-
349- onWalletEvent ?.( { account : identity , type : "sending_blob" , message : "Sending blob transaction" } ) ;
350-
351- const txHash = await hashBlobTransaction ( blobTx ) ;
352- onWalletEvent ?.( { account : identity , type : "blob_sent" , message : `Blob transaction sent: ${ txHash } ` } ) ;
353-
354- await nodeService . client . sendBlobTx ( blobTx ) ;
321+ const sessionKeyPromise = registerSessionKey ? WalletOperations . getOrReuseSessionKey ( wallet ) : undefined ;
355322
356- // TODO(?): Assert transaction settles to assure the session key is valid (?)
323+ if ( registerSessionKey ) {
324+ try {
325+ const existingSessionKey = sessionKeyPromise ? await sessionKeyPromise : undefined ;
326+ if ( existingSessionKey ) {
327+ wallet . sessionKey = existingSessionKey ;
328+ } else {
329+ const nodeService = NodeService . getInstance ( ) ;
330+ const secp256k1Blob : Secp256k1Blob = {
331+ identity,
332+ data : digest ,
333+ public_key : publicKey ,
334+ signature : compactSignature ,
335+ } ;
336+ const secp_blob = {
337+ contract_name : "secp256k1" ,
338+ data : serializeSecp256k1Blob ( secp256k1Blob ) ,
339+ } ;
340+
341+ const wallet_blob = verifyIdentityBlob ( username , nonce ) ;
342+
343+ const { duration, whitelist, laneId } = registerSessionKey ;
344+ const expiration = Date . now ( ) + duration ;
345+ const generatedSessionKey = sessionKeyService . generateSessionKey ( expiration , whitelist ) ;
346+ const newSessionKey = laneId
347+ ? { ...generatedSessionKey , laneId }
348+ : generatedSessionKey ;
349+
350+ const newPKblob = addSessionKeyBlob (
351+ username ,
352+ newSessionKey . publicKey ,
353+ expiration ,
354+ nonce ,
355+ whitelist ,
356+ laneId ,
357+ ) ;
358+ const blobTx : BlobTransaction = {
359+ identity,
360+ // warning: secp_blob need to be at index 1
361+ blobs : [ wallet_blob , secp_blob , newPKblob ] ,
362+ } ;
363+
364+ onWalletEvent ?.( { account : identity , type : "sending_blob" , message : "Sending blob transaction" } ) ;
365+
366+ const txHash = await hashBlobTransaction ( blobTx ) ;
367+ onWalletEvent ?.( {
368+ account : identity ,
369+ type : "blob_sent" ,
370+ message : `Blob transaction sent: ${ txHash } ` ,
371+ } ) ;
372+
373+ await nodeService . client . sendBlobTx ( blobTx ) ;
374+
375+ // TODO(?): Assert transaction settles to assure the session key is valid (?)
376+ wallet . sessionKey = newSessionKey ;
377+ }
378+ } catch ( sessionKeyError ) {
379+ console . error ( "Failed to register session key via MetaMask:" , sessionKeyError ) ;
380+ onError ?.(
381+ sessionKeyError instanceof Error
382+ ? sessionKeyError
383+ : new Error ( "Failed to register MetaMask session key" ) ,
384+ ) ;
385+ }
357386 }
358387
388+ wallet = WalletOperations . cleanExpiredSessionKeys ( wallet ) ;
389+
359390 onWalletEvent ?.( {
360391 account : identity ,
361392 type : "logged_in" ,
0 commit comments