Skip to content

Commit

Permalink
fix(extension): #32: save chainIn in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
VanishMax committed Jun 26, 2024
1 parent 402cd2f commit f4ade13
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const useGrpcEndpointForm = () => {
if (!appParameters?.chainId) throw new ConnectError('', Code.NotFound);

setIsSubmitButtonEnabled(true);
setChainId(appParameters.chainId);
void setChainId(appParameters.chainId);

// Only set the original chain ID the first time, so that we can compare
// it on submit.
Expand Down
11 changes: 11 additions & 0 deletions apps/extension/src/state/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ describe('Network Slice', () => {
expect(urlFromChromeStorage).toBe(testUrl);
});
});

describe('setChainId', () => {
test('grpc endpoint can be set', async () => {
const testChain = 'testnet-deimos-8';
await useStore.getState().network.setChainId(testChain);

expect(useStore.getState().network.chainId).toBe(testChain);
const idFromChromeStorage = await localStorage.get('chainId');
expect(idFromChromeStorage).toBe(testChain);
});
});
});
6 changes: 4 additions & 2 deletions apps/extension/src/state/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface NetworkSlice {
fullSyncHeight?: number;
chainId?: string;
setGRPCEndpoint: (endpoint: string) => Promise<void>;
setChainId: (chainId: string) => void;
setChainId: (chainId: string) => Promise<void>;
}

export const createNetworkSlice =
Expand All @@ -24,10 +24,12 @@ export const createNetworkSlice =

await local.set('grpcEndpoint', endpoint);
},
setChainId: (chainId: string) => {
setChainId: async (chainId: string) => {
set(state => {
state.network.chainId = chainId;
});

await local.set('chainId', chainId);
},
};
};
Expand Down
13 changes: 6 additions & 7 deletions apps/extension/src/state/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { LocalStorageState } from '../storage/types';
import { sessionExtStorage, SessionStorageState } from '../storage/session';
import { StorageItem } from '../storage/base';
import { walletsFromJson } from '@penumbra-zone/types/wallet';
import { AppParameters } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/app/v1/app_pb';

export type Middleware = <
T,
Expand All @@ -30,6 +29,7 @@ export const customPersistImpl: Persist = f => (set, get, store) => {
const passwordKey = await sessionExtStorage.get('passwordKey');
const wallets = await localExtStorage.get('wallets');
const grpcEndpoint = await localExtStorage.get('grpcEndpoint');
const chainId = await localExtStorage.get('chainId');
const knownSites = await localExtStorage.get('knownSites');
const frontendUrl = await localExtStorage.get('frontendUrl');
const numeraires = await localExtStorage.get('numeraires');
Expand All @@ -39,6 +39,7 @@ export const customPersistImpl: Persist = f => (set, get, store) => {
state.password.key = passwordKey;
state.wallets.all = walletsFromJson(wallets);
state.network.grpcEndpoint = grpcEndpoint;
state.network.chainId = chainId;
state.connectedSites.knownSites = knownSites;
state.defaultFrontend.url = frontendUrl;
state.numeraires.selectedNumeraires = numeraires;
Expand Down Expand Up @@ -122,15 +123,13 @@ function syncLocal(changes: Record<string, chrome.storage.StorageChange>, set: S
);
}

if (changes['params']) {
const stored = changes['params'].newValue as
| StorageItem<LocalStorageState['params']>
if (changes['chainId']) {
const stored = changes['chainId'].newValue as
| StorageItem<LocalStorageState['chainId']>
| undefined;
set(
produce((state: AllSlices) => {
state.network.chainId = stored?.value
? AppParameters.fromJsonString(stored.value).chainId
: state.network.chainId;
state.network.chainId = stored?.value ?? state.network.chainId;
}),
);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/src/storage/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const localDefaults: Required<LocalStorageState> = {
fullSyncHeight: undefined,
grpcEndpoint: undefined,
knownSites: [],
params: undefined,
chainId: undefined,
passwordKeyPrint: undefined,
frontendUrl: undefined,
numeraires: [],
Expand Down
3 changes: 1 addition & 2 deletions apps/extension/src/storage/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AppParameters } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/app/v1/app_pb';
import { KeyPrintJson } from '@penumbra-zone/crypto-web/encryption';
import { Stringified } from '@penumbra-zone/types/jsonified';
import { UserChoice } from '@penumbra-zone/types/user-choice';
Expand All @@ -20,9 +19,9 @@ export interface LocalStorageState {
wallets: WalletJson[];
grpcEndpoint: string | undefined;
frontendUrl: string | undefined;
chainId: string | undefined;
passwordKeyPrint: KeyPrintJson | undefined;
fullSyncHeight: number | undefined;
knownSites: OriginRecord[];
params: Stringified<AppParameters> | undefined;
numeraires: Stringified<AssetId>[];
}
2 changes: 1 addition & 1 deletion apps/extension/src/utils/test-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const localTestDefaults: LocalStorageState = {
wallets: [],
fullSyncHeight: undefined,
knownSites: [{ origin: EXAMPLE_MINIFRONT_URL, choice: UserChoice.Approved, date: Date.now() }],
params: undefined,
chainId: undefined,
grpcEndpoint: undefined,
passwordKeyPrint: undefined,
frontendUrl: EXAMPLE_MINIFRONT_URL,
Expand Down
7 changes: 2 additions & 5 deletions apps/extension/src/wallet-services.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AppParameters } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/app/v1/app_pb';
import { AppService } from '@penumbra-zone/protobuf';
import { createGrpcWebTransport } from '@connectrpc/connect-web';
import { createPromiseClient } from '@connectrpc/connect';
Expand Down Expand Up @@ -39,9 +38,7 @@ export const startWalletServices = async () => {
* local storage.
*/
const getChainId = async (baseUrl: string) => {
const localChainId = await localExtStorage
.get('params')
.then(json => json && AppParameters.fromJsonString(json).chainId);
const localChainId = await localExtStorage.get('chainId');

if (localChainId) return localChainId;

Expand Down Expand Up @@ -87,7 +84,7 @@ const attachServiceControlListener = ({
case ServicesMessage.ClearCache:
void (async () => {
blockProcessor.stop('clearCache');
await Promise.allSettled([localExtStorage.remove('params'), indexedDb.clear()]);
await Promise.allSettled([localExtStorage.remove('chainId'), indexedDb.clear()]);
})()
.then(() => respond())
.finally(() => chrome.runtime.reload());
Expand Down

0 comments on commit f4ade13

Please sign in to comment.