Skip to content

Commit 17e01d7

Browse files
authored
feat: add interface to trigger session activity for a different client id (#2461)
1 parent 6e11807 commit 17e01d7

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

packages/passport/sdk-sample-app/src/components/zkevm/Request.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ const EthereumMethods: EthereumMethod[] = [
142142
{ name: 'blockNumber/tag', default: 'latest' },
143143
],
144144
},
145+
{
146+
name: 'im_addSessionActivity',
147+
params: [
148+
{ name: 'clientId' },
149+
],
150+
},
145151
];
146152

147153
function Request({ showModal, setShowModal }: ModalProps) {

packages/passport/sdk/src/zkEvm/sessionActivity/sessionActivity.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ const SESSION_ACTIVITY_COUNT_KEY = 'sessionActivitySendCount';
1010
const SESSION_ACTIVITY_DAY_KEY = 'sessionActivityDate';
1111

1212
// Maintain a few local counters for session activity
13-
let checkCount = 0;
14-
let sendCount = 0;
15-
let currentSessionTrackCall = false;
13+
const checkCount: { [k: string]: number } = {};
14+
let sendCount: { [k: string]: number } = {};
15+
const currentSessionTrackCall: { [k: string]: boolean } = {};
1616

1717
// Sync sendCount to localStorage
1818
const syncSendCount = () => {
19-
sendCount = getItem(SESSION_ACTIVITY_COUNT_KEY) || 0;
19+
sendCount = getItem(SESSION_ACTIVITY_COUNT_KEY) || {};
2020
const sendDay = getItem(SESSION_ACTIVITY_DAY_KEY);
2121

2222
// If no day, set count to zero. If not today, reset sendCount to 0
2323
const today = new Date().toISOString().split('T')[0];
2424
if (!sendDay || sendDay !== today) {
25-
sendCount = 0;
25+
sendCount = {};
2626
}
2727

2828
setItem(SESSION_ACTIVITY_DAY_KEY, today);
@@ -31,12 +31,12 @@ const syncSendCount = () => {
3131
// Run as soon as module initialised.
3232
syncSendCount();
3333

34-
const incrementSendCount = () => {
34+
const incrementSendCount = (clientId: string) => {
3535
syncSendCount();
36-
sendCount++;
36+
sendCount[clientId]++;
3737
setItem(SESSION_ACTIVITY_COUNT_KEY, sendCount);
3838
// Reset checkCount to zero on sending
39-
checkCount = 0;
39+
checkCount[clientId] = 0;
4040
};
4141

4242
// Fix no-promise-executor-return
@@ -47,12 +47,17 @@ const wait = async (seconds: number) => new Promise((resolve) => {
4747
const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
4848
// Use an existing flow if one is provided, or create a new one
4949
const flow = args.flow || trackFlow('passport', 'sendSessionActivity');
50+
const clientId = args.passportClient;
51+
if (!clientId) {
52+
flow.addEvent('No Passport Client ID');
53+
throw new Error('No Passport Client ID provided');
54+
}
5055
// If there is already a tracking call in progress, do nothing
51-
if (currentSessionTrackCall) {
56+
if (currentSessionTrackCall[clientId]) {
5257
flow.addEvent('Existing Delay Early Exit');
5358
return;
5459
}
55-
currentSessionTrackCall = true;
60+
currentSessionTrackCall[clientId] = true;
5661

5762
const { sendTransaction, environment } = args;
5863
if (!sendTransaction) {
@@ -64,12 +69,6 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
6469
}
6570
setupClient(environment);
6671

67-
const clientId = args.passportClient;
68-
if (!clientId) {
69-
flow.addEvent('No Passport Client ID');
70-
throw new Error('No Passport Client ID provided');
71-
}
72-
7372
const from = args.walletAddress;
7473
if (!from) {
7574
flow.addEvent('No Passport Wallet Address');
@@ -84,11 +83,11 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
8483
details = await get({
8584
clientId,
8685
wallet: from,
87-
checkCount,
88-
sendCount,
86+
checkCount: checkCount[clientId] || 0,
87+
sendCount: sendCount[clientId] || 0,
8988
});
90-
checkCount++;
91-
flow.addEvent('Fetched details', { checkCount });
89+
checkCount[clientId]++;
90+
flow.addEvent('Fetched details', { checkCount: checkCount[clientId] });
9291

9392
if (!details) {
9493
flow.addEvent('No details found');
@@ -108,7 +107,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
108107
try {
109108
flow.addEvent('Start Sending Transaction');
110109
const tx = await args.sendTransaction([{ to, from, data }], flow);
111-
incrementSendCount();
110+
incrementSendCount(clientId);
112111
flow.addEvent('Transaction Sent', { tx });
113112
} catch (error) {
114113
flow.addEvent('Failed to send Transaction');
@@ -123,7 +122,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
123122
await wait(details.delay);
124123
setTimeout(() => {
125124
flow.addEvent('Retrying after Delay');
126-
currentSessionTrackCall = false;
125+
currentSessionTrackCall[clientId] = false;
127126
// eslint-disable-next-line
128127
trackSessionWrapper({ ...args, flow });
129128
}, 0);
@@ -132,7 +131,7 @@ const trackSessionActivityFn = async (args: AccountsRequestedEvent) => {
132131

133132
// Wrapper design to ensure that after track function is called, current session Track call is false.
134133
const trackSessionWrapper = (args: AccountsRequestedEvent) => errorBoundary(trackSessionActivityFn)(args).then(() => {
135-
currentSessionTrackCall = false;
134+
currentSessionTrackCall[args.passportClient] = false;
136135
});
137136

138137
export const trackSessionActivity = trackSessionWrapper;

packages/passport/sdk/src/zkEvm/zkEvmProvider.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class ZkEvmProvider implements Provider {
192192
return ethSigner;
193193
}
194194

195-
async #callSessionActivity(zkEvmAddress: string) {
195+
async #callSessionActivity(zkEvmAddress: string, clientId?: string) {
196196
const sendTransactionClosure = async (params: Array<any>, flow: Flow) => {
197197
const ethSigner = await this.#getSigner();
198198
return await sendTransaction({
@@ -209,7 +209,7 @@ export class ZkEvmProvider implements Provider {
209209
environment: this.#config.baseConfig.environment,
210210
sendTransaction: sendTransactionClosure,
211211
walletAddress: zkEvmAddress,
212-
passportClient: this.#config.oidcConfiguration.clientId,
212+
passportClient: clientId || this.#config.oidcConfiguration.clientId,
213213
});
214214
}
215215

@@ -498,6 +498,14 @@ export class ZkEvmProvider implements Provider {
498498
flow.addEvent('End');
499499
}
500500
}
501+
case 'im_addSessionActivity': {
502+
const [clientId] = request.params || [];
503+
const zkEvmAddress = await this.#getZkEvmAddress();
504+
if (zkEvmAddress) {
505+
this.#callSessionActivity(zkEvmAddress, clientId);
506+
}
507+
return null;
508+
}
501509
default: {
502510
throw new JsonRpcError(
503511
ProviderErrorCode.UNSUPPORTED_METHOD,

0 commit comments

Comments
 (0)