fix(error-handling): close modal on reject and fix overflow error (#147)

* fix(error-handling): improve auth examples error handling & close modal on auth request rejection

* fix(bignum-overflow): convert bignumber to string to avoid overflow
This commit is contained in:
Cali 2023-04-05 12:03:28 +03:00 committed by GitHub
parent 9bf2b02602
commit 649cf28159
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 25 deletions

View File

@ -1,4 +1,4 @@
import { Box } from "@chakra-ui/react"; import { Box, useToast } from "@chakra-ui/react";
import AuthClient, { generateNonce } from "@walletconnect/auth-client"; import AuthClient, { generateNonce } from "@walletconnect/auth-client";
import { Web3Modal } from "@web3modal/standalone"; import { Web3Modal } from "@web3modal/standalone";
import type { NextPage } from "next"; import type { NextPage } from "next";
@ -23,6 +23,7 @@ const Home: NextPage = () => {
const [hasInitialized, setHasInitialized] = useState(false); const [hasInitialized, setHasInitialized] = useState(false);
const [uri, setUri] = useState<string>(""); const [uri, setUri] = useState<string>("");
const [address, setAddress] = useState<string>(""); const [address, setAddress] = useState<string>("");
const toast = useToast();
const onSignIn = useCallback(() => { const onSignIn = useCallback(() => {
if (!client) return; if (!client) return;
@ -66,15 +67,26 @@ const Home: NextPage = () => {
client.on("auth_response", ({ params }) => { client.on("auth_response", ({ params }) => {
if ("code" in params) { if ("code" in params) {
console.error(params); console.error(params);
return; return web3Modal.closeModal();
} }
if ("error" in params) { if ("error" in params) {
console.error(params.error); console.error(params.error);
return; if ("message" in params.error) {
toast({
status: "error",
title: params.error.message,
});
}
return web3Modal.closeModal();
} }
toast({
status: "success",
title: "Auth request successfully approved",
colorScheme: "whatsapp",
});
setAddress(params.result.p.iss.split(":")[4]); setAddress(params.result.p.iss.split(":")[4]);
}); });
}, [client]); }, [client, toast]);
const [view, changeView] = useState<"default" | "qr" | "signedIn">("default"); const [view, changeView] = useState<"default" | "qr" | "signedIn">("default");

View File

@ -14,7 +14,7 @@ import { providers } from "ethers";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useState } from "react";
const SignedInView: React.FC<{ address: string }> = ({ address }) => { const SignedInView: React.FC<{ address: string }> = ({ address }) => {
const [balance, setBalance] = useState<number>(); const [balance, setBalance] = useState<string>();
const [avatar, setAvatar] = useState<string | null>(); const [avatar, setAvatar] = useState<string | null>();
const [isLoading, setLoading] = useState<boolean>(false); const [isLoading, setLoading] = useState<boolean>(false);
@ -25,9 +25,9 @@ const SignedInView: React.FC<{ address: string }> = ({ address }) => {
`https://rpc.walletconnect.com/v1/?chainId=eip155:1&projectId=${process.env.NEXT_PUBLIC_PROJECT_ID}` `https://rpc.walletconnect.com/v1/?chainId=eip155:1&projectId=${process.env.NEXT_PUBLIC_PROJECT_ID}`
); );
const avatar = await provider.getAvatar(address); const avatar = await provider.getAvatar(address);
const balance = await provider.getBalance(address); const bigNumBalance = await provider.getBalance(address);
setAvatar(avatar); setAvatar(avatar);
setBalance(balance.toNumber()); setBalance(bigNumBalance.toString());
setLoading(false); setLoading(false);
}; };
if (address) { if (address) {

View File

@ -1,7 +1,7 @@
import { Fragment, useCallback, useEffect, useState } from 'react' import { Fragment, useCallback, useEffect, useState } from 'react'
import RequestModalContainer from '@/components/RequestModalContainer' import RequestModalContainer from '@/components/RequestModalContainer'
import ModalStore from '@/store/ModalStore' import ModalStore from '@/store/ModalStore'
import { Button, Col, Divider, Modal, Row, Text } from '@nextui-org/react' import { Button, Col, Modal, Row, Text } from '@nextui-org/react'
import { createOrRestoreEIP155Wallet } from '@/utils/EIP155WalletUtil' import { createOrRestoreEIP155Wallet } from '@/utils/EIP155WalletUtil'
import { authClient } from '@/utils/WalletConnectUtil' import { authClient } from '@/utils/WalletConnectUtil'
@ -15,27 +15,30 @@ export default function AuthenticationRequestModal() {
useEffect(() => { useEffect(() => {
if (message) return if (message) return
const address = eip155Addresses[0] const address = eip155Addresses[0]
const iss = `did:pkh:${authenticationRequest.params.cacaoPayload.chainId}:${address}` const iss = `did:pkh:${params.cacaoPayload.chainId}:${address}`
setMessage(authClient.formatMessage(authenticationRequest.params.cacaoPayload, iss)) setMessage(authClient.formatMessage(params.cacaoPayload, iss))
setIss(iss) setIss(iss)
}, [authenticationRequest.params.cacaoPayload, eip155Addresses, message]) }, [params.cacaoPayload, eip155Addresses, message])
const onApprove = useCallback(async () => { const onApprove = useCallback(async () => {
if (authenticationRequest && iss && message) { if (authenticationRequest && iss && message) {
console.log({ eip155Wallets }) try {
const signature = await eip155Wallets[eip155Addresses[0]].signMessage(message)
const signature = await eip155Wallets[eip155Addresses[0]].signMessage(message) await authClient.respond(
await authClient.respond( {
{ id,
id, signature: {
signature: { s: signature,
s: signature, t: 'eip191'
t: 'eip191' }
} },
}, iss
iss )
) } catch (onApproveError) {
ModalStore.close() console.log({ onApproveError })
} finally {
ModalStore.close()
}
} }
}, [authenticationRequest, eip155Addresses, eip155Wallets, id, iss, message]) }, [authenticationRequest, eip155Addresses, eip155Wallets, id, iss, message])
@ -45,6 +48,22 @@ export default function AuthenticationRequestModal() {
// Handle reject action // Handle reject action
async function onReject() { async function onReject() {
if (id && iss) {
try {
await authClient.respond(
{
id,
error: {
code: 4001,
message: 'Auth request has been rejected'
}
},
iss
)
} catch (onRejectError) {
console.log({ onRejectError })
}
}
ModalStore.close() ModalStore.close()
} }