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

fix: clear pairing URI on rejection #2643

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions src/components/walletconnect/HeaderWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ const WalletConnectHeaderWidget = (): ReactElement => {
return walletConnect.onSessionAdd(onSuccess)
}, [onSuccess, walletConnect])

useEffect(() => {
if (!walletConnect) {
return
}

return walletConnect.onSessionReject(clearUri)
}, [clearUri, walletConnect])

return (
<>
<div ref={iconRef}>
Expand Down
8 changes: 2 additions & 6 deletions src/components/walletconnect/WcInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ReactElement } from 'react'

import { WalletConnectContext } from '@/services/walletconnect/WalletConnectContext'
import { asError } from '@/services/exceptions/utils'
import { getClipboard } from '@/utils/clipboard'
import { getClipboard, isFirefox } from '@/utils/clipboard'

import css from '../SessionList/styles.module.css'

Expand All @@ -14,10 +14,6 @@ const WcInput = ({ uri }: { uri: string }): ReactElement => {
const [error, setError] = useState<Error>()
const [connecting, setConnecting] = useState(false)

// readText is not supported by Firefox
// @see https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText#browser_compatibility
const isFirefox = navigator?.userAgent.includes('Firefox')

const onInput = useCallback(
async (uri: string) => {
if (!walletConnect) return
Expand Down Expand Up @@ -66,7 +62,7 @@ const WcInput = ({ uri }: { uri: string }): ReactElement => {
label={error ? error.message : 'Pairing UI'}
placeholder="wc:"
InputProps={{
endAdornment: isFirefox ? undefined : (
endAdornment: isFirefox() ? undefined : (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
endAdornment: isFirefox() ? undefined : (
endAdornment: isPastingSupported() ? undefined : (

<InputAdornment position="end">
<Button variant="contained" onClick={onPaste} className={css.button}>
Paste
Expand Down
17 changes: 17 additions & 0 deletions src/services/walletconnect/WalletConnectWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { invariant } from '@/utils/helpers'
import { getEip155ChainId, stripEip155Prefix } from './utils'

const SESSION_ADD_EVENT = 'session_add' as Web3WalletTypes.Event // Workaround: WalletConnect doesn't emit session_add event
const SESSION_REJECT_EVENT = 'session_reject' as Web3WalletTypes.Event // Workaround: WalletConnect doesn't emit session_reject event

function assertWeb3Wallet<T extends Web3WalletType | null>(web3Wallet: T): asserts web3Wallet {
return invariant(web3Wallet, 'WalletConnect not initialized')
Expand Down Expand Up @@ -173,6 +174,9 @@ class WalletConnectWallet {
id: proposal.id,
reason: getSdkError('UNSUPPORTED_CHAINS'),
})

// Workaround: WalletConnect doesn't have a session_reject event
this.web3Wallet?.events.emit(SESSION_REJECT_EVENT, proposal)
}

/**
Expand All @@ -188,6 +192,19 @@ class WalletConnectWallet {
}
}

/**
* Subscribe to session proposal rejections
*/
public onSessionReject(handler: (e: Web3WalletTypes.SessionProposal) => void) {
// @ts-expect-error - custom event payload
this.web3Wallet?.on(SESSION_REJECT_EVENT, handler)

return () => {
// @ts-expect-error
this.web3Wallet?.off(SESSION_REJECT_EVENT, handler)
}
}

/**
* Subscribe to session add
*/
Expand Down
15 changes: 11 additions & 4 deletions src/utils/clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { logError, Errors } from '@/services/exceptions'

export const isFirefox = (): boolean => {
// 'clipboard-read' and `readText` are not supported by Firefox
// @see https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText#browser_compatibility
return navigator.userAgent.includes('Firefox')
}

export const isClipboardGranted = async (): Promise<boolean> => {
if (isFirefox()) {
return false
}

let isGranted = false

try {
Expand All @@ -15,10 +25,7 @@ export const isClipboardGranted = async (): Promise<boolean> => {
}

export const getClipboard = async (): Promise<string> => {
// readText is not supported by Firefox
// @see https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText#browser_compatibility
const isFirefox = navigator.userAgent.includes('Firefox')
if (isFirefox) {
if (isFirefox()) {
return ''
}

Expand Down
Loading