@@ -4,20 +4,51 @@ import {
44 type Transport , type WalletActions } from "viem" ;
55import {
66 deployContract , getAddresses , getCallsStatus , getCapabilities , getChainId , prepareAuthorization , sendCalls , sendRawTransaction ,
7- showCallsStatus ,
8- signAuthorization ,
9- signMessage , signTypedData , waitForCallsStatus , writeContract ,
7+ showCallsStatus , signAuthorization , signMessage , signTypedData , waitForCallsStatus , writeContract ,
108} from "viem/actions" ;
119import { signTransaction , type TransactionRequestEIP712 , type ZksyncEip712Meta } from "viem/zksync" ;
1210
1311import { getTransactionWithPaymasterData } from "../../../paymaster/index.js" ;
12+ import { SessionErrorType , SessionEventType , type SessionState , validateSessionTransaction } from "../../../utils/session.js" ;
1413import { sendEip712Transaction } from "../actions/sendEip712Transaction.js" ;
14+ import { getSessionState , sessionStateNotify } from "../actions/session.js" ;
1515import type { ClientWithZksyncSsoSessionData } from "../client.js" ;
1616
1717export type ZksyncSsoWalletActions < chain extends Chain , account extends Account > = Omit <
1818 WalletActions < chain , account > , "addChain" | "getPermissions" | "requestAddresses" | "requestPermissions" | "switchChain" | "watchAsset" | "prepareTransactionRequest"
1919> ;
2020
21+ const sessionErrorToSessionEventType = {
22+ [ SessionErrorType . SessionInactive ] : SessionEventType . Inactive ,
23+ [ SessionErrorType . SessionExpired ] : SessionEventType . Expired ,
24+ } ;
25+
26+ /**
27+ * Helper function to check session state and notify via callback
28+ */
29+ async function getSessionStateAndNotify <
30+ transport extends Transport ,
31+ chain extends Chain ,
32+ account extends Account ,
33+ > ( client : ClientWithZksyncSsoSessionData < transport , chain , account > ) : Promise < SessionState > {
34+ const { sessionState } = await getSessionState ( client , {
35+ account : client . account . address ,
36+ sessionConfig : client . sessionConfig ,
37+ contracts : client . contracts ,
38+ } ) ;
39+
40+ if ( client . onSessionStateChange ) {
41+ sessionStateNotify ( {
42+ sessionConfig : client . sessionConfig ,
43+ sessionState,
44+ onSessionStateChange : client . onSessionStateChange ,
45+ sessionNotifyTimeout : client . _sessionNotifyTimeout ,
46+ } ) ;
47+ }
48+
49+ return sessionState ;
50+ }
51+
2152export function zksyncSsoWalletActions <
2253 transport extends Transport ,
2354 chain extends Chain ,
@@ -29,6 +60,29 @@ export function zksyncSsoWalletActions<
2960 getChainId : ( ) => getChainId ( client ) ,
3061 sendRawTransaction : ( args ) => sendRawTransaction ( client , args ) ,
3162 sendTransaction : async ( args ) => {
63+ // Get current session state and trigger callback if needed
64+ const sessionState = await getSessionStateAndNotify ( client ) ;
65+
66+ // Validate transaction against session constraints
67+ const validationResult = validateSessionTransaction ( {
68+ sessionState,
69+ sessionConfig : client . sessionConfig ,
70+ transaction : args as any ,
71+ } ) ;
72+
73+ // Throw error if validation fails
74+ if ( validationResult . error ) {
75+ // If validation fails due to session issues, notify via callback
76+ if ( client . onSessionStateChange && Object . keys ( sessionErrorToSessionEventType ) . includes ( validationResult . error . type ) ) {
77+ client . onSessionStateChange ( {
78+ type : sessionErrorToSessionEventType [ validationResult . error . type as keyof typeof sessionErrorToSessionEventType ] ,
79+ message : validationResult . error . message ,
80+ } ) ;
81+ }
82+ throw new Error ( `Session validation failed: ${ validationResult . error . message } (${ validationResult . error . type } )` ) ;
83+ }
84+
85+ // Process transaction if it's valid
3286 // eslint-disable-next-line @typescript-eslint/no-explicit-any
3387 const unformattedTx : any = Object . assign ( { } , args ) ;
3488
0 commit comments