@cartridge/controller 0.12.1 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cartridge/controller",
3
- "version": "0.12.1",
3
+ "version": "0.12.2",
4
4
  "description": "Cartridge Controller",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,7 +21,7 @@
21
21
  }
22
22
  },
23
23
  "dependencies": {
24
- "@cartridge/controller-wasm": "0.8.0",
24
+ "@cartridge/controller-wasm": "0.9.1",
25
25
  "@cartridge/penpal": "^6.2.4",
26
26
  "micro-sol-signer": "^0.5.0",
27
27
  "bs58": "^6.0.0",
@@ -50,7 +50,7 @@
50
50
  "vite-plugin-node-polyfills": "^0.23.0",
51
51
  "vite-plugin-top-level-await": "^1.4.4",
52
52
  "vite-plugin-wasm": "^3.4.1",
53
- "@cartridge/tsconfig": "0.12.1"
53
+ "@cartridge/tsconfig": "0.12.2"
54
54
  },
55
55
  "scripts": {
56
56
  "build:deps": "pnpm build",
@@ -72,6 +72,13 @@ export default class SessionAccount extends WalletAccount {
72
72
  * @returns response from addTransaction
73
73
  */
74
74
  async execute(calls: Call | Call[]): Promise<InvokeFunctionResponse> {
75
- return this.controller.execute(normalizeCalls(calls));
75
+ try {
76
+ const res = await this.controller.executeFromOutside(
77
+ normalizeCalls(calls),
78
+ );
79
+ return res;
80
+ } catch (e) {
81
+ return this.controller.execute(normalizeCalls(calls));
82
+ }
76
83
  }
77
84
  }
@@ -166,6 +166,56 @@ export default class SessionProvider extends BaseProvider {
166
166
  return true;
167
167
  }
168
168
 
169
+ private padBase64(value: string): string {
170
+ const padding = value.length % 4;
171
+ if (padding === 0) {
172
+ return value;
173
+ }
174
+ return value + "=".repeat(4 - padding);
175
+ }
176
+
177
+ private normalizeSession(
178
+ session: Partial<SessionRegistration>,
179
+ ): SessionRegistration | undefined {
180
+ if (
181
+ session.username === undefined ||
182
+ session.address === undefined ||
183
+ session.ownerGuid === undefined ||
184
+ session.expiresAt === undefined
185
+ ) {
186
+ return undefined;
187
+ }
188
+
189
+ return {
190
+ username: session.username,
191
+ address: session.address,
192
+ ownerGuid: session.ownerGuid,
193
+ transactionHash: session.transactionHash,
194
+ expiresAt: session.expiresAt,
195
+ guardianKeyGuid: session.guardianKeyGuid ?? "0x0",
196
+ metadataHash: session.metadataHash ?? "0x0",
197
+ sessionKeyGuid: session.sessionKeyGuid ?? this._sessionKeyGuid,
198
+ };
199
+ }
200
+
201
+ public ingestSessionFromRedirect(
202
+ encodedSession: string,
203
+ ): SessionRegistration | undefined {
204
+ try {
205
+ const decoded = atob(this.padBase64(encodedSession));
206
+ const parsed = JSON.parse(decoded) as Partial<SessionRegistration>;
207
+ const normalized = this.normalizeSession(parsed);
208
+ if (!normalized) {
209
+ return undefined;
210
+ }
211
+ localStorage.setItem("session", JSON.stringify(normalized));
212
+ return normalized;
213
+ } catch (e) {
214
+ console.error("Failed to ingest session redirect", e);
215
+ return undefined;
216
+ }
217
+ }
218
+
169
219
  async username() {
170
220
  await this.tryRetrieveFromQueryOrStorage();
171
221
  return this._username;
@@ -299,23 +349,27 @@ export default class SessionProvider extends BaseProvider {
299
349
 
300
350
  const sessionString = localStorage.getItem("session");
301
351
  if (sessionString) {
302
- sessionRegistration = JSON.parse(sessionString);
352
+ const parsed = JSON.parse(sessionString) as Partial<SessionRegistration>;
353
+ const normalized = this.normalizeSession(parsed);
354
+ if (normalized) {
355
+ sessionRegistration = normalized;
356
+ localStorage.setItem("session", JSON.stringify(sessionRegistration));
357
+ } else {
358
+ this.clearStoredSession();
359
+ }
303
360
  }
304
361
 
305
362
  if (window.location.search.includes("startapp")) {
306
363
  const params = new URLSearchParams(window.location.search);
307
364
  const session = params.get("startapp");
308
365
  if (session) {
309
- const possibleNewSession: SessionRegistration = JSON.parse(
310
- atob(session),
311
- );
312
-
366
+ const normalizedSession = this.ingestSessionFromRedirect(session);
313
367
  if (
314
- Number(possibleNewSession.expiresAt) !==
315
- Number(sessionRegistration?.expiresAt)
368
+ normalizedSession &&
369
+ Number(normalizedSession.expiresAt) !==
370
+ Number(sessionRegistration?.expiresAt)
316
371
  ) {
317
- sessionRegistration = possibleNewSession;
318
- localStorage.setItem("session", JSON.stringify(sessionRegistration));
372
+ sessionRegistration = normalizedSession;
319
373
  }
320
374
 
321
375
  // Remove the session query parameter