Skip to content

Commit c2286f9

Browse files
committed
use sessionStorage for keys
1 parent 6aefe65 commit c2286f9

File tree

5 files changed

+46
-25
lines changed

5 files changed

+46
-25
lines changed

apps/connect/src/Boot.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ declare module '@tanstack/react-router' {
1818

1919
const queryClient = new QueryClient();
2020

21-
const storage = localStorage;
21+
const addressStorage = localStorage;
22+
const keysStorage = sessionStorage;
2223

2324
export function Boot() {
2425
// check if the user is already authenticated on initial render
2526
const initialRenderAuthCheckRef = useRef(false);
2627
// using a layout effect to avoid a re-render
2728
useLayoutEffect(() => {
2829
if (!initialRenderAuthCheckRef.current) {
29-
const accountAddress = Connect.loadAccountAddress(storage);
30+
const accountAddress = Connect.loadAccountAddress(addressStorage);
3031
if (accountAddress) {
31-
const keys = Connect.loadKeys(storage, accountAddress);
32+
const keys = Connect.loadKeys(keysStorage, accountAddress);
3233
if (keys) {
3334
// user is already authenticated, set state
3435
StoreConnect.store.send({

apps/connect/src/components/logout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function Logout() {
1111
const [isLoading, setIsLoading] = useState(false);
1212
const disconnectWallet = async () => {
1313
setIsLoading(true);
14-
Connect.wipeAllAuthData(localStorage);
14+
Connect.wipeAllAuthData(localStorage, sessionStorage);
1515
await privyLogout();
1616
router.navigate({
1717
to: '/login',

apps/connect/src/routes/login.lazy.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { type WalletClient, createWalletClient, custom } from 'viem';
88

99
const CHAIN = import.meta.env.VITE_HYPERGRAPH_CHAIN === 'geogenesis' ? GEOGENESIS : GEO_TESTNET;
1010
const syncServerUri = import.meta.env.VITE_HYPERGRAPH_SYNC_SERVER_ORIGIN;
11-
const storage = localStorage;
11+
const addressStorage = localStorage;
12+
const keysStorage = sessionStorage;
1213

1314
export const Route = createLazyFileRoute('/login')({
1415
component: () => <Login />,
@@ -52,7 +53,16 @@ function Login() {
5253
console.log(walletClient);
5354
console.log(rpcUrl);
5455
console.log(CHAIN);
55-
await Connect.login({ walletClient, signer, syncServerUri, storage, identityToken, rpcUrl, chain: CHAIN });
56+
await Connect.login({
57+
walletClient,
58+
signer,
59+
syncServerUri,
60+
addressStorage,
61+
keysStorage,
62+
identityToken,
63+
rpcUrl,
64+
chain: CHAIN,
65+
});
5666
},
5767
[identityToken, signMessage],
5868
);

packages/hypergraph/src/connect/auth-storage.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ export const wipeAccountAddress = (storage: Storage) => {
6666
storage.removeItem(buildAccountAddressStorageKey());
6767
};
6868

69-
export const wipeAllAuthData = (storage: Storage) => {
70-
const accountAddress = loadAccountAddress(storage);
71-
wipeAccountAddress(storage);
69+
export const wipeAllAuthData = (addressStorage: Storage, keysAndTokenStorage: Storage) => {
70+
const accountAddress = loadAccountAddress(addressStorage);
71+
wipeAccountAddress(addressStorage);
7272
if (accountAddress) {
73-
wipeKeys(storage, accountAddress);
74-
wipeSyncServerSessionToken(storage, accountAddress);
73+
wipeKeys(keysAndTokenStorage, accountAddress);
74+
wipeSyncServerSessionToken(keysAndTokenStorage, accountAddress);
7575
}
7676
};

packages/hypergraph/src/connect/login.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export async function signup(
3131
smartAccountClient: SmartAccountClient,
3232
accountAddress: Address,
3333
syncServerUri: string,
34-
storage: Storage,
34+
addressStorage: Storage,
35+
keysStorage: Storage,
3536
identityToken: string,
3637
chain: Chain,
3738
rpcUrl: string,
@@ -74,8 +75,8 @@ export async function signup(
7475
if (!decoded.success) {
7576
throw new Error('Error creating identity');
7677
}
77-
storeKeys(storage, accountAddress, keys);
78-
storeAccountAddress(storage, accountAddress);
78+
storeKeys(keysStorage, accountAddress, keys);
79+
storeAccountAddress(addressStorage, accountAddress);
7980
return {
8081
accountAddress,
8182
keys,
@@ -86,7 +87,8 @@ export async function restoreKeys(
8687
signer: Signer,
8788
accountAddress: Address,
8889
syncServerUri: string,
89-
storage: Storage,
90+
addressStorage: Storage,
91+
keysStorage: Storage,
9092
identityToken: string,
9193
) {
9294
const res = await fetch(new URL('/connect/identity/encrypted', syncServerUri), {
@@ -103,8 +105,8 @@ export async function restoreKeys(
103105
const { keyBox } = decoded;
104106
const { ciphertext, nonce } = keyBox;
105107
const keys = await decryptIdentity(signer, ciphertext, nonce);
106-
storeKeys(storage, accountAddress, keys);
107-
storeAccountAddress(storage, accountAddress);
108+
storeKeys(keysStorage, accountAddress, keys);
109+
storeAccountAddress(addressStorage, accountAddress);
108110
return {
109111
accountAddress,
110112
keys,
@@ -113,8 +115,13 @@ export async function restoreKeys(
113115
throw new Error(`Error fetching identity ${res.status}`);
114116
}
115117

116-
const getAndUpdateSmartAccount = async (walletClient: WalletClient, rpcUrl: string, chain: Chain, storage: Storage) => {
117-
const accountAddressFromStorage = loadAccountAddress(storage) as Hex;
118+
const getAndUpdateSmartAccount = async (
119+
walletClient: WalletClient,
120+
rpcUrl: string,
121+
chain: Chain,
122+
addressStorage: Storage,
123+
) => {
124+
const accountAddressFromStorage = loadAccountAddress(addressStorage) as Hex;
118125
const smartAccountParams: SmartAccountParams = {
119126
owner: walletClient,
120127
rpcUrl,
@@ -145,25 +152,27 @@ export async function login({
145152
walletClient,
146153
signer,
147154
syncServerUri,
148-
storage,
155+
addressStorage,
156+
keysStorage,
149157
identityToken,
150158
rpcUrl,
151159
chain,
152160
}: {
153161
walletClient: WalletClient;
154162
signer: Signer;
155163
syncServerUri: string;
156-
storage: Storage;
164+
addressStorage: Storage;
165+
keysStorage: Storage;
157166
identityToken: string;
158167
rpcUrl: string;
159168
chain: Chain;
160169
}) {
161-
const smartAccountWalletClient = await getAndUpdateSmartAccount(walletClient, rpcUrl, chain, storage);
170+
const smartAccountWalletClient = await getAndUpdateSmartAccount(walletClient, rpcUrl, chain, addressStorage);
162171
if (!smartAccountWalletClient.account) {
163172
throw new Error('Smart account wallet client account not found');
164173
}
165174
const accountAddress = smartAccountWalletClient.account.address;
166-
// const keys = loadKeys(storage, accountAddress);
175+
167176
let authData: {
168177
accountAddress: Address;
169178
keys: IdentityKeys;
@@ -176,13 +185,14 @@ export async function login({
176185
smartAccountWalletClient,
177186
accountAddress,
178187
syncServerUri,
179-
storage,
188+
addressStorage,
189+
keysStorage,
180190
identityToken,
181191
chain,
182192
rpcUrl,
183193
);
184194
} else {
185-
authData = await restoreKeys(signer, accountAddress, syncServerUri, storage, identityToken);
195+
authData = await restoreKeys(signer, accountAddress, syncServerUri, addressStorage, keysStorage, identityToken);
186196
}
187197
store.send({ type: 'reset' });
188198
store.send({

0 commit comments

Comments
 (0)