2024-07-19 19:21:36 +00:00
|
|
|
import QtQuick 2.15
|
|
|
|
import QtQuick.Controls 2.15
|
|
|
|
import QtQuick.Layouts 1.15
|
|
|
|
|
|
|
|
import QtWebEngine 1.10
|
|
|
|
import QtWebChannel 1.15
|
|
|
|
|
2024-10-15 19:26:12 +00:00
|
|
|
import StatusQ.Core.Theme 0.1
|
2024-07-19 19:21:36 +00:00
|
|
|
import StatusQ.Core.Utils 0.1 as SQUtils
|
|
|
|
import StatusQ.Components 0.1
|
|
|
|
|
|
|
|
import StatusQ 0.1
|
|
|
|
import SortFilterProxyModel 0.2
|
|
|
|
import AppLayouts.Wallet.controls 1.0
|
|
|
|
import shared.popups.walletconnect 1.0
|
|
|
|
import AppLayouts.Wallet.services.dapps 1.0
|
|
|
|
import AppLayouts.Wallet.services.dapps.types 1.0
|
|
|
|
|
|
|
|
import shared.stores 1.0
|
|
|
|
import utils 1.0
|
|
|
|
|
|
|
|
import "types"
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
/// Act as another layer of abstraction to the WalletConnectSDKBase
|
|
|
|
/// Converts the store requests into WalletConnect standard requests
|
2024-07-19 19:21:36 +00:00
|
|
|
WalletConnectSDKBase {
|
|
|
|
id: root
|
2024-11-07 09:10:10 +00:00
|
|
|
|
|
|
|
required property BrowserConnectStore store
|
|
|
|
/// Required roles: chainId
|
|
|
|
required property var networksModel
|
|
|
|
/// Required roles: address
|
|
|
|
required property var accountsModel
|
2024-08-06 20:20:08 +00:00
|
|
|
|
2024-07-19 19:21:36 +00:00
|
|
|
projectId: ""
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
Connections {
|
|
|
|
target: root.store
|
|
|
|
enabled: root.enabled
|
2024-07-19 19:21:36 +00:00
|
|
|
|
2024-11-11 16:22:45 +00:00
|
|
|
function onSendTransaction(requestId, dappInfoString) {
|
2024-11-07 09:10:10 +00:00
|
|
|
try {
|
|
|
|
var dappInfo = JSON.parse(dappInfoString)
|
|
|
|
var txArgsParams = JSON.parse(dappInfo.txArgs)
|
2024-11-11 16:22:45 +00:00
|
|
|
let event = d.buildTransactionRequest(requestId, dappInfo.url, dappInfo.chainId, txArgsParams)
|
|
|
|
d.sessionRequests.set(requestId, event)
|
2024-07-22 18:02:35 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
root.sessionRequestEvent(event)
|
|
|
|
} catch (e) {
|
2024-11-11 16:22:45 +00:00
|
|
|
d.sessionRequests.delete(requestId)
|
|
|
|
root.store.rejectTransaction("", requestId, "Failed to parse dappInfo for session request")
|
|
|
|
console.error("Failed to parse dappInfo for session request", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:14:59 +00:00
|
|
|
function onSign(requestId, dappInfoString) {
|
2024-11-11 16:22:45 +00:00
|
|
|
try {
|
|
|
|
const dappInfo = JSON.parse(dappInfoString)
|
|
|
|
const mainNet = SQUtils.ModelUtils.getByKey(root.networksModel, "layer", 1)
|
|
|
|
if (!mainNet) {
|
2024-11-19 12:14:59 +00:00
|
|
|
root.store.rejectSign(requestId)
|
|
|
|
console.error("Mainnet not found - sign failed")
|
2024-11-11 16:22:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:14:59 +00:00
|
|
|
const event = d.buildSignRequest(requestId, dappInfo.url, mainNet.chainId, dappInfo.challenge, dappInfo.address, dappInfo.method)
|
2024-11-11 16:22:45 +00:00
|
|
|
d.sessionRequests.set(requestId, event)
|
|
|
|
root.sessionRequestEvent(event)
|
|
|
|
} catch (e) {
|
|
|
|
d.sessionRequests.delete(requestId)
|
2024-11-19 12:14:59 +00:00
|
|
|
root.store.rejectSign("", requestId)
|
2024-11-07 09:10:10 +00:00
|
|
|
console.error("Failed to parse dappInfo for session request", e)
|
2024-07-22 18:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
function onConnectRequested(requestId, dappInfoString) {
|
|
|
|
try {
|
|
|
|
var dappInfo = JSON.parse(dappInfoString)
|
|
|
|
dappInfo.proposal = d.buildSessionProposal(requestId, dappInfo.url, dappInfo.name, dappInfo.icon, SessionRequest.getSupportedMethods())
|
|
|
|
d.sessionRequests.set(requestId, dappInfo)
|
|
|
|
root.sessionProposal(dappInfo.proposal)
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to parse dappInfo for connection request", e)
|
2024-07-22 18:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
function onDisconnected(dappInfoString) {
|
|
|
|
try {
|
|
|
|
let dappItem = JSON.parse(dappInfoString)
|
|
|
|
root.sessionDelete(dappItem.url, false)
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to parse dappInfo for disconnection", e)
|
2024-09-19 19:12:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
function onApproveTransactionResponse(topic, requestId, error) {
|
|
|
|
try {
|
|
|
|
const errorStr = error ? "Faled to approve trasnsaction" : ""
|
|
|
|
root.sessionRequestUserAnswerResult(topic, requestId, true, errorStr)
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to approve transaction response", e)
|
2024-09-19 19:12:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
function onRejectTransactionResponse(topic, requestId, error) {
|
|
|
|
try {
|
|
|
|
const errorStr = error ? "Faled to reject trasnsaction" : ""
|
|
|
|
root.sessionRequestUserAnswerResult(topic, requestId, false, errorStr)
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to reject transaction response", e)
|
2024-09-19 19:12:20 +00:00
|
|
|
}
|
2024-07-22 18:02:35 +00:00
|
|
|
}
|
2024-11-11 16:22:45 +00:00
|
|
|
|
2024-11-19 12:14:59 +00:00
|
|
|
function onApproveSignResponse(topic, requestId, error) {
|
2024-11-11 16:22:45 +00:00
|
|
|
try {
|
2024-11-19 12:14:59 +00:00
|
|
|
const errorStr = error ? "Faled to approve sign" : ""
|
2024-11-11 16:22:45 +00:00
|
|
|
root.sessionRequestUserAnswerResult(topic, requestId, true, errorStr)
|
|
|
|
} catch (e) {
|
2024-11-19 12:14:59 +00:00
|
|
|
console.error("Failed to approve sign response", e)
|
2024-11-11 16:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:14:59 +00:00
|
|
|
function onRejectSignResponse(topic, requestId, error) {
|
2024-11-11 16:22:45 +00:00
|
|
|
try {
|
2024-11-19 12:14:59 +00:00
|
|
|
const errorStr = error ? "Faled to reject sign" : ""
|
2024-11-11 16:22:45 +00:00
|
|
|
root.sessionRequestUserAnswerResult(topic, requestId, false, errorStr)
|
|
|
|
} catch (e) {
|
2024-11-19 12:14:59 +00:00
|
|
|
console.error("Failed to reject sign response", e)
|
2024-11-11 16:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
}
|
2024-07-22 18:02:35 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
approveSession: function(requestId, account, selectedChains) {
|
|
|
|
try {
|
|
|
|
if (!d.sessionRequests.has(requestId)) {
|
|
|
|
console.error("Session request not found")
|
|
|
|
return
|
2024-08-09 10:39:24 +00:00
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
const dappInfo = d.sessionRequests.get(requestId)
|
|
|
|
root.store.approveConnection(requestId, account, JSON.stringify(selectedChains))
|
|
|
|
const newSession = d.buildSession(dappInfo.url, dappInfo.name, dappInfo.icon, requestId, account, selectedChains)
|
|
|
|
root.approveSessionResult(requestId, newSession, "")
|
|
|
|
d.sessionRequests.delete(requestId)
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to approve session", e)
|
2024-07-22 18:02:35 +00:00
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
}
|
2024-07-22 18:02:35 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
rejectSession: function(requestId) {
|
|
|
|
try {
|
|
|
|
root.store.rejectConnection(requestId)
|
|
|
|
root.rejectSessionResult(requestId, "")
|
|
|
|
d.sessionRequests.delete(requestId)
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to reject session", e)
|
2024-07-22 18:02:35 +00:00
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
}
|
2024-07-22 18:02:35 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
acceptSessionRequest: function(topic, requestId, signature) {
|
2024-11-11 16:22:45 +00:00
|
|
|
if (!d.sessionRequests.has(requestId)) {
|
|
|
|
root.sessionRequestUserAnswerResult(topic, requestId, false, "Unknown request method")
|
|
|
|
console.error("Session request not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const event = d.sessionRequests.get(requestId)
|
|
|
|
if (event.params.request.method === SessionRequest.methods.sendTransaction.name) {
|
|
|
|
root.store.approveTransaction(topic, requestId, signature)
|
2024-11-19 12:14:59 +00:00
|
|
|
} else if (event.params.request.method === SessionRequest.methods.personalSign.name ||
|
|
|
|
event.params.request.method === SessionRequest.methods.signTypedData_v4.name) {
|
|
|
|
root.store.approveSign(topic, requestId, signature)
|
2024-11-11 16:22:45 +00:00
|
|
|
} else {
|
|
|
|
root.sessionRequestUserAnswerResult(topic, requestId, false, "Unknown request method")
|
|
|
|
console.error("Unknown request method", event.params.request.method)
|
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
}
|
2024-07-22 18:02:35 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
rejectSessionRequest: function(topic, requestId, error) {
|
2024-11-11 16:22:45 +00:00
|
|
|
if (!d.sessionRequests.has(requestId)) {
|
|
|
|
root.sessionRequestUserAnswerResult(topic, requestId, false, "Unknown request method")
|
|
|
|
console.error("Session request not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const event = d.sessionRequests.get(requestId)
|
|
|
|
if (event.params.request.method === SessionRequest.methods.sendTransaction.name) {
|
|
|
|
root.store.rejectTransaction(topic, requestId, error)
|
2024-11-19 12:14:59 +00:00
|
|
|
} else if (event.params.request.method === SessionRequest.methods.personalSign.name ||
|
|
|
|
event.params.request.method === SessionRequest.methods.signTypedData_v4.name) {
|
|
|
|
root.store.rejectSign(topic, requestId)
|
2024-11-11 16:22:45 +00:00
|
|
|
} else {
|
|
|
|
root.sessionRequestUserAnswerResult(topic, requestId, false, "Unknown request method")
|
|
|
|
console.error("Unknown request method", event.params.request.method)
|
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
}
|
2024-07-22 18:02:35 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
disconnectSession: function(topic) {
|
|
|
|
root.store.disconnect(topic)
|
|
|
|
}
|
2024-08-09 14:00:28 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
getActiveSessions: function(callback) {
|
|
|
|
try {
|
|
|
|
const dappsStr = root.store.getDApps()
|
|
|
|
const dapps = JSON.parse(dappsStr)
|
|
|
|
let activeSessions = {}
|
|
|
|
for (let i = 0; i < dapps.length; i++) {
|
|
|
|
const dapp = dapps[i]
|
|
|
|
activeSessions[dapp.url] = d.buildSession(dapp.url, dapp.name, dapp.iconUrl, "", dapp.sharedAccount, [dapp.chainId])
|
2024-07-22 18:02:35 +00:00
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
callback(activeSessions)
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to get active sessions", e)
|
|
|
|
callback([])
|
2024-07-22 18:02:35 +00:00
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
}
|
2024-07-22 18:02:35 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
// We don't expect requests for these. They are here only to spot errors
|
|
|
|
pair: function(pairLink) { console.error("ConnectorSDK.pair: not implemented") }
|
|
|
|
getPairings: function(callback) { console.error("ConnectorSDK.getPairings: not implemented") }
|
|
|
|
disconnectPairing: function(topic) { console.error("ConnectorSDK.disconnectPairing: not implemented") }
|
|
|
|
buildApprovedNamespaces: function(params, supportedNamespaces) { console.error("ConnectorSDK.buildApprovedNamespaces: not implemented") }
|
2024-07-22 18:02:35 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
QtObject {
|
|
|
|
id: d
|
|
|
|
readonly property var sessionRequests: new Map()
|
|
|
|
function buildSession(dappUrl, dappName, dappIcon, proposalId, account, chains) {
|
|
|
|
let sessionTemplate = (dappUrl, dappName, dappIcon, proposalId, eipAccount, eipChains) => {
|
2024-07-22 18:02:35 +00:00
|
|
|
return {
|
2024-11-07 09:10:10 +00:00
|
|
|
peer: {
|
|
|
|
metadata: {
|
|
|
|
description: "-",
|
|
|
|
icons: [
|
2024-07-22 18:02:35 +00:00
|
|
|
dappIcon
|
|
|
|
],
|
2024-11-07 09:10:10 +00:00
|
|
|
name: dappName,
|
|
|
|
url: dappUrl
|
|
|
|
}
|
|
|
|
},
|
|
|
|
namespaces: {
|
|
|
|
eip155: {
|
|
|
|
accounts: [eipAccount],
|
|
|
|
chains: eipChains
|
2024-07-22 18:02:35 +00:00
|
|
|
}
|
|
|
|
},
|
2024-11-07 09:10:10 +00:00
|
|
|
pairingTopic: proposalId,
|
|
|
|
topic: dappUrl
|
2024-07-22 18:02:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
const eipAccount = account ? `eip155:${account}` : ""
|
|
|
|
const eipChains = chains ? chains.map((chain) => `eip155:${chain}`) : []
|
2024-07-19 19:21:36 +00:00
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
return sessionTemplate(dappUrl, dappName, dappIcon, proposalId, eipAccount, eipChains)
|
2024-07-19 19:21:36 +00:00
|
|
|
}
|
2024-09-17 07:42:01 +00:00
|
|
|
|
2024-11-11 16:22:45 +00:00
|
|
|
function buildTransactionRequest(requestId, topic, chainId, txArgs) {
|
|
|
|
var paramsObj = {}
|
|
|
|
if (txArgs.gasPrice) {
|
|
|
|
paramsObj.gasPrice = txArgs.gasPrice
|
|
|
|
}
|
|
|
|
if (txArgs.gas) {
|
|
|
|
paramsObj.gasLimit = txArgs.gas
|
|
|
|
}
|
|
|
|
if (txArgs.maxFeePerGas) {
|
|
|
|
paramsObj.maxFeePerGas = txArgs.maxFeePerGas
|
|
|
|
}
|
|
|
|
if (txArgs.maxPriorityFeePerGas) {
|
|
|
|
paramsObj.maxPriorityFeePerGas = txArgs.maxPriorityFeePerGas
|
|
|
|
}
|
|
|
|
if (txArgs.nonce) {
|
|
|
|
paramsObj.nonce = txArgs.nonce
|
|
|
|
}
|
|
|
|
if (!!txArgs.data && txArgs.data !== "0x") {
|
|
|
|
paramsObj.data = txArgs.data
|
|
|
|
}
|
|
|
|
if (txArgs.to) {
|
|
|
|
paramsObj.to = txArgs.to
|
|
|
|
}
|
|
|
|
if (txArgs.from) {
|
|
|
|
paramsObj.from = txArgs.from
|
|
|
|
}
|
|
|
|
paramsObj.value = txArgs.value
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
return {
|
|
|
|
id: requestId,
|
|
|
|
topic,
|
|
|
|
params: {
|
|
|
|
chainId: `eip155:${chainId}`,
|
|
|
|
request: {
|
|
|
|
method: SessionRequest.methods.sendTransaction.name,
|
|
|
|
params: [
|
2024-11-11 16:22:45 +00:00
|
|
|
paramsObj
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:14:59 +00:00
|
|
|
function buildSignRequest(requestId, topic, chainId, challenge, address, method) {
|
|
|
|
let params = []
|
|
|
|
if (method == SessionRequest.methods.personalSign.name) {
|
|
|
|
params = [challenge, address]
|
|
|
|
} else if (method == SessionRequest.methods.signTypedData_v4.name) {
|
|
|
|
params = [address, challenge]
|
|
|
|
} else {
|
|
|
|
console.error("Unknown sign method", method)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-11 16:22:45 +00:00
|
|
|
return {
|
|
|
|
id: requestId,
|
|
|
|
topic,
|
|
|
|
params: {
|
|
|
|
chainId: `eip155:${chainId}`,
|
|
|
|
request: {
|
2024-11-19 12:14:59 +00:00
|
|
|
method,
|
|
|
|
params
|
2024-07-22 18:02:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 09:10:10 +00:00
|
|
|
function buildSessionProposal(id, url, name, icon, methods) {
|
|
|
|
const supportedNamespaces = DAppsHelpers.buildSupportedNamespacesFromModels(root.networksModel, root.accountsModel, methods)
|
|
|
|
const proposal = {
|
|
|
|
id,
|
|
|
|
params: {
|
|
|
|
optionalNamespaces: {},
|
|
|
|
proposer: {
|
|
|
|
metadata: {
|
|
|
|
description: "-",
|
|
|
|
icons: [
|
|
|
|
icon
|
2024-07-19 19:21:36 +00:00
|
|
|
],
|
2024-11-07 09:10:10 +00:00
|
|
|
name,
|
|
|
|
url
|
2024-07-19 19:21:36 +00:00
|
|
|
}
|
|
|
|
},
|
2024-11-07 09:10:10 +00:00
|
|
|
requiredNamespaces: supportedNamespaces
|
2024-07-19 19:21:36 +00:00
|
|
|
}
|
2024-08-06 20:20:08 +00:00
|
|
|
}
|
2024-11-07 09:10:10 +00:00
|
|
|
return proposal
|
2024-08-06 20:20:08 +00:00
|
|
|
}
|
2024-07-19 19:21:36 +00:00
|
|
|
}
|
|
|
|
}
|