on master help sign in
Merge pull request #1013 from urbit/t/skip-stale-cache-when-reticketing

invite: clear stored invite from cache on reticket

commitb87eb420537f4ae7c543ae6487aaa1ca7d3bb791 authortomholford <16504501+tomholford@users.noreply.github.com> dateTue 5 Apr 2022, 05:36 (5 years ago) parentc3a479d15bea83
2 files changed+31−15
src/store/storage/roller.ts+14 src/views/Invite/useInvites.ts+17−15
src/store/storage/roller.ts+14
@@ -34,6 +34,20 @@ export const setStoredInvite = (ls: SecureLS, newInvite: Invite) => {
3434 ls.set(INVITES_KEY, invites);
3535};
3636
37+export const removeStoredInvite = (ls: SecureLS, key: number) => {
38+ let invites: InviteTickets = {};
39+ try {
40+ invites = ls.get(INVITES_KEY) || {};
41+ } catch (e) {}
42+
43+ if (key in invites) {
44+ let { [key]: _, ...filtered } = invites;
45+ invites = filtered;
46+ }
47+
48+ ls.set(INVITES_KEY, invites);
49+};
50+
3751export const clearInvitesStorage = () => localStorage.removeItem(INVITES_KEY);
3852
3953export const getHideMigrationMessage = () =>
src/views/Invite/useInvites.ts+17−15
@@ -13,12 +13,13 @@ import {
1313 submitL2Transaction,
1414 registerProxyAddress,
1515} from 'lib/utils/roller';
16−import { useCallback, useRef } from 'react';
16+import { useCallback } from 'react';
1717import { useNetwork } from 'store/network';
1818import { usePointCursor } from 'store/pointCursor';
1919import { useRollerStore } from 'store/rollerStore';
2020import {
2121 getStoredInvites,
22+ removeStoredInvite,
2223 setStoredInvite,
2324 setStoredInvites,
2425} from 'store/storage/roller';
@@ -170,21 +171,21 @@ export function useInvites() {
170171 const invite =
171172 storedInvite || (await generateInviteInfo(planet, _authToken));
172173
173− // TODO: check if the invite point's owner still matches the deterministic wallet address
174174 const planetInfo = await api.getPoint(planet);
175− setStoredInvite(ls, invite);
176−
175+ // unclaimed point: if still owned by the deterministic invite wallet
177176 if (
178177 invite.owner.toLowerCase() ===
179178 planetInfo.ownership?.owner?.address &&
180179 planetInfo.dominion === POINT_DOMINIONS.L2
181180 ) {
181+ setStoredInvite(ls, invite);
182182 addInvite(curPoint, invite);
183183 newInvites.push(invite);
184− }
185−
186− if (storedInvite) {
187− setStoredInvite(ls, invite);
184+ } else {
185+ // The point has been claimed, remove if it's in the cache
186+ if (storedInvite) {
187+ removeStoredInvite(ls, planet);
188+ }
188189 }
189190 }
190191
@@ -225,14 +226,8 @@ export function useInvites() {
225226 throw new Error('Internal Error: Missing Contracts/Web3/Wallet');
226227 }
227228
228− // TODO: remove when UI disables management proxy from spawning invites
229− // if (Just.hasInstance(authMnemonic)) {
230− // throw new Error(
231− // "Auth key Error: A management mnemonic can't create invite codes"
232− // );
233− // }
234−
235229 const invites: Invite[] = [];
230+ const storedInvites = getStoredInvites(ls);
236231
237232 const proxy = point.getSpawnProxy();
238233
@@ -249,6 +244,13 @@ export function useInvites() {
249244 const planet = planets[i];
250245 const nonceInc = i + nonce;
251246
247+ // When reticketing a planet in a browser that previously generated
248+ // the invite, we need to remove it from the cache.
249+ const storedInvite = storedInvites[planet];
250+ if (storedInvite) {
251+ removeStoredInvite(ls, planet);
252+ }
253+
252254 const { ticket, inviteWallet } = await generateInviteWallet(
253255 planet,
254256 _authToken