Skip to content

Commit

Permalink
refactor: sign functions in useForumConnect (#1056)
Browse files Browse the repository at this point in the history
* refactor: sign functions in useForumConnect

* reset timer if sign message fails
  • Loading branch information
andyesp committed Jun 29, 2023
1 parent bf2eb8c commit 6e43262
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions src/hooks/useForumConnect.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<NodeJS.Timer>()
const [isValidated, setIsValidated] = useState<boolean>()

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)
Expand All @@ -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)
Expand Down

0 comments on commit 6e43262

Please sign in to comment.