Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: sign functions in useForumConnect #1056

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
andyesp marked this conversation as resolved.
Show resolved Hide resolved
}

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