Alex Jbanca 106988d534 fix(WC): Refactor dapps service to work with multiple SDKs
This PR is refactoring the dapps service to avoid code duplication between SDKs and also to avoid overlapping requests/responses.
It brings Browser Connect inline with Wallet Connect in terms of session management and sign transactions.

New architecture:

WalletConnectService becomes DAppsService. Its responsibility is to provide dapp access to the app. This is the component currently used by the UI
What does it do:
1. Provide dapp APIs line connect, disconnect, session requests etc
2. Spawn app notifications on dapp events
3. Timeout requests if the dapp does not respons

DAppsRequestHandler becomes DAppsModule. This component is consumed by the DAppService. Its responsibility is to aggregate all the building blocks for the dapps, but does not control any of the dapp features or consume the SDKs requests.
What does it do:
1. Aggregate all the building blocks for dapps (currently known as plugins)

DAppConnectionsPlugin - This component provides the session management features line connect, disconnect and provide a model with the connected dapps.
SignRequestPlugin - This component provides the sign request management. It receives the sign request from the dapp, translates it to what Status understands and manages the lifecycle of the request.
2024-11-20 18:10:29 +02:00

95 lines
2.6 KiB
QML

import QtQuick 2.15
import AppLayouts.Wallet.services.dapps 1.0
import StatusQ.Core.Utils 0.1
import shared.stores 1.0
import utils 1.0
DAppsModel {
id: root
required property WalletConnectSDKBase bcSDK
readonly property int connectorId: Constants.StatusConnect
readonly property bool enabled: bcSDK.enabled
signal connected(string pairingId, string topic, string dAppUrl)
signal disconnected(string topic, string dAppUrl)
Connections {
target: root.bcSDK
enabled: root.enabled
function onSessionDelete(topic, err) {
const dapp = root.getByTopic(topic)
if (!dapp) {
console.warn("DApp not found for topic - cannot delete session", topic)
return
}
root.remove(topic)
root.disconnected(topic, dapp.url)
}
function onApproveSessionResult(proposalId, session, error) {
if (error) {
console.warn("Failed to approve session", error)
return
}
const dapp = d.sessionToDApp(session)
root.append(dapp)
root.connected(proposalId, dapp.topic, dapp.url)
}
}
QtObject {
id: d
function sessionToDApp(session) {
const dapp = session.peer.metadata
if (!!dapp.icons && dapp.icons.length > 0) {
dapp.iconUrl = dapp.icons[0]
} else {
dapp.iconUrl = ""
}
const accounts = DAppsHelpers.getAccountsInSession(session)
dapp.accountAddresses = accounts.map(account => ({address: account}))
dapp.topic = session.topic
dapp.rawSessions = [session]
dapp.connectorId = root.connectorId
return dapp
}
function getPersistedDapps() {
if (!root.enabled) {
return []
}
let dapps = []
root.bcSDK.getActiveSessions((allSessions) => {
if (!allSessions) {
return
}
for (const sessionID in allSessions) {
const session = allSessions[sessionID]
const dapp = sessionToDApp(session)
dapps.push(dapp)
}
})
return dapps
}
function resetModel() {
root.clear()
const dapps = d.getPersistedDapps()
for (let i = 0; i < dapps.length; i++) {
root.append(dapps[i])
}
}
}
Component.onCompleted: {
d.resetModel()
}
}