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

enhancement: match dapp and request by session topic #2496

Merged
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 @@ -137,7 +137,7 @@
id: PopupId.SuccessfulDappInteraction,
props: {
successMessage: getSuccessMessage(),
url: dapp?.metadata?.url,
dapp,
},
})
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
id: PopupId.SuccessfulDappInteraction,
props: {
successMessage: localize('popups.signMessage.success'),
url: dapp.metadata?.url,
dapp,
},
})
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { SignTypedDataVersion } from '@metamask/eth-sig-util'
import { signEip712Message } from '@core/wallet/actions/signEip712Message'
import { DappVerification } from '@auxiliary/wallet-connect/enums'
import { handleError } from '@core/error/handlers'

export let data: string
export let version: SignTypedDataVersion.V3 | SignTypedDataVersion.V4
Expand All @@ -32,7 +33,6 @@
}

isBusy = true

try {
const result = await signEip712Message(data, version, evmNetwork.coinType, account)
closePopup({ forceClose: true })
Expand All @@ -42,12 +42,11 @@
id: PopupId.SuccessfulDappInteraction,
props: {
successMessage: localize('popups.signMessage.success'),
url: dapp.metadata?.url,
dapp,
},
})
} catch (err) {
closePopup({ forceClose: true })
callback({ error: err.message ?? localize('error.global.generic') })
handleError(err)
} finally {
isBusy = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
id: PopupId.SuccessfulDappInteraction,
props: {
successMessage: localize('popups.signMessage.success'),
url: dapp.metadata?.url,
dapp,
},
})
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import { Alert, Text } from '@bloomwalletio/ui'
import { localize } from '@core/i18n'
import { closePopup } from '@desktop/auxiliary/popup/actions'
import { getConnectedDappByOrigin } from '@auxiliary/wallet-connect/stores'
import { DappInfo } from '@ui'
import { IConnectedDapp } from '@auxiliary/wallet-connect/interface'

export let url: string | undefined
export let dapp: IConnectedDapp | undefined
export let successMessage: string

const dapp = getConnectedDappByOrigin(url)
</script>

<PopupTemplate
Expand All @@ -21,7 +19,7 @@
>
<DappInfo
slot="banner"
metadata={dapp.metadata}
metadata={dapp?.metadata}
showLink={false}
classes="bg-surface-1 dark:bg-surface-1-dark pb-4"
/>
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/components/molecules/DappInfo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import { Avatar, Icon, IconName, Link, Text } from '@bloomwalletio/ui'
import { DappVerificationPill } from '@ui'
import { VERIFICATION_ICONS } from '@auxiliary/wallet-connect/constants/verification-icons.constant'
import { IDappMetadata } from '@auxiliary/wallet-connect/interface'

export let metadata: CoreTypes.Metadata
export let metadata: CoreTypes.Metadata | IDappMetadata | undefined
export let verifiedState: DappVerification | undefined = undefined
export let showLink: boolean = true
export let classes: string = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import { NetworkId, getEvmNetwork } from '@core/network'
import { JsonRpcResponse } from '@walletconnect/jsonrpc-types'
import { getSdkError } from '@walletconnect/utils'
import { Web3WalletTypes } from '@walletconnect/web3wallet'
import { getConnectedDappByOrigin, getWalletClient, setConnectedDapps, updateVerificationStateForDapp } from '../stores'
import {
getConnectedDappBySessionTopic,
getWalletClient,
setConnectedDapps,
updateVerificationStateForDapp,
} from '../stores'
import { CallbackParameters } from '../types'
import { handleEthSignTypedData } from './eth_signTypedData.handler'
import { handleEthTransaction } from './eth_transaction.handler'
import { handleSignMessage } from './sign_message.handler'
import { handleWatchAsset } from '@auxiliary/wallet-connect/handlers'
import { DappVerification, RpcMethod } from '../enums'
import { EvmTransactionData, getEvmTransactionFromHexString } from '@core/layer-2'
import { activeProfileId } from '@core/profile/stores'
import { get } from 'svelte/store'

export function onSessionRequest(event: Web3WalletTypes.SessionRequest): void {
// We need to call this here, because if the dapp requests too fast after approval, we won't have the dapp in the store yet
Expand All @@ -19,12 +26,6 @@ export function onSessionRequest(event: Web3WalletTypes.SessionRequest): void {
const { request, chainId } = params
const method = request.method as RpcMethod

const dapp = getConnectedDappByOrigin(verifyContext.verified.origin)
const verifiedState = verifyContext.verified.isScam
? DappVerification.Scam
: (verifyContext.verified.validation as DappVerification)
updateVerificationStateForDapp(verifyContext.verified.origin, verifiedState)

function returnResponse({ result, error }: CallbackParameters): void {
const response: JsonRpcResponse | undefined = result
? {
Expand All @@ -49,6 +50,17 @@ export function onSessionRequest(event: Web3WalletTypes.SessionRequest): void {
}
}

if (!get(activeProfileId)) {
returnResponse({ error: getSdkError('SESSION_SETTLEMENT_FAILED') })
return
}

const dapp = getConnectedDappBySessionTopic(topic)
const verifiedState = verifyContext.verified.isScam
? DappVerification.Scam
: (verifyContext.verified.validation as DappVerification)
updateVerificationStateForDapp(verifyContext.verified.origin, verifiedState)

if (!dapp) {
returnResponse({ error: getSdkError('SESSION_SETTLEMENT_FAILED') })
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ export function getConnectedDapps(): IConnectedDapp[] {
export function getConnectedDappByOrigin(origin: string): IConnectedDapp | undefined {
return get(connectedDapps).find((dapp) => dapp.metadata?.url === origin)
}

export function getConnectedDappBySessionTopic(sessionTopic: string): IConnectedDapp | undefined {
return get(connectedDapps).find((dapp) => dapp.session?.topic === sessionTopic)
}
Loading