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);
36 const resetConnector = () => {
37 const newConnector = new WalletConnect({
38 bridge: 'https://bridge.walletconnect.org',
39 qrcodeModal: QRCodeModal,
42 setConnector(newConnector);
44 // restore state from cached connection
45 if (newConnector.accounts.length > 0) {
46 setAddress(newConnector.accounts[0]);
49 if (newConnector.peerMeta) {
50 setPeerMeta(newConnector.peerMeta);
57 // eslint-disable-next-line react-hooks/exhaustive-deps
61 const connect = () => {
62 if (!connector || connector.connected) {
66 connector.createSession();
69 const disconnect = async () => {
74 await connector.killSession();
77 type AuthenticateArgs = {
81 const authenticate = async ({
82 hdPath = DEFAULT_HD_PATH,
83 }: AuthenticateArgs) => {
84 if (!(address && connector)) {
88 const authToken = await getAuthToken({
91 walletType: WALLET_TYPES.WALLET_CONNECT,
94 // Inspired by the MetamaskWallet implementation :)
95 // TODO: We should refactor / unify how we handle wallets
96 const wallet: WalletConnectWallet = {
100 setAuthToken(Just(authToken));
101 setWallet(Just(wallet));
102 setWalletHdPath(hdPath);
105 const isConnected = () => {
110 return connector.connected;
113 const initConnectHandler = () => {
118 connector.on('connect', async (error, payload: ConnectEvent) => {
123 const address = payload.params[0].accounts[0];
125 setPeerMeta(payload.params[0].peerMeta);
129 const initDisconnectHandler = () => {
134 connector.on('disconnect', (error, _payload) => {
145 const initSessionUpdateHandler = () => {
150 connector.on('session_update', (error, payload) => {
155 // TODO: Confirm what should happen on update?
156 // For now, disconnect and logout
161 const initModalClosedHandler = () => {
166 connector.on('modal_closed', () => {
178 initConnectHandler();
179 initDisconnectHandler();
180 initSessionUpdateHandler();
181 initModalClosedHandler();
184 // Clean up listeners
185 connector.off('connect');
186 connector.off('disconnect');
187 connector.off('session_update');
189 // 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