2024-10-30 15:18:24 +00:00
|
|
|
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
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
required property WalletConnectSDKBase bcSDK
|
2024-10-30 15:18:24 +00:00
|
|
|
|
|
|
|
readonly property int connectorId: Constants.StatusConnect
|
2024-11-07 09:10:10 +00:00
|
|
|
readonly property bool enabled: bcSDK.enabled
|
|
|
|
|
|
|
|
signal connected(string pairingId, string topic, string dAppUrl)
|
|
|
|
signal disconnected(string topic, string dAppUrl)
|
2024-10-30 15:18:24 +00:00
|
|
|
|
|
|
|
Connections {
|
2024-11-07 09:10:10 +00:00
|
|
|
target: root.bcSDK
|
2024-10-30 15:18:24 +00:00
|
|
|
enabled: root.enabled
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
function onSessionDelete(topic, err) {
|
|
|
|
const dapp = root.getByTopic(topic)
|
|
|
|
if (!dapp) {
|
|
|
|
console.warn("DApp not found for topic - cannot delete session", topic)
|
2024-10-30 15:18:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
root.remove(topic)
|
|
|
|
root.disconnected(topic, dapp.url)
|
|
|
|
}
|
2024-10-30 15:18:24 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
function onApproveSessionResult(proposalId, session, error) {
|
|
|
|
if (error) {
|
|
|
|
console.warn("Failed to approve session", error)
|
2024-10-30 15:18:24 +00:00
|
|
|
return
|
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
|
|
|
|
const dapp = d.sessionToDApp(session)
|
|
|
|
root.append(dapp)
|
|
|
|
root.connected(proposalId, dapp.topic, dapp.url)
|
2024-10-30 15:18:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
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 = ""
|
2024-10-30 15:18:24 +00:00
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
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
|
2024-10-30 15:18:24 +00:00
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
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()
|
2024-10-30 15:18:24 +00:00
|
|
|
}
|
|
|
|
}
|