Skip to content

Commit

Permalink
refactor: sign functions in useForumConnect
Browse files Browse the repository at this point in the history
  • Loading branch information
andyesp committed Jun 28, 2023
1 parent 7442aba commit 1a3c56f
Showing 1 changed file with 27 additions and 36 deletions.
63 changes: 27 additions & 36 deletions src/hooks/useForumConnect.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 useClipboardCopy from 'decentraland-gatsby/dist/hooks/useClipboardCopy'
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,18 @@ 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 [message, setMessage] = useState('')
const [signature, setSignature] = useState('')
const [copied, clipboardState] = 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 +47,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')
}

setMessage(message)
const signer = new Web3Provider(userState.provider).getSigner()
startTimer()
setIsValidated(undefined)
const signedMessage = await signer.signMessage(message)
if (!signedMessage) {
throw new Error('Failed to sign message')
}

setSignature(signedMessage)
return signedMessage
}, [startTimer, userState.provider])

const copyMessageToClipboard = useCallback(() => {
const { message, signature } = sign
if (message && signature) {
clipboardState.copy(`${message}\nSignature: ${signature}`)
}
}, [clipboardState, sign])
}, [clipboardState, message, signature])

const openThread = () => {
openUrl(THREAD_URL)
Expand Down

0 comments on commit 1a3c56f

Please sign in to comment.