@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.
@@ -29570,78 +29570,158 @@ import {
29570
29570
  useState as useState16
29571
29571
  } from "react";
29572
29572
 
29573
+ // src/lib/auth/tokenRows.ts
29574
+ var ENCRYPTED_PREFIX = "enc:oauth:";
29575
+ function parsePlaintextToken(raw, walletAddress) {
29576
+ let parsed;
29577
+ try {
29578
+ parsed = JSON.parse(raw);
29579
+ } catch {
29580
+ return null;
29581
+ }
29582
+ if (!parsed || typeof parsed !== "object") return null;
29583
+ const record = parsed;
29584
+ const data = record.token?.accessToken ? record.token : record;
29585
+ if (!data?.accessToken) return null;
29586
+ if (record.wallet && record.wallet !== walletAddress) return null;
29587
+ return data;
29588
+ }
29589
+ function promoteUnscopedRow(raw, data, store, scopedKey, unscopedKey, walletAddress) {
29590
+ const row = raw.startsWith(ENCRYPTED_PREFIX) ? raw : JSON.stringify({ wallet: walletAddress, token: data });
29591
+ if (!store.getItem(scopedKey)) {
29592
+ store.setItem(scopedKey, row);
29593
+ }
29594
+ store.removeItem(unscopedKey);
29595
+ }
29596
+ function readPlaintextToken(scopedKey, unscopedKey, walletAddress) {
29597
+ if (typeof window === "undefined") return null;
29598
+ const keys = scopedKey === unscopedKey ? [scopedKey] : [scopedKey, unscopedKey];
29599
+ for (const key of keys) {
29600
+ const candidates = [
29601
+ [localStorage.getItem(key), localStorage],
29602
+ [sessionStorage.getItem(key), sessionStorage]
29603
+ ];
29604
+ for (const [raw, store] of candidates) {
29605
+ if (!raw) continue;
29606
+ const data = parsePlaintextToken(raw, walletAddress);
29607
+ if (!data) continue;
29608
+ if (walletAddress && key === unscopedKey) {
29609
+ promoteUnscopedRow(raw, data, store, scopedKey, unscopedKey, walletAddress);
29610
+ }
29611
+ return data;
29612
+ }
29613
+ }
29614
+ return null;
29615
+ }
29616
+
29573
29617
  // src/lib/backup/oauth/storage.ts
29574
29618
  var STORAGE_KEY_PREFIX = "oauth_token_";
29575
- var ENCRYPTED_PREFIX = "enc:oauth:";
29576
- function getStorageKey(provider) {
29577
- return `${STORAGE_KEY_PREFIX}${provider}`;
29619
+ var ENCRYPTED_PREFIX2 = "enc:oauth:";
29620
+ function getStorageKey(provider, walletAddress) {
29621
+ const base = `${STORAGE_KEY_PREFIX}${provider}`;
29622
+ return walletAddress ? `${base}:${walletAddress}` : base;
29623
+ }
29624
+ function readRawRows(keys) {
29625
+ const rows = [];
29626
+ for (const key of keys) {
29627
+ for (const store of [localStorage, sessionStorage]) {
29628
+ const raw = store.getItem(key);
29629
+ if (raw) rows.push({ raw, key, store });
29630
+ }
29631
+ }
29632
+ return rows;
29578
29633
  }
29579
29634
  async function getStoredTokenData(provider, walletAddress) {
29580
29635
  if (typeof window === "undefined") return null;
29581
29636
  try {
29582
- const key = getStorageKey(provider);
29583
- let stored = localStorage.getItem(key);
29584
- if (!stored) {
29585
- stored = sessionStorage.getItem(key);
29586
- }
29587
- if (!stored) return null;
29588
- if (stored.startsWith(ENCRYPTED_PREFIX)) {
29589
- if (!walletAddress) {
29590
- getLogger().warn(
29591
- `Encrypted OAuth token found for ${provider} but no wallet address provided`
29592
- );
29593
- return null;
29594
- }
29595
- try {
29596
- const encryptedData = stored.slice(ENCRYPTED_PREFIX.length);
29597
- let decryptedJson;
29598
- try {
29599
- decryptedJson = await decryptData(encryptedData, walletAddress);
29600
- } catch {
29601
- decryptedJson = await decryptData(encryptedData, walletAddress, "v2");
29602
- }
29603
- const data2 = JSON.parse(decryptedJson);
29604
- if (!data2.accessToken) return null;
29605
- return data2;
29606
- } catch (error) {
29607
- getLogger().error(`Failed to decrypt OAuth token for ${provider}:`, error);
29608
- return null;
29637
+ const scopedKey = getStorageKey(provider, walletAddress);
29638
+ const legacyKey = getStorageKey(provider);
29639
+ const keys = scopedKey === legacyKey ? [legacyKey] : [scopedKey, legacyKey];
29640
+ for (const row of readRawRows(keys)) {
29641
+ const data = await readTokenRow(row.raw, provider, walletAddress);
29642
+ if (!data) continue;
29643
+ if (walletAddress && row.key === legacyKey) {
29644
+ promoteUnscopedRow(row.raw, data, row.store, scopedKey, legacyKey, walletAddress);
29609
29645
  }
29646
+ return data;
29610
29647
  }
29611
- const data = JSON.parse(stored);
29612
- if (!data.accessToken) return null;
29613
- return data;
29648
+ return null;
29614
29649
  } catch {
29615
29650
  return null;
29616
29651
  }
29617
29652
  }
29653
+ async function readTokenRow(stored, provider, walletAddress) {
29654
+ if (stored.startsWith(ENCRYPTED_PREFIX2)) {
29655
+ if (!walletAddress) {
29656
+ getLogger().warn(
29657
+ `Encrypted OAuth token found for ${provider} but no wallet address provided`
29658
+ );
29659
+ return null;
29660
+ }
29661
+ try {
29662
+ const encryptedData = stored.slice(ENCRYPTED_PREFIX2.length);
29663
+ let decryptedJson;
29664
+ try {
29665
+ decryptedJson = await decryptData(encryptedData, walletAddress);
29666
+ } catch {
29667
+ decryptedJson = await decryptData(encryptedData, walletAddress, "v2");
29668
+ }
29669
+ const data = JSON.parse(decryptedJson);
29670
+ if (!data.accessToken) return null;
29671
+ return data;
29672
+ } catch (error) {
29673
+ getLogger().error(`Failed to decrypt OAuth token for ${provider}:`, error);
29674
+ return null;
29675
+ }
29676
+ }
29677
+ return parsePlaintextToken(stored, walletAddress);
29678
+ }
29618
29679
  async function storeTokenData(provider, data, walletAddress) {
29619
29680
  if (typeof window === "undefined") return;
29620
- const key = getStorageKey(provider);
29681
+ const key = getStorageKey(provider, walletAddress);
29621
29682
  const json4 = JSON.stringify(data);
29683
+ const plaintextRecord = JSON.stringify({
29684
+ wallet: walletAddress,
29685
+ token: data
29686
+ });
29622
29687
  if (walletAddress) {
29623
29688
  try {
29624
29689
  const encrypted = await encryptData(json4, walletAddress);
29625
- localStorage.setItem(key, `${ENCRYPTED_PREFIX}${encrypted}`);
29690
+ localStorage.setItem(key, `${ENCRYPTED_PREFIX2}${encrypted}`);
29691
+ sessionStorage.removeItem(key);
29626
29692
  } catch (error) {
29627
29693
  getLogger().warn(
29628
29694
  `Failed to encrypt OAuth token for ${provider}, storing temporarily:`,
29629
29695
  error
29630
29696
  );
29631
- sessionStorage.setItem(key, json4);
29697
+ sessionStorage.setItem(key, plaintextRecord);
29632
29698
  throw new Error(
29633
29699
  `OAuth token encryption failed: ${error instanceof Error ? error.message : String(error)}`
29634
29700
  );
29635
29701
  }
29636
29702
  } else {
29637
- sessionStorage.setItem(key, json4);
29703
+ sessionStorage.setItem(key, plaintextRecord);
29638
29704
  }
29639
29705
  }
29640
- function clearTokenData(provider) {
29706
+ function clearTokenData(provider, walletAddress) {
29641
29707
  if (typeof window === "undefined") return;
29642
- const key = getStorageKey(provider);
29643
- localStorage.removeItem(key);
29644
- sessionStorage.removeItem(key);
29708
+ const base = getStorageKey(provider);
29709
+ const keys = walletAddress ? [getStorageKey(provider, walletAddress), base] : [base];
29710
+ if (!walletAddress) keys.push(...suffixedKeys(base));
29711
+ for (const key of keys) {
29712
+ localStorage.removeItem(key);
29713
+ sessionStorage.removeItem(key);
29714
+ }
29715
+ }
29716
+ function suffixedKeys(base) {
29717
+ const found = [];
29718
+ for (const storage of [localStorage, sessionStorage]) {
29719
+ for (let i = 0; i < storage.length; i++) {
29720
+ const key = storage.key(i);
29721
+ if (key && key.startsWith(`${base}:`) && !found.includes(key)) found.push(key);
29722
+ }
29723
+ }
29724
+ return found;
29645
29725
  }
29646
29726
  function isTokenExpired(data, bufferSeconds = 60) {
29647
29727
  if (!data) return true;
@@ -29665,22 +29745,40 @@ async function getRefreshToken(provider, walletAddress) {
29665
29745
  async function migrateUnencryptedTokens(provider, walletAddress) {
29666
29746
  if (typeof window === "undefined") return false;
29667
29747
  try {
29668
- const key = getStorageKey(provider);
29669
- const localStored = localStorage.getItem(key);
29670
- const sessionStored = sessionStorage.getItem(key);
29671
- const isEncrypted2 = (value) => !!value && value.startsWith(ENCRYPTED_PREFIX);
29672
- const tokenToMigrate = sessionStored && !isEncrypted2(sessionStored) ? sessionStored : localStored && !isEncrypted2(localStored) ? localStored : null;
29748
+ const scopedKey = getStorageKey(provider, walletAddress);
29749
+ const legacyKey = getStorageKey(provider);
29750
+ const keys = [scopedKey, legacyKey];
29751
+ const isEncrypted2 = (value) => !!value && value.startsWith(ENCRYPTED_PREFIX2);
29752
+ const plaintextAt = (key) => {
29753
+ const stored = sessionStorage.getItem(key) ?? localStorage.getItem(key);
29754
+ return stored && !isEncrypted2(stored) ? stored : null;
29755
+ };
29756
+ let tokenToMigrate = null;
29757
+ for (const key of keys) {
29758
+ const value = plaintextAt(key);
29759
+ if (value) {
29760
+ tokenToMigrate = value;
29761
+ break;
29762
+ }
29763
+ }
29673
29764
  if (!tokenToMigrate) {
29674
- if (isEncrypted2(localStored) && sessionStored && !isEncrypted2(sessionStored)) {
29675
- sessionStorage.removeItem(key);
29765
+ for (const key of keys) {
29766
+ const stored = sessionStorage.getItem(key);
29767
+ if (isEncrypted2(localStorage.getItem(key)) && stored && !isEncrypted2(stored)) {
29768
+ sessionStorage.removeItem(key);
29769
+ }
29676
29770
  }
29677
29771
  return false;
29678
29772
  }
29679
29773
  try {
29680
- const data = JSON.parse(tokenToMigrate);
29774
+ const data = parsePlaintextToken(tokenToMigrate, walletAddress);
29775
+ if (!data) return false;
29681
29776
  await storeTokenData(provider, data, walletAddress);
29682
- if (sessionStored && !isEncrypted2(sessionStored)) {
29683
- sessionStorage.removeItem(key);
29777
+ for (const key of keys) {
29778
+ if (plaintextAt(key)) sessionStorage.removeItem(key);
29779
+ if (key === legacyKey && !isEncrypted2(localStorage.getItem(key))) {
29780
+ localStorage.removeItem(key);
29781
+ }
29684
29782
  }
29685
29783
  return true;
29686
29784
  } catch (error) {
@@ -29849,7 +29947,7 @@ async function refreshDropboxToken(apiClient, walletAddress) {
29849
29947
  await storeTokenData(PROVIDER, tokenData, walletAddress);
29850
29948
  return response.data.access_token;
29851
29949
  } catch {
29852
- clearTokenData(PROVIDER);
29950
+ clearTokenData(PROVIDER, walletAddress);
29853
29951
  return null;
29854
29952
  }
29855
29953
  }
@@ -29865,7 +29963,7 @@ async function revokeDropboxToken(apiClient, walletAddress) {
29865
29963
  });
29866
29964
  } catch {
29867
29965
  } finally {
29868
- clearTokenData(PROVIDER);
29966
+ clearTokenData(PROVIDER, walletAddress);
29869
29967
  }
29870
29968
  }
29871
29969
  async function getDropboxAccessToken(apiClient, walletAddress) {
@@ -30474,7 +30572,7 @@ async function refreshGoogleDriveToken(apiClient, walletAddress) {
30474
30572
  await storeTokenData(PROVIDER2, tokenData, walletAddress);
30475
30573
  return response.data.access_token;
30476
30574
  } catch {
30477
- clearTokenData(PROVIDER2);
30575
+ clearTokenData(PROVIDER2, walletAddress);
30478
30576
  return null;
30479
30577
  }
30480
30578
  }
@@ -30490,7 +30588,7 @@ async function revokeGoogleDriveToken(apiClient, walletAddress) {
30490
30588
  });
30491
30589
  } catch {
30492
30590
  } finally {
30493
- clearTokenData(PROVIDER2);
30591
+ clearTokenData(PROVIDER2, walletAddress);
30494
30592
  }
30495
30593
  }
30496
30594
  async function getGoogleDriveAccessToken(apiClient, walletAddress) {
@@ -32133,7 +32231,7 @@ var CODE_STORAGE_KEY2 = "google_calendar_oauth_state";
32133
32231
  var TOKEN_STORAGE_KEY = "oauth_token_google-calendar";
32134
32232
  var RETURN_URL_KEY = "google_calendar_return_url";
32135
32233
  var PENDING_MESSAGE_KEY = "google_calendar_pending_message";
32136
- var ENCRYPTED_PREFIX2 = "enc:oauth:";
32234
+ var ENCRYPTED_PREFIX3 = "enc:oauth:";
32137
32235
  var cachedAccessToken = null;
32138
32236
  var cachedExpiresAt = null;
32139
32237
  var cachedRefreshToken = null;
@@ -32164,9 +32262,9 @@ async function getStoredTokenData2(walletAddress) {
32164
32262
  }
32165
32263
  try {
32166
32264
  const scopedStored = localStorage.getItem(getTokenStorageKey(walletAddress));
32167
- if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX2) && walletAddress && hasEncryptionKey(walletAddress)) {
32265
+ if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX3) && walletAddress && hasEncryptionKey(walletAddress)) {
32168
32266
  try {
32169
- const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX2.length);
32267
+ const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX3.length);
32170
32268
  const cryptoKey = await getEncryptionKey(walletAddress);
32171
32269
  const decryptedJson = await decryptDataWithKey(encryptedData, cryptoKey);
32172
32270
  const data = JSON.parse(decryptedJson);
@@ -32181,33 +32279,18 @@ async function getStoredTokenData2(walletAddress) {
32181
32279
  getLogger().error("Failed to decrypt Calendar OAuth token:", error);
32182
32280
  }
32183
32281
  }
32184
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY);
32185
- if (legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX2)) {
32186
- try {
32187
- const data = JSON.parse(legacyStored);
32188
- if (data.accessToken) {
32189
- cachedAccessToken = data.accessToken;
32190
- cachedExpiresAt = data.expiresAt ?? null;
32191
- cachedRefreshToken = data.refreshToken ?? null;
32192
- cachedWalletAddress = walletAddress ?? null;
32193
- return data;
32194
- }
32195
- } catch {
32196
- }
32197
- }
32198
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY);
32199
- if (sessionStored) {
32200
- try {
32201
- const data = JSON.parse(sessionStored);
32202
- if (!data.accessToken) return null;
32203
- cachedAccessToken = data.accessToken;
32204
- cachedExpiresAt = data.expiresAt ?? null;
32205
- cachedRefreshToken = data.refreshToken ?? null;
32206
- cachedScope = data.scope ?? null;
32207
- cachedWalletAddress = walletAddress ?? null;
32208
- return data;
32209
- } catch {
32210
- }
32282
+ const plaintext = readPlaintextToken(
32283
+ getTokenStorageKey(walletAddress),
32284
+ TOKEN_STORAGE_KEY,
32285
+ walletAddress
32286
+ );
32287
+ if (plaintext) {
32288
+ cachedAccessToken = plaintext.accessToken;
32289
+ cachedExpiresAt = plaintext.expiresAt ?? null;
32290
+ cachedRefreshToken = plaintext.refreshToken ?? null;
32291
+ cachedScope = plaintext.scope ?? null;
32292
+ cachedWalletAddress = walletAddress ?? null;
32293
+ return plaintext;
32211
32294
  }
32212
32295
  return null;
32213
32296
  } catch {
@@ -32222,16 +32305,19 @@ async function storeTokenData2(data, walletAddress) {
32222
32305
  cachedScope = data.scope ?? null;
32223
32306
  cachedWalletAddress = walletAddress ?? null;
32224
32307
  const json4 = JSON.stringify(data);
32225
- sessionStorage.setItem(TOKEN_STORAGE_KEY, json4);
32226
32308
  if (walletAddress && hasEncryptionKey(walletAddress)) {
32227
32309
  try {
32228
32310
  const cryptoKey = await getEncryptionKey(walletAddress);
32229
32311
  const encrypted = await encryptDataWithKey(json4, cryptoKey);
32230
- localStorage.setItem(getTokenStorageKey(walletAddress), ENCRYPTED_PREFIX2 + encrypted);
32312
+ localStorage.setItem(getTokenStorageKey(walletAddress), ENCRYPTED_PREFIX3 + encrypted);
32313
+ sessionStorage.removeItem(getTokenStorageKey(walletAddress));
32314
+ return;
32231
32315
  } catch (error) {
32232
32316
  getLogger().warn("Failed to encrypt Calendar OAuth token:", error);
32233
32317
  }
32234
32318
  }
32319
+ const record = { wallet: walletAddress, token: data };
32320
+ sessionStorage.setItem(getTokenStorageKey(walletAddress), JSON.stringify(record));
32235
32321
  }
32236
32322
  function clearCalendarToken(walletAddress) {
32237
32323
  if (typeof window === "undefined") return;
@@ -32245,6 +32331,7 @@ function clearCalendarToken(walletAddress) {
32245
32331
  localStorage.removeItem(TOKEN_STORAGE_KEY);
32246
32332
  }
32247
32333
  sessionStorage.removeItem(TOKEN_STORAGE_KEY);
32334
+ sessionStorage.removeItem(getTokenStorageKey(walletAddress));
32248
32335
  }
32249
32336
  function isTokenExpired2(data, bufferSeconds = 60) {
32250
32337
  if (!data) return true;
@@ -32426,24 +32513,38 @@ async function migrateCalendarToken(walletAddress) {
32426
32513
  if (typeof window === "undefined") return false;
32427
32514
  if (!walletAddress || !hasEncryptionKey(walletAddress)) return false;
32428
32515
  try {
32429
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY);
32430
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY);
32431
- const isLegacyUnencrypted = legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX2);
32432
- const unencryptedJson = sessionStored || (isLegacyUnencrypted ? legacyStored : null);
32433
- if (!unencryptedJson) return false;
32434
32516
  const scopedKey = getTokenStorageKey(walletAddress);
32517
+ const sources = [
32518
+ { key: scopedKey, store: sessionStorage },
32519
+ // A read moves a legacy localStorage row here as plain text.
32520
+ { key: scopedKey, store: localStorage },
32521
+ { key: TOKEN_STORAGE_KEY, store: sessionStorage },
32522
+ { key: TOKEN_STORAGE_KEY, store: localStorage }
32523
+ ];
32524
+ let used = null;
32525
+ let unencryptedJson = "";
32526
+ for (const source of sources) {
32527
+ const value = source.store.getItem(source.key);
32528
+ if (value && !value.startsWith(ENCRYPTED_PREFIX3)) {
32529
+ unencryptedJson = value;
32530
+ used = source;
32531
+ break;
32532
+ }
32533
+ }
32534
+ if (!used) return false;
32435
32535
  const existingEncrypted = localStorage.getItem(scopedKey);
32436
- if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX2)) {
32437
- sessionStorage.removeItem(TOKEN_STORAGE_KEY);
32438
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY);
32536
+ if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX3)) {
32537
+ used.store.removeItem(used.key);
32439
32538
  return true;
32440
32539
  }
32441
- const data = JSON.parse(unencryptedJson);
32540
+ const data = parsePlaintextToken(unencryptedJson, walletAddress);
32541
+ if (!data) return false;
32442
32542
  await storeTokenData2(data, walletAddress);
32443
32543
  const migrated = localStorage.getItem(scopedKey);
32444
- if (!migrated?.startsWith(ENCRYPTED_PREFIX2)) return false;
32445
- sessionStorage.removeItem(TOKEN_STORAGE_KEY);
32446
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY);
32544
+ if (!migrated?.startsWith(ENCRYPTED_PREFIX3)) return false;
32545
+ if (used.store !== localStorage || used.key !== scopedKey) {
32546
+ used.store.removeItem(used.key);
32547
+ }
32447
32548
  return true;
32448
32549
  } catch {
32449
32550
  return false;
@@ -32456,7 +32557,7 @@ var CODE_STORAGE_KEY3 = "google_drive_oauth_state";
32456
32557
  var TOKEN_STORAGE_KEY2 = "oauth_token_google-drive-full";
32457
32558
  var RETURN_URL_KEY2 = "google_drive_return_url";
32458
32559
  var PENDING_MESSAGE_KEY2 = "google_drive_pending_message";
32459
- var ENCRYPTED_PREFIX3 = "enc:oauth:";
32560
+ var ENCRYPTED_PREFIX4 = "enc:oauth:";
32460
32561
  var cachedAccessToken2 = null;
32461
32562
  var cachedExpiresAt2 = null;
32462
32563
  var cachedRefreshToken2 = null;
@@ -32487,9 +32588,9 @@ async function getStoredTokenData3(walletAddress) {
32487
32588
  }
32488
32589
  try {
32489
32590
  const scopedStored = localStorage.getItem(getTokenStorageKey2(walletAddress));
32490
- if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX3) && walletAddress && hasEncryptionKey(walletAddress)) {
32591
+ if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX4) && walletAddress && hasEncryptionKey(walletAddress)) {
32491
32592
  try {
32492
- const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX3.length);
32593
+ const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX4.length);
32493
32594
  const cryptoKey = await getEncryptionKey(walletAddress);
32494
32595
  const decryptedJson = await decryptDataWithKey(encryptedData, cryptoKey);
32495
32596
  const data = JSON.parse(decryptedJson);
@@ -32504,33 +32605,18 @@ async function getStoredTokenData3(walletAddress) {
32504
32605
  getLogger().error("Failed to decrypt Drive OAuth token:", error);
32505
32606
  }
32506
32607
  }
32507
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY2);
32508
- if (legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX3)) {
32509
- try {
32510
- const data = JSON.parse(legacyStored);
32511
- if (data.accessToken) {
32512
- cachedAccessToken2 = data.accessToken;
32513
- cachedExpiresAt2 = data.expiresAt ?? null;
32514
- cachedRefreshToken2 = data.refreshToken ?? null;
32515
- cachedWalletAddress2 = walletAddress ?? null;
32516
- return data;
32517
- }
32518
- } catch {
32519
- }
32520
- }
32521
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY2);
32522
- if (sessionStored) {
32523
- try {
32524
- const data = JSON.parse(sessionStored);
32525
- if (!data.accessToken) return null;
32526
- cachedAccessToken2 = data.accessToken;
32527
- cachedExpiresAt2 = data.expiresAt ?? null;
32528
- cachedRefreshToken2 = data.refreshToken ?? null;
32529
- cachedScope2 = data.scope ?? null;
32530
- cachedWalletAddress2 = walletAddress ?? null;
32531
- return data;
32532
- } catch {
32533
- }
32608
+ const plaintext = readPlaintextToken(
32609
+ getTokenStorageKey2(walletAddress),
32610
+ TOKEN_STORAGE_KEY2,
32611
+ walletAddress
32612
+ );
32613
+ if (plaintext) {
32614
+ cachedAccessToken2 = plaintext.accessToken;
32615
+ cachedExpiresAt2 = plaintext.expiresAt ?? null;
32616
+ cachedRefreshToken2 = plaintext.refreshToken ?? null;
32617
+ cachedScope2 = plaintext.scope ?? null;
32618
+ cachedWalletAddress2 = walletAddress ?? null;
32619
+ return plaintext;
32534
32620
  }
32535
32621
  return null;
32536
32622
  } catch {
@@ -32545,16 +32631,19 @@ async function storeTokenData3(data, walletAddress) {
32545
32631
  cachedScope2 = data.scope ?? null;
32546
32632
  cachedWalletAddress2 = walletAddress ?? null;
32547
32633
  const json4 = JSON.stringify(data);
32548
- sessionStorage.setItem(TOKEN_STORAGE_KEY2, json4);
32549
32634
  if (walletAddress && hasEncryptionKey(walletAddress)) {
32550
32635
  try {
32551
32636
  const cryptoKey = await getEncryptionKey(walletAddress);
32552
32637
  const encrypted = await encryptDataWithKey(json4, cryptoKey);
32553
- localStorage.setItem(getTokenStorageKey2(walletAddress), `${ENCRYPTED_PREFIX3}${encrypted}`);
32638
+ localStorage.setItem(getTokenStorageKey2(walletAddress), `${ENCRYPTED_PREFIX4}${encrypted}`);
32639
+ sessionStorage.removeItem(getTokenStorageKey2(walletAddress));
32640
+ return;
32554
32641
  } catch (error) {
32555
32642
  getLogger().warn("Failed to encrypt Drive OAuth token:", error);
32556
32643
  }
32557
32644
  }
32645
+ const record = { wallet: walletAddress, token: data };
32646
+ sessionStorage.setItem(getTokenStorageKey2(walletAddress), JSON.stringify(record));
32558
32647
  }
32559
32648
  function clearDriveToken(walletAddress) {
32560
32649
  if (typeof window === "undefined") return;
@@ -32568,6 +32657,7 @@ function clearDriveToken(walletAddress) {
32568
32657
  localStorage.removeItem(TOKEN_STORAGE_KEY2);
32569
32658
  }
32570
32659
  sessionStorage.removeItem(TOKEN_STORAGE_KEY2);
32660
+ sessionStorage.removeItem(getTokenStorageKey2(walletAddress));
32571
32661
  }
32572
32662
  function isTokenExpired3(data, bufferSeconds = 60) {
32573
32663
  if (!data) return true;
@@ -32749,24 +32839,38 @@ async function migrateDriveToken(walletAddress) {
32749
32839
  if (typeof window === "undefined") return false;
32750
32840
  if (!walletAddress || !hasEncryptionKey(walletAddress)) return false;
32751
32841
  try {
32752
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY2);
32753
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY2);
32754
- const isLegacyUnencrypted = legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX3);
32755
- const unencryptedJson = sessionStored || (isLegacyUnencrypted ? legacyStored : null);
32756
- if (!unencryptedJson) return false;
32757
32842
  const scopedKey = getTokenStorageKey2(walletAddress);
32843
+ const sources = [
32844
+ { key: scopedKey, store: sessionStorage },
32845
+ // A read moves a legacy localStorage row here as plain text.
32846
+ { key: scopedKey, store: localStorage },
32847
+ { key: TOKEN_STORAGE_KEY2, store: sessionStorage },
32848
+ { key: TOKEN_STORAGE_KEY2, store: localStorage }
32849
+ ];
32850
+ let used = null;
32851
+ let unencryptedJson = "";
32852
+ for (const source of sources) {
32853
+ const value = source.store.getItem(source.key);
32854
+ if (value && !value.startsWith(ENCRYPTED_PREFIX4)) {
32855
+ unencryptedJson = value;
32856
+ used = source;
32857
+ break;
32858
+ }
32859
+ }
32860
+ if (!used) return false;
32758
32861
  const existingEncrypted = localStorage.getItem(scopedKey);
32759
- if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX3)) {
32760
- sessionStorage.removeItem(TOKEN_STORAGE_KEY2);
32761
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY2);
32862
+ if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX4)) {
32863
+ used.store.removeItem(used.key);
32762
32864
  return true;
32763
32865
  }
32764
- const data = JSON.parse(unencryptedJson);
32866
+ const data = parsePlaintextToken(unencryptedJson, walletAddress);
32867
+ if (!data) return false;
32765
32868
  await storeTokenData3(data, walletAddress);
32766
32869
  const migrated = localStorage.getItem(scopedKey);
32767
- if (!migrated?.startsWith(ENCRYPTED_PREFIX3)) return false;
32768
- sessionStorage.removeItem(TOKEN_STORAGE_KEY2);
32769
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY2);
32870
+ if (!migrated?.startsWith(ENCRYPTED_PREFIX4)) return false;
32871
+ if (used.store !== localStorage || used.key !== scopedKey) {
32872
+ used.store.removeItem(used.key);
32873
+ }
32770
32874
  return true;
32771
32875
  } catch {
32772
32876
  return false;
@@ -32798,7 +32902,7 @@ var FALLBACK_OAUTH_AUTHORIZE = "https://api.notion.com/v1/oauth/authorize";
32798
32902
  var FALLBACK_OAUTH_TOKEN = "https://api.notion.com/v1/oauth/token";
32799
32903
  var FALLBACK_REGISTRATION = "https://api.notion.com/v1/oauth/register";
32800
32904
  var DEFAULT_TOKEN_EXPIRY_SECONDS = 8 * 3600;
32801
- var ENCRYPTED_PREFIX4 = "enc:oauth:";
32905
+ var ENCRYPTED_PREFIX5 = "enc:oauth:";
32802
32906
  var cachedAccessToken3 = null;
32803
32907
  var cachedExpiresAt3 = null;
32804
32908
  var cachedWalletAddress3 = null;
@@ -32936,12 +33040,14 @@ async function storeTokenData4(data, walletAddress) {
32936
33040
  try {
32937
33041
  const cryptoKey = await getEncryptionKey(walletAddress);
32938
33042
  const encrypted = await encryptDataWithKey(json4, cryptoKey);
32939
- localStorage.setItem(getTokenStorageKey3(walletAddress), `${ENCRYPTED_PREFIX4}${encrypted}`);
33043
+ localStorage.setItem(getTokenStorageKey3(walletAddress), `${ENCRYPTED_PREFIX5}${encrypted}`);
33044
+ sessionStorage.removeItem(getTokenStorageKey3(walletAddress));
32940
33045
  return;
32941
33046
  } catch {
32942
33047
  }
32943
33048
  }
32944
- sessionStorage.setItem(TOKEN_STORAGE_KEY3, json4);
33049
+ const record = { wallet: walletAddress, token: data };
33050
+ sessionStorage.setItem(getTokenStorageKey3(walletAddress), JSON.stringify(record));
32945
33051
  }
32946
33052
  async function getStoredTokenData4(walletAddress) {
32947
33053
  if (typeof window === "undefined") return null;
@@ -32958,11 +33064,11 @@ async function getStoredTokenData4(walletAddress) {
32958
33064
  }
32959
33065
  try {
32960
33066
  const stored = localStorage.getItem(getTokenStorageKey3(walletAddress));
32961
- if (stored?.startsWith(ENCRYPTED_PREFIX4)) {
33067
+ if (stored?.startsWith(ENCRYPTED_PREFIX5)) {
32962
33068
  if (walletAddress && hasEncryptionKey(walletAddress)) {
32963
33069
  try {
32964
33070
  const cryptoKey = await getEncryptionKey(walletAddress);
32965
- const encrypted = stored.slice(ENCRYPTED_PREFIX4.length);
33071
+ const encrypted = stored.slice(ENCRYPTED_PREFIX5.length);
32966
33072
  const decrypted = await decryptDataWithKey(encrypted, cryptoKey);
32967
33073
  const data = JSON.parse(decrypted);
32968
33074
  if (data.accessToken) return data;
@@ -32970,12 +33076,12 @@ async function getStoredTokenData4(walletAddress) {
32970
33076
  }
32971
33077
  }
32972
33078
  }
32973
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY3);
32974
- if (sessionStored) {
32975
- const data = JSON.parse(sessionStored);
32976
- if (!data.accessToken) return null;
32977
- return data;
32978
- }
33079
+ const plaintext = readPlaintextToken(
33080
+ getTokenStorageKey3(walletAddress),
33081
+ TOKEN_STORAGE_KEY3,
33082
+ walletAddress
33083
+ );
33084
+ if (plaintext) return plaintext;
32979
33085
  return null;
32980
33086
  } catch {
32981
33087
  return null;
@@ -32985,6 +33091,7 @@ function clearNotionToken(walletAddress) {
32985
33091
  if (typeof window === "undefined") return;
32986
33092
  localStorage.removeItem(getTokenStorageKey3(walletAddress));
32987
33093
  sessionStorage.removeItem(TOKEN_STORAGE_KEY3);
33094
+ sessionStorage.removeItem(getTokenStorageKey3(walletAddress));
32988
33095
  cachedAccessToken3 = null;
32989
33096
  cachedExpiresAt3 = null;
32990
33097
  cachedWalletAddress3 = null;
@@ -33000,20 +33107,34 @@ async function migrateNotionToken(walletAddress) {
33000
33107
  if (typeof window === "undefined") return false;
33001
33108
  if (!walletAddress || !hasEncryptionKey(walletAddress)) return false;
33002
33109
  try {
33003
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY3);
33004
- if (!sessionStored) return false;
33005
33110
  const scopedKey = getTokenStorageKey3(walletAddress);
33111
+ const sources = [
33112
+ { key: scopedKey, store: sessionStorage },
33113
+ { key: TOKEN_STORAGE_KEY3, store: sessionStorage }
33114
+ ];
33115
+ let used = null;
33116
+ let unencryptedJson = "";
33117
+ for (const source of sources) {
33118
+ const value = source.store.getItem(source.key);
33119
+ if (value && !value.startsWith(ENCRYPTED_PREFIX5)) {
33120
+ unencryptedJson = value;
33121
+ used = source;
33122
+ break;
33123
+ }
33124
+ }
33125
+ if (!used) return false;
33006
33126
  const localStored = localStorage.getItem(scopedKey);
33007
- if (localStored?.startsWith(ENCRYPTED_PREFIX4)) {
33127
+ if (localStored?.startsWith(ENCRYPTED_PREFIX5)) {
33008
33128
  localStorage.removeItem(scopedKey);
33009
33129
  }
33010
- const data = JSON.parse(sessionStored);
33130
+ const data = parsePlaintextToken(unencryptedJson, walletAddress);
33131
+ if (!data) return false;
33011
33132
  await storeTokenData4(data, walletAddress);
33012
33133
  const migrated = localStorage.getItem(scopedKey);
33013
- if (!migrated?.startsWith(ENCRYPTED_PREFIX4)) {
33134
+ if (!migrated?.startsWith(ENCRYPTED_PREFIX5)) {
33014
33135
  return false;
33015
33136
  }
33016
- sessionStorage.removeItem(TOKEN_STORAGE_KEY3);
33137
+ used.store.removeItem(used.key);
33017
33138
  return true;
33018
33139
  } catch {
33019
33140
  return false;
@@ -33025,12 +33146,12 @@ async function migrateNotionClientRegistration(walletAddress) {
33025
33146
  try {
33026
33147
  const sessionStored = sessionStorage.getItem(CLIENT_REGISTRATION_KEY);
33027
33148
  const legacyStored = localStorage.getItem(CLIENT_REGISTRATION_KEY);
33028
- const isLegacyUnencrypted = legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX4);
33149
+ const isLegacyUnencrypted = legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX5);
33029
33150
  const unencryptedJson = sessionStored || (isLegacyUnencrypted ? legacyStored : null);
33030
33151
  if (!unencryptedJson) return false;
33031
33152
  const scopedKey = getClientRegistrationStorageKey(walletAddress);
33032
33153
  const existingEncrypted = localStorage.getItem(scopedKey);
33033
- if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX4)) {
33154
+ if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX5)) {
33034
33155
  sessionStorage.removeItem(CLIENT_REGISTRATION_KEY);
33035
33156
  if (isLegacyUnencrypted) {
33036
33157
  localStorage.removeItem(CLIENT_REGISTRATION_KEY);
@@ -33040,7 +33161,7 @@ async function migrateNotionClientRegistration(walletAddress) {
33040
33161
  const data = JSON.parse(unencryptedJson);
33041
33162
  await storeClientRegistration(data, walletAddress);
33042
33163
  const migrated = localStorage.getItem(scopedKey);
33043
- if (!migrated?.startsWith(ENCRYPTED_PREFIX4)) {
33164
+ if (!migrated?.startsWith(ENCRYPTED_PREFIX5)) {
33044
33165
  return false;
33045
33166
  }
33046
33167
  sessionStorage.removeItem(CLIENT_REGISTRATION_KEY);
@@ -33057,11 +33178,11 @@ async function getClientRegistration(walletAddress) {
33057
33178
  try {
33058
33179
  const scopedKey = getClientRegistrationStorageKey(walletAddress);
33059
33180
  const stored = localStorage.getItem(scopedKey);
33060
- if (stored?.startsWith(ENCRYPTED_PREFIX4)) {
33181
+ if (stored?.startsWith(ENCRYPTED_PREFIX5)) {
33061
33182
  if (walletAddress && hasEncryptionKey(walletAddress)) {
33062
33183
  try {
33063
33184
  const cryptoKey = await getEncryptionKey(walletAddress);
33064
- const encrypted = stored.slice(ENCRYPTED_PREFIX4.length);
33185
+ const encrypted = stored.slice(ENCRYPTED_PREFIX5.length);
33065
33186
  const decrypted = await decryptDataWithKey(encrypted, cryptoKey);
33066
33187
  const data = JSON.parse(decrypted);
33067
33188
  if (data.clientId) {
@@ -33071,7 +33192,7 @@ async function getClientRegistration(walletAddress) {
33071
33192
  }
33072
33193
  }
33073
33194
  }
33074
- if (stored && !stored.startsWith(ENCRYPTED_PREFIX4)) {
33195
+ if (stored && !stored.startsWith(ENCRYPTED_PREFIX5)) {
33075
33196
  const data = JSON.parse(stored);
33076
33197
  if (!data.clientId) return null;
33077
33198
  return data;
@@ -33096,7 +33217,7 @@ async function storeClientRegistration(registration, walletAddress) {
33096
33217
  const cryptoKey = await getEncryptionKey(walletAddress);
33097
33218
  const encrypted = await encryptDataWithKey(json4, cryptoKey);
33098
33219
  const scopedKey = getClientRegistrationStorageKey(walletAddress);
33099
- localStorage.setItem(scopedKey, `${ENCRYPTED_PREFIX4}${encrypted}`);
33220
+ localStorage.setItem(scopedKey, `${ENCRYPTED_PREFIX5}${encrypted}`);
33100
33221
  } catch {
33101
33222
  }
33102
33223
  }
@@ -33328,7 +33449,7 @@ var CODE_STORAGE_KEY4 = "github_oauth_state";
33328
33449
  var TOKEN_STORAGE_KEY4 = "oauth_token_github";
33329
33450
  var RETURN_URL_KEY4 = "github_return_url";
33330
33451
  var PENDING_MESSAGE_KEY4 = "github_pending_message";
33331
- var ENCRYPTED_PREFIX5 = "enc:oauth:";
33452
+ var ENCRYPTED_PREFIX6 = "enc:oauth:";
33332
33453
  var cachedAccessToken4 = null;
33333
33454
  var cachedExpiresAt4 = null;
33334
33455
  var cachedRefreshToken3 = null;
@@ -33361,9 +33482,9 @@ async function getStoredTokenData5(walletAddress) {
33361
33482
  }
33362
33483
  try {
33363
33484
  const scopedStored = localStorage.getItem(getTokenStorageKey4(walletAddress));
33364
- if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX5) && walletAddress && hasEncryptionKey(walletAddress)) {
33485
+ if (scopedStored && scopedStored.startsWith(ENCRYPTED_PREFIX6) && walletAddress && hasEncryptionKey(walletAddress)) {
33365
33486
  try {
33366
- const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX5.length);
33487
+ const encryptedData = scopedStored.slice(ENCRYPTED_PREFIX6.length);
33367
33488
  const cryptoKey = await getEncryptionKey(walletAddress);
33368
33489
  const decryptedJson = await decryptDataWithKey(encryptedData, cryptoKey);
33369
33490
  const data = JSON.parse(decryptedJson);
@@ -33378,34 +33499,18 @@ async function getStoredTokenData5(walletAddress) {
33378
33499
  getLogger().error("Failed to decrypt GitHub OAuth token:", error);
33379
33500
  }
33380
33501
  }
33381
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY4);
33382
- if (legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX5)) {
33383
- try {
33384
- const data = JSON.parse(legacyStored);
33385
- if (data.accessToken) {
33386
- cachedAccessToken4 = data.accessToken;
33387
- cachedExpiresAt4 = data.expiresAt ?? null;
33388
- cachedRefreshToken3 = data.refreshToken ?? null;
33389
- cachedScope3 = data.scope ?? null;
33390
- cachedWalletAddress4 = walletAddress ?? null;
33391
- return data;
33392
- }
33393
- } catch {
33394
- }
33395
- }
33396
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY4);
33397
- if (sessionStored) {
33398
- try {
33399
- const data = JSON.parse(sessionStored);
33400
- if (!data.accessToken) return null;
33401
- cachedAccessToken4 = data.accessToken;
33402
- cachedExpiresAt4 = data.expiresAt ?? null;
33403
- cachedRefreshToken3 = data.refreshToken ?? null;
33404
- cachedScope3 = data.scope ?? null;
33405
- cachedWalletAddress4 = walletAddress ?? null;
33406
- return data;
33407
- } catch {
33408
- }
33502
+ const plaintext = readPlaintextToken(
33503
+ getTokenStorageKey4(walletAddress),
33504
+ TOKEN_STORAGE_KEY4,
33505
+ walletAddress
33506
+ );
33507
+ if (plaintext) {
33508
+ cachedAccessToken4 = plaintext.accessToken;
33509
+ cachedExpiresAt4 = plaintext.expiresAt ?? null;
33510
+ cachedRefreshToken3 = plaintext.refreshToken ?? null;
33511
+ cachedScope3 = plaintext.scope ?? null;
33512
+ cachedWalletAddress4 = walletAddress ?? null;
33513
+ return plaintext;
33409
33514
  }
33410
33515
  return null;
33411
33516
  } catch {
@@ -33420,16 +33525,19 @@ async function storeTokenData5(data, walletAddress) {
33420
33525
  cachedScope3 = data.scope ?? null;
33421
33526
  cachedWalletAddress4 = walletAddress ?? null;
33422
33527
  const json4 = JSON.stringify(data);
33423
- sessionStorage.setItem(TOKEN_STORAGE_KEY4, json4);
33424
33528
  if (walletAddress && hasEncryptionKey(walletAddress)) {
33425
33529
  try {
33426
33530
  const cryptoKey = await getEncryptionKey(walletAddress);
33427
33531
  const encrypted = await encryptDataWithKey(json4, cryptoKey);
33428
- localStorage.setItem(getTokenStorageKey4(walletAddress), `${ENCRYPTED_PREFIX5}${encrypted}`);
33532
+ localStorage.setItem(getTokenStorageKey4(walletAddress), `${ENCRYPTED_PREFIX6}${encrypted}`);
33533
+ sessionStorage.removeItem(getTokenStorageKey4(walletAddress));
33534
+ return;
33429
33535
  } catch (error) {
33430
33536
  getLogger().warn("Failed to encrypt GitHub OAuth token:", error);
33431
33537
  }
33432
33538
  }
33539
+ const record = { wallet: walletAddress, token: data };
33540
+ sessionStorage.setItem(getTokenStorageKey4(walletAddress), JSON.stringify(record));
33433
33541
  }
33434
33542
  function clearGithubToken(walletAddress) {
33435
33543
  if (typeof window === "undefined") return;
@@ -33443,6 +33551,7 @@ function clearGithubToken(walletAddress) {
33443
33551
  localStorage.removeItem(TOKEN_STORAGE_KEY4);
33444
33552
  }
33445
33553
  sessionStorage.removeItem(TOKEN_STORAGE_KEY4);
33554
+ sessionStorage.removeItem(getTokenStorageKey4(walletAddress));
33446
33555
  }
33447
33556
  function isTokenExpired5(data, bufferSeconds = 60) {
33448
33557
  if (!data) return true;
@@ -33651,24 +33760,38 @@ async function migrateGithubToken(walletAddress) {
33651
33760
  if (typeof window === "undefined") return false;
33652
33761
  if (!walletAddress || !hasEncryptionKey(walletAddress)) return false;
33653
33762
  try {
33654
- const sessionStored = sessionStorage.getItem(TOKEN_STORAGE_KEY4);
33655
- const legacyStored = localStorage.getItem(TOKEN_STORAGE_KEY4);
33656
- const isLegacyUnencrypted = legacyStored && !legacyStored.startsWith(ENCRYPTED_PREFIX5);
33657
- const unencryptedJson = sessionStored || (isLegacyUnencrypted ? legacyStored : null);
33658
- if (!unencryptedJson) return false;
33659
33763
  const scopedKey = getTokenStorageKey4(walletAddress);
33764
+ const sources = [
33765
+ { key: scopedKey, store: sessionStorage },
33766
+ // A read moves a legacy localStorage row here as plain text.
33767
+ { key: scopedKey, store: localStorage },
33768
+ { key: TOKEN_STORAGE_KEY4, store: sessionStorage },
33769
+ { key: TOKEN_STORAGE_KEY4, store: localStorage }
33770
+ ];
33771
+ let used = null;
33772
+ let unencryptedJson = "";
33773
+ for (const source of sources) {
33774
+ const value = source.store.getItem(source.key);
33775
+ if (value && !value.startsWith(ENCRYPTED_PREFIX6)) {
33776
+ unencryptedJson = value;
33777
+ used = source;
33778
+ break;
33779
+ }
33780
+ }
33781
+ if (!used) return false;
33660
33782
  const existingEncrypted = localStorage.getItem(scopedKey);
33661
- if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX5)) {
33662
- sessionStorage.removeItem(TOKEN_STORAGE_KEY4);
33663
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY4);
33783
+ if (existingEncrypted?.startsWith(ENCRYPTED_PREFIX6)) {
33784
+ used.store.removeItem(used.key);
33664
33785
  return true;
33665
33786
  }
33666
- const data = JSON.parse(unencryptedJson);
33787
+ const data = parsePlaintextToken(unencryptedJson, walletAddress);
33788
+ if (!data) return false;
33667
33789
  await storeTokenData5(data, walletAddress);
33668
33790
  const migrated = localStorage.getItem(scopedKey);
33669
- if (!migrated?.startsWith(ENCRYPTED_PREFIX5)) return false;
33670
- sessionStorage.removeItem(TOKEN_STORAGE_KEY4);
33671
- if (isLegacyUnencrypted) localStorage.removeItem(TOKEN_STORAGE_KEY4);
33791
+ if (!migrated?.startsWith(ENCRYPTED_PREFIX6)) return false;
33792
+ if (used.store !== localStorage || used.key !== scopedKey) {
33793
+ used.store.removeItem(used.key);
33794
+ }
33672
33795
  return true;
33673
33796
  } catch {
33674
33797
  return false;