@anuma/sdk 6.31.1 → 6.31.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.
@@ -31540,79 +31540,159 @@ var import_react19 = require("react");
31540
31540
  // src/lib/backup/dropbox/auth.ts
31541
31541
  init_logger();
31542
31542
 
31543
+ // src/lib/auth/tokenRows.ts
31544
+ var ENCRYPTED_PREFIX = "enc:oauth:";
31545
+ function parsePlaintextToken(raw, walletAddress) {
31546
+ let parsed;
31547
+ try {
31548
+ parsed = JSON.parse(raw);
31549
+ } catch {
31550
+ return null;
31551
+ }
31552
+ if (!parsed || typeof parsed !== "object") return null;
31553
+ const record = parsed;
31554
+ const data = record.token?.accessToken ? record.token : record;
31555
+ if (!data?.accessToken) return null;
31556
+ if (record.wallet && record.wallet !== walletAddress) return null;
31557
+ return data;
31558
+ }
31559
+ function promoteUnscopedRow(raw, data, store, scopedKey, unscopedKey, walletAddress) {
31560
+ const row = raw.startsWith(ENCRYPTED_PREFIX) ? raw : JSON.stringify({ wallet: walletAddress, token: data });
31561
+ if (!store.getItem(scopedKey)) {
31562
+ store.setItem(scopedKey, row);
31563
+ }
31564
+ store.removeItem(unscopedKey);
31565
+ }
31566
+ function readPlaintextToken(scopedKey, unscopedKey, walletAddress) {
31567
+ if (typeof window === "undefined") return null;
31568
+ const keys = scopedKey === unscopedKey ? [scopedKey] : [scopedKey, unscopedKey];
31569
+ for (const key of keys) {
31570
+ const candidates = [
31571
+ [localStorage.getItem(key), localStorage],
31572
+ [sessionStorage.getItem(key), sessionStorage]
31573
+ ];
31574
+ for (const [raw, store] of candidates) {
31575
+ if (!raw) continue;
31576
+ const data = parsePlaintextToken(raw, walletAddress);
31577
+ if (!data) continue;
31578
+ if (walletAddress && key === unscopedKey) {
31579
+ promoteUnscopedRow(raw, data, store, scopedKey, unscopedKey, walletAddress);
31580
+ }
31581
+ return data;
31582
+ }
31583
+ }
31584
+ return null;
31585
+ }
31586
+
31543
31587
  // src/lib/backup/oauth/storage.ts
31544
31588
  init_logger();
31545
31589
  var STORAGE_KEY_PREFIX = "oauth_token_";
31546
- var ENCRYPTED_PREFIX = "enc:oauth:";
31547
- function getStorageKey(provider) {
31548
- return `${STORAGE_KEY_PREFIX}${provider}`;
31590
+ var ENCRYPTED_PREFIX2 = "enc:oauth:";
31591
+ function getStorageKey(provider, walletAddress) {
31592
+ const base = `${STORAGE_KEY_PREFIX}${provider}`;
31593
+ return walletAddress ? `${base}:${walletAddress}` : base;
31594
+ }
31595
+ function readRawRows(keys) {
31596
+ const rows = [];
31597
+ for (const key of keys) {
31598
+ for (const store of [localStorage, sessionStorage]) {
31599
+ const raw = store.getItem(key);
31600
+ if (raw) rows.push({ raw, key, store });
31601
+ }
31602
+ }
31603
+ return rows;
31549
31604
  }
31550
31605
  async function getStoredTokenData(provider, walletAddress) {
31551
31606
  if (typeof window === "undefined") return null;
31552
31607
  try {
31553
- const key = getStorageKey(provider);
31554
- let stored = localStorage.getItem(key);
31555
- if (!stored) {
31556
- stored = sessionStorage.getItem(key);
31557
- }
31558
- if (!stored) return null;
31559
- if (stored.startsWith(ENCRYPTED_PREFIX)) {
31560
- if (!walletAddress) {
31561
- getLogger().warn(
31562
- `Encrypted OAuth token found for ${provider} but no wallet address provided`
31563
- );
31564
- return null;
31565
- }
31566
- try {
31567
- const encryptedData = stored.slice(ENCRYPTED_PREFIX.length);
31568
- let decryptedJson;
31569
- try {
31570
- decryptedJson = await decryptData(encryptedData, walletAddress);
31571
- } catch {
31572
- decryptedJson = await decryptData(encryptedData, walletAddress, "v2");
31573
- }
31574
- const data2 = JSON.parse(decryptedJson);
31575
- if (!data2.accessToken) return null;
31576
- return data2;
31577
- } catch (error) {
31578
- getLogger().error(`Failed to decrypt OAuth token for ${provider}:`, error);
31579
- return null;
31608
+ const scopedKey = getStorageKey(provider, walletAddress);
31609
+ const legacyKey = getStorageKey(provider);
31610
+ const keys = scopedKey === legacyKey ? [legacyKey] : [scopedKey, legacyKey];
31611
+ for (const row of readRawRows(keys)) {
31612
+ const data = await readTokenRow(row.raw, provider, walletAddress);
31613
+ if (!data) continue;
31614
+ if (walletAddress && row.key === legacyKey) {
31615
+ promoteUnscopedRow(row.raw, data, row.store, scopedKey, legacyKey, walletAddress);
31580
31616
  }
31617
+ return data;
31581
31618
  }
31582
- const data = JSON.parse(stored);
31583
- if (!data.accessToken) return null;
31584
- return data;
31619
+ return null;
31585
31620
  } catch {
31586
31621
  return null;
31587
31622
  }
31588
31623
  }
31624
+ async function readTokenRow(stored, provider, walletAddress) {
31625
+ if (stored.startsWith(ENCRYPTED_PREFIX2)) {
31626
+ if (!walletAddress) {
31627
+ getLogger().warn(
31628
+ `Encrypted OAuth token found for ${provider} but no wallet address provided`
31629
+ );
31630
+ return null;
31631
+ }
31632
+ try {
31633
+ const encryptedData = stored.slice(ENCRYPTED_PREFIX2.length);
31634
+ let decryptedJson;
31635
+ try {
31636
+ decryptedJson = await decryptData(encryptedData, walletAddress);
31637
+ } catch {
31638
+ decryptedJson = await decryptData(encryptedData, walletAddress, "v2");
31639
+ }
31640
+ const data = JSON.parse(decryptedJson);
31641
+ if (!data.accessToken) return null;
31642
+ return data;
31643
+ } catch (error) {
31644
+ getLogger().error(`Failed to decrypt OAuth token for ${provider}:`, error);
31645
+ return null;
31646
+ }
31647
+ }
31648
+ return parsePlaintextToken(stored, walletAddress);
31649
+ }
31589
31650
  async function storeTokenData(provider, data, walletAddress) {
31590
31651
  if (typeof window === "undefined") return;
31591
- const key = getStorageKey(provider);
31652
+ const key = getStorageKey(provider, walletAddress);
31592
31653
  const json4 = JSON.stringify(data);
31654
+ const plaintextRecord = JSON.stringify({
31655
+ wallet: walletAddress,
31656
+ token: data
31657
+ });
31593
31658
  if (walletAddress) {
31594
31659
  try {
31595
31660
  const encrypted = await encryptData(json4, walletAddress);
31596
- localStorage.setItem(key, `${ENCRYPTED_PREFIX}${encrypted}`);
31661
+ localStorage.setItem(key, `${ENCRYPTED_PREFIX2}${encrypted}`);
31662
+ sessionStorage.removeItem(key);
31597
31663
  } catch (error) {
31598
31664
  getLogger().warn(
31599
31665
  `Failed to encrypt OAuth token for ${provider}, storing temporarily:`,
31600
31666
  error
31601
31667
  );
31602
- sessionStorage.setItem(key, json4);
31668
+ sessionStorage.setItem(key, plaintextRecord);
31603
31669
  throw new Error(
31604
31670
  `OAuth token encryption failed: ${error instanceof Error ? error.message : String(error)}`
31605
31671
  );
31606
31672
  }
31607
31673
  } else {
31608
- sessionStorage.setItem(key, json4);
31674
+ sessionStorage.setItem(key, plaintextRecord);
31609
31675
  }
31610
31676
  }
31611
- function clearTokenData(provider) {
31677
+ function clearTokenData(provider, walletAddress) {
31612
31678
  if (typeof window === "undefined") return;
31613
- const key = getStorageKey(provider);
31614
- localStorage.removeItem(key);
31615
- sessionStorage.removeItem(key);
31679
+ const base = getStorageKey(provider);
31680
+ const keys = walletAddress ? [getStorageKey(provider, walletAddress), base] : [base];
31681
+ if (!walletAddress) keys.push(...suffixedKeys(base));
31682
+ for (const key of keys) {
31683
+ localStorage.removeItem(key);
31684
+ sessionStorage.removeItem(key);
31685
+ }
31686
+ }
31687
+ function suffixedKeys(base) {
31688
+ const found = [];
31689
+ for (const storage of [localStorage, sessionStorage]) {
31690
+ for (let i = 0; i < storage.length; i++) {
31691
+ const key = storage.key(i);
31692
+ if (key && key.startsWith(`${base}:`) && !found.includes(key)) found.push(key);
31693
+ }
31694
+ }
31695
+ return found;
31616
31696
  }
31617
31697
  function isTokenExpired(data, bufferSeconds = 60) {
31618
31698
  if (!data) return true;
@@ -31636,22 +31716,40 @@ async function getRefreshToken(provider, walletAddress) {
31636
31716
  async function migrateUnencryptedTokens(provider, walletAddress) {
31637
31717
  if (typeof window === "undefined") return false;
31638
31718
  try {
31639
- const key = getStorageKey(provider);
31640
- const localStored = localStorage.getItem(key);
31641
- const sessionStored = sessionStorage.getItem(key);
31642
- const isEncrypted2 = (value) => !!value && value.startsWith(ENCRYPTED_PREFIX);
31643
- const tokenToMigrate = sessionStored && !isEncrypted2(sessionStored) ? sessionStored : localStored && !isEncrypted2(localStored) ? localStored : null;
31719
+ const scopedKey = getStorageKey(provider, walletAddress);
31720
+ const legacyKey = getStorageKey(provider);
31721
+ const keys = [scopedKey, legacyKey];
31722
+ const isEncrypted2 = (value) => !!value && value.startsWith(ENCRYPTED_PREFIX2);
31723
+ const plaintextAt = (key) => {
31724
+ const stored = sessionStorage.getItem(key) ?? localStorage.getItem(key);
31725
+ return stored && !isEncrypted2(stored) ? stored : null;
31726
+ };
31727
+ let tokenToMigrate = null;
31728
+ for (const key of keys) {
31729
+ const value = plaintextAt(key);
31730
+ if (value) {
31731
+ tokenToMigrate = value;
31732
+ break;
31733
+ }
31734
+ }
31644
31735
  if (!tokenToMigrate) {
31645
- if (isEncrypted2(localStored) && sessionStored && !isEncrypted2(sessionStored)) {
31646
- sessionStorage.removeItem(key);
31736
+ for (const key of keys) {
31737
+ const stored = sessionStorage.getItem(key);
31738
+ if (isEncrypted2(localStorage.getItem(key)) && stored && !isEncrypted2(stored)) {
31739
+ sessionStorage.removeItem(key);
31740
+ }
31647
31741
  }
31648
31742
  return false;
31649
31743
  }
31650
31744
  try {
31651
- const data = JSON.parse(tokenToMigrate);
31745
+ const data = parsePlaintextToken(tokenToMigrate, walletAddress);
31746
+ if (!data) return false;
31652
31747
  await storeTokenData(provider, data, walletAddress);
31653
- if (sessionStored && !isEncrypted2(sessionStored)) {
31654
- sessionStorage.removeItem(key);
31748
+ for (const key of keys) {
31749
+ if (plaintextAt(key)) sessionStorage.removeItem(key);
31750
+ if (key === legacyKey && !isEncrypted2(localStorage.getItem(key))) {
31751
+ localStorage.removeItem(key);
31752
+ }
31655
31753
  }
31656
31754
  return true;
31657
31755
  } catch (error) {
@@ -31820,7 +31918,7 @@ async function refreshDropboxToken(apiClient, walletAddress) {
31820
31918
  await storeTokenData(PROVIDER, tokenData, walletAddress);
31821
31919
  return response.data.access_token;
31822
31920
  } catch {
31823
- clearTokenData(PROVIDER);
31921
+ clearTokenData(PROVIDER, walletAddress);
31824
31922
  return null;
31825
31923
  }
31826
31924
  }
@@ -31836,7 +31934,7 @@ async function revokeDropboxToken(apiClient, walletAddress) {
31836
31934
  });
31837
31935
  } catch {
31838
31936
  } finally {
31839
- clearTokenData(PROVIDER);
31937
+ clearTokenData(PROVIDER, walletAddress);
31840
31938
  }
31841
31939
  }
31842
31940
  async function getDropboxAccessToken(apiClient, walletAddress) {
@@ -32440,7 +32538,7 @@ async function refreshGoogleDriveToken(apiClient, walletAddress) {
32440
32538
  await storeTokenData(PROVIDER2, tokenData, walletAddress);
32441
32539
  return response.data.access_token;
32442
32540
  } catch {
32443
- clearTokenData(PROVIDER2);
32541
+ clearTokenData(PROVIDER2, walletAddress);
32444
32542
  return null;
32445
32543
  }
32446
32544
  }
@@ -32456,7 +32554,7 @@ async function revokeGoogleDriveToken(apiClient, walletAddress) {
32456
32554
  });
32457
32555
  } catch {
32458
32556
  } finally {
32459
- clearTokenData(PROVIDER2);
32557
+ clearTokenData(PROVIDER2, walletAddress);
32460
32558
  }
32461
32559
  }
32462
32560
  async function getGoogleDriveAccessToken(apiClient, walletAddress) {
@@ -34090,7 +34188,7 @@ var CODE_STORAGE_KEY2 = "google_calendar_oauth_state";
34090
34188
  var TOKEN_STORAGE_KEY = "oauth_token_google-calendar";
34091
34189
  var RETURN_URL_KEY = "google_calendar_return_url";
34092
34190
  var PENDING_MESSAGE_KEY = "google_calendar_pending_message";
34093
- var ENCRYPTED_PREFIX2 = "enc:oauth:";
34191
+ var ENCRYPTED_PREFIX3 = "enc:oauth:";
34094
34192
  var cachedAccessToken = null;
34095
34193
  var cachedExpiresAt = null;
34096
34194
  var cachedRefreshToken = null;
@@ -34121,9 +34219,9 @@ async function getStoredTokenData2(walletAddress) {
34121
34219
  }
34122
34220
  try {
34123
34221
  const scopedStored = localStorage.getItem(getTokenStorageKey(walletAddress));
34124
- if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX2) && walletAddress && hasEncryptionKey(walletAddress)) {
34222
+ if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX3) && walletAddress && hasEncryptionKey(walletAddress)) {
34125
34223
  try {
34126
- const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX2.length);
34224
+ const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX3.length);
34127
34225
  const cryptoKey = await getEncryptionKey(walletAddress);
34128
34226
  const decryptedJson = await decryptDataWithKey(encryptedData, cryptoKey);
34129
34227
  const data = JSON.parse(decryptedJson);
@@ -34138,33 +34236,18 @@ async function getStoredTokenData2(walletAddress) {
34138
34236
  getLogger().error("Failed to decrypt Calendar OAuth token:", error);
34139
34237
  }
34140
34238
  }
34141
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY);
34142
- if (legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX2)) {
34143
- try {
34144
- const data = JSON.parse(legacyStored);
34145
- if (data.accessToken) {
34146
- cachedAccessToken = data.accessToken;
34147
- cachedExpiresAt = data.expiresAt ?? null;
34148
- cachedRefreshToken = data.refreshToken ?? null;
34149
- cachedWalletAddress = walletAddress ?? null;
34150
- return data;
34151
- }
34152
- } catch {
34153
- }
34154
- }
34155
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY);
34156
- if (sessionStored) {
34157
- try {
34158
- const data = JSON.parse(sessionStored);
34159
- if (!data.accessToken) return null;
34160
- cachedAccessToken = data.accessToken;
34161
- cachedExpiresAt = data.expiresAt ?? null;
34162
- cachedRefreshToken = data.refreshToken ?? null;
34163
- cachedScope = data.scope ?? null;
34164
- cachedWalletAddress = walletAddress ?? null;
34165
- return data;
34166
- } catch {
34167
- }
34239
+ const plaintext = readPlaintextToken(
34240
+ getTokenStorageKey(walletAddress),
34241
+ TOKEN_STORAGE_KEY,
34242
+ walletAddress
34243
+ );
34244
+ if (plaintext) {
34245
+ cachedAccessToken = plaintext.accessToken;
34246
+ cachedExpiresAt = plaintext.expiresAt ?? null;
34247
+ cachedRefreshToken = plaintext.refreshToken ?? null;
34248
+ cachedScope = plaintext.scope ?? null;
34249
+ cachedWalletAddress = walletAddress ?? null;
34250
+ return plaintext;
34168
34251
  }
34169
34252
  return null;
34170
34253
  } catch {
@@ -34179,16 +34262,19 @@ async function storeTokenData2(data, walletAddress) {
34179
34262
  cachedScope = data.scope ?? null;
34180
34263
  cachedWalletAddress = walletAddress ?? null;
34181
34264
  const json4 = JSON.stringify(data);
34182
- sessionStorage.setItem(TOKEN_STORAGE_KEY, json4);
34183
34265
  if (walletAddress && hasEncryptionKey(walletAddress)) {
34184
34266
  try {
34185
34267
  const cryptoKey = await getEncryptionKey(walletAddress);
34186
34268
  const encrypted = await encryptDataWithKey(json4, cryptoKey);
34187
- localStorage.setItem(getTokenStorageKey(walletAddress), ENCRYPTED_PREFIX2 + encrypted);
34269
+ localStorage.setItem(getTokenStorageKey(walletAddress), ENCRYPTED_PREFIX3 + encrypted);
34270
+ sessionStorage.removeItem(getTokenStorageKey(walletAddress));
34271
+ return;
34188
34272
  } catch (error) {
34189
34273
  getLogger().warn("Failed to encrypt Calendar OAuth token:", error);
34190
34274
  }
34191
34275
  }
34276
+ const record = { wallet: walletAddress, token: data };
34277
+ sessionStorage.setItem(getTokenStorageKey(walletAddress), JSON.stringify(record));
34192
34278
  }
34193
34279
  function clearCalendarToken(walletAddress) {
34194
34280
  if (typeof window === "undefined") return;
@@ -34202,6 +34288,7 @@ function clearCalendarToken(walletAddress) {
34202
34288
  localStorage.removeItem(TOKEN_STORAGE_KEY);
34203
34289
  }
34204
34290
  sessionStorage.removeItem(TOKEN_STORAGE_KEY);
34291
+ sessionStorage.removeItem(getTokenStorageKey(walletAddress));
34205
34292
  }
34206
34293
  function isTokenExpired2(data, bufferSeconds = 60) {
34207
34294
  if (!data) return true;
@@ -34383,24 +34470,38 @@ async function migrateCalendarToken(walletAddress) {
34383
34470
  if (typeof window === "undefined") return false;
34384
34471
  if (!walletAddress || !hasEncryptionKey(walletAddress)) return false;
34385
34472
  try {
34386
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY);
34387
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY);
34388
- const isLegacyUnencrypted = legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX2);
34389
- const unencryptedJson = sessionStored || (isLegacyUnencrypted ? legacyStored : null);
34390
- if (!unencryptedJson) return false;
34391
34473
  const scopedKey = getTokenStorageKey(walletAddress);
34474
+ const sources = [
34475
+ { key: scopedKey, store: sessionStorage },
34476
+ // A read moves a legacy localStorage row here as plain text.
34477
+ { key: scopedKey, store: localStorage },
34478
+ { key: TOKEN_STORAGE_KEY, store: sessionStorage },
34479
+ { key: TOKEN_STORAGE_KEY, store: localStorage }
34480
+ ];
34481
+ let used = null;
34482
+ let unencryptedJson = "";
34483
+ for (const source of sources) {
34484
+ const value = source.store.getItem(source.key);
34485
+ if (value && !value.startsWith(ENCRYPTED_PREFIX3)) {
34486
+ unencryptedJson = value;
34487
+ used = source;
34488
+ break;
34489
+ }
34490
+ }
34491
+ if (!used) return false;
34392
34492
  const existingEncrypted = localStorage.getItem(scopedKey);
34393
- if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX2)) {
34394
- sessionStorage.removeItem(TOKEN_STORAGE_KEY);
34395
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY);
34493
+ if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX3)) {
34494
+ used.store.removeItem(used.key);
34396
34495
  return true;
34397
34496
  }
34398
- const data = JSON.parse(unencryptedJson);
34497
+ const data = parsePlaintextToken(unencryptedJson, walletAddress);
34498
+ if (!data) return false;
34399
34499
  await storeTokenData2(data, walletAddress);
34400
34500
  const migrated = localStorage.getItem(scopedKey);
34401
- if (!migrated?.startsWith(ENCRYPTED_PREFIX2)) return false;
34402
- sessionStorage.removeItem(TOKEN_STORAGE_KEY);
34403
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY);
34501
+ if (!migrated?.startsWith(ENCRYPTED_PREFIX3)) return false;
34502
+ if (used.store !== localStorage || used.key !== scopedKey) {
34503
+ used.store.removeItem(used.key);
34504
+ }
34404
34505
  return true;
34405
34506
  } catch {
34406
34507
  return false;
@@ -34414,7 +34515,7 @@ var CODE_STORAGE_KEY3 = "google_drive_oauth_state";
34414
34515
  var TOKEN_STORAGE_KEY2 = "oauth_token_google-drive-full";
34415
34516
  var RETURN_URL_KEY2 = "google_drive_return_url";
34416
34517
  var PENDING_MESSAGE_KEY2 = "google_drive_pending_message";
34417
- var ENCRYPTED_PREFIX3 = "enc:oauth:";
34518
+ var ENCRYPTED_PREFIX4 = "enc:oauth:";
34418
34519
  var cachedAccessToken2 = null;
34419
34520
  var cachedExpiresAt2 = null;
34420
34521
  var cachedRefreshToken2 = null;
@@ -34445,9 +34546,9 @@ async function getStoredTokenData3(walletAddress) {
34445
34546
  }
34446
34547
  try {
34447
34548
  const scopedStored = localStorage.getItem(getTokenStorageKey2(walletAddress));
34448
- if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX3) && walletAddress && hasEncryptionKey(walletAddress)) {
34549
+ if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX4) && walletAddress && hasEncryptionKey(walletAddress)) {
34449
34550
  try {
34450
- const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX3.length);
34551
+ const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX4.length);
34451
34552
  const cryptoKey = await getEncryptionKey(walletAddress);
34452
34553
  const decryptedJson = await decryptDataWithKey(encryptedData, cryptoKey);
34453
34554
  const data = JSON.parse(decryptedJson);
@@ -34462,33 +34563,18 @@ async function getStoredTokenData3(walletAddress) {
34462
34563
  getLogger().error("Failed to decrypt Drive OAuth token:", error);
34463
34564
  }
34464
34565
  }
34465
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY2);
34466
- if (legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX3)) {
34467
- try {
34468
- const data = JSON.parse(legacyStored);
34469
- if (data.accessToken) {
34470
- cachedAccessToken2 = data.accessToken;
34471
- cachedExpiresAt2 = data.expiresAt ?? null;
34472
- cachedRefreshToken2 = data.refreshToken ?? null;
34473
- cachedWalletAddress2 = walletAddress ?? null;
34474
- return data;
34475
- }
34476
- } catch {
34477
- }
34478
- }
34479
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY2);
34480
- if (sessionStored) {
34481
- try {
34482
- const data = JSON.parse(sessionStored);
34483
- if (!data.accessToken) return null;
34484
- cachedAccessToken2 = data.accessToken;
34485
- cachedExpiresAt2 = data.expiresAt ?? null;
34486
- cachedRefreshToken2 = data.refreshToken ?? null;
34487
- cachedScope2 = data.scope ?? null;
34488
- cachedWalletAddress2 = walletAddress ?? null;
34489
- return data;
34490
- } catch {
34491
- }
34566
+ const plaintext = readPlaintextToken(
34567
+ getTokenStorageKey2(walletAddress),
34568
+ TOKEN_STORAGE_KEY2,
34569
+ walletAddress
34570
+ );
34571
+ if (plaintext) {
34572
+ cachedAccessToken2 = plaintext.accessToken;
34573
+ cachedExpiresAt2 = plaintext.expiresAt ?? null;
34574
+ cachedRefreshToken2 = plaintext.refreshToken ?? null;
34575
+ cachedScope2 = plaintext.scope ?? null;
34576
+ cachedWalletAddress2 = walletAddress ?? null;
34577
+ return plaintext;
34492
34578
  }
34493
34579
  return null;
34494
34580
  } catch {
@@ -34503,16 +34589,19 @@ async function storeTokenData3(data, walletAddress) {
34503
34589
  cachedScope2 = data.scope ?? null;
34504
34590
  cachedWalletAddress2 = walletAddress ?? null;
34505
34591
  const json4 = JSON.stringify(data);
34506
- sessionStorage.setItem(TOKEN_STORAGE_KEY2, json4);
34507
34592
  if (walletAddress && hasEncryptionKey(walletAddress)) {
34508
34593
  try {
34509
34594
  const cryptoKey = await getEncryptionKey(walletAddress);
34510
34595
  const encrypted = await encryptDataWithKey(json4, cryptoKey);
34511
- localStorage.setItem(getTokenStorageKey2(walletAddress), `${ENCRYPTED_PREFIX3}${encrypted}`);
34596
+ localStorage.setItem(getTokenStorageKey2(walletAddress), `${ENCRYPTED_PREFIX4}${encrypted}`);
34597
+ sessionStorage.removeItem(getTokenStorageKey2(walletAddress));
34598
+ return;
34512
34599
  } catch (error) {
34513
34600
  getLogger().warn("Failed to encrypt Drive OAuth token:", error);
34514
34601
  }
34515
34602
  }
34603
+ const record = { wallet: walletAddress, token: data };
34604
+ sessionStorage.setItem(getTokenStorageKey2(walletAddress), JSON.stringify(record));
34516
34605
  }
34517
34606
  function clearDriveToken(walletAddress) {
34518
34607
  if (typeof window === "undefined") return;
@@ -34526,6 +34615,7 @@ function clearDriveToken(walletAddress) {
34526
34615
  localStorage.removeItem(TOKEN_STORAGE_KEY2);
34527
34616
  }
34528
34617
  sessionStorage.removeItem(TOKEN_STORAGE_KEY2);
34618
+ sessionStorage.removeItem(getTokenStorageKey2(walletAddress));
34529
34619
  }
34530
34620
  function isTokenExpired3(data, bufferSeconds = 60) {
34531
34621
  if (!data) return true;
@@ -34707,24 +34797,38 @@ async function migrateDriveToken(walletAddress) {
34707
34797
  if (typeof window === "undefined") return false;
34708
34798
  if (!walletAddress || !hasEncryptionKey(walletAddress)) return false;
34709
34799
  try {
34710
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY2);
34711
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY2);
34712
- const isLegacyUnencrypted = legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX3);
34713
- const unencryptedJson = sessionStored || (isLegacyUnencrypted ? legacyStored : null);
34714
- if (!unencryptedJson) return false;
34715
34800
  const scopedKey = getTokenStorageKey2(walletAddress);
34801
+ const sources = [
34802
+ { key: scopedKey, store: sessionStorage },
34803
+ // A read moves a legacy localStorage row here as plain text.
34804
+ { key: scopedKey, store: localStorage },
34805
+ { key: TOKEN_STORAGE_KEY2, store: sessionStorage },
34806
+ { key: TOKEN_STORAGE_KEY2, store: localStorage }
34807
+ ];
34808
+ let used = null;
34809
+ let unencryptedJson = "";
34810
+ for (const source of sources) {
34811
+ const value = source.store.getItem(source.key);
34812
+ if (value && !value.startsWith(ENCRYPTED_PREFIX4)) {
34813
+ unencryptedJson = value;
34814
+ used = source;
34815
+ break;
34816
+ }
34817
+ }
34818
+ if (!used) return false;
34716
34819
  const existingEncrypted = localStorage.getItem(scopedKey);
34717
- if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX3)) {
34718
- sessionStorage.removeItem(TOKEN_STORAGE_KEY2);
34719
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY2);
34820
+ if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX4)) {
34821
+ used.store.removeItem(used.key);
34720
34822
  return true;
34721
34823
  }
34722
- const data = JSON.parse(unencryptedJson);
34824
+ const data = parsePlaintextToken(unencryptedJson, walletAddress);
34825
+ if (!data) return false;
34723
34826
  await storeTokenData3(data, walletAddress);
34724
34827
  const migrated = localStorage.getItem(scopedKey);
34725
- if (!migrated?.startsWith(ENCRYPTED_PREFIX3)) return false;
34726
- sessionStorage.removeItem(TOKEN_STORAGE_KEY2);
34727
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY2);
34828
+ if (!migrated?.startsWith(ENCRYPTED_PREFIX4)) return false;
34829
+ if (used.store !== localStorage || used.key !== scopedKey) {
34830
+ used.store.removeItem(used.key);
34831
+ }
34728
34832
  return true;
34729
34833
  } catch {
34730
34834
  return false;
@@ -34757,7 +34861,7 @@ var FALLBACK_OAUTH_AUTHORIZE = "https://api.notion.com/v1/oauth/authorize";
34757
34861
  var FALLBACK_OAUTH_TOKEN = "https://api.notion.com/v1/oauth/token";
34758
34862
  var FALLBACK_REGISTRATION = "https://api.notion.com/v1/oauth/register";
34759
34863
  var DEFAULT_TOKEN_EXPIRY_SECONDS = 8 * 3600;
34760
- var ENCRYPTED_PREFIX4 = "enc:oauth:";
34864
+ var ENCRYPTED_PREFIX5 = "enc:oauth:";
34761
34865
  var cachedAccessToken3 = null;
34762
34866
  var cachedExpiresAt3 = null;
34763
34867
  var cachedWalletAddress3 = null;
@@ -34895,12 +34999,14 @@ async function storeTokenData4(data, walletAddress) {
34895
34999
  try {
34896
35000
  const cryptoKey = await getEncryptionKey(walletAddress);
34897
35001
  const encrypted = await encryptDataWithKey(json4, cryptoKey);
34898
- localStorage.setItem(getTokenStorageKey3(walletAddress), `${ENCRYPTED_PREFIX4}${encrypted}`);
35002
+ localStorage.setItem(getTokenStorageKey3(walletAddress), `${ENCRYPTED_PREFIX5}${encrypted}`);
35003
+ sessionStorage.removeItem(getTokenStorageKey3(walletAddress));
34899
35004
  return;
34900
35005
  } catch {
34901
35006
  }
34902
35007
  }
34903
- sessionStorage.setItem(TOKEN_STORAGE_KEY3, json4);
35008
+ const record = { wallet: walletAddress, token: data };
35009
+ sessionStorage.setItem(getTokenStorageKey3(walletAddress), JSON.stringify(record));
34904
35010
  }
34905
35011
  async function getStoredTokenData4(walletAddress) {
34906
35012
  if (typeof window === "undefined") return null;
@@ -34917,11 +35023,11 @@ async function getStoredTokenData4(walletAddress) {
34917
35023
  }
34918
35024
  try {
34919
35025
  const stored = localStorage.getItem(getTokenStorageKey3(walletAddress));
34920
- if (stored?.startsWith(ENCRYPTED_PREFIX4)) {
35026
+ if (stored?.startsWith(ENCRYPTED_PREFIX5)) {
34921
35027
  if (walletAddress && hasEncryptionKey(walletAddress)) {
34922
35028
  try {
34923
35029
  const cryptoKey = await getEncryptionKey(walletAddress);
34924
- const encrypted = stored.slice(ENCRYPTED_PREFIX4.length);
35030
+ const encrypted = stored.slice(ENCRYPTED_PREFIX5.length);
34925
35031
  const decrypted = await decryptDataWithKey(encrypted, cryptoKey);
34926
35032
  const data = JSON.parse(decrypted);
34927
35033
  if (data.accessToken) return data;
@@ -34929,12 +35035,12 @@ async function getStoredTokenData4(walletAddress) {
34929
35035
  }
34930
35036
  }
34931
35037
  }
34932
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY3);
34933
- if (sessionStored) {
34934
- const data = JSON.parse(sessionStored);
34935
- if (!data.accessToken) return null;
34936
- return data;
34937
- }
35038
+ const plaintext = readPlaintextToken(
35039
+ getTokenStorageKey3(walletAddress),
35040
+ TOKEN_STORAGE_KEY3,
35041
+ walletAddress
35042
+ );
35043
+ if (plaintext) return plaintext;
34938
35044
  return null;
34939
35045
  } catch {
34940
35046
  return null;
@@ -34944,6 +35050,7 @@ function clearNotionToken(walletAddress) {
34944
35050
  if (typeof window === "undefined") return;
34945
35051
  localStorage.removeItem(getTokenStorageKey3(walletAddress));
34946
35052
  sessionStorage.removeItem(TOKEN_STORAGE_KEY3);
35053
+ sessionStorage.removeItem(getTokenStorageKey3(walletAddress));
34947
35054
  cachedAccessToken3 = null;
34948
35055
  cachedExpiresAt3 = null;
34949
35056
  cachedWalletAddress3 = null;
@@ -34959,20 +35066,34 @@ async function migrateNotionToken(walletAddress) {
34959
35066
  if (typeof window === "undefined") return false;
34960
35067
  if (!walletAddress || !hasEncryptionKey(walletAddress)) return false;
34961
35068
  try {
34962
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY3);
34963
- if (!sessionStored) return false;
34964
35069
  const scopedKey = getTokenStorageKey3(walletAddress);
35070
+ const sources = [
35071
+ { key: scopedKey, store: sessionStorage },
35072
+ { key: TOKEN_STORAGE_KEY3, store: sessionStorage }
35073
+ ];
35074
+ let used = null;
35075
+ let unencryptedJson = "";
35076
+ for (const source of sources) {
35077
+ const value = source.store.getItem(source.key);
35078
+ if (value && !value.startsWith(ENCRYPTED_PREFIX5)) {
35079
+ unencryptedJson = value;
35080
+ used = source;
35081
+ break;
35082
+ }
35083
+ }
35084
+ if (!used) return false;
34965
35085
  const localStored = localStorage.getItem(scopedKey);
34966
- if (localStored?.startsWith(ENCRYPTED_PREFIX4)) {
35086
+ if (localStored?.startsWith(ENCRYPTED_PREFIX5)) {
34967
35087
  localStorage.removeItem(scopedKey);
34968
35088
  }
34969
- const data = JSON.parse(sessionStored);
35089
+ const data = parsePlaintextToken(unencryptedJson, walletAddress);
35090
+ if (!data) return false;
34970
35091
  await storeTokenData4(data, walletAddress);
34971
35092
  const migrated = localStorage.getItem(scopedKey);
34972
- if (!migrated?.startsWith(ENCRYPTED_PREFIX4)) {
35093
+ if (!migrated?.startsWith(ENCRYPTED_PREFIX5)) {
34973
35094
  return false;
34974
35095
  }
34975
- sessionStorage.removeItem(TOKEN_STORAGE_KEY3);
35096
+ used.store.removeItem(used.key);
34976
35097
  return true;
34977
35098
  } catch {
34978
35099
  return false;
@@ -34984,12 +35105,12 @@ async function migrateNotionClientRegistration(walletAddress) {
34984
35105
  try {
34985
35106
  const sessionStored = sessionStorage.getItem(CLIENT_REGISTRATION_KEY);
34986
35107
  const legacyStored = localStorage.getItem(CLIENT_REGISTRATION_KEY);
34987
- const isLegacyUnencrypted = legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX4);
35108
+ const isLegacyUnencrypted = legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX5);
34988
35109
  const unencryptedJson = sessionStored || (isLegacyUnencrypted ? legacyStored : null);
34989
35110
  if (!unencryptedJson) return false;
34990
35111
  const scopedKey = getClientRegistrationStorageKey(walletAddress);
34991
35112
  const existingEncrypted = localStorage.getItem(scopedKey);
34992
- if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX4)) {
35113
+ if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX5)) {
34993
35114
  sessionStorage.removeItem(CLIENT_REGISTRATION_KEY);
34994
35115
  if (isLegacyUnencrypted) {
34995
35116
  localStorage.removeItem(CLIENT_REGISTRATION_KEY);
@@ -34999,7 +35120,7 @@ async function migrateNotionClientRegistration(walletAddress) {
34999
35120
  const data = JSON.parse(unencryptedJson);
35000
35121
  await storeClientRegistration(data, walletAddress);
35001
35122
  const migrated = localStorage.getItem(scopedKey);
35002
- if (!migrated?.startsWith(ENCRYPTED_PREFIX4)) {
35123
+ if (!migrated?.startsWith(ENCRYPTED_PREFIX5)) {
35003
35124
  return false;
35004
35125
  }
35005
35126
  sessionStorage.removeItem(CLIENT_REGISTRATION_KEY);
@@ -35016,11 +35137,11 @@ async function getClientRegistration(walletAddress) {
35016
35137
  try {
35017
35138
  const scopedKey = getClientRegistrationStorageKey(walletAddress);
35018
35139
  const stored = localStorage.getItem(scopedKey);
35019
- if (stored?.startsWith(ENCRYPTED_PREFIX4)) {
35140
+ if (stored?.startsWith(ENCRYPTED_PREFIX5)) {
35020
35141
  if (walletAddress && hasEncryptionKey(walletAddress)) {
35021
35142
  try {
35022
35143
  const cryptoKey = await getEncryptionKey(walletAddress);
35023
- const encrypted = stored.slice(ENCRYPTED_PREFIX4.length);
35144
+ const encrypted = stored.slice(ENCRYPTED_PREFIX5.length);
35024
35145
  const decrypted = await decryptDataWithKey(encrypted, cryptoKey);
35025
35146
  const data = JSON.parse(decrypted);
35026
35147
  if (data.clientId) {
@@ -35030,7 +35151,7 @@ async function getClientRegistration(walletAddress) {
35030
35151
  }
35031
35152
  }
35032
35153
  }
35033
- if (stored && !stored.startsWith(ENCRYPTED_PREFIX4)) {
35154
+ if (stored && !stored.startsWith(ENCRYPTED_PREFIX5)) {
35034
35155
  const data = JSON.parse(stored);
35035
35156
  if (!data.clientId) return null;
35036
35157
  return data;
@@ -35055,7 +35176,7 @@ async function storeClientRegistration(registration, walletAddress) {
35055
35176
  const cryptoKey = await getEncryptionKey(walletAddress);
35056
35177
  const encrypted = await encryptDataWithKey(json4, cryptoKey);
35057
35178
  const scopedKey = getClientRegistrationStorageKey(walletAddress);
35058
- localStorage.setItem(scopedKey, `${ENCRYPTED_PREFIX4}${encrypted}`);
35179
+ localStorage.setItem(scopedKey, `${ENCRYPTED_PREFIX5}${encrypted}`);
35059
35180
  } catch {
35060
35181
  }
35061
35182
  }
@@ -35288,7 +35409,7 @@ var CODE_STORAGE_KEY4 = "github_oauth_state";
35288
35409
  var TOKEN_STORAGE_KEY4 = "oauth_token_github";
35289
35410
  var RETURN_URL_KEY4 = "github_return_url";
35290
35411
  var PENDING_MESSAGE_KEY4 = "github_pending_message";
35291
- var ENCRYPTED_PREFIX5 = "enc:oauth:";
35412
+ var ENCRYPTED_PREFIX6 = "enc:oauth:";
35292
35413
  var cachedAccessToken4 = null;
35293
35414
  var cachedExpiresAt4 = null;
35294
35415
  var cachedRefreshToken3 = null;
@@ -35321,9 +35442,9 @@ async function getStoredTokenData5(walletAddress) {
35321
35442
  }
35322
35443
  try {
35323
35444
  const scopedStored = localStorage.getItem(getTokenStorageKey4(walletAddress));
35324
- if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX5) && walletAddress && hasEncryptionKey(walletAddress)) {
35445
+ if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX6) && walletAddress && hasEncryptionKey(walletAddress)) {
35325
35446
  try {
35326
- const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX5.length);
35447
+ const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX6.length);
35327
35448
  const cryptoKey = await getEncryptionKey(walletAddress);
35328
35449
  const decryptedJson = await decryptDataWithKey(encryptedData, cryptoKey);
35329
35450
  const data = JSON.parse(decryptedJson);
@@ -35338,34 +35459,18 @@ async function getStoredTokenData5(walletAddress) {
35338
35459
  getLogger().error("Failed to decrypt GitHub OAuth token:", error);
35339
35460
  }
35340
35461
  }
35341
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY4);
35342
- if (legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX5)) {
35343
- try {
35344
- const data = JSON.parse(legacyStored);
35345
- if (data.accessToken) {
35346
- cachedAccessToken4 = data.accessToken;
35347
- cachedExpiresAt4 = data.expiresAt ?? null;
35348
- cachedRefreshToken3 = data.refreshToken ?? null;
35349
- cachedScope3 = data.scope ?? null;
35350
- cachedWalletAddress4 = walletAddress ?? null;
35351
- return data;
35352
- }
35353
- } catch {
35354
- }
35355
- }
35356
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY4);
35357
- if (sessionStored) {
35358
- try {
35359
- const data = JSON.parse(sessionStored);
35360
- if (!data.accessToken) return null;
35361
- cachedAccessToken4 = data.accessToken;
35362
- cachedExpiresAt4 = data.expiresAt ?? null;
35363
- cachedRefreshToken3 = data.refreshToken ?? null;
35364
- cachedScope3 = data.scope ?? null;
35365
- cachedWalletAddress4 = walletAddress ?? null;
35366
- return data;
35367
- } catch {
35368
- }
35462
+ const plaintext = readPlaintextToken(
35463
+ getTokenStorageKey4(walletAddress),
35464
+ TOKEN_STORAGE_KEY4,
35465
+ walletAddress
35466
+ );
35467
+ if (plaintext) {
35468
+ cachedAccessToken4 = plaintext.accessToken;
35469
+ cachedExpiresAt4 = plaintext.expiresAt ?? null;
35470
+ cachedRefreshToken3 = plaintext.refreshToken ?? null;
35471
+ cachedScope3 = plaintext.scope ?? null;
35472
+ cachedWalletAddress4 = walletAddress ?? null;
35473
+ return plaintext;
35369
35474
  }
35370
35475
  return null;
35371
35476
  } catch {
@@ -35380,16 +35485,19 @@ async function storeTokenData5(data, walletAddress) {
35380
35485
  cachedScope3 = data.scope ?? null;
35381
35486
  cachedWalletAddress4 = walletAddress ?? null;
35382
35487
  const json4 = JSON.stringify(data);
35383
- sessionStorage.setItem(TOKEN_STORAGE_KEY4, json4);
35384
35488
  if (walletAddress && hasEncryptionKey(walletAddress)) {
35385
35489
  try {
35386
35490
  const cryptoKey = await getEncryptionKey(walletAddress);
35387
35491
  const encrypted = await encryptDataWithKey(json4, cryptoKey);
35388
- localStorage.setItem(getTokenStorageKey4(walletAddress), `${ENCRYPTED_PREFIX5}${encrypted}`);
35492
+ localStorage.setItem(getTokenStorageKey4(walletAddress), `${ENCRYPTED_PREFIX6}${encrypted}`);
35493
+ sessionStorage.removeItem(getTokenStorageKey4(walletAddress));
35494
+ return;
35389
35495
  } catch (error) {
35390
35496
  getLogger().warn("Failed to encrypt GitHub OAuth token:", error);
35391
35497
  }
35392
35498
  }
35499
+ const record = { wallet: walletAddress, token: data };
35500
+ sessionStorage.setItem(getTokenStorageKey4(walletAddress), JSON.stringify(record));
35393
35501
  }
35394
35502
  function clearGithubToken(walletAddress) {
35395
35503
  if (typeof window === "undefined") return;
@@ -35403,6 +35511,7 @@ function clearGithubToken(walletAddress) {
35403
35511
  localStorage.removeItem(TOKEN_STORAGE_KEY4);
35404
35512
  }
35405
35513
  sessionStorage.removeItem(TOKEN_STORAGE_KEY4);
35514
+ sessionStorage.removeItem(getTokenStorageKey4(walletAddress));
35406
35515
  }
35407
35516
  function isTokenExpired5(data, bufferSeconds = 60) {
35408
35517
  if (!data) return true;
@@ -35611,24 +35720,38 @@ async function migrateGithubToken(walletAddress) {
35611
35720
  if (typeof window === "undefined") return false;
35612
35721
  if (!walletAddress || !hasEncryptionKey(walletAddress)) return false;
35613
35722
  try {
35614
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY4);
35615
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY4);
35616
- const isLegacyUnencrypted = legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX5);
35617
- const unencryptedJson = sessionStored || (isLegacyUnencrypted ? legacyStored : null);
35618
- if (!unencryptedJson) return false;
35619
35723
  const scopedKey = getTokenStorageKey4(walletAddress);
35724
+ const sources = [
35725
+ { key: scopedKey, store: sessionStorage },
35726
+ // A read moves a legacy localStorage row here as plain text.
35727
+ { key: scopedKey, store: localStorage },
35728
+ { key: TOKEN_STORAGE_KEY4, store: sessionStorage },
35729
+ { key: TOKEN_STORAGE_KEY4, store: localStorage }
35730
+ ];
35731
+ let used = null;
35732
+ let unencryptedJson = "";
35733
+ for (const source of sources) {
35734
+ const value = source.store.getItem(source.key);
35735
+ if (value && !value.startsWith(ENCRYPTED_PREFIX6)) {
35736
+ unencryptedJson = value;
35737
+ used = source;
35738
+ break;
35739
+ }
35740
+ }
35741
+ if (!used) return false;
35620
35742
  const existingEncrypted = localStorage.getItem(scopedKey);
35621
- if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX5)) {
35622
- sessionStorage.removeItem(TOKEN_STORAGE_KEY4);
35623
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY4);
35743
+ if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX6)) {
35744
+ used.store.removeItem(used.key);
35624
35745
  return true;
35625
35746
  }
35626
- const data = JSON.parse(unencryptedJson);
35747
+ const data = parsePlaintextToken(unencryptedJson, walletAddress);
35748
+ if (!data) return false;
35627
35749
  await storeTokenData5(data, walletAddress);
35628
35750
  const migrated = localStorage.getItem(scopedKey);
35629
- if (!migrated?.startsWith(ENCRYPTED_PREFIX5)) return false;
35630
- sessionStorage.removeItem(TOKEN_STORAGE_KEY4);
35631
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY4);
35751
+ if (!migrated?.startsWith(ENCRYPTED_PREFIX6)) return false;
35752
+ if (used.store !== localStorage || used.key !== scopedKey) {
35753
+ used.store.removeItem(used.key);
35754
+ }
35632
35755
  return true;
35633
35756
  } catch {
35634
35757
  return false;