Handle case where wallet rejects the pairing request (#7)

* Handle case where pairing request is rejected

* Remove unused import

---------

Co-authored-by: Adw8 <adwait@deepstacksoft.com>
This commit is contained in:
Adwait Gharpure 2024-03-26 11:40:28 +05:30 committed by GitHub
parent 229d9e62d3
commit 6505899598
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 45 deletions

View File

@ -7,6 +7,7 @@ import React, {
useEffect, useEffect,
useCallback, useCallback,
} from "react"; } from "react";
import { SnackbarProvider, enqueueSnackbar } from "notistack";
import SignClient from "@walletconnect/sign-client"; import SignClient from "@walletconnect/sign-client";
import { WalletConnectModal } from "@walletconnect/modal"; import { WalletConnectModal } from "@walletconnect/modal";
@ -20,7 +21,7 @@ interface ContextValue {
connect: () => Promise<void>; connect: () => Promise<void>;
session: SessionTypes.Struct | null; session: SessionTypes.Struct | null;
signClient: SignClient | undefined; signClient: SignClient | undefined;
checkPersistedState: (client: SignClient)=> Promise<void> checkPersistedState: (client: SignClient) => Promise<void>;
disconnect: () => Promise<void>; disconnect: () => Promise<void>;
} }
@ -28,7 +29,7 @@ const walletConnectContext = createContext<ContextValue>({
connect: () => Promise.resolve(), connect: () => Promise.resolve(),
session: null, session: null,
signClient: undefined, signClient: undefined,
checkPersistedState: ()=> Promise.resolve(), checkPersistedState: () => Promise.resolve(),
disconnect: () => Promise.resolve(), disconnect: () => Promise.resolve(),
}); });
@ -37,7 +38,6 @@ const web3Modal = new WalletConnectModal({
chains: ["eip155:1"], chains: ["eip155:1"],
}); });
// TODO: Disconnect session
export const WalletConnectProvider = ({ export const WalletConnectProvider = ({
children, children,
}: { }: {
@ -54,18 +54,16 @@ export const WalletConnectProvider = ({
}); });
} }
setSession(null) setSession(null);
}, [signClient, session]); }, [signClient, session]);
const checkPersistedState = useCallback(async(client: SignClient)=>{ const checkPersistedState = useCallback(async (client: SignClient) => {
if (client.session.length) { if (client.session.length) {
const lastKeyIndex = client.session.keys.length - 1; const lastKeyIndex = client.session.keys.length - 1;
const session = client.session.get( const session = client.session.get(client.session.keys[lastKeyIndex]);
client.session.keys[lastKeyIndex] setSession(session);
);
setSession(session)
} }
}, []) }, []);
const createClient = useCallback(async () => { const createClient = useCallback(async () => {
const signClient = await SignClient.init({ const signClient = await SignClient.init({
@ -79,8 +77,8 @@ export const WalletConnectProvider = ({
}); });
setSignClient(signClient); setSignClient(signClient);
await checkPersistedState(signClient) await checkPersistedState(signClient);
}, [checkPersistedState]) }, [checkPersistedState]);
const connect = async () => { const connect = async () => {
if (!signClient) { if (!signClient) {
@ -106,8 +104,13 @@ export const WalletConnectProvider = ({
if (uri) { if (uri) {
web3Modal.openModal({ uri }); web3Modal.openModal({ uri });
const session = await approval(); try {
setSession(session); // Update session state const session = await approval();
setSession(session);
} catch (error) {
enqueueSnackbar("User rejected pairing request", { variant: "error" });
console.log(error);
}
web3Modal.closeModal(); web3Modal.closeModal();
} }
}; };
@ -119,34 +122,32 @@ export const WalletConnectProvider = ({
}, [signClient, createClient]); }, [signClient, createClient]);
return ( return (
<walletConnectContext.Provider <>
value={{ <walletConnectContext.Provider
connect, value={{
session, connect,
signClient, session,
checkPersistedState, signClient,
disconnect checkPersistedState,
}} disconnect,
> }}
{children} >
</walletConnectContext.Provider> {children}
</walletConnectContext.Provider>
<SnackbarProvider />
</>
); );
}; };
export const useWalletConnectContext = () => { export const useWalletConnectContext = () => {
const { const { connect, session, signClient, checkPersistedState, disconnect } =
connect, useContext(walletConnectContext);
session,
signClient,
checkPersistedState,
disconnect
} = useContext(walletConnectContext);
return { return {
connect, connect,
session, session,
signClient, signClient,
checkPersistedState, checkPersistedState,
disconnect disconnect,
}; };
}; };

View File

@ -15,7 +15,6 @@ const ConnectWallet = () => {
} }
const handler = async () => { const handler = async () => {
await connect(); await connect();
navigate("/sign-with-ethereum")
} }
return ( return (

View File

@ -42,15 +42,11 @@ const SignWithCosmos = () => {
}, [ethAddress, cosmosSignature, ethSignature, innerMessage]); }, [ethAddress, cosmosSignature, ethSignature, innerMessage]);
const message = useMemo(() => { const message = useMemo(() => {
return canonicalStringify( return {
{ msg: "Onboarding my Azimuth ID onto UrbitChain",
msg: "Onboarding my Azimuth ID onto UrbitChain", address: ethAddress,
address: ethAddress, payload: innerMessage,
payload: innerMessage, };
},
null,
2
);
}, [ethAddress, innerMessage]); }, [ethAddress, innerMessage]);
const signCosmos = async () => { const signCosmos = async () => {
@ -60,7 +56,7 @@ const SignWithCosmos = () => {
msgs: [], msgs: [],
fee: { amount: [], gas: "23" }, fee: { amount: [], gas: "23" },
chain_id: "cosmos:cosmoshub-4", chain_id: "cosmos:cosmoshub-4",
memo: message, memo: canonicalStringify(message),
account_number: "7", account_number: "7",
sequence: "54", sequence: "54",
}; };
@ -106,7 +102,9 @@ const SignWithCosmos = () => {
wordWrap: "break-word", wordWrap: "break-word",
}} }}
> >
<pre style={{ whiteSpace: "pre-wrap", margin: 0 }}>{message} </pre> <pre style={{ whiteSpace: "pre-wrap", margin: 0 }}>
{JSON.stringify(message, null, 2)}{" "}
</pre>
</Box> </Box>
<Box> <Box>