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-10-03 18:15:24 +00:00
|
|
|
//input properties
|
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-10-03 18:15:24 +00:00
|
|
|
//output properties
|
|
|
|
/// Model contaning all dApps available for the currently selected account
|
|
|
|
readonly property var dappsModel: d.filteredDappsModel
|
|
|
|
/// Model containig the dApps session requests to be resolved by the user
|
|
|
|
readonly property SessionRequestsModel sessionRequestsModel: requestHandler.requestsModel
|
|
|
|
/// Model containing the valid accounts a dApp can interact with
|
2024-10-04 15:23:26 +00:00
|
|
|
readonly property var validAccounts: root.walletRootStore.nonWatchAccounts
|
2024-10-03 18:15:24 +00:00
|
|
|
/// Model containing the networks a dApp can interact with
|
|
|
|
readonly property var flatNetworks: root.walletRootStore.filteredFlatModel
|
|
|
|
/// Service can interact with the current address selection
|
|
|
|
/// Default value: true
|
|
|
|
readonly property bool serviceAvailableToCurrentAddress: !root.walletRootStore.selectedAddress ||
|
|
|
|
ModelUtils.contains(root.validAccounts, "address", root.walletRootStore.selectedAddress, Qt.CaseInsensitive)
|
|
|
|
/// TODO: refactor
|
2024-08-06 20:20:08 +00:00
|
|
|
readonly property alias connectorDAppsProvider: connectorDAppsProvider
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
// methods
|
|
|
|
/// Triggers the signing process for the given session request
|
|
|
|
/// @param topic The topic of the session
|
|
|
|
/// @param id The id of the session request
|
|
|
|
function sign(topic, id) {
|
|
|
|
// The authentication triggers the signing process
|
|
|
|
// authenticate -> sign -> inform the dApp
|
2024-10-18 21:22:06 +00:00
|
|
|
d.sign(topic, id)
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
2024-07-05 09:32:31 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
function rejectSign(topic, id, hasError) {
|
2024-10-18 21:22:06 +00:00
|
|
|
d.rejectSign(topic, id, hasError)
|
2024-10-03 18:15:24 +00:00
|
|
|
}
|
2024-07-05 09:32:31 +00:00
|
|
|
|
2024-10-30 21:37:42 +00:00
|
|
|
function subscribeForFeeUpdates(topic, id) {
|
|
|
|
requestHandler.subscribeForFeeUpdates(topic, id)
|
|
|
|
}
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
/// Validates the pairing URI
|
|
|
|
function validatePairingUri(uri) {
|
|
|
|
d.validatePairingUri(uri)
|
2024-07-05 09:32:31 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
/// Initiates the pairing process with the given URI
|
2024-05-06 20:22:43 +00:00
|
|
|
function pair(uri) {
|
2024-07-08 11:18:14 +00:00
|
|
|
timeoutTimer.start()
|
2024-10-18 21:22:06 +00:00
|
|
|
requestHandler.pair(uri)
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
2024-10-03 18:15:24 +00:00
|
|
|
|
|
|
|
/// Approves or rejects the session proposal
|
|
|
|
function approvePairSession(key, approvedChainIds, accountAddress) {
|
2024-10-18 21:22:06 +00:00
|
|
|
requestHandler.approvePairSession(key, approvedChainIds, accountAddress)
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-18 21:22:06 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
/// Rejects the session proposal
|
2024-05-06 20:22:43 +00:00
|
|
|
function rejectPairSession(id) {
|
2024-10-18 21:22:06 +00:00
|
|
|
requestHandler.rejectPairSession(id)
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
/// Disconnects the dApp with the given topic
|
|
|
|
/// @param topic The topic of the dApp
|
|
|
|
/// @param source The source of the dApp; either "walletConnect" or "connector"
|
|
|
|
function disconnectDapp(topic) {
|
|
|
|
d.disconnectDapp(topic)
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
// signals
|
|
|
|
signal connectDApp(var dappChains, url dappUrl, string dappName, url dappIcon, var key)
|
|
|
|
// Emitted as a response to WalletConnectService.approveSession
|
|
|
|
// @param key The key of the session proposal
|
|
|
|
// @param error The error message
|
|
|
|
// @param topic The new topic of the session
|
|
|
|
signal approveSessionResult(var key, var error, var topic)
|
|
|
|
// Emitted when a new session is requested by a dApp
|
|
|
|
signal sessionRequest(string id)
|
2024-10-04 12:49:16 +00:00
|
|
|
// Emitted when the services requests to display a toast message
|
|
|
|
// @param message The message to display
|
|
|
|
// @param type The type of the message. Maps to Constants.ephemeralNotificationType
|
|
|
|
signal displayToastMessage(string message, int type)
|
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-10-03 18:15:24 +00:00
|
|
|
signal revokeSession(string topic)
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
QObject {
|
|
|
|
id: d
|
2024-08-06 20:20:08 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
readonly property var dappsModel: ConcatModel {
|
|
|
|
id: dappsModel
|
|
|
|
markerRoleName: "source"
|
|
|
|
|
|
|
|
sources: [
|
|
|
|
SourceModel {
|
|
|
|
model: dappsProvider.dappsModel
|
|
|
|
markerRoleValue: "walletConnect"
|
|
|
|
},
|
|
|
|
SourceModel {
|
|
|
|
model: connectorDAppsProvider.dappsModel
|
|
|
|
markerRoleValue: "statusConnect"
|
|
|
|
}
|
|
|
|
]
|
2024-07-08 11:18:14 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
readonly property var filteredDappsModel: SortFilterProxyModel {
|
|
|
|
id: dappsFilteredModel
|
|
|
|
objectName: "DAppsModelFiltered"
|
|
|
|
sourceModel: d.dappsModel
|
|
|
|
readonly property string selectedAddress: root.walletRootStore.selectedAddress
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
filters: FastExpressionFilter {
|
|
|
|
enabled: !!dappsFilteredModel.selectedAddress
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
function isAddressIncluded(accountAddressesSubModel, selectedAddress) {
|
|
|
|
if (!accountAddressesSubModel) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const addresses = ModelUtils.modelToFlatArray(accountAddressesSubModel, "address")
|
|
|
|
return addresses.includes(selectedAddress)
|
2024-07-08 10:40:43 +00:00
|
|
|
}
|
2024-10-03 18:15:24 +00:00
|
|
|
expression: isAddressIncluded(model.accountAddresses, dappsFilteredModel.selectedAddress)
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
expectedRoles: "accountAddresses"
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
function disconnectDapp(topic) {
|
|
|
|
const dApp = d.getDAppByTopic(topic)
|
|
|
|
if (!dApp) {
|
|
|
|
console.error("Disconnecting dApp: dApp not found")
|
|
|
|
return
|
2024-06-07 16:54:19 +00:00
|
|
|
}
|
2024-05-21 10:42:50 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
if (!dApp.connectorId == undefined) {
|
|
|
|
console.error("Disconnecting dApp: connectorId not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: refactor
|
|
|
|
if (dApp.connectorId === connectorDAppsProvider.connectorId) {
|
|
|
|
root.revokeSession(topic)
|
|
|
|
d.notifyDappDisconnect(dApp.url, false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: refactor
|
|
|
|
if (dApp.connectorId === dappsProvider.connectorId) {
|
|
|
|
// Currently disconnect acts on all sessions!
|
|
|
|
for (let i = 0; i < dApp.sessions.ModelCount.count; i++) {
|
2024-10-18 21:22:06 +00:00
|
|
|
requestHandler.disconnectSession(dApp.sessions.get(i).topic)
|
2024-10-03 18:15:24 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
function validatePairingUri(uri) {
|
|
|
|
// Check if emoji inside the URI
|
|
|
|
if(Constants.regularExpressions.emoji.test(uri)) {
|
|
|
|
root.pairingValidated(Pairing.errors.tooCool)
|
|
|
|
return
|
|
|
|
} else if(!DAppsHelpers.validURI(uri)) {
|
|
|
|
root.pairingValidated(Pairing.errors.invalidUri)
|
|
|
|
return
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-18 21:22:06 +00:00
|
|
|
requestHandler.validatePairingUri(uri)
|
2024-10-03 18:15:24 +00:00
|
|
|
}
|
|
|
|
|
2024-10-18 21:22:06 +00:00
|
|
|
function sign(topic, id) {
|
2024-10-03 18:15:24 +00:00
|
|
|
const request = sessionRequestsModel.findRequest(topic, id)
|
|
|
|
if (!request) {
|
|
|
|
console.error("Session request not found")
|
|
|
|
return
|
|
|
|
}
|
2024-10-18 21:22:06 +00:00
|
|
|
request.accept()
|
|
|
|
}
|
|
|
|
|
|
|
|
function rejectSign(topic, id, hasError) {
|
|
|
|
const request = sessionRequestsModel.findRequest(topic, id)
|
|
|
|
if (!request) {
|
|
|
|
console.error("Session request not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
request.reject(hasError)
|
2024-08-05 13:41:20 +00:00
|
|
|
}
|
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 notifyDappDisconnect(dappUrl, err) {
|
|
|
|
const appDomain = StringUtils.extractDomainFromLink(dappUrl)
|
|
|
|
if(err) {
|
2024-10-04 12:49:16 +00:00
|
|
|
root.displayToastMessage(qsTr("Failed to disconnect from %1").arg(appDomain), Constants.ephemeralNotificationType.danger)
|
2024-08-02 11:18:58 +00:00
|
|
|
} else {
|
2024-10-04 12:49:16 +00:00
|
|
|
root.displayToastMessage(qsTr("Disconnected from %1").arg(appDomain), Constants.ephemeralNotificationType.success)
|
2024-08-02 11:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-03 18:15:24 +00:00
|
|
|
|
|
|
|
function getDAppByTopic(topic) {
|
|
|
|
return ModelUtils.getFirstModelEntryIf(d.dappsModel, (modelItem) => {
|
|
|
|
if (modelItem.topic == topic) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (!modelItem.sessions) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for (let i = 0; i < modelItem.sessions.ModelCount.count; i++) {
|
|
|
|
if (modelItem.sessions.get(i).topic == topic) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
onSessionRequest: (id) => {
|
2024-07-08 11:18:14 +00:00
|
|
|
timeoutTimer.stop()
|
2024-10-03 18:15:24 +00:00
|
|
|
root.sessionRequest(id)
|
2024-05-31 09:58:47 +00:00
|
|
|
}
|
2024-10-04 12:49:16 +00:00
|
|
|
onDisplayToastMessage: (message, type) => {
|
|
|
|
root.displayToastMessage(message, type)
|
2024-06-04 20:45:03 +00:00
|
|
|
}
|
2024-10-18 21:22:06 +00:00
|
|
|
onPairingResponse: (state) => {
|
|
|
|
if (state != Pairing.errors.uriOk) {
|
|
|
|
d.reportPairErrorState(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onConnectDApp: (dappChains, dappUrl, dappName, dappIcon, key) => {
|
|
|
|
root.connectDApp(dappChains, dappUrl, dappName, dappIcon, key)
|
|
|
|
}
|
|
|
|
onApproveSessionResult: (key, error, topic) => {
|
|
|
|
root.approveSessionResult(key, error, topic)
|
|
|
|
}
|
|
|
|
onPairingValidated: (validationState) => {
|
|
|
|
timeoutTimer.stop()
|
|
|
|
root.pairingValidated(validationState)
|
|
|
|
}
|
|
|
|
onDappDisconnected: (_, dappUrl, err) => {
|
|
|
|
d.notifyDappDisconnect(dappUrl, err)
|
|
|
|
}
|
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-10-03 18:15:24 +00:00
|
|
|
supportedAccountsModel: root.walletRootStore.nonWatchAccounts
|
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
|
|
|
}
|