From 7d0d88544939bec7e5727c4d7f4ad3bd75ef0bde Mon Sep 17 00:00:00 2001 From: punarv pawade Date: Tue, 7 Nov 2023 01:55:45 +0530 Subject: [PATCH] webapp: revert old petra integration, fix signing, fix buttin display logics --- webapp/src/components/Header.tsx | 84 ++++++++------------------------ webapp/src/pages/Server.tsx | 15 +++++- 2 files changed, 34 insertions(+), 65 deletions(-) diff --git a/webapp/src/components/Header.tsx b/webapp/src/components/Header.tsx index f84f478..041548c 100644 --- a/webapp/src/components/Header.tsx +++ b/webapp/src/components/Header.tsx @@ -4,7 +4,6 @@ import { AuthContext } from "../context/AuthContext"; import { useAccount, useSignMessage, useNetwork } from "wagmi"; import { getChallengeId, getToken } from "../modules/api"; import { useWallet } from "@aptos-labs/wallet-adapter-react"; -import nacl from 'tweetnacl'; const Header = () => { const navigate = useNavigate(); @@ -14,8 +13,6 @@ const Header = () => { const [challengeId, setChallengeId] = useState(""); const [signature, setSignature] = useState(); const { signMessageAsync } = useSignMessage(); - const [profileId, setProfileId] = useState(null); - const [userWallet, setUserWallet] = useState(null); const { connect, @@ -45,19 +42,26 @@ const Header = () => { let timeoutId: string | number | NodeJS.Timeout | null = null; const getSignMessage = async () => { - if (connected == false) { + if (localStorage.getItem("token") === "") { console.log("clearing localstorage") if (timeoutId !== null) { clearTimeout(timeoutId); } - + if (account?.address){ + const response = await getChallengeId(account?.address); + setMessage(response.data.eula); + setChallengeId(response.data.challangeId); + if (response.data.isAuthorized == true) { + authContext?.setIsAuthorized(true); + } else { + authContext?.setIsAuthorized(false); + } + } timeoutId = setTimeout(() => { signOut(); }, 500); - } else if (!authContext?.isSignedIn) { - if (timeoutId !== null) { - clearTimeout(timeoutId); - } + } else { + wallets[0].connect() } }; @@ -70,54 +74,6 @@ const Header = () => { }; }, [authContext?.isSignedIn, account?.address, connected]); - const getAptosWallet = () => { - if ('aptos' in window) { - return (window as any).aptos; - } else { - window.open('https://petra.app/', '_blank'); - } - } - const connectWallet = async () => { - const wallet = getAptosWallet(); - try { - const response = await wallet.connect(); - - const account = await wallet.account(); - console.log("account",account) - - const challangeIdResponse = await getChallengeId(account?.address); - if (challangeIdResponse.data.isAuthorized == true) { - authContext?.setIsAuthorized(true); - } else { - authContext?.setIsAuthorized(false); - } - - - const message = challangeIdResponse.data.eula; - const nonce = challangeIdResponse.data.challangeId; - const publicKey = account.publicKey; - - const { signature, fullMessage } = await wallet.signMessage({ - message, - nonce - }); - console.log("sign", signature, "full message", fullMessage); - - const getTokenResponse = await getToken(`0x${signature}`, nonce, publicKey); - if (getTokenResponse.data.token) { - sessionStorage.setItem("token", getTokenResponse.data.token); - localStorage.setItem("token", getTokenResponse.data.token); - authContext?.setIsSignedIn(true); - } - - setProfileId(""); - setUserWallet(""); - - } catch (err) { - console.log(err); - } - } - const signOut = () => { sessionStorage.removeItem("token"); localStorage.removeItem("token"); @@ -145,7 +101,7 @@ const Header = () => { const signres = await petraSignMesssage(payload); console.log("response", signres); - const response = await getToken(signres?.signature, challengeId, account?.publicKey); + const response = await getToken(`0x${signres?.signature}`, challengeId, account?.publicKey); if (response.data.token) { sessionStorage.setItem("token", response.data.token); localStorage.setItem("token", response.data.token); @@ -179,10 +135,10 @@ const Header = () => { - {(!account?.address || authContext?.isSignedIn) && ( + {!(connected) && (
  • )} - {/* {!(connected && authContext?.isSignedIn) && ( + {(connected) && !(authContext?.isSignedIn) && (
  • - )} */} - {(authContext?.isSignedIn) && ( + )} + {connected && (
  • - Details + Account: {account?.address.slice(0,8)}...
  • )} diff --git a/webapp/src/pages/Server.tsx b/webapp/src/pages/Server.tsx index bc998e4..32d0c6b 100644 --- a/webapp/src/pages/Server.tsx +++ b/webapp/src/pages/Server.tsx @@ -12,6 +12,7 @@ import NotSigned from "../components/NotSigned"; import { AuthContext } from "../context/AuthContext"; import NotAuthorized from "../components/NotAuthorized"; import CustomTable from "../components/Server/CustomTable"; +import { useWallet } from "@aptos-labs/wallet-adapter-react"; const NotConnected: React.FC = () => { return ( @@ -34,6 +35,18 @@ const ServerPage: React.FC = () => { const { address, isConnecting, isDisconnected } = useAccount(); const authContext = useContext(AuthContext); + const { + connect, + wallets, + disconnect, + wallet, + account, + network, + connected, + signMessage: petraSignMesssage, + signMessageAndVerify, + } = useWallet(); + const handleDownload = async () => { try { const config = serverConfig; @@ -60,7 +73,7 @@ const ServerPage: React.FC = () => { fetchData(); }, []); - if (!address || isDisconnected) { + if (!connected) { return ; }