tidy up storage keys in wallet provider

This commit is contained in:
Matthew Russell 2022-03-08 10:57:07 -08:00
parent 847d51e060
commit fdc5e68158
7 changed files with 21 additions and 12 deletions

View File

@ -5,6 +5,7 @@ import {
useVegaWallet,
VegaConnectDialog,
VegaWalletProvider,
WALLET_CONFIG,
} from '@vegaprotocol/react-helpers';
import { Connectors } from '../lib/connectors';
import { useCallback, useEffect, useMemo, useState } from 'react';
@ -115,7 +116,7 @@ function useEagerConnect() {
const { connect } = useVegaWallet();
useEffect(() => {
const cfg = LocalStorage.getItem('vega_wallet');
const cfg = LocalStorage.getItem(WALLET_CONFIG);
const cfgObj = JSON.parse(cfg);
// No stored config, user has never connected or manually cleared storage

View File

@ -5,6 +5,7 @@ import {
VegaKey,
} from '@vegaprotocol/vegawallet-service-api-client';
import { LocalStorage } from '@vegaprotocol/storage';
import { WALLET_CONFIG } from './storage-keys';
export interface VegaConnector {
/** Description of how to use this connector */
@ -28,7 +29,7 @@ interface RestConnectorConfig {
* Connector for using the Vega Wallet Service rest api, requires authentication to get a session token
*/
export class RestConnector implements VegaConnector {
static storageKey = 'vega_wallet';
configKey = WALLET_CONFIG;
apiConfig: Configuration;
service: DefaultApi;
description = 'Connects using REST to the Vega wallet desktop app';
@ -95,11 +96,11 @@ export class RestConnector implements VegaConnector {
}
private setConfig(cfg: RestConnectorConfig) {
LocalStorage.setItem(RestConnector.storageKey, JSON.stringify(cfg));
LocalStorage.setItem(this.configKey, JSON.stringify(cfg));
}
private getConfig(): RestConnectorConfig | null {
const cfg = LocalStorage.getItem(RestConnector.storageKey);
const cfg = LocalStorage.getItem(this.configKey);
if (cfg) {
try {
const obj = JSON.parse(cfg);
@ -113,7 +114,7 @@ export class RestConnector implements VegaConnector {
}
private clearConfig() {
LocalStorage.removeItem(RestConnector.storageKey);
LocalStorage.removeItem(this.configKey);
}
}

View File

@ -14,10 +14,10 @@ export interface VegaWalletContextShape {
keypairs: VegaKeyExtended[] | null;
/** Calls connect on the supplied connector, storing the returned keys */
connect: (connector: VegaConnector) => Promise<void>;
connect: (connector: VegaConnector) => Promise<VegaKey[] | null>;
/** Disconnects from the connector and clears public key state */
disconnect: () => Promise<void>;
disconnect: () => Promise<boolean>;
/** Sets the current selected public key */
selectPublicKey: (publicKey: string) => void;

View File

@ -3,3 +3,4 @@ export * from './context';
export * from './hooks';
export * from './connect-dialog';
export * from './connectors';
export * from './storage-keys';

View File

@ -10,6 +10,7 @@ import {
import { VegaKeyExtended, VegaWalletContextShape } from '.';
import { VegaConnector } from './connectors';
import { VegaWalletContext } from './context';
import { WALLET_KEY } from './storage-keys';
interface VegaWalletProviderProps {
children: ReactNode;
@ -18,7 +19,7 @@ interface VegaWalletProviderProps {
export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
// Current selected publicKey, default with value from local storage
const [publicKey, setPublicKey] = useState<string | null>(() => {
const pk = LocalStorage.getItem('vega_selected_publickey');
const pk = LocalStorage.getItem(WALLET_KEY);
return pk ? pk : null;
});
@ -34,8 +35,7 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
const res = await connector.current.connect();
if (!res) {
console.log('connect failed', res);
return;
return null;
}
const publicKeysWithName = res.map((pk) => {
@ -46,8 +46,10 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
};
});
setKeypairs(publicKeysWithName);
return publicKeysWithName;
} catch (err) {
console.error(err);
return null;
}
}, []);
@ -56,8 +58,10 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
await connector.current?.disconnect();
setKeypairs(null);
connector.current = null;
return true;
} catch (err) {
console.error(err);
return false;
}
}, []);
@ -75,7 +79,7 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
// Whenever selected public key changes store it
useEffect(() => {
if (publicKey) {
LocalStorage.setItem('vega_selected_publickey', publicKey);
LocalStorage.setItem(WALLET_KEY, publicKey);
}
}, [publicKey]);

View File

@ -1,5 +1,5 @@
import { Button, FormGroup, Input, InputError } from '@vegaprotocol/ui-toolkit';
import { useEffect, useState } from 'react';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { RestConnector } from '.';

View File

@ -0,0 +1,2 @@
export const WALLET_CONFIG = 'vega_wallet_config';
export const WALLET_KEY = 'vega_wallet_key';