Skip to content

Commit bc87787

Browse files
committed
feat: unify localstorage usage in misc-browser
1 parent a45c133 commit bc87787

File tree

5 files changed

+33
-27
lines changed

5 files changed

+33
-27
lines changed

packages/auth/src/lib/authenticators/metamask/eth.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ import {
2828
LIT_CHAINS_KEYS,
2929
} from '@lit-protocol/constants';
3030
import { validateSessionSig } from '@lit-protocol/lit-node-client';
31-
import { getStorageItem } from '@lit-protocol/misc-browser';
31+
import {
32+
getStorageItem,
33+
setStorageItem,
34+
removeStorageItem,
35+
} from '@lit-protocol/misc-browser';
3236
import { AuthCallbackParams, AuthSig } from '@lit-protocol/types';
3337

3438
import LitConnectModal from './connect-modal/modal';
@@ -350,7 +354,7 @@ export const connectWeb3 = async ({
350354
/**
351355
* @browserOnly
352356
* Delete any saved AuthSigs from local storage. Takes no params and returns
353-
* nothing. This will also clear out the WalletConnect cache in local storage.
357+
* nothing. This will also clear out the WalletConnect cache in localstorage.
354358
* We often run this function as a result of the user pressing a "Logout" button.
355359
*
356360
* @return { void }
@@ -374,8 +378,8 @@ export const disconnectWeb3 = (): void => {
374378

375379
const storage = LOCAL_STORAGE_KEYS;
376380

377-
localStorage.removeItem(storage.AUTH_SIGNATURE);
378-
localStorage.removeItem(storage.WALLET_SIGNATURE);
381+
removeStorageItem(storage.AUTH_SIGNATURE);
382+
removeStorageItem(storage.WALLET_SIGNATURE);
379383
};
380384

381385
/**
@@ -702,10 +706,7 @@ export const signAndSaveAuthMessage = async ({
702706

703707
// -- 4. store auth and a keypair in localstorage for communication with sgx
704708
if (Environment.isBrowser) {
705-
localStorage.setItem(
706-
LOCAL_STORAGE_KEYS.AUTH_SIGNATURE,
707-
JSON.stringify(authSig)
708-
);
709+
setStorageItem(LOCAL_STORAGE_KEYS.AUTH_SIGNATURE, JSON.stringify(authSig));
709710
}
710711

711712
return authSig;

packages/contracts-sdk/src/lib/contracts-sdk.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
TransactionError,
2828
WrongNetworkException,
2929
} from '@lit-protocol/constants';
30+
import { getStorageItem, setStorageItem } from '@lit-protocol/misc-browser';
3031
import {
3132
ContractName,
3233
EpochInfo,
@@ -248,7 +249,7 @@ export class LitContracts {
248249
let storagePrivateKey;
249250

250251
try {
251-
storagePrivateKey = localStorage.getItem(STORAGE_KEY);
252+
storagePrivateKey = getStorageItem(STORAGE_KEY);
252253
} catch (e) {
253254
// swallow
254255
// this.#logger.info('Not a problem.');
@@ -286,7 +287,7 @@ export class LitContracts {
286287
this.#logger.info(
287288
"You've set the option to store your private key in local storage."
288289
);
289-
localStorage.setItem(STORAGE_KEY, storagePrivateKey);
290+
setStorageItem(STORAGE_KEY, storagePrivateKey);
290291
}
291292
} else {
292293
// ----------------------------------------

packages/contracts-sdk/src/lib/helpers/addresses.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ParamsMissingError,
1414
} from '@lit-protocol/constants';
1515
import { publicKeyCompress } from '@lit-protocol/crypto';
16+
import { getStorageItem, setStorageItem } from '@lit-protocol/misc-browser';
1617
import { DerivedAddresses } from '@lit-protocol/types';
1718

1819
const logger = pino({ level: 'info', name: 'addresses' });
@@ -180,13 +181,16 @@ export const derivedAddresses = async (
180181
const CACHE_KEY = 'lit-cached-pkps';
181182
let cachedPkpJSON;
182183
try {
183-
const cachedPkp = localStorage.getItem(CACHE_KEY);
184+
const cachedPkp = getStorageItem(CACHE_KEY);
184185
if (cachedPkp) {
185186
cachedPkpJSON = JSON.parse(cachedPkp);
186187
publicKey = cachedPkpJSON[pkpTokenId];
187188
}
188189
} catch (e) {
189-
logger.error(e);
190+
logger.error({
191+
msg: `Could not get ${CACHE_KEY} from storage. Continuing...`,
192+
error: e,
193+
});
190194
}
191195

192196
if (!publicKey) {
@@ -222,18 +226,18 @@ export const derivedAddresses = async (
222226
if (options.cacheContractCall) {
223227
// trying to store key value pair in local storage
224228
try {
225-
const cachedPkp = localStorage.getItem(CACHE_KEY);
226-
if (cachedPkp) {
227-
const cachedPkpJSON = JSON.parse(cachedPkp);
228-
cachedPkpJSON[pkpTokenId] = publicKey;
229-
localStorage.setItem(CACHE_KEY, JSON.stringify(cachedPkpJSON));
230-
} else {
231-
const cachedPkpJSON: Record<string, unknown> = {};
232-
cachedPkpJSON[pkpTokenId] = publicKey;
233-
localStorage.setItem(CACHE_KEY, JSON.stringify(cachedPkpJSON));
234-
}
229+
const cachedPkp = getStorageItem(CACHE_KEY);
230+
const cachedPkpJSON: Record<string, unknown> = cachedPkp
231+
? JSON.parse(cachedPkp)
232+
: {};
233+
234+
cachedPkpJSON[pkpTokenId] = publicKey;
235+
setStorageItem(CACHE_KEY, JSON.stringify(cachedPkpJSON));
235236
} catch (e) {
236-
logger.error(e);
237+
logger.error({
238+
msg: `Could not get ${CACHE_KEY} from storage. Continuing...`,
239+
error: e,
240+
});
237241
}
238242
}
239243
}

packages/crypto/src/lib/crypto.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
UnknownError,
1212
UnknownSignatureError,
1313
} from '@lit-protocol/constants';
14+
import { getStorageItem, setStorageItem } from '@lit-protocol/misc-browser';
1415
import { NodeAttestation, SessionKeyPair, SigShare } from '@lit-protocol/types';
1516
import {
1617
blsCombine,
@@ -507,12 +508,12 @@ export const checkSevSnpAttestation = async (
507508
// use local storage if we have one available
508509
if (globalThis.localStorage) {
509510
logger.info('Using local storage for certificate caching');
510-
vcekCert = localStorage.getItem(vcekUrl);
511+
vcekCert = getStorageItem(vcekUrl);
511512
if (vcekCert) {
512513
vcekCert = Buffer.from(vcekCert, 'base64');
513514
} else {
514515
vcekCert = await getAmdCert(vcekUrl);
515-
localStorage.setItem(vcekUrl, Buffer.from(vcekCert).toString('base64'));
516+
setStorageItem(vcekUrl, Buffer.from(vcekCert).toString('base64'));
516517
}
517518
} else {
518519
const cache = ((

packages/lit-node-client/src/lib/lit-node-client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export class LitNodeClient extends LitCore implements ILitNodeClient {
256256
const newSessionKey = generateSessionKeyPair();
257257

258258
try {
259-
localStorage.setItem(storageKey, JSON.stringify(newSessionKey));
259+
setStorageItem(storageKey, JSON.stringify(newSessionKey));
260260
} catch (e) {
261261
this.#logger.info(
262262
`Localstorage not available. Not a problem. Continue...`
@@ -356,7 +356,6 @@ export class LitNodeClient extends LitCore implements ILitNodeClient {
356356
try {
357357
setStorageItem(storageKey, JSON.stringify(walletSig));
358358
} catch (e) {
359-
this.#logger.info('getWalletSig - flow 1.4');
360359
this.#logger.warn({
361360
msg: `Unable to store walletSig in local storage. Not a problem. Continue...`,
362361
error: e,

0 commit comments

Comments
 (0)