Skip to content

Commit 355e21d

Browse files
Improve ethAuth util's caching
1 parent 0fee0b3 commit 355e21d

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

packages/connect/src/utils/ethAuth.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,58 @@ export const signEthAuthProof = async (
1414
const walletAddress = walletClient.account.address
1515
const normalizedWalletAddress = walletAddress.toLowerCase()
1616

17-
const proofInformation = await storage.getItem(LocalStorageKey.EthAuthProof)
17+
const [proofInformation, proofSettings] = await Promise.all([
18+
storage.getItem(LocalStorageKey.EthAuthProof),
19+
storage.getItem(LocalStorageKey.EthAuthSettings)
20+
])
21+
const clearCachedProof = () => storage.removeItem(LocalStorageKey.EthAuthProof)
22+
23+
if (!proofSettings) {
24+
if (proofInformation) {
25+
await clearCachedProof()
26+
}
27+
throw new Error('No ETHAuth settings found')
28+
}
29+
30+
const expectedApp = proofSettings.app || 'app'
31+
const expectedOrigin = proofSettings.origin
32+
const expectedNonce = proofSettings.nonce
33+
const expectedExpiry = proofSettings.expiry ? Math.max(proofSettings.expiry, 200) : DEFAULT_SESSION_EXPIRATION
1834

1935
// if proof information was generated and saved upon wallet connection, use that
2036
if (proofInformation) {
2137
try {
2238
const decodedProof = await ethAuth.decodeProof(proofInformation.proofString, true)
23-
24-
if (decodedProof.address === normalizedWalletAddress) {
39+
const cachedExpiry =
40+
decodedProof.claims.exp && decodedProof.claims.iat ? decodedProof.claims.exp - decodedProof.claims.iat : null
41+
42+
const isMatchingProof =
43+
decodedProof.address === normalizedWalletAddress &&
44+
(decodedProof.claims.app || 'app') === expectedApp &&
45+
(decodedProof.claims.ogn ?? undefined) === (expectedOrigin ?? undefined) &&
46+
(decodedProof.claims.n ?? undefined) === (expectedNonce ?? undefined) &&
47+
cachedExpiry !== null &&
48+
Math.abs(cachedExpiry - expectedExpiry) <= 1
49+
50+
if (isMatchingProof) {
2551
return proofInformation
2652
}
2753

28-
await storage.removeItem(LocalStorageKey.EthAuthProof)
54+
await clearCachedProof()
2955
} catch {
30-
await storage.removeItem(LocalStorageKey.EthAuthProof)
56+
await clearCachedProof()
3157
}
3258
}
3359

34-
// generate a new proof
35-
const proofSettings = await storage.getItem(LocalStorageKey.EthAuthSettings)
36-
37-
if (!proofSettings) {
38-
throw new Error('No ETHAuth settings found')
39-
}
40-
4160
const proof = new Proof()
4261
proof.address = walletAddress
4362
proof.setIssuedAtNow()
4463

45-
proof.claims.app = proofSettings.app || 'app'
46-
proof.claims.ogn = proofSettings.origin
47-
proof.claims.n = proofSettings.nonce
64+
proof.claims.app = expectedApp
65+
proof.claims.ogn = expectedOrigin
66+
proof.claims.n = expectedNonce
4867

49-
proof.setExpiryIn(proofSettings.expiry ? Math.max(proofSettings.expiry, 200) : DEFAULT_SESSION_EXPIRATION)
68+
proof.setExpiryIn(expectedExpiry)
5069

5170
const typedData = proof.messageTypedData()
5271

0 commit comments

Comments
 (0)