2022-03-31 01:08:25 +00:00
|
|
|
import { LocalStorage, t } from '@vegaprotocol/react-helpers';
|
2022-03-30 09:49:48 +00:00
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
|
|
import type { VegaKeyExtended, VegaWalletContextShape } from '.';
|
2022-06-29 09:03:20 +00:00
|
|
|
import type { VegaConnector } from './connectors/vega-connector';
|
2022-02-22 23:06:35 +00:00
|
|
|
import { VegaWalletContext } from './context';
|
2022-03-08 18:57:07 +00:00
|
|
|
import { WALLET_KEY } from './storage-keys';
|
2022-07-13 14:23:46 +00:00
|
|
|
import type { TransactionSubmission } from './wallet-types';
|
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);
|
|
|
|
|
2022-03-20 01:31:44 +00:00
|
|
|
const connect = useCallback(
|
|
|
|
async (c: VegaConnector) => {
|
|
|
|
connector.current = c;
|
|
|
|
try {
|
|
|
|
const res = await connector.current.connect();
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const publicKeysWithName = res.map((pk) => {
|
|
|
|
const nameMeta = pk.meta?.find((m) => m.key === 'name');
|
|
|
|
return {
|
|
|
|
...pk,
|
2022-03-31 01:08:25 +00:00
|
|
|
name: nameMeta?.value ? nameMeta.value : t('None'),
|
2022-03-20 01:31:44 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
setKeypairs(publicKeysWithName);
|
|
|
|
if (publicKey === null) {
|
|
|
|
setPublicKey(publicKeysWithName[0].pub);
|
|
|
|
}
|
2022-02-23 19:24:30 +00:00
|
|
|
|
2022-03-20 01:31:44 +00:00
|
|
|
return publicKeysWithName;
|
|
|
|
} catch (err) {
|
2022-03-08 18:57:07 +00:00
|
|
|
return null;
|
2022-02-23 19:24:30 +00:00
|
|
|
}
|
2022-03-20 01:31:44 +00:00
|
|
|
},
|
|
|
|
[publicKey]
|
|
|
|
);
|
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-06-29 09:52:00 +00:00
|
|
|
setPublicKey(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-05-17 13:04:41 +00:00
|
|
|
const sendTx = useCallback((body: TransactionSubmission) => {
|
|
|
|
if (!connector.current) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-10 00:39:59 +00:00
|
|
|
|
2022-05-17 13:04:41 +00:00
|
|
|
return connector.current.sendTx(body);
|
|
|
|
}, []);
|
2022-03-10 00:39:59 +00:00
|
|
|
|
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-06-29 09:03:20 +00:00
|
|
|
} as VegaWalletContextShape;
|
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>
|
|
|
|
);
|
|
|
|
};
|