1import WalletConnect from '@walletconnect/client';
2import QRCodeModal from '@walletconnect/qrcode-modal';
3import { ITxData } from '@walletconnect/types';
5import { useEffect, useMemo, useState } from 'react';
6import { Just, Nothing } from 'folktale/maybe';
8import { useWallet } from 'store/wallet';
9import { getAuthToken } from './authToken';
10import { WALLET_TYPES } from './constants';
11import WalletConnectWallet from './types/WalletConnectWallet';
32export const useWalletConnect = () => {
33 const { setWallet, setAuthToken, resetWallet }: any = useWallet();
34 const [connector, setConnector] = useState<WalletConnect | null>(null);
35 const [address, setAddress] = useState<string | null>(null);
36 const [peerMeta, setPeerMeta] = useState<PeerMeta | null>(null);
38 const resetConnector = () => {
39 const newConnector = new WalletConnect({
40 bridge: 'https://bridge.walletconnect.org',
41 qrcodeModal: QRCodeModal,
44 setConnector(newConnector);
46 // restore state from cached connection
47 if (newConnector.accounts.length > 0) {
48 setAddress(newConnector.accounts[0]);
51 if (newConnector.peerMeta) {
52 setPeerMeta(newConnector.peerMeta);
59 // eslint-disable-next-line react-hooks/exhaustive-deps
63 const connect = async () => {
64 if (!connector || connector.connected) {
68 await connector.createSession();
71 const disconnect = async () => {
76 await connector.killSession();
79 const authenticate = async () => {
80 if (!(address && connector)) {
84 let authToken = Nothing();
86 const token = await getAuthToken({
89 walletType: WALLET_TYPES.WALLET_CONNECT,
91 authToken = Just(token);
93 if (e.message === 'METHOD_NOT_SUPPORTED') {
95 'wallet does not support message signing. proceeding without auth token.'
99 //TODO should errors with this *really* prevent login?
103 const wallet: WalletConnectWallet = {
107 setAuthToken(authToken);
108 setWallet(Just(wallet));
111 const isConnected = () => {
116 return connector.connected;
119 const peerIcon = useMemo(() => {
120 if (!peerMeta?.icons) {
124 // Some peers return a list of empty string(s) :)
125 const iconCandidates = peerMeta.icons.filter(pm => pm !== '');
127 return iconCandidates.length > 0 ? iconCandidates[0] : null;
130 const signTransaction = async ({
138 }: ITxData): Promise<string> => {
139 return new Promise((resolve, reject) => {
140 if (!connector || !isConnected()) {
141 reject(new Error('No connected wallet available for signing'));
155 .then((signature: string) => resolve(signature))
156 .catch((error: Error) => reject(error));
160 const sendTransaction = async ({
168 }: ITxData): Promise<string> => {
169 return new Promise((resolve, reject) => {
170 if (!connector || !isConnected()) {
171 reject(new Error('No connected wallet available for sending'));
175 //REVIEW .then path untested
186 .then((txHash: string) => resolve(txHash))
187 .catch((error: Error) => reject(error));
192 const initConnectHandler = () => {
197 connector.on('connect', async (error, payload: ConnectEvent) => {
202 const address = payload.params[0].accounts[0];
204 setPeerMeta(payload.params[0].peerMeta);
208 const initDisconnectHandler = () => {
213 connector.on('disconnect', (error, _payload) => {
224 const initSessionUpdateHandler = () => {
229 connector.on('session_update', (error, payload) => {
238 const initModalClosedHandler = () => {
243 connector.on('modal_closed', () => {
255 initConnectHandler();
256 initDisconnectHandler();
257 initSessionUpdateHandler();
258 initModalClosedHandler();
261 // Clean up listeners
262 connector.off('connect');
263 connector.off('disconnect');
264 connector.off('session_update');
265 connector.off('modal_closed');
267 // 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