1import WalletConnect from '@walletconnect/client';
2import QRCodeModal from '@walletconnect/qrcode-modal';
3import { useEffect, useState } from 'react';
4import { Just } from 'folktale/maybe';
6import { useWallet } from 'store/wallet';
7import { getAuthToken } from './authToken';
8import { DEFAULT_HD_PATH, WALLET_TYPES } from './constants';
9import WalletConnectWallet from './types/WalletConnectWallet';
30export const useWalletConnect = () => {
31 const { setWallet, setWalletHdPath, setAuthToken, resetWallet } = useWallet();
32 const [connector, setConnector] = useState<WalletConnect | null>(null);
33 const [address, setAddress] = useState<string | null>(null);
34 const [peerMeta, setPeerMeta] = useState<PeerMeta | null>(null);
38 const newConnector = new WalletConnect({
39 bridge: 'https://bridge.walletconnect.org',
40 qrcodeModal: QRCodeModal,
43 setConnector(newConnector);
45 // restore state from cached connection
46 if (newConnector.accounts.length > 0) {
47 setAddress(newConnector.accounts[0]);
50 if (newConnector.peerMeta) {
51 setPeerMeta(newConnector.peerMeta);
53 // eslint-disable-next-line react-hooks/exhaustive-deps
57 const connect = () => {
58 if (!connector || connector.connected) {
62 connector.createSession();
65 const disconnect = () => {
67 console.warn('Nothing to disconnect');
71 connector.killSession();
74 type AuthenticateArgs = {
78 const authenticate = async ({
79 hdPath = DEFAULT_HD_PATH,
80 }: AuthenticateArgs) => {
81 if (!(address && connector)) {
85 const authToken = await getAuthToken({
88 walletType: WALLET_TYPES.WALLET_CONNECT,
91 // Inspired by the MetamaskWallet implementation :)
92 // TODO: We should refactor / unify how we handle wallets
93 const wallet: WalletConnectWallet = {
97 setAuthToken(Just(authToken));
98 setWallet(Just(wallet));
99 setWalletHdPath(hdPath);
102 const isConnected = () => {
107 return connector.connected;
110 const initConnectHandler = () => {
115 connector.on('connect', async (error, payload: ConnectEvent) => {
120 const address = payload.params[0].accounts[0];
122 setPeerMeta(payload.params[0].peerMeta);
126 const initDisconnectHandler = () => {
131 connector.on('disconnect', (error, _payload) => {
142 const initSessionUpdateHandler = () => {
147 connector.on('session_update', (error, payload) => {
152 // TODO: Confirm what should happen on update?
153 // For now, disconnect and logout
164 initConnectHandler();
165 initDisconnectHandler();
166 initSessionUpdateHandler();
169 // Clean up listeners
170 connector.off('connect');
171 connector.off('disconnect');
172 connector.off('session_update');
174 // eslint-disable-next-line react-hooks/exhaustive-deps
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list