2024-05-06 20:22:43 +00:00
|
|
|
import QtQuick 2.15
|
|
|
|
|
2024-06-07 12:27:56 +00:00
|
|
|
import StatusQ 0.1
|
2024-05-14 18:22:50 +00:00
|
|
|
import StatusQ.Core.Theme 0.1
|
2024-05-21 10:42:50 +00:00
|
|
|
import StatusQ.Core.Utils 0.1
|
2024-05-14 18:22:50 +00:00
|
|
|
|
2024-06-07 12:27:56 +00:00
|
|
|
import AppLayouts.Wallet 1.0
|
2024-05-06 20:22:43 +00:00
|
|
|
import AppLayouts.Wallet.services.dapps 1.0
|
2024-06-12 13:48:44 +00:00
|
|
|
import AppLayouts.Wallet.services.dapps.types 1.0
|
2024-05-06 20:22:43 +00:00
|
|
|
import AppLayouts.Profile.stores 1.0
|
|
|
|
import shared.stores 1.0
|
|
|
|
import shared.popups.walletconnect 1.0
|
|
|
|
|
|
|
|
import SortFilterProxyModel 0.2
|
|
|
|
import utils 1.0
|
|
|
|
|
2024-05-31 09:58:47 +00:00
|
|
|
import "types"
|
|
|
|
|
2024-07-08 11:18:14 +00:00
|
|
|
// The WC SDK has an async (function call then signal response)
|
|
|
|
// A complete pairing flow to connect a dApp:
|
|
|
|
// - user provides pairing url -> root.validatePairingUri -> signal pairingValidated
|
|
|
|
// - user requests pair -> root.pair(uri) -> pairResponse(ok)
|
|
|
|
// -> if pairResponse ok -> onSessionProposal -> sdk.buildApprovedNamespaces
|
|
|
|
// -> onBuildApprovedNamespace -> signal connectDApp
|
|
|
|
// - user requests root.approvePairSession/root.rejectPairSession
|
|
|
|
// -> if approvePairSession -> sdk.buildApprovedNamespaces
|
|
|
|
// -> onBuildApprovedNamespace -> sdk.approveSession -> onApproveSessionResult
|
2024-05-21 10:42:50 +00:00
|
|
|
QObject {
|
2024-05-06 20:22:43 +00:00
|
|
|
id: root
|
|
|
|
|
2024-05-31 09:34:59 +00:00
|
|
|
required property WalletConnectSDKBase wcSDK
|
2024-05-20 18:42:31 +00:00
|
|
|
required property DAppsStore store
|
2024-07-03 20:46:00 +00:00
|
|
|
required property var walletRootStore
|
2024-06-29 20:24:05 +00:00
|
|
|
|
2024-08-06 20:20:08 +00:00
|
|
|
readonly property var dappsModel: ConcatModel {
|
|
|
|
markerRoleName: "source"
|
|
|
|
|
|
|
|
sources: [
|
|
|
|
SourceModel {
|
|
|
|
model: dappsProvider.dappsModel
|
|
|
|
markerRoleValue: "walletConnect"
|
|
|
|
},
|
|
|
|
SourceModel {
|
|
|
|
model: connectorDAppsProvider.dappsModel
|
|
|
|
markerRoleValue: "connector"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2024-05-31 09:58:47 +00:00
|
|
|
readonly property alias requestHandler: requestHandler
|
2024-05-21 10:42:50 +00:00
|
|
|
|
2024-08-05 13:41:20 +00:00
|
|
|
readonly property bool isServiceAvailableForAddressSelection: dappsProvider.supportedAccountsModel.ModelCount.count
|
|
|
|
|
2024-08-06 20:20:08 +00:00
|
|
|
readonly property alias connectorDAppsProvider: connectorDAppsProvider
|
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
readonly property var validAccounts: SortFilterProxyModel {
|
2024-07-23 14:26:55 +00:00
|
|
|
sourceModel: d.supportedAccountsModel
|
2024-06-07 12:27:56 +00:00
|
|
|
proxyRoles: [
|
|
|
|
FastExpressionRole {
|
|
|
|
name: "colorizedChainPrefixes"
|
|
|
|
function getChainShortNames(chainIds) {
|
2024-06-29 20:24:05 +00:00
|
|
|
const chainShortNames = root.walletRootStore.getNetworkShortNames(chainIds)
|
2024-06-07 12:27:56 +00:00
|
|
|
return WalletUtils.colorizedChainPrefix(chainShortNames)
|
|
|
|
}
|
|
|
|
expression: getChainShortNames(model.preferredSharingChainIds)
|
|
|
|
expectedRoles: ["preferredSharingChainIds"]
|
|
|
|
}
|
|
|
|
]
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
2024-06-29 20:24:05 +00:00
|
|
|
readonly property var flatNetworks: root.walletRootStore.filteredFlatModel
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-07-05 09:32:31 +00:00
|
|
|
function validatePairingUri(uri) {
|
2024-07-24 18:25:15 +00:00
|
|
|
// Check if emoji inside the URI
|
|
|
|
if(Constants.regularExpressions.emoji.test(uri)) {
|
2024-07-08 11:18:14 +00:00
|
|
|
root.pairingValidated(Pairing.errors.tooCool)
|
2024-07-05 09:32:31 +00:00
|
|
|
return
|
2024-07-29 14:39:56 +00:00
|
|
|
} else if(!DAppsHelpers.validURI(uri)) {
|
2024-07-08 11:18:14 +00:00
|
|
|
root.pairingValidated(Pairing.errors.invalidUri)
|
2024-07-05 09:32:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-29 14:39:56 +00:00
|
|
|
const info = DAppsHelpers.extractInfoFromPairUri(uri)
|
2024-07-05 09:32:31 +00:00
|
|
|
wcSDK.getActiveSessions((sessions) => {
|
|
|
|
// Check if the URI is already paired
|
2024-07-29 14:39:56 +00:00
|
|
|
let validationState = Pairing.errors.uriOk
|
|
|
|
for (const key in sessions) {
|
|
|
|
if (sessions[key].pairingTopic === info.topic) {
|
2024-07-08 11:18:14 +00:00
|
|
|
validationState = Pairing.errors.alreadyUsed
|
2024-07-05 09:32:31 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if expired
|
2024-07-29 14:39:56 +00:00
|
|
|
if (validationState === Pairing.errors.uriOk) {
|
2024-07-05 09:32:31 +00:00
|
|
|
const now = (new Date().getTime())/1000
|
|
|
|
if (info.expiry < now) {
|
2024-07-08 11:18:14 +00:00
|
|
|
validationState = Pairing.errors.expired
|
2024-07-05 09:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-08 11:18:14 +00:00
|
|
|
root.pairingValidated(validationState)
|
2024-07-05 09:32:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
function pair(uri) {
|
2024-05-21 10:42:50 +00:00
|
|
|
d.acceptedSessionProposal = null
|
2024-07-08 11:18:14 +00:00
|
|
|
timeoutTimer.start()
|
2024-05-06 20:22:43 +00:00
|
|
|
wcSDK.pair(uri)
|
|
|
|
}
|
|
|
|
|
|
|
|
function approvePairSession(sessionProposal, approvedChainIds, approvedAccount) {
|
2024-05-21 10:42:50 +00:00
|
|
|
d.acceptedSessionProposal = sessionProposal
|
2024-07-29 14:39:56 +00:00
|
|
|
const approvedNamespaces = JSON.parse(
|
|
|
|
DAppsHelpers.buildSupportedNamespaces(approvedChainIds,
|
2024-05-31 09:58:47 +00:00
|
|
|
[approvedAccount.address],
|
2024-06-12 13:48:44 +00:00
|
|
|
SessionRequest.getSupportedMethods())
|
2024-05-31 09:58:47 +00:00
|
|
|
)
|
2024-05-06 20:22:43 +00:00
|
|
|
wcSDK.buildApprovedNamespaces(sessionProposal.params, approvedNamespaces)
|
|
|
|
}
|
|
|
|
|
|
|
|
function rejectPairSession(id) {
|
|
|
|
wcSDK.rejectSession(id)
|
|
|
|
}
|
|
|
|
|
2024-06-24 21:26:53 +00:00
|
|
|
function disconnectSession(sessionTopic) {
|
2024-05-06 20:22:43 +00:00
|
|
|
wcSDK.disconnectSession(sessionTopic)
|
|
|
|
}
|
|
|
|
|
2024-06-24 21:26:53 +00:00
|
|
|
function disconnectDapp(url) {
|
2024-08-05 13:41:20 +00:00
|
|
|
wcSDK.getActiveSessions((allSessionsAllProfiles) => {
|
|
|
|
const sessions = DAppsHelpers.filterActiveSessionsForKnownAccounts(allSessionsAllProfiles, validAccounts)
|
2024-08-08 19:38:32 +00:00
|
|
|
let dappFoundInWcSessions = false
|
2024-07-29 14:39:56 +00:00
|
|
|
for (const sessionID in sessions) {
|
|
|
|
const session = sessions[sessionID]
|
2024-08-05 13:41:20 +00:00
|
|
|
const accountsInSession = DAppsHelpers.getAccountsInSession(session)
|
2024-07-29 14:39:56 +00:00
|
|
|
const dapp = session.peer.metadata
|
|
|
|
const topic = session.topic
|
|
|
|
if (dapp.url === url) {
|
2024-08-05 13:41:20 +00:00
|
|
|
if (!dappsProvider.selectedAddress ||
|
|
|
|
(accountsInSession.includes(dappsProvider.selectedAddress)))
|
|
|
|
{
|
2024-08-08 19:38:32 +00:00
|
|
|
dappFoundInWcSessions = true
|
2024-08-05 13:41:20 +00:00
|
|
|
wcSDK.disconnectSession(topic)
|
|
|
|
}
|
2024-06-24 21:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-06 20:20:08 +00:00
|
|
|
|
2024-08-08 19:38:32 +00:00
|
|
|
// TODO: #16044 - Refactor Wallet connect service to handle multiple SDKs
|
|
|
|
if (!dappFoundInWcSessions) {
|
|
|
|
// Revoke browser plugin session
|
|
|
|
root.revokeSession(url)
|
|
|
|
d.notifyDappDisconnect(url, false)
|
|
|
|
}
|
|
|
|
});
|
2024-06-24 21:26:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 09:23:35 +00:00
|
|
|
function getDApp(dAppUrl) {
|
|
|
|
return ModelUtils.getByKey(dappsModel, "url", dAppUrl);
|
|
|
|
}
|
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
signal connectDApp(var dappChains, var sessionProposal, var approvedNamespaces)
|
|
|
|
signal approveSessionResult(var session, var error)
|
2024-05-31 09:58:47 +00:00
|
|
|
signal sessionRequest(SessionRequestResolved request)
|
2024-05-31 09:34:59 +00:00
|
|
|
signal displayToastMessage(string message, bool error)
|
2024-07-08 11:18:14 +00:00
|
|
|
// Emitted as a response to WalletConnectService.validatePairingUri or other WalletConnectService.pair
|
|
|
|
// and WalletConnectService.approvePair errors
|
|
|
|
signal pairingValidated(int validationState)
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-08-06 20:20:08 +00:00
|
|
|
signal revokeSession(string dAppUrl)
|
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
readonly property Connections sdkConnections: Connections {
|
|
|
|
target: wcSDK
|
|
|
|
|
2024-07-08 11:18:14 +00:00
|
|
|
function onPairResponse(ok) {
|
|
|
|
if (!ok) {
|
|
|
|
d.reportPairErrorState(Pairing.errors.unknownError)
|
|
|
|
} // else waiting for onSessionProposal
|
|
|
|
}
|
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
function onSessionProposal(sessionProposal) {
|
2024-05-21 10:42:50 +00:00
|
|
|
d.currentSessionProposal = sessionProposal
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-07-29 14:39:56 +00:00
|
|
|
const supportedNamespacesStr = DAppsHelpers.buildSupportedNamespacesFromModels(
|
|
|
|
root.flatNetworks, root.validAccounts, SessionRequest.getSupportedMethods())
|
2024-05-06 20:22:43 +00:00
|
|
|
wcSDK.buildApprovedNamespaces(sessionProposal.params, JSON.parse(supportedNamespacesStr))
|
|
|
|
}
|
|
|
|
|
|
|
|
function onBuildApprovedNamespacesResult(approvedNamespaces, error) {
|
2024-07-24 12:50:54 +00:00
|
|
|
if(error || !approvedNamespaces) {
|
2024-07-08 10:40:43 +00:00
|
|
|
// Check that it contains Non conforming namespaces"
|
|
|
|
if (error.includes("Non conforming namespaces")) {
|
2024-07-08 11:18:14 +00:00
|
|
|
d.reportPairErrorState(Pairing.errors.unsupportedNetwork)
|
2024-07-08 10:40:43 +00:00
|
|
|
} else {
|
2024-07-08 11:18:14 +00:00
|
|
|
d.reportPairErrorState(Pairing.errors.unknownError)
|
2024-07-08 10:40:43 +00:00
|
|
|
}
|
2024-05-06 20:22:43 +00:00
|
|
|
return
|
|
|
|
}
|
2024-07-24 15:37:08 +00:00
|
|
|
const an = approvedNamespaces.eip155
|
|
|
|
if (!(an.accounts) || an.accounts.length === 0 || (!(an.chains) || an.chains.length === 0)) {
|
2024-07-24 12:50:54 +00:00
|
|
|
d.reportPairErrorState(Pairing.errors.unsupportedNetwork)
|
|
|
|
return
|
|
|
|
}
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-05-21 10:42:50 +00:00
|
|
|
if (d.acceptedSessionProposal) {
|
|
|
|
wcSDK.approveSession(d.acceptedSessionProposal, approvedNamespaces)
|
2024-05-06 20:22:43 +00:00
|
|
|
} else {
|
2024-07-29 14:39:56 +00:00
|
|
|
const res = DAppsHelpers.extractChainsAndAccountsFromApprovedNamespaces(approvedNamespaces)
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-05-21 10:42:50 +00:00
|
|
|
root.connectDApp(res.chains, d.currentSessionProposal, approvedNamespaces)
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onApproveSessionResult(session, err) {
|
2024-05-20 18:42:31 +00:00
|
|
|
if (err) {
|
2024-07-08 11:18:14 +00:00
|
|
|
d.reportPairErrorState(Pairing.errors.unknownError)
|
2024-05-20 18:42:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-14 18:22:50 +00:00
|
|
|
// TODO #14754: implement custom dApp notification
|
2024-06-29 20:24:05 +00:00
|
|
|
const app_url = d.currentSessionProposal ? d.currentSessionProposal.params.proposer.metadata.url : "-"
|
|
|
|
const app_domain = StringUtils.extractDomainFromLink(app_url)
|
|
|
|
root.displayToastMessage(qsTr("Connected to %1 via WalletConnect").arg(app_domain), false)
|
2024-05-20 18:42:31 +00:00
|
|
|
|
|
|
|
// Persist session
|
2024-06-07 16:54:19 +00:00
|
|
|
if(!store.addWalletConnectSession(JSON.stringify(session))) {
|
|
|
|
console.error("Failed to persist session")
|
|
|
|
}
|
2024-05-21 10:42:50 +00:00
|
|
|
|
2024-05-31 09:34:59 +00:00
|
|
|
// Notify client
|
|
|
|
root.approveSessionResult(session, err)
|
|
|
|
|
|
|
|
dappsProvider.updateDapps()
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onRejectSessionResult(err) {
|
2024-06-29 20:24:05 +00:00
|
|
|
const app_url = d.currentSessionProposal ? d.currentSessionProposal.params.proposer.metadata.url : "-"
|
|
|
|
const app_domain = StringUtils.extractDomainFromLink(app_url)
|
2024-05-06 20:22:43 +00:00
|
|
|
if(err) {
|
2024-07-08 11:18:14 +00:00
|
|
|
d.reportPairErrorState(Pairing.errors.unknownError)
|
2024-06-29 20:24:05 +00:00
|
|
|
root.displayToastMessage(qsTr("Failed to reject connection request for %1").arg(app_domain), true)
|
2024-05-06 20:22:43 +00:00
|
|
|
} else {
|
2024-06-29 20:24:05 +00:00
|
|
|
root.displayToastMessage(qsTr("Connection request for %1 was rejected").arg(app_domain), false)
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 18:22:50 +00:00
|
|
|
function onSessionDelete(topic, err) {
|
2024-08-02 11:18:58 +00:00
|
|
|
d.disconnectSessionRequested(topic, err)
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-31 09:34:59 +00:00
|
|
|
QObject {
|
2024-05-21 10:42:50 +00:00
|
|
|
id: d
|
|
|
|
|
2024-08-05 13:41:20 +00:00
|
|
|
readonly property var supportedAccountsModel: SortFilterProxyModel {
|
|
|
|
sourceModel: root.walletRootStore.nonWatchAccounts
|
|
|
|
}
|
2024-07-23 14:26:55 +00:00
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
property var currentSessionProposal: null
|
|
|
|
property var acceptedSessionProposal: null
|
2024-05-16 17:57:37 +00:00
|
|
|
|
2024-07-08 11:18:14 +00:00
|
|
|
function reportPairErrorState(state) {
|
|
|
|
timeoutTimer.stop()
|
|
|
|
root.pairingValidated(state)
|
2024-05-21 10:42:50 +00:00
|
|
|
}
|
2024-08-02 11:18:58 +00:00
|
|
|
|
|
|
|
function disconnectSessionRequested(topic, err) {
|
|
|
|
// Get all sessions and filter the active ones for known accounts
|
|
|
|
// Act on the first matching session with the same topic
|
|
|
|
const activeSessionsCallback = (allSessions, success) => {
|
|
|
|
store.activeSessionsReceived.disconnect(activeSessionsCallback)
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
// TODO #14754: implement custom dApp notification
|
|
|
|
d.notifyDappDisconnect("-", true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to original format
|
|
|
|
const webSdkSessions = allSessions.map((session) => {
|
|
|
|
return JSON.parse(session.sessionJson)
|
|
|
|
})
|
|
|
|
|
|
|
|
const sessions = DAppsHelpers.filterActiveSessionsForKnownAccounts(webSdkSessions, root.validAccounts)
|
|
|
|
|
|
|
|
for (const sessionID in sessions) {
|
|
|
|
const session = sessions[sessionID]
|
|
|
|
if (session.topic === topic) {
|
|
|
|
store.deactivateWalletConnectSession(topic)
|
|
|
|
dappsProvider.updateDapps()
|
|
|
|
|
|
|
|
const dappUrl = session.peer.metadata.url ?? "-"
|
|
|
|
d.notifyDappDisconnect(dappUrl, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
store.activeSessionsReceived.connect(activeSessionsCallback)
|
|
|
|
if (!store.getActiveSessions()) {
|
|
|
|
store.activeSessionsReceived.disconnect(activeSessionsCallback)
|
|
|
|
// TODO #14754: implement custom dApp notification
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function notifyDappDisconnect(dappUrl, err) {
|
|
|
|
const appDomain = StringUtils.extractDomainFromLink(dappUrl)
|
|
|
|
if(err) {
|
|
|
|
root.displayToastMessage(qsTr("Failed to disconnect from %1").arg(appDomain), true)
|
|
|
|
} else {
|
|
|
|
root.displayToastMessage(qsTr("Disconnected from %1").arg(appDomain), false)
|
|
|
|
}
|
|
|
|
}
|
2024-05-21 10:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Component.onCompleted: {
|
2024-05-31 09:34:59 +00:00
|
|
|
dappsProvider.updateDapps()
|
|
|
|
}
|
|
|
|
|
2024-05-31 09:58:47 +00:00
|
|
|
DAppsRequestHandler {
|
|
|
|
id: requestHandler
|
|
|
|
|
|
|
|
sdk: root.wcSDK
|
|
|
|
store: root.store
|
2024-06-29 20:24:05 +00:00
|
|
|
accountsModel: root.validAccounts
|
|
|
|
networksModel: root.flatNetworks
|
2024-07-17 10:46:43 +00:00
|
|
|
currenciesStore: root.walletRootStore.currencyStore
|
2024-07-17 15:49:30 +00:00
|
|
|
assetsStore: root.walletRootStore.walletAssetsStore
|
2024-05-31 09:58:47 +00:00
|
|
|
|
|
|
|
onSessionRequest: (request) => {
|
2024-07-08 11:18:14 +00:00
|
|
|
timeoutTimer.stop()
|
2024-05-31 09:58:47 +00:00
|
|
|
root.sessionRequest(request)
|
|
|
|
}
|
2024-06-04 20:45:03 +00:00
|
|
|
onDisplayToastMessage: (message, error) => {
|
|
|
|
root.displayToastMessage(message, error)
|
|
|
|
}
|
2024-05-31 09:58:47 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 09:34:59 +00:00
|
|
|
DAppsListProvider {
|
|
|
|
id: dappsProvider
|
|
|
|
|
|
|
|
sdk: root.wcSDK
|
|
|
|
store: root.store
|
2024-08-08 19:38:32 +00:00
|
|
|
supportedAccountsModel: d.supportedAccountsModel
|
2024-08-05 13:41:20 +00:00
|
|
|
selectedAddress: root.walletRootStore.selectedAddress
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
2024-07-08 11:18:14 +00:00
|
|
|
|
2024-08-06 20:20:08 +00:00
|
|
|
ConnectorDAppsListProvider {
|
|
|
|
id: connectorDAppsProvider
|
|
|
|
}
|
|
|
|
|
2024-07-08 11:18:14 +00:00
|
|
|
// Timeout for the corner case where the URL was already dismissed and the SDK doesn't respond with an error nor advances with the proposal
|
|
|
|
Timer {
|
|
|
|
id: timeoutTimer
|
|
|
|
|
|
|
|
interval: 10000 // (10 seconds)
|
|
|
|
running: false
|
|
|
|
repeat: false
|
|
|
|
|
|
|
|
onTriggered: {
|
|
|
|
d.reportPairErrorState(Pairing.errors.unknownError)
|
|
|
|
}
|
|
|
|
}
|
2024-07-15 09:23:35 +00:00
|
|
|
}
|