tidy up storage keys in wallet provider
This commit is contained in:
parent
847d51e060
commit
fdc5e68158
@ -5,6 +5,7 @@ import {
|
|||||||
useVegaWallet,
|
useVegaWallet,
|
||||||
VegaConnectDialog,
|
VegaConnectDialog,
|
||||||
VegaWalletProvider,
|
VegaWalletProvider,
|
||||||
|
WALLET_CONFIG,
|
||||||
} from '@vegaprotocol/react-helpers';
|
} from '@vegaprotocol/react-helpers';
|
||||||
import { Connectors } from '../lib/connectors';
|
import { Connectors } from '../lib/connectors';
|
||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
@ -115,7 +116,7 @@ function useEagerConnect() {
|
|||||||
const { connect } = useVegaWallet();
|
const { connect } = useVegaWallet();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const cfg = LocalStorage.getItem('vega_wallet');
|
const cfg = LocalStorage.getItem(WALLET_CONFIG);
|
||||||
const cfgObj = JSON.parse(cfg);
|
const cfgObj = JSON.parse(cfg);
|
||||||
|
|
||||||
// No stored config, user has never connected or manually cleared storage
|
// No stored config, user has never connected or manually cleared storage
|
||||||
|
@ -5,6 +5,7 @@ import {
|
|||||||
VegaKey,
|
VegaKey,
|
||||||
} from '@vegaprotocol/vegawallet-service-api-client';
|
} from '@vegaprotocol/vegawallet-service-api-client';
|
||||||
import { LocalStorage } from '@vegaprotocol/storage';
|
import { LocalStorage } from '@vegaprotocol/storage';
|
||||||
|
import { WALLET_CONFIG } from './storage-keys';
|
||||||
|
|
||||||
export interface VegaConnector {
|
export interface VegaConnector {
|
||||||
/** Description of how to use this connector */
|
/** 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
|
* Connector for using the Vega Wallet Service rest api, requires authentication to get a session token
|
||||||
*/
|
*/
|
||||||
export class RestConnector implements VegaConnector {
|
export class RestConnector implements VegaConnector {
|
||||||
static storageKey = 'vega_wallet';
|
configKey = WALLET_CONFIG;
|
||||||
apiConfig: Configuration;
|
apiConfig: Configuration;
|
||||||
service: DefaultApi;
|
service: DefaultApi;
|
||||||
description = 'Connects using REST to the Vega wallet desktop app';
|
description = 'Connects using REST to the Vega wallet desktop app';
|
||||||
@ -95,11 +96,11 @@ export class RestConnector implements VegaConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private setConfig(cfg: RestConnectorConfig) {
|
private setConfig(cfg: RestConnectorConfig) {
|
||||||
LocalStorage.setItem(RestConnector.storageKey, JSON.stringify(cfg));
|
LocalStorage.setItem(this.configKey, JSON.stringify(cfg));
|
||||||
}
|
}
|
||||||
|
|
||||||
private getConfig(): RestConnectorConfig | null {
|
private getConfig(): RestConnectorConfig | null {
|
||||||
const cfg = LocalStorage.getItem(RestConnector.storageKey);
|
const cfg = LocalStorage.getItem(this.configKey);
|
||||||
if (cfg) {
|
if (cfg) {
|
||||||
try {
|
try {
|
||||||
const obj = JSON.parse(cfg);
|
const obj = JSON.parse(cfg);
|
||||||
@ -113,7 +114,7 @@ export class RestConnector implements VegaConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private clearConfig() {
|
private clearConfig() {
|
||||||
LocalStorage.removeItem(RestConnector.storageKey);
|
LocalStorage.removeItem(this.configKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,10 +14,10 @@ export interface VegaWalletContextShape {
|
|||||||
keypairs: VegaKeyExtended[] | null;
|
keypairs: VegaKeyExtended[] | null;
|
||||||
|
|
||||||
/** Calls connect on the supplied connector, storing the returned keys */
|
/** 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 */
|
/** Disconnects from the connector and clears public key state */
|
||||||
disconnect: () => Promise<void>;
|
disconnect: () => Promise<boolean>;
|
||||||
|
|
||||||
/** Sets the current selected public key */
|
/** Sets the current selected public key */
|
||||||
selectPublicKey: (publicKey: string) => void;
|
selectPublicKey: (publicKey: string) => void;
|
||||||
|
@ -3,3 +3,4 @@ export * from './context';
|
|||||||
export * from './hooks';
|
export * from './hooks';
|
||||||
export * from './connect-dialog';
|
export * from './connect-dialog';
|
||||||
export * from './connectors';
|
export * from './connectors';
|
||||||
|
export * from './storage-keys';
|
||||||
|
@ -10,6 +10,7 @@ import {
|
|||||||
import { VegaKeyExtended, VegaWalletContextShape } from '.';
|
import { VegaKeyExtended, VegaWalletContextShape } from '.';
|
||||||
import { VegaConnector } from './connectors';
|
import { VegaConnector } from './connectors';
|
||||||
import { VegaWalletContext } from './context';
|
import { VegaWalletContext } from './context';
|
||||||
|
import { WALLET_KEY } from './storage-keys';
|
||||||
|
|
||||||
interface VegaWalletProviderProps {
|
interface VegaWalletProviderProps {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
@ -18,7 +19,7 @@ interface VegaWalletProviderProps {
|
|||||||
export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
|
export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
|
||||||
// Current selected publicKey, default with value from local storage
|
// Current selected publicKey, default with value from local storage
|
||||||
const [publicKey, setPublicKey] = useState<string | null>(() => {
|
const [publicKey, setPublicKey] = useState<string | null>(() => {
|
||||||
const pk = LocalStorage.getItem('vega_selected_publickey');
|
const pk = LocalStorage.getItem(WALLET_KEY);
|
||||||
return pk ? pk : null;
|
return pk ? pk : null;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -34,8 +35,7 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
|
|||||||
const res = await connector.current.connect();
|
const res = await connector.current.connect();
|
||||||
|
|
||||||
if (!res) {
|
if (!res) {
|
||||||
console.log('connect failed', res);
|
return null;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const publicKeysWithName = res.map((pk) => {
|
const publicKeysWithName = res.map((pk) => {
|
||||||
@ -46,8 +46,10 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
setKeypairs(publicKeysWithName);
|
setKeypairs(publicKeysWithName);
|
||||||
|
return publicKeysWithName;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@ -56,8 +58,10 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
|
|||||||
await connector.current?.disconnect();
|
await connector.current?.disconnect();
|
||||||
setKeypairs(null);
|
setKeypairs(null);
|
||||||
connector.current = null;
|
connector.current = null;
|
||||||
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@ -75,7 +79,7 @@ export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
|
|||||||
// Whenever selected public key changes store it
|
// Whenever selected public key changes store it
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (publicKey) {
|
if (publicKey) {
|
||||||
LocalStorage.setItem('vega_selected_publickey', publicKey);
|
LocalStorage.setItem(WALLET_KEY, publicKey);
|
||||||
}
|
}
|
||||||
}, [publicKey]);
|
}, [publicKey]);
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Button, FormGroup, Input, InputError } from '@vegaprotocol/ui-toolkit';
|
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 { useForm } from 'react-hook-form';
|
||||||
import { RestConnector } from '.';
|
import { RestConnector } from '.';
|
||||||
|
|
||||||
|
2
libs/react-helpers/src/lib/vega-wallet/storage-keys.ts
Normal file
2
libs/react-helpers/src/lib/vega-wallet/storage-keys.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export const WALLET_CONFIG = 'vega_wallet_config';
|
||||||
|
export const WALLET_KEY = 'vega_wallet_key';
|
Loading…
Reference in New Issue
Block a user