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: handle empty metadata #2660

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,115 @@ import * as bridges from '../bridges'
import { useCompatibilityWarning } from '../useCompatibilityWarning'

describe('useCompatibilityWarning', () => {
it('should return an error for a dangerous bridge', () => {
jest.spyOn(bridges, 'isStrictAddressBridge').mockReturnValue(true)
describe('should return an error for a dangerous bridge', () => {
it('if the dApp is named', () => {
jest.spyOn(bridges, 'isStrictAddressBridge').mockReturnValue(true)

const proposal = {
params: { proposer: { metadata: { name: 'Fake Bridge' } } },
verifyContext: { verified: { origin: '' } },
} as unknown as Web3WalletTypes.SessionProposal
const proposal = {
params: { proposer: { metadata: { name: 'Fake Bridge' } } },
verifyContext: { verified: { origin: '' } },
} as unknown as Web3WalletTypes.SessionProposal

const { result } = renderHook(() => useCompatibilityWarning(proposal, false))

expect(result.current).toEqual({
message:
'Fake Bridge is a bridge that is unusable in Safe{Wallet} due to the current implementation of WalletConnect — the bridged funds will be lost. Consider using a different bridge.',
severity: 'error',
})
})

it('if the dApp is not named', () => {
jest.spyOn(bridges, 'isStrictAddressBridge').mockReturnValue(true)

const proposal = {
params: { proposer: { metadata: { name: '' } } },
verifyContext: { verified: { origin: '' } },
} as unknown as Web3WalletTypes.SessionProposal

const { result } = renderHook(() => useCompatibilityWarning(proposal, false))
const { result } = renderHook(() => useCompatibilityWarning(proposal, false))

expect(result.current).toEqual({
message:
'Fake Bridge is a bridge that is unusable in Safe{Wallet} due to the current implementation of WalletConnect — the bridged funds will be lost. Consider using a different bridge.',
severity: 'error',
expect(result.current).toEqual({
message:
'This dApp is a bridge that is unusable in Safe{Wallet} due to the current implementation of WalletConnect — the bridged funds will be lost. Consider using a different bridge.',
severity: 'error',
})
})
})

it('should return a warning for a risky bridge', () => {
jest.spyOn(bridges, 'isStrictAddressBridge').mockReturnValue(false)
jest.spyOn(bridges, 'isDefaultAddressBridge').mockReturnValue(true)
describe('should return a warning for a risky bridge', () => {
it('if the dApp is named', () => {
jest.spyOn(bridges, 'isStrictAddressBridge').mockReturnValue(false)
jest.spyOn(bridges, 'isDefaultAddressBridge').mockReturnValue(true)

const proposal = {
params: { proposer: { metadata: { name: 'Fake Bridge' } } },
verifyContext: { verified: { origin: '' } },
} as unknown as Web3WalletTypes.SessionProposal

const { result } = renderHook(() => useCompatibilityWarning(proposal, false))

expect(result.current).toEqual({
message:
'While using Fake Bridge, please make sure that the desination address you send funds to matches the Safe address you have on the respective chain. Otherwise, the funds will be lost.',
severity: 'warning',
})
})

it('if the dApp is not named', () => {
jest.spyOn(bridges, 'isStrictAddressBridge').mockReturnValue(false)
jest.spyOn(bridges, 'isDefaultAddressBridge').mockReturnValue(true)

const proposal = {
params: { proposer: { metadata: { name: 'Fake Bridge' } } },
verifyContext: { verified: { origin: '' } },
} as unknown as Web3WalletTypes.SessionProposal
const proposal = {
params: { proposer: { metadata: { name: '' } } },
verifyContext: { verified: { origin: '' } },
} as unknown as Web3WalletTypes.SessionProposal

const { result } = renderHook(() => useCompatibilityWarning(proposal, false))
const { result } = renderHook(() => useCompatibilityWarning(proposal, false))

expect(result.current).toEqual({
message:
'While using Fake Bridge, please make sure that the desination address you send funds to matches the Safe address you have on the respective chain. Otherwise, the funds will be lost.',
severity: 'warning',
expect(result.current).toEqual({
message:
'While using this dApp, please make sure that the desination address you send funds to matches the Safe address you have on the respective chain. Otherwise, the funds will be lost.',
severity: 'warning',
})
})
})

it('should return an error for an unsupported chain', () => {
jest.spyOn(bridges, 'isStrictAddressBridge').mockReturnValue(false)
jest.spyOn(bridges, 'isDefaultAddressBridge').mockReturnValue(false)
describe('it should return an error for an unsupported chain', () => {
it('if the dApp is named', () => {
jest.spyOn(bridges, 'isStrictAddressBridge').mockReturnValue(false)
jest.spyOn(bridges, 'isDefaultAddressBridge').mockReturnValue(false)

const proposal = {
params: { proposer: { metadata: { name: 'Fake dApp' } } },
verifyContext: { verified: { origin: '' } },
} as unknown as Web3WalletTypes.SessionProposal

const { result } = renderHook(() => useCompatibilityWarning(proposal, true))

expect(result.current).toEqual({
message:
'Fake dApp does not support the Safe Account network. If you want to interact with Fake dApp, please switch to a Safe Account on a supported network.',
severity: 'error',
})
})

it('if the dApp is not named', () => {
jest.spyOn(bridges, 'isStrictAddressBridge').mockReturnValue(false)
jest.spyOn(bridges, 'isDefaultAddressBridge').mockReturnValue(false)

const proposal = {
params: { proposer: { metadata: { name: 'Fake dApp' } } },
verifyContext: { verified: { origin: '' } },
} as unknown as Web3WalletTypes.SessionProposal
const proposal = {
params: { proposer: { metadata: { name: '' } } },
verifyContext: { verified: { origin: '' } },
} as unknown as Web3WalletTypes.SessionProposal

const { result } = renderHook(() => useCompatibilityWarning(proposal, true))
const { result } = renderHook(() => useCompatibilityWarning(proposal, true))

expect(result.current).toEqual({
message:
'Fake dApp does not support the Safe Account network. If you want to interact with Fake dApp, please switch to a Safe Account on a supported network.',
severity: 'error',
expect(result.current).toEqual({
message:
'This dApp does not support the Safe Account network. If you want to interact with this dApp, please switch to a Safe Account on a supported network.',
severity: 'error',
})
})
})

Expand Down
18 changes: 10 additions & 8 deletions src/components/walletconnect/ProposalForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ const ProposalForm = ({ proposal, onApprove, onReject }: ProposalFormProps): Rea
WalletConnect
</Typography>

<div className={css.icon}>
<SafeAppIconCard
src={proposer.metadata.icons[0]}
width={32}
height={32}
alt={`${proposer.metadata.name} logo`}
/>
</div>
{proposer.metadata.icons[0] && (
<div className={css.icon}>
<SafeAppIconCard
src={proposer.metadata.icons[0]}
width={32}
height={32}
alt={`${proposer.metadata.name || 'dApp'} logo`}
/>
</div>
)}

<Typography mb={1}>{proposer.metadata.name} wants to connect</Typography>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import type { Web3WalletTypes } from '@walletconnect/web3wallet'
import useChains from '@/hooks/useChains'
import useSafeInfo from '@/hooks/useSafeInfo'
import { isStrictAddressBridge, isDefaultAddressBridge } from './bridges'
import { capitalize } from '@/utils/formatters'

const NAME_FALLBACK = 'this dApp'

const NAME_PLACEHOLDER = '%%name%%'
const CHAIN_PLACEHOLDER = '%%chain%%'
Expand All @@ -28,6 +31,22 @@ const Warnings: Record<string, { severity: AlertColor; message: string }> = {
},
}

export const _getWarning = (origin: string, isUnsupportedChain: boolean) => {
if (isStrictAddressBridge(origin)) {
return Warnings.BLOCKED_BRIDGE
}

if (isUnsupportedChain) {
return Warnings.UNSUPPORTED_CHAIN
}

if (isDefaultAddressBridge(origin)) {
return Warnings.WARNED_BRIDGE
}

return Warnings.WRONG_CHAIN
}

export const useCompatibilityWarning = (
proposal: Web3WalletTypes.SessionProposal,
isUnsupportedChain: boolean,
Expand All @@ -39,16 +58,13 @@ export const useCompatibilityWarning = (
const { origin } = proposal.verifyContext.verified
const { proposer } = proposal.params

let { message, severity } = isStrictAddressBridge(origin)
? Warnings.BLOCKED_BRIDGE
: isDefaultAddressBridge(origin)
? Warnings.WARNED_BRIDGE
: isUnsupportedChain
? Warnings.UNSUPPORTED_CHAIN
: Warnings.WRONG_CHAIN
let { message, severity } = _getWarning(origin, isUnsupportedChain)

if (message.includes(NAME_PLACEHOLDER)) {
message = message.replaceAll(NAME_PLACEHOLDER, proposer.metadata.name)
message = message.replaceAll(NAME_PLACEHOLDER, proposer.metadata.name || NAME_FALLBACK)
if (message.includes(NAME_FALLBACK)) {
message = capitalize(message)
}
}

if (message.includes(CHAIN_PLACEHOLDER)) {
Expand Down
Loading