2022-03-14 13:18:11 +00:00
|
|
|
import { LocalStorage } from '@vegaprotocol/react-helpers';
|
2022-02-24 00:03:59 +00:00
|
|
|
import {
|
|
|
|
ReactNode,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'react';
|
2022-02-23 05:05:39 +00:00
|
|
|
import { VegaKeyExtended, VegaWalletContextShape } from '.';
|
2022-02-22 23:06:35 +00:00
|
|
|
import { VegaConnector } from './connectors';
|
|
|
|
import { VegaWalletContext } from './context';
|
2022-03-08 18:57:07 +00:00
|
|
|
import { WALLET_KEY } from './storage-keys';
|
2022-03-10 00:39:59 +00:00
|
|
|
import { OrderSubmissionBody } from '@vegaprotocol/vegawallet-service-api-client';
|
2022-02-22 06:40:55 +00:00
|
|
|
|
|
|
|
interface VegaWalletProviderProps {
|
|
|
|
children: ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
|
2022-02-24 00:03:59 +00:00
|
|
|
// Current selected publicKey, default with value from local storage
|
|
|
|
const [publicKey, setPublicKey] = useState<string | null>(() => {
|
2022-03-08 18:57:07 +00:00
|
|
|
const pk = LocalStorage.getItem(WALLET_KEY);
|
2022-02-24 00:03:59 +00:00
|
|
|
return pk ? pk : null;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Keypair objects retrieved from the connector
|
|
|
|
const [keypairs, setKeypairs] = useState<VegaKeyExtended[] | null>(null);
|
|
|
|
|
|
|
|
// Reference to the current connector instance
|
2022-02-22 06:40:55 +00:00
|
|
|
const connector = useRef<VegaConnector | null>(null);
|
|
|
|
|
|
|
|
const connect = useCallback(async (c: VegaConnector) => {
|
|
|
|
connector.current = c;
|
|
|
|
try {
|
2022-02-23 05:05:39 +00:00
|
|
|
const res = await connector.current.connect();
|
2022-02-23 19:24:30 +00:00
|
|
|
|
|
|
|
if (!res) {
|
2022-03-08 18:57:07 +00:00
|
|
|
return null;
|
2022-02-23 19:24:30 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 05:05:39 +00:00
|
|
|
const publicKeysWithName = res.map((pk) => {
|
|
|
|
const nameMeta = pk.meta?.find((m) => m.key === 'name');
|
|
|
|
return {
|
|
|
|
...pk,
|
|
|
|
name: nameMeta?.value ? nameMeta.value : 'None',
|
|
|
|
};
|
|
|
|
});
|
2022-02-24 00:03:59 +00:00
|
|
|
setKeypairs(publicKeysWithName);
|
2022-03-08 18:57:07 +00:00
|
|
|
return publicKeysWithName;
|
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 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-02-24 00:03:59 +00:00
|
|
|
setKeypairs(null);
|
2022-02-22 06:40:55 +00:00
|
|
|
connector.current = null;
|
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-03-10 00:39:59 +00:00
|
|
|
const sendTx = useCallback(async (body: OrderSubmissionBody) => {
|
|
|
|
if (!connector.current) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return connector.current.sendTx(body);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2022-02-24 00:03:59 +00:00
|
|
|
// Current selected keypair derived from publicKey state
|
|
|
|
const keypair = useMemo(() => {
|
|
|
|
const found = keypairs?.find((x) => x.pub === publicKey);
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}, [publicKey, keypairs]);
|
|
|
|
|
|
|
|
// Whenever selected public key changes store it
|
|
|
|
useEffect(() => {
|
|
|
|
if (publicKey) {
|
2022-03-08 18:57:07 +00:00
|
|
|
LocalStorage.setItem(WALLET_KEY, publicKey);
|
2022-02-24 00:03:59 +00:00
|
|
|
}
|
|
|
|
}, [publicKey]);
|
|
|
|
|
2022-02-23 05:05:39 +00:00
|
|
|
const contextValue = useMemo<VegaWalletContextShape>(() => {
|
2022-02-22 06:40:55 +00:00
|
|
|
return {
|
2022-02-24 00:03:59 +00:00
|
|
|
keypair,
|
|
|
|
keypairs,
|
|
|
|
selectPublicKey: setPublicKey,
|
2022-02-22 06:40:55 +00:00
|
|
|
connect,
|
|
|
|
disconnect,
|
|
|
|
connector: connector.current,
|
2022-03-10 00:39:59 +00:00
|
|
|
sendTx,
|
2022-02-22 06:40:55 +00:00
|
|
|
};
|
2022-03-10 00:39:59 +00:00
|
|
|
}, [keypair, keypairs, setPublicKey, connect, disconnect, connector, sendTx]);
|
2022-02-22 06:40:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<VegaWalletContext.Provider value={contextValue}>
|
|
|
|
{children}
|
|
|
|
</VegaWalletContext.Provider>
|
|
|
|
);
|
|
|
|
};
|