2022-10-03 18:12:34 +00:00
|
|
|
import { LocalStorage } from '@vegaprotocol/react-helpers';
|
2022-03-30 09:49:48 +00:00
|
|
|
import type { ReactNode } from 'react';
|
2022-10-03 18:12:34 +00:00
|
|
|
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
|
|
import type { VegaWalletContextShape } from '.';
|
|
|
|
import type {
|
|
|
|
PubKey,
|
|
|
|
Transaction,
|
|
|
|
VegaConnector,
|
|
|
|
} from './connectors/vega-connector';
|
2022-02-22 23:06:35 +00:00
|
|
|
import { VegaWalletContext } from './context';
|
2022-10-03 18:12:34 +00:00
|
|
|
import { WALLET_KEY } from './storage';
|
2022-02-22 06:40:55 +00:00
|
|
|
|
|
|
|
interface VegaWalletProviderProps {
|
|
|
|
children: ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
|
2022-10-03 18:12:34 +00:00
|
|
|
// Current selected pubKey
|
|
|
|
const [pubKey, setPubKey] = useState<string | null>(null);
|
2022-02-24 00:03:59 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
// Arary of pubkeys retrieved from the connector
|
|
|
|
const [pubKeys, setPubKeys] = useState<PubKey[] | null>(null);
|
2022-02-24 00:03:59 +00:00
|
|
|
|
|
|
|
// Reference to the current connector instance
|
2022-02-22 06:40:55 +00:00
|
|
|
const connector = useRef<VegaConnector | null>(null);
|
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
const selectPubKey = useCallback((pk: string) => {
|
|
|
|
setPubKey(pk);
|
|
|
|
LocalStorage.setItem(WALLET_KEY, pk);
|
|
|
|
}, []);
|
2022-03-20 01:31:44 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
const connect = useCallback(async (c: VegaConnector) => {
|
|
|
|
connector.current = c;
|
|
|
|
try {
|
|
|
|
const keys = await connector.current.connect();
|
2022-03-20 01:31:44 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
if (keys?.length) {
|
|
|
|
setPubKeys(keys);
|
2022-03-20 01:31:44 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
const lastUsedPubKey = LocalStorage.getItem(WALLET_KEY);
|
|
|
|
const foundKey = keys.find((key) => key.publicKey === lastUsedPubKey);
|
|
|
|
if (foundKey) {
|
|
|
|
setPubKey(foundKey.publicKey);
|
|
|
|
} else {
|
|
|
|
setPubKey(keys[0].publicKey);
|
2022-03-20 01:31:44 +00:00
|
|
|
}
|
2022-02-23 19:24:30 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
return keys;
|
|
|
|
} else {
|
2022-03-08 18:57:07 +00:00
|
|
|
return null;
|
2022-02-23 19:24:30 +00:00
|
|
|
}
|
2022-10-03 18:12:34 +00:00
|
|
|
} catch (err) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}, []);
|
2022-02-22 06:40:55 +00:00
|
|
|
|
|
|
|
const disconnect = useCallback(async () => {
|
|
|
|
try {
|
2022-02-22 23:06:35 +00:00
|
|
|
await connector.current?.disconnect();
|
2022-10-03 18:12:34 +00:00
|
|
|
setPubKeys(null);
|
|
|
|
setPubKey(null);
|
2022-02-22 06:40:55 +00:00
|
|
|
connector.current = null;
|
2022-03-25 07:43:49 +00:00
|
|
|
LocalStorage.removeItem(WALLET_KEY);
|
2022-03-08 18:57:07 +00:00
|
|
|
return true;
|
2022-02-22 06:40:55 +00:00
|
|
|
} catch (err) {
|
2022-02-23 05:39:12 +00:00
|
|
|
console.error(err);
|
2022-03-08 18:57:07 +00:00
|
|
|
return false;
|
2022-02-22 06:40:55 +00:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
const sendTx = useCallback((pubkey: string, transaction: Transaction) => {
|
2022-05-17 13:04:41 +00:00
|
|
|
if (!connector.current) {
|
2022-10-03 18:12:34 +00:00
|
|
|
throw new Error('No connector');
|
2022-05-17 13:04:41 +00:00
|
|
|
}
|
2022-03-10 00:39:59 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
return connector.current.sendTx(pubkey, transaction);
|
2022-05-17 13:04:41 +00:00
|
|
|
}, []);
|
2022-03-10 00:39:59 +00:00
|
|
|
|
2022-02-23 05:05:39 +00:00
|
|
|
const contextValue = useMemo<VegaWalletContextShape>(() => {
|
2022-02-22 06:40:55 +00:00
|
|
|
return {
|
2022-10-03 18:12:34 +00:00
|
|
|
pubKey,
|
|
|
|
pubKeys,
|
|
|
|
selectPubKey,
|
2022-02-22 06:40:55 +00:00
|
|
|
connect,
|
|
|
|
disconnect,
|
2022-03-10 00:39:59 +00:00
|
|
|
sendTx,
|
2022-10-03 18:12:34 +00:00
|
|
|
};
|
|
|
|
}, [pubKey, pubKeys, selectPubKey, connect, disconnect, sendTx]);
|
2022-02-22 06:40:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<VegaWalletContext.Provider value={contextValue}>
|
|
|
|
{children}
|
|
|
|
</VegaWalletContext.Provider>
|
|
|
|
);
|
|
|
|
};
|