From 6e43262cc536e13a5309351a3b4762dbf357d87a Mon Sep 17 00:00:00 2001 From: Andy Espagnolo Date: Thu, 29 Jun 2023 13:52:38 -0300 Subject: [PATCH] refactor: sign functions in useForumConnect (#1056) * refactor: sign functions in useForumConnect * reset timer if sign message fails --- src/hooks/useForumConnect.ts | 66 +++++++++++++++--------------------- 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/src/hooks/useForumConnect.ts b/src/hooks/useForumConnect.ts index 1acc68fc6..31e4b4121 100644 --- a/src/hooks/useForumConnect.ts +++ b/src/hooks/useForumConnect.ts @@ -1,8 +1,8 @@ import { useCallback, useEffect, useState } from 'react' +import { Web3Provider } from '@ethersproject/providers' import useAuthContext from 'decentraland-gatsby/dist/context/Auth/useAuthContext' import useTrackContext from 'decentraland-gatsby/dist/context/Track/useTrackContext' -import useSign from 'decentraland-gatsby/dist/hooks/useSign' import { Governance } from '../clients/Governance' import { AccountType } from '../components/Modal/IdentityConnectModal/AccountsConnectModal' @@ -21,31 +21,17 @@ export const THREAD_URL = `${DISCOURSE_API}${ const VALIDATION_CHECK_INTERVAL = 10 * 1000 // 10 seconds const getMessage = async () => Governance.get().getValidationMessage() + export default function useForumConnect() { const [user, userState] = useAuthContext() const track = useTrackContext() - const [sign, signState] = useSign(user, userState.provider) + + const [clipboardMessage, setClipboardMessage] = useState('') const { handleCopy } = useClipboardCopy(Time.Second) - const [signatureResolution, setSignatureResolution] = useState<{ - resolve: (value: unknown) => void - reject: (reason?: unknown) => void - }>() const { startTimer, resetTimer, time } = useTimer(MESSAGE_TIMEOUT_TIME / 1000 - 1) const [validatingProfile, setValidatingProfile] = useState() const [isValidated, setIsValidated] = useState() - useEffect(() => { - if (signatureResolution && !signState.signing) { - if (!signState.error) { - signatureResolution.resolve(undefined) - } else { - resetTimer() - signatureResolution.reject(signState.error) - } - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [signatureResolution, signState]) - useEffect(() => { if (time <= 0 && validatingProfile) { clearInterval(validatingProfile) @@ -60,29 +46,33 @@ export default function useForumConnect() { }, [isValidated, resetTimer]) const getSignedMessage = useCallback(async () => { - return new Promise((resolve, reject) => { - getMessage() - .then((message) => { - if (message) { - startTimer() - setIsValidated(undefined) - signState.sign(message) - setSignatureResolution({ resolve, reject }) - } else { - reject(new Error('No message')) - } - }) - .catch(reject) - }) - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [signState]) + if (!userState.provider) { + return + } + + const message = await getMessage() + if (!message) { + throw new Error('No message') + } + + const signer = new Web3Provider(userState.provider).getSigner() + startTimer() + setIsValidated(undefined) + const signedMessage = await signer.signMessage(message) + if (!signedMessage) { + resetTimer() + throw new Error('Failed to sign message') + } + + setClipboardMessage(`${message}\nSignature: ${signedMessage}`) + return signedMessage + }, [startTimer, resetTimer, userState.provider]) const copyMessageToClipboard = useCallback(() => { - const { message, signature } = sign - if (message && signature) { - handleCopy(`${message}\nSignature: ${signature}`) + if (clipboardMessage) { + handleCopy(clipboardMessage) } - }, [handleCopy, sign]) + }, [clipboardMessage, handleCopy]) const openThread = () => { openUrl(THREAD_URL)