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-05-31 09:34:59 +00:00
|
|
|
readonly property alias dappsModel: dappsProvider.dappsModel
|
2024-05-31 09:58:47 +00:00
|
|
|
readonly property alias requestHandler: requestHandler
|
2024-05-21 10:42:50 +00:00
|
|
|
|
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) {
|
|
|
|
if(Helpers.containsOnlyEmoji(uri)) {
|
2024-07-08 11:18:14 +00:00
|
|
|
root.pairingValidated(Pairing.errors.tooCool)
|
2024-07-05 09:32:31 +00:00
|
|
|
return
|
|
|
|
} else if(!Helpers.validURI(uri)) {
|
2024-07-08 11:18:14 +00:00
|
|
|
root.pairingValidated(Pairing.errors.invalidUri)
|
2024-07-05 09:32:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let info = Helpers.extractInfoFromPairUri(uri)
|
|
|
|
wcSDK.getActiveSessions((sessions) => {
|
|
|
|
// Check if the URI is already paired
|
2024-07-08 11:18:14 +00:00
|
|
|
var validationState = Pairing.errors.ok
|
2024-07-05 09:32:31 +00:00
|
|
|
for (let 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-08 11:18:14 +00:00
|
|
|
if (validationState == Pairing.errors.ok) {
|
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-05-31 09:58:47 +00:00
|
|
|
let approvedNamespaces = JSON.parse(
|
|
|
|
Helpers.buildSupportedNamespaces(approvedChainIds,
|
|
|
|
[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-07-23 14:26:55 +00:00
|
|
|
wcSDK.getActiveSessions((allSessions) => {
|
|
|
|
const sessions = Helpers.filterActiveSessionsForKnownAccounts(allSessions, d.supportedAccountsModel)
|
|
|
|
for (let session in sessions) {
|
|
|
|
let dapp = session.peer.metadata
|
|
|
|
let topic = session.topic
|
2024-06-24 21:26:53 +00:00
|
|
|
if (dapp.url == url) {
|
|
|
|
wcSDK.disconnectSession(topic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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-05-31 09:58:47 +00:00
|
|
|
let supportedNamespacesStr = Helpers.buildSupportedNamespacesFromModels(
|
2024-06-12 13:48:44 +00:00
|
|
|
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 12:50:54 +00:00
|
|
|
if (!(approvedNamespaces.eip155.accounts) || approvedNamespaces.eip155.accounts.length === 0
|
|
|
|
|| (!(approvedNamespaces.eip155.chains) || approvedNamespaces.eip155.chains.length === 0)
|
|
|
|
) {
|
|
|
|
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 {
|
|
|
|
let res = Helpers.extractChainsAndAccountsFromApprovedNamespaces(approvedNamespaces)
|
|
|
|
|
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-06-24 21:26:53 +00:00
|
|
|
store.deactivateWalletConnectSession(topic)
|
|
|
|
dappsProvider.updateDapps()
|
|
|
|
|
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-14 18:22:50 +00:00
|
|
|
if(err) {
|
2024-06-29 20:24:05 +00:00
|
|
|
root.displayToastMessage(qsTr("Failed to disconnect from %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("Disconnected from %1").arg(app_domain), false)
|
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-07-23 14:26:55 +00:00
|
|
|
readonly property var supportedAccountsModel: root.walletRootStore.nonWatchAccounts
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-23 14:26:55 +00:00
|
|
|
supportedAccountsModel: d.supportedAccountsModel
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
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
|
|
|
}
|