2024-05-31 09:58:47 +00:00
|
|
|
import QtQuick 2.15
|
|
|
|
|
|
|
|
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-31 09:58:47 +00:00
|
|
|
|
|
|
|
import StatusQ.Core.Utils 0.1
|
|
|
|
|
|
|
|
import shared.stores 1.0
|
|
|
|
import utils 1.0
|
|
|
|
|
|
|
|
import "types"
|
|
|
|
|
|
|
|
QObject {
|
|
|
|
id: root
|
|
|
|
|
|
|
|
required property WalletConnectSDKBase sdk
|
|
|
|
required property DAppsStore store
|
2024-06-29 20:24:05 +00:00
|
|
|
required property var accountsModel
|
|
|
|
required property var networksModel
|
2024-05-31 09:58:47 +00:00
|
|
|
|
|
|
|
property alias requestsModel: requests
|
|
|
|
|
|
|
|
function rejectSessionRequest(request, userRejected) {
|
|
|
|
let error = userRejected ? false : true
|
|
|
|
sdk.rejectSessionRequest(request.topic, request.id, error)
|
|
|
|
}
|
|
|
|
|
2024-06-04 20:45:03 +00:00
|
|
|
/// Beware, it will fail if called multiple times before getting an answer
|
|
|
|
function authenticate(request) {
|
|
|
|
return store.authenticateUser(request.topic, request.id, request.account.address)
|
|
|
|
}
|
|
|
|
|
2024-05-31 09:58:47 +00:00
|
|
|
signal sessionRequest(SessionRequestResolved request)
|
2024-06-04 20:45:03 +00:00
|
|
|
signal displayToastMessage(string message, bool error)
|
2024-06-12 13:48:44 +00:00
|
|
|
signal sessionRequestResult(/*model entry of SessionRequestResolved*/ var request, bool isSuccess)
|
2024-06-23 08:27:29 +00:00
|
|
|
signal maxFeesUpdated(real maxFees, int maxFeesWei, bool haveEnoughFunds, string symbol)
|
2024-06-18 19:45:56 +00:00
|
|
|
signal estimatedTimeUpdated(int minMinutes, int maxMinutes)
|
2024-05-31 09:58:47 +00:00
|
|
|
|
|
|
|
Connections {
|
|
|
|
target: sdk
|
|
|
|
|
|
|
|
function onSessionRequestEvent(event) {
|
|
|
|
let obj = d.resolveAsync(event)
|
|
|
|
if (obj === null) {
|
|
|
|
let error = true
|
|
|
|
sdk.rejectSessionRequest(event.topic, event.id, error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
requests.enqueue(obj)
|
|
|
|
}
|
2024-06-04 20:45:03 +00:00
|
|
|
|
|
|
|
function onSessionRequestUserAnswerResult(topic, id, accept, error) {
|
|
|
|
var request = requests.findRequest(topic, id)
|
|
|
|
if (request === null) {
|
|
|
|
console.error("Error finding event for topic", topic, "id", id)
|
|
|
|
return
|
|
|
|
}
|
2024-06-12 13:48:44 +00:00
|
|
|
let methodStr = SessionRequest.methodToUserString(request.method)
|
2024-06-04 20:45:03 +00:00
|
|
|
if (!methodStr) {
|
|
|
|
console.error("Error finding user string for method", request.method)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d.lookupSession(topic, function(session) {
|
|
|
|
if (session === null)
|
|
|
|
return
|
|
|
|
if (error) {
|
|
|
|
root.displayToastMessage(qsTr("Fail to %1 from %2").arg(methodStr).arg(session.peer.metadata.url), true)
|
2024-06-07 16:54:19 +00:00
|
|
|
|
2024-06-12 13:48:44 +00:00
|
|
|
root.sessionRequestResult(request, false /*isSuccessful*/)
|
2024-06-07 16:54:19 +00:00
|
|
|
|
2024-06-04 20:45:03 +00:00
|
|
|
console.error(`Error accepting session request for topic: ${topic}, id: ${id}, accept: ${accept}, error: ${error}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let actionStr = accept ? qsTr("accepted") : qsTr("rejected")
|
|
|
|
root.displayToastMessage("%1 %2 %3".arg(session.peer.metadata.url).arg(methodStr).arg(actionStr), false)
|
2024-06-07 16:54:19 +00:00
|
|
|
|
2024-06-12 13:48:44 +00:00
|
|
|
root.sessionRequestResult(request, true /*isSuccessful*/)
|
2024-06-04 20:45:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Connections {
|
|
|
|
target: root.store
|
|
|
|
|
2024-06-07 16:54:19 +00:00
|
|
|
function onUserAuthenticated(topic, id, password, pin) {
|
2024-06-04 20:45:03 +00:00
|
|
|
var request = requests.findRequest(topic, id)
|
|
|
|
if (request === null) {
|
|
|
|
console.error("Error finding event for topic", topic, "id", id)
|
|
|
|
return
|
|
|
|
}
|
2024-06-07 16:54:19 +00:00
|
|
|
d.executeSessionRequest(request, password, pin)
|
2024-06-04 20:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onUserAuthenticationFailed(topic, id) {
|
|
|
|
var request = requests.findRequest(topic, id)
|
2024-06-12 13:48:44 +00:00
|
|
|
let methodStr = SessionRequest.methodToUserString(request.method)
|
2024-06-04 20:45:03 +00:00
|
|
|
if (request === null || !methodStr) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.lookupSession(topic, function(session) {
|
|
|
|
if (session === null)
|
|
|
|
return
|
|
|
|
root.displayToastMessage(qsTr("Failed to authenticate %1 from %2").arg(methodStr).arg(session.peer.metadata.url), true)
|
|
|
|
})
|
|
|
|
}
|
2024-05-31 09:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QObject {
|
|
|
|
id: d
|
|
|
|
|
|
|
|
function resolveAsync(event) {
|
|
|
|
let method = event.params.request.method
|
|
|
|
let account = lookupAccountFromEvent(event, method)
|
2024-06-20 20:34:59 +00:00
|
|
|
if(!account) {
|
|
|
|
console.error("Error finding account for event", JSON.stringify(event))
|
|
|
|
return null
|
|
|
|
}
|
2024-05-31 09:58:47 +00:00
|
|
|
let network = lookupNetworkFromEvent(event, method)
|
2024-06-20 20:34:59 +00:00
|
|
|
if(!network) {
|
|
|
|
console.error("Error finding network for event", JSON.stringify(event))
|
|
|
|
return null
|
|
|
|
}
|
2024-05-31 09:58:47 +00:00
|
|
|
let data = extractMethodData(event, method)
|
2024-06-20 20:34:59 +00:00
|
|
|
if(!data) {
|
|
|
|
console.error("Error in event data lookup", JSON.stringify(event))
|
|
|
|
return null
|
|
|
|
}
|
2024-05-31 09:58:47 +00:00
|
|
|
let obj = sessionRequestComponent.createObject(null, {
|
|
|
|
event,
|
|
|
|
topic: event.topic,
|
|
|
|
id: event.id,
|
|
|
|
method,
|
|
|
|
account,
|
|
|
|
network,
|
2024-06-18 19:45:56 +00:00
|
|
|
data,
|
2024-06-23 08:27:29 +00:00
|
|
|
maxFeesText: "?",
|
|
|
|
maxFeesEthText: "?",
|
|
|
|
enoughFunds: false,
|
|
|
|
estimatedTimeText: "?"
|
2024-05-31 09:58:47 +00:00
|
|
|
})
|
|
|
|
if (obj === null) {
|
|
|
|
console.error("Error creating SessionRequestResolved for event")
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check later to have a valid request object
|
2024-06-23 08:27:29 +00:00
|
|
|
if (!SessionRequest.getSupportedMethods().includes(method)) {
|
2024-05-31 09:58:47 +00:00
|
|
|
console.error("Unsupported method", method)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2024-06-04 20:45:03 +00:00
|
|
|
d.lookupSession(obj.topic, function(session) {
|
|
|
|
if (session === null) {
|
|
|
|
console.error("DAppsRequestHandler.lookupSession: error finding session for topic", obj.topic)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obj.resolveDappInfoFromSession(session)
|
|
|
|
root.sessionRequest(obj)
|
2024-06-18 19:45:56 +00:00
|
|
|
// TODO #15192: update maxFees
|
2024-07-11 21:00:15 +00:00
|
|
|
if (!event.params.request.params[0].gasLimit || !event.params.request.params[0].gasPrice) {
|
|
|
|
root.maxFeesUpdated(0, 0, true, "")
|
|
|
|
root.estimatedTimeUpdated(0, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-18 19:45:56 +00:00
|
|
|
let gasLimit = parseFloat(parseInt(event.params.request.params[0].gasLimit, 16));
|
|
|
|
let gasPrice = parseFloat(parseInt(event.params.request.params[0].gasPrice, 16));
|
2024-06-23 08:27:29 +00:00
|
|
|
let maxFees = gasLimit * gasPrice
|
|
|
|
root.maxFeesUpdated(maxFees/1000000000, maxFees, true, "Gwei")
|
2024-06-18 19:45:56 +00:00
|
|
|
// TODO #15192: update estimatedTime
|
2024-06-23 08:27:29 +00:00
|
|
|
root.estimatedTimeUpdated(1, 12)
|
2024-05-31 09:58:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns null if the account is not found
|
|
|
|
function lookupAccountFromEvent(event, method) {
|
2024-06-07 16:54:19 +00:00
|
|
|
var address = ""
|
2024-06-12 13:48:44 +00:00
|
|
|
if (method === SessionRequest.methods.personalSign.name) {
|
2024-05-31 09:58:47 +00:00
|
|
|
if (event.params.request.params.length < 2) {
|
|
|
|
return null
|
|
|
|
}
|
2024-06-07 16:54:19 +00:00
|
|
|
address = event.params.request.params[1]
|
2024-07-01 21:02:05 +00:00
|
|
|
} else if (method === SessionRequest.methods.sign.name) {
|
|
|
|
if (event.params.request.params.length === 1) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
address = event.params.request.params[0]
|
|
|
|
} else if(method === SessionRequest.methods.signTypedData_v4.name ||
|
|
|
|
method === SessionRequest.methods.signTypedData.name)
|
|
|
|
{
|
2024-06-07 16:54:19 +00:00
|
|
|
if (event.params.request.params.length < 2) {
|
|
|
|
return null
|
2024-05-31 09:58:47 +00:00
|
|
|
}
|
2024-06-07 16:54:19 +00:00
|
|
|
address = event.params.request.params[0]
|
2024-06-23 08:27:29 +00:00
|
|
|
} else if (method === SessionRequest.methods.signTransaction.name
|
|
|
|
|| method === SessionRequest.methods.sendTransaction.name) {
|
2024-06-12 13:48:44 +00:00
|
|
|
if (event.params.request.params.length == 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
address = event.params.request.params[0].from
|
2024-05-31 09:58:47 +00:00
|
|
|
}
|
2024-06-29 20:24:05 +00:00
|
|
|
return ModelUtils.getFirstModelEntryIf(root.accountsModel, (account) => {
|
|
|
|
return account.address.toLowerCase() === address.toLowerCase();
|
2024-06-20 20:34:59 +00:00
|
|
|
})
|
2024-05-31 09:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns null if the network is not found
|
|
|
|
function lookupNetworkFromEvent(event, method) {
|
2024-06-12 13:48:44 +00:00
|
|
|
if (SessionRequest.getSupportedMethods().includes(method) === false) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
let chainId = Helpers.chainIdFromEip155(event.params.chainId)
|
2024-06-29 20:24:05 +00:00
|
|
|
return ModelUtils.getByKey(root.networksModel, "chainId", chainId)
|
2024-05-31 09:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function extractMethodData(event, method) {
|
2024-07-01 21:02:05 +00:00
|
|
|
if (method === SessionRequest.methods.personalSign.name ||
|
|
|
|
method === SessionRequest.methods.sign.name)
|
|
|
|
{
|
|
|
|
if (event.params.request.params.length < 1) {
|
2024-05-31 09:58:47 +00:00
|
|
|
return null
|
|
|
|
}
|
2024-06-07 16:54:19 +00:00
|
|
|
var message = ""
|
2024-07-01 21:02:05 +00:00
|
|
|
let messageIndex = (method === SessionRequest.methods.personalSign.name ? 0 : 1)
|
|
|
|
let messageParam = event.params.request.params[messageIndex]
|
2024-06-07 16:54:19 +00:00
|
|
|
// There is no standard on how data is encoded. Therefore we support hex or utf8
|
|
|
|
if (Helpers.isHex(messageParam)) {
|
|
|
|
message = Helpers.hexToString(messageParam)
|
|
|
|
} else {
|
|
|
|
message = messageParam
|
|
|
|
}
|
2024-06-12 13:48:44 +00:00
|
|
|
return SessionRequest.methods.personalSign.buildDataObject(message)
|
2024-07-01 21:02:05 +00:00
|
|
|
} else if (method === SessionRequest.methods.signTypedData_v4.name ||
|
|
|
|
method === SessionRequest.methods.signTypedData.name)
|
|
|
|
{
|
2024-06-07 16:54:19 +00:00
|
|
|
if (event.params.request.params.length < 2) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
let jsonMessage = event.params.request.params[1]
|
2024-07-01 21:02:05 +00:00
|
|
|
let methodObj = method === SessionRequest.methods.signTypedData_v4.name
|
|
|
|
? SessionRequest.methods.signTypedData_v4
|
|
|
|
: SessionRequest.methods.signTypedData
|
|
|
|
return methodObj.buildDataObject(jsonMessage)
|
2024-06-12 13:48:44 +00:00
|
|
|
} else if (method === SessionRequest.methods.signTransaction.name) {
|
|
|
|
if (event.params.request.params.length == 0) {
|
|
|
|
return null
|
2024-06-04 20:45:03 +00:00
|
|
|
}
|
2024-06-12 13:48:44 +00:00
|
|
|
let tx = event.params.request.params[0]
|
|
|
|
return SessionRequest.methods.signTransaction.buildDataObject(tx)
|
2024-06-23 08:27:29 +00:00
|
|
|
} else if (method === SessionRequest.methods.sendTransaction.name) {
|
|
|
|
if (event.params.request.params.length == 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
let tx = event.params.request.params[0]
|
|
|
|
return SessionRequest.methods.sendTransaction.buildDataObject(tx)
|
2024-06-12 13:48:44 +00:00
|
|
|
} else {
|
|
|
|
return null
|
2024-06-04 20:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function lookupSession(topicToLookup, callback) {
|
|
|
|
sdk.getActiveSessions((res) => {
|
|
|
|
Object.keys(res).forEach((topic) => {
|
|
|
|
if (topic === topicToLookup) {
|
|
|
|
let session = res[topic]
|
|
|
|
callback(session)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-07 16:54:19 +00:00
|
|
|
function executeSessionRequest(request, password, pin) {
|
2024-06-12 13:48:44 +00:00
|
|
|
if (!SessionRequest.getSupportedMethods().includes(request.method)) {
|
|
|
|
console.error("Unsupported method to execute: ", request.method)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (password !== "") {
|
2024-06-23 08:27:29 +00:00
|
|
|
var actionResult = ""
|
2024-07-01 21:02:05 +00:00
|
|
|
if (request.method === SessionRequest.methods.sign.name) {
|
|
|
|
actionResult = store.signMessageUnsafe(request.topic, request.id,
|
|
|
|
request.account.address, password,
|
|
|
|
SessionRequest.methods.personalSign.getMessageFromData(request.data))
|
|
|
|
} else if (request.method === SessionRequest.methods.personalSign.name) {
|
2024-06-23 08:27:29 +00:00
|
|
|
actionResult = store.signMessage(request.topic, request.id,
|
2024-06-12 13:48:44 +00:00
|
|
|
request.account.address, password,
|
|
|
|
SessionRequest.methods.personalSign.getMessageFromData(request.data))
|
2024-07-01 21:02:05 +00:00
|
|
|
} else if (request.method === SessionRequest.methods.signTypedData_v4.name ||
|
|
|
|
request.method === SessionRequest.methods.signTypedData.name)
|
|
|
|
{
|
|
|
|
let legacy = request.method === SessionRequest.methods.signTypedData.name
|
|
|
|
actionResult = store.safeSignTypedData(request.topic, request.id,
|
2024-06-12 13:48:44 +00:00
|
|
|
request.account.address, password,
|
2024-07-01 21:02:05 +00:00
|
|
|
SessionRequest.methods.signTypedData.getMessageFromData(request.data),
|
|
|
|
request.network.chainId, legacy)
|
2024-06-12 13:48:44 +00:00
|
|
|
} else if (request.method === SessionRequest.methods.signTransaction.name) {
|
2024-06-18 19:45:56 +00:00
|
|
|
let txObj = SessionRequest.methods.signTransaction.getTxObjFromData(request.data)
|
2024-06-23 08:27:29 +00:00
|
|
|
actionResult = store.signTransaction(request.topic, request.id,
|
|
|
|
request.account.address, request.network.chainId, password, txObj)
|
|
|
|
} else if (request.method === SessionRequest.methods.sendTransaction.name) {
|
|
|
|
let txObj = SessionRequest.methods.sendTransaction.getTxObjFromData(request.data)
|
|
|
|
actionResult = store.sendTransaction(request.topic, request.id,
|
2024-06-18 19:45:56 +00:00
|
|
|
request.account.address, request.network.chainId, password, txObj)
|
2024-06-12 13:48:44 +00:00
|
|
|
}
|
2024-06-23 08:27:29 +00:00
|
|
|
let isSuccessful = (actionResult != "")
|
2024-06-12 13:48:44 +00:00
|
|
|
if (isSuccessful) {
|
|
|
|
// acceptSessionRequest will trigger an sdk.sessionRequestUserAnswerResult signal
|
2024-06-23 08:27:29 +00:00
|
|
|
sdk.acceptSessionRequest(request.topic, request.id, actionResult)
|
2024-06-07 16:54:19 +00:00
|
|
|
} else {
|
2024-06-12 13:48:44 +00:00
|
|
|
root.sessionRequestResult(request, isSuccessful)
|
2024-06-07 16:54:19 +00:00
|
|
|
}
|
2024-06-12 13:48:44 +00:00
|
|
|
} else if (pin !== "") {
|
|
|
|
console.debug("TODO #15097 sign message using keycard: ", request.data)
|
2024-06-04 20:45:03 +00:00
|
|
|
} else {
|
2024-06-12 13:48:44 +00:00
|
|
|
console.error("No password or pin provided to sign message")
|
2024-06-04 20:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-31 09:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The queue is used to ensure that the events are processed in the order they are received but they could be
|
|
|
|
/// processed handled randomly on user intervention through activity center
|
|
|
|
SessionRequestsModel {
|
|
|
|
id: requests
|
|
|
|
}
|
|
|
|
|
|
|
|
Component {
|
|
|
|
id: sessionRequestComponent
|
|
|
|
|
|
|
|
SessionRequestResolved {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|