From 7a08c619b0ec22b28c843790c81f8eaa46f0e9b5 Mon Sep 17 00:00:00 2001 From: abhishek-01k Date: Fri, 22 Nov 2024 12:18:37 +0530 Subject: [PATCH] Fixed the flow for Snaps in user settings page --- src/components/PushSnap/PushSnapSettings.tsx | 112 ++++++++++++++++--- 1 file changed, 95 insertions(+), 17 deletions(-) diff --git a/src/components/PushSnap/PushSnapSettings.tsx b/src/components/PushSnap/PushSnapSettings.tsx index e509132ac4..44f941abdf 100644 --- a/src/components/PushSnap/PushSnapSettings.tsx +++ b/src/components/PushSnap/PushSnapSettings.tsx @@ -10,14 +10,15 @@ import useModalBlur, { MODAL_POSITION } from 'hooks/useModalBlur'; import AboutSnapModal from 'modules/snap/AboutSnapModal'; import styled, { useTheme } from 'styled-components'; import PushSnapConfigureModal from './PushSnapConfigureModal'; -import { Alert, Box, Button, Text } from 'blocks'; +import { Alert, Box, Button, Metamask, Text } from 'blocks'; import { SnoozeDurationType } from 'types'; const PushSnapSettings = () => { - const { account, isWalletConnected, connect } = useAccount(); + const { account, isWalletConnected, connect, provider } = useAccount(); const theme = useTheme(); const [loading, setLoading] = useState(false); + const [walletConnected, setWalletConnected] = useState(false); const [addedAddress, setAddedAddress] = useState(false); const [errorMessage, setErrorMessage] = useState(null); @@ -31,11 +32,11 @@ const PushSnapSettings = () => { async function getInstalledSnaps() { if (!isWalletConnected) { - setErrorMessage("Connect your metamask wallet to install Snap"); + setErrorMessage('Connect your metamask wallet to install Snap'); setSnapInstalled(false); - return + return; } - setErrorMessage('') + setErrorMessage(''); const installedSnaps = await window.ethereum.request({ method: 'wallet_getSnaps', }); @@ -46,6 +47,39 @@ const PushSnapSettings = () => { } }); } + + async function getSignature(account: string) { + const signer = provider.getSigner(account); + const signature = await signer.signMessage(`Add address ${account} to receive notifications through Push Snap`); + return signature; + } + + async function addwalletAddress() { + try { + const signatureResult = await getSignature(account); + if (signatureResult) { + if (account) { + await window.ethereum?.request({ + method: 'wallet_invokeSnap', + params: { + snapId: defaultSnapOrigin, + request: { + method: 'pushproto_addaddress', + params: { address: account }, + }, + }, + }); + console.debug('Added', account); + setWalletConnected(true); + } + } else { + console.error('Signature Validation Failed'); + } + } catch (error: any) { + setErrorMessage(error.message); + } + } + async function getWalletAddresses() { const result = await window.ethereum?.request({ method: 'wallet_invokeSnap', @@ -55,10 +89,10 @@ const PushSnapSettings = () => { }, }); - console.debug(account); if (result.includes(account)) { setAddedAddress(true); + setWalletConnected(true); } else { setAddedAddress(false); } @@ -88,12 +122,15 @@ const PushSnapSettings = () => { setErrorMessage('Connect your metamask wallet to install Snap'); return; } - setErrorMessage('') + setErrorMessage(''); setLoading(true); try { if (!isWalletConnected) await connect(); if (!snapInstalled) { await connectSnap(); + getInstalledSnaps(); + } else { + await addwalletAddress(); } setLoading(false); } catch (error) { @@ -164,8 +201,12 @@ const PushSnapSettings = () => { /> )} - - {loading ? ( + + {/* {loading ? ( { ) : ( )} - + */} + {loading && !snapInstalled ? ( + + ) : ( + + )} + {loading && snapInstalled ? ( + + ) : ( + + )} + { return ( <> - {!snapInstalled ? ( + {!walletConnected ? ( ) : ( <> - Push Snap Settings + Push Snap Settings - )} @@ -224,7 +302,7 @@ export default PushSnapSettings; const Container = styled(Section)` width: 438px; - height: 423px; + height: auto; border-radius: 32px; background: #fff; background: ${(props) => props.theme.default.bg};