2024-05-06 17:55:11 +00:00
|
|
|
import QtQuick 2.15
|
|
|
|
import QtQuick.Controls 2.15
|
|
|
|
import QtQuick.Layouts 1.15
|
|
|
|
|
2024-06-29 20:24:05 +00:00
|
|
|
import StatusQ 0.1
|
2024-10-15 19:26:12 +00:00
|
|
|
import StatusQ.Core.Theme 0.1
|
2024-07-17 10:46:43 +00:00
|
|
|
import StatusQ.Core.Utils 0.1 as SQUtils
|
2024-06-29 20:24:05 +00:00
|
|
|
import SortFilterProxyModel 0.2
|
|
|
|
|
2024-07-09 17:49:00 +00:00
|
|
|
import AppLayouts.Wallet 1.0
|
2024-05-06 17:55:11 +00:00
|
|
|
import AppLayouts.Wallet.controls 1.0
|
2024-05-06 20:22:43 +00:00
|
|
|
import AppLayouts.Wallet.services.dapps 1.0
|
2024-05-31 09:58:47 +00:00
|
|
|
import AppLayouts.Wallet.services.dapps.types 1.0
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-07-09 17:49:00 +00:00
|
|
|
import shared.popups.walletconnect 1.0
|
|
|
|
|
2024-05-31 09:34:59 +00:00
|
|
|
import utils 1.0
|
2024-05-06 17:55:11 +00:00
|
|
|
|
2024-06-19 13:13:32 +00:00
|
|
|
DappsComboBox {
|
2024-05-06 17:55:11 +00:00
|
|
|
id: root
|
|
|
|
|
2024-07-11 21:00:15 +00:00
|
|
|
// Values mapped to Constants.LoginType
|
|
|
|
required property int loginType
|
2024-10-03 18:41:41 +00:00
|
|
|
/*
|
|
|
|
Accounts model
|
|
|
|
|
|
|
|
Expected model structure:
|
|
|
|
name [string] - account name e.g. "Piggy Bank"
|
|
|
|
address [string] - wallet account address e.g. "0x1234567890"
|
|
|
|
colorizedChainPrefixes [string] - chain prefixes with rich text colors e.g. "<font color=\"red\">eth:</font><font color=\"blue\">oeth:</font><font color=\"green\">arb:</font>"
|
|
|
|
emoji [string] - emoji for account e.g. "🐷"
|
|
|
|
colorId [string] - color id for account e.g. "1"
|
|
|
|
currencyBalance [var] - fiat currency balance
|
|
|
|
amount [number] - amount of currency e.g. 1234
|
|
|
|
symbol [string] - currency symbol e.g. "USD"
|
|
|
|
optDisplayDecimals [number] - optional number of decimals to display
|
|
|
|
stripTrailingZeroes [bool] - strip trailing zeroes
|
|
|
|
walletType [string] - wallet type e.g. Constants.watchWalletType. See `Constants` for possible values
|
|
|
|
migratedToKeycard [bool] - whether account is migrated to keycard
|
|
|
|
accountBalance [var] - account balance for a specific network
|
|
|
|
formattedBalance [string] - formatted balance e.g. "1234.56B"
|
|
|
|
balance [string] - balance e.g. "123456000000"
|
|
|
|
iconUrl [string] - icon url e.g. "network/Network=Hermez"
|
|
|
|
chainColor [string] - chain color e.g. "#FF0000"
|
|
|
|
*/
|
2024-10-03 18:15:24 +00:00
|
|
|
property var accountsModel
|
2024-10-03 18:41:41 +00:00
|
|
|
/*
|
|
|
|
Networks model
|
|
|
|
Expected model structure:
|
|
|
|
chainName [string] - chain long name. e.g. "Ethereum" or "Optimism"
|
|
|
|
chainId [int] - chain unique identifier
|
|
|
|
iconUrl [string] - SVG icon name. e.g. "network/Network=Ethereum"
|
|
|
|
layer [int] - chain layer. e.g. 1 or 2
|
|
|
|
isTest [bool] - true if the chain is a testnet
|
|
|
|
*/
|
2024-10-03 18:15:24 +00:00
|
|
|
property var networksModel
|
2024-10-03 18:41:41 +00:00
|
|
|
/*
|
|
|
|
ObjectModel containig session requests
|
|
|
|
requestId [string] - unique identifier for the request
|
|
|
|
requestItem [SessionRequestResolved] - request object
|
|
|
|
*/
|
2024-10-03 18:15:24 +00:00
|
|
|
property SessionRequestsModel sessionRequestsModel
|
2024-07-11 21:00:15 +00:00
|
|
|
property string selectedAccountAddress
|
2024-05-06 20:22:43 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
property var formatBigNumber: (number, symbol, noSymbolOption) => console.error("formatBigNumber not set")
|
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
signal pairWCReady()
|
2024-05-06 17:55:11 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
signal disconnectRequested(string connectionId)
|
|
|
|
signal pairingRequested(string uri)
|
|
|
|
signal pairingValidationRequested(string uri)
|
|
|
|
signal connectionAccepted(var pairingId, var chainIds, string selectedAccount)
|
|
|
|
signal connectionDeclined(var pairingId)
|
|
|
|
signal signRequestAccepted(string connectionId, string requestId)
|
|
|
|
signal signRequestRejected(string connectionId, string requestId)
|
|
|
|
|
2024-10-30 21:37:42 +00:00
|
|
|
signal subscribeForFeeUpdates(string connectionId, string requestId)
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
/// Response to pairingValidationRequested
|
|
|
|
function pairingValidated(validationState) {
|
|
|
|
if (pairWCLoader.item) {
|
|
|
|
pairWCLoader.item.pairingValidated(validationState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Confirmation received on connectionAccepted
|
|
|
|
function connectionSuccessful(pairingId, newConnectionId) {
|
|
|
|
connectDappLoader.connectionSuccessful(pairingId, newConnectionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Confirmation received on connectionAccepted
|
|
|
|
function connectionFailed(pairingId) {
|
|
|
|
connectDappLoader.connectionFailed(pairingId)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Request to connect to a dApp
|
|
|
|
function connectDApp(dappChains, dappUrl, dappName, dappIcon, pairingId) {
|
|
|
|
connectDappLoader.connect(dappChains, dappUrl, dappName, dappIcon, pairingId)
|
|
|
|
}
|
2024-05-06 17:55:11 +00:00
|
|
|
|
2024-06-19 13:13:32 +00:00
|
|
|
onPairDapp: {
|
|
|
|
pairWCLoader.active = true
|
|
|
|
}
|
2024-05-06 17:55:11 +00:00
|
|
|
|
2024-06-24 21:26:53 +00:00
|
|
|
onDisconnectDapp: (dappUrl) => {
|
2024-07-15 09:23:35 +00:00
|
|
|
disconnectdAppDialogLoader.dAppUrl = dappUrl
|
|
|
|
disconnectdAppDialogLoader.active = true
|
|
|
|
}
|
|
|
|
|
|
|
|
Loader {
|
|
|
|
id: disconnectdAppDialogLoader
|
|
|
|
|
|
|
|
property string dAppUrl
|
|
|
|
|
|
|
|
active: false
|
|
|
|
|
|
|
|
onLoaded: {
|
2024-10-03 18:15:24 +00:00
|
|
|
const dApp = SQUtils.ModelUtils.getByKey(root.model, "url", dAppUrl);
|
2024-07-15 09:23:35 +00:00
|
|
|
if (dApp) {
|
|
|
|
item.dappName = dApp.name;
|
|
|
|
item.dappIcon = dApp.iconUrl;
|
|
|
|
item.dappUrl = disconnectdAppDialogLoader.dAppUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
item.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceComponent: DAppConfirmDisconnectPopup {
|
|
|
|
|
|
|
|
visible: true
|
|
|
|
|
|
|
|
onClosed: {
|
|
|
|
disconnectdAppDialogLoader.active = false
|
|
|
|
}
|
|
|
|
|
|
|
|
onAccepted: {
|
2024-10-03 18:15:24 +00:00
|
|
|
SQUtils.ModelUtils.forEach(model, (dApp) => {
|
|
|
|
if (dApp.url === dAppUrl) {
|
|
|
|
root.disconnectRequested(dApp.topic)
|
|
|
|
}
|
|
|
|
})
|
2024-07-15 09:23:35 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-24 21:26:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 17:55:11 +00:00
|
|
|
Loader {
|
2024-05-06 20:22:43 +00:00
|
|
|
id: pairWCLoader
|
2024-05-06 17:55:11 +00:00
|
|
|
|
|
|
|
active: false
|
|
|
|
|
|
|
|
onLoaded: {
|
|
|
|
item.open()
|
2024-05-06 20:22:43 +00:00
|
|
|
root.pairWCReady()
|
2024-05-06 17:55:11 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
sourceComponent: PairWCModal {
|
2024-05-06 17:55:11 +00:00
|
|
|
visible: true
|
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
onClosed: pairWCLoader.active = false
|
2024-10-03 18:15:24 +00:00
|
|
|
onPair: (uri) => root.pairingRequested(uri)
|
|
|
|
onPairUriChanged: (uri) => root.pairingValidationRequested(uri)
|
2024-05-06 17:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
Loader {
|
|
|
|
id: connectDappLoader
|
|
|
|
|
|
|
|
active: false
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
// Array of chaind ids
|
|
|
|
property var dappChains
|
|
|
|
property url dappUrl
|
|
|
|
property string dappName
|
|
|
|
property url dappIcon
|
|
|
|
property var key
|
|
|
|
property var topic
|
|
|
|
|
|
|
|
property var connectionQueue: []
|
|
|
|
onActiveChanged: {
|
|
|
|
if (!active && connectionQueue.length > 0) {
|
|
|
|
connect(connectionQueue[0].dappChains,
|
|
|
|
connectionQueue[0].dappUrl,
|
|
|
|
connectionQueue[0].dappName,
|
|
|
|
connectionQueue[0].dappIcon,
|
|
|
|
connectionQueue[0].key)
|
|
|
|
connectionQueue.shift()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function connect(dappChains, dappUrl, dappName, dappIcon, key) {
|
|
|
|
if (connectDappLoader.active) {
|
|
|
|
connectionQueue.push({ dappChains, dappUrl, dappName, dappIcon, key })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
connectDappLoader.dappChains = dappChains
|
|
|
|
connectDappLoader.dappUrl = dappUrl
|
|
|
|
connectDappLoader.dappName = dappName
|
|
|
|
connectDappLoader.dappIcon = dappIcon
|
|
|
|
connectDappLoader.key = key
|
|
|
|
|
|
|
|
if (pairWCLoader.item) {
|
|
|
|
// Allow user to get the uri valid confirmation
|
|
|
|
pairWCLoader.item.pairingValidated(Pairing.errors.dappReadyForApproval)
|
|
|
|
connectDappTimer.start()
|
|
|
|
} else {
|
|
|
|
connectDappLoader.active = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function connectionSuccessful(key, newTopic) {
|
|
|
|
if (connectDappLoader.key === key && connectDappLoader.item) {
|
|
|
|
connectDappLoader.topic = newTopic
|
|
|
|
connectDappLoader.item.pairSuccessful()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function connectionFailed(id) {
|
|
|
|
if (connectDappLoader.key === key && connectDappLoader.item) {
|
|
|
|
connectDappLoader.item.pairFailed()
|
|
|
|
}
|
|
|
|
}
|
2024-05-06 20:22:43 +00:00
|
|
|
|
|
|
|
sourceComponent: ConnectDAppModal {
|
|
|
|
visible: true
|
2024-10-03 18:41:41 +00:00
|
|
|
|
2024-05-06 20:22:43 +00:00
|
|
|
onClosed: connectDappLoader.active = false
|
2024-10-03 18:15:24 +00:00
|
|
|
accounts: root.accountsModel
|
2024-06-29 20:24:05 +00:00
|
|
|
flatNetworks: SortFilterProxyModel {
|
2024-10-03 18:15:24 +00:00
|
|
|
sourceModel: root.networksModel
|
2024-06-29 20:24:05 +00:00
|
|
|
filters: [
|
|
|
|
FastExpressionFilter {
|
|
|
|
inverted: true
|
|
|
|
expression: connectDappLoader.dappChains.indexOf(chainId) === -1
|
|
|
|
expectedRoles: ["chainId"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2024-07-11 21:00:15 +00:00
|
|
|
selectedAccountAddress: root.selectedAccountAddress
|
2024-06-29 20:24:05 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
dAppUrl: connectDappLoader.dappUrl
|
|
|
|
dAppName: connectDappLoader.dappName
|
|
|
|
dAppIconUrl: connectDappLoader.dappIcon
|
2024-05-06 20:22:43 +00:00
|
|
|
|
|
|
|
onConnect: {
|
2024-10-03 18:15:24 +00:00
|
|
|
if (!selectedAccount || !selectedAccount.address) {
|
|
|
|
console.error("Missing account selection")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!selectedChains || selectedChains.length === 0) {
|
|
|
|
console.error("Missing chain selection")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
root.connectionAccepted(connectDappLoader.key, selectedChains, selectedAccount.address)
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onDecline: {
|
2024-10-03 18:15:24 +00:00
|
|
|
root.connectionDeclined(connectDappLoader.key)
|
|
|
|
close()
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
onDisconnect: {
|
|
|
|
root.disconnectRequested(connectDappLoader.topic)
|
|
|
|
close()
|
2024-05-06 20:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
Instantiator {
|
|
|
|
model: root.sessionRequestsModel
|
|
|
|
delegate: DAppSignRequestModal {
|
2024-07-09 17:49:00 +00:00
|
|
|
id: dappRequestModal
|
2024-07-11 21:00:15 +00:00
|
|
|
objectName: "dappsRequestModal"
|
2024-05-31 09:58:47 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
required property var model
|
|
|
|
required property int index
|
|
|
|
|
|
|
|
readonly property var request: model.requestItem
|
2024-09-17 07:42:01 +00:00
|
|
|
readonly property var account: accountEntry.available ? accountEntry.item : {
|
|
|
|
name: "",
|
|
|
|
address: "",
|
|
|
|
emoji: "",
|
2024-09-25 17:15:13 +00:00
|
|
|
colorId: 0,
|
|
|
|
migratedToKeycard: false
|
2024-09-17 07:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readonly property var network: networkEntry.available ? networkEntry.item : {
|
|
|
|
chainName: "",
|
|
|
|
iconUrl: ""
|
|
|
|
}
|
2024-10-03 18:15:24 +00:00
|
|
|
property bool requestHandled: false
|
|
|
|
|
|
|
|
function rejectRequest() {
|
|
|
|
// Allow rejecting only once
|
|
|
|
if (requestHandled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
requestHandled = true
|
|
|
|
root.signRequestRejected(request.topic, request.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
parent: root
|
2024-09-17 07:42:01 +00:00
|
|
|
|
2024-09-25 17:15:13 +00:00
|
|
|
loginType: account.migratedToKeycard ? Constants.LoginType.Keycard : root.loginType
|
2024-10-03 18:15:24 +00:00
|
|
|
formatBigNumber: root.formatBigNumber
|
|
|
|
|
|
|
|
visible: !!request.dappUrl
|
2024-07-17 10:46:43 +00:00
|
|
|
|
2024-05-31 09:58:47 +00:00
|
|
|
dappUrl: request.dappUrl
|
|
|
|
dappIcon: request.dappIcon
|
2024-07-11 21:00:15 +00:00
|
|
|
dappName: request.dappName
|
2024-05-31 09:58:47 +00:00
|
|
|
|
2024-09-17 07:42:01 +00:00
|
|
|
accountColor: Utils.getColorForId(account.colorId)
|
|
|
|
accountName: account.name
|
|
|
|
accountAddress: account.address
|
|
|
|
accountEmoji: account.emoji
|
2024-07-11 21:00:15 +00:00
|
|
|
|
2024-09-17 07:42:01 +00:00
|
|
|
networkName: network.chainName
|
2024-10-15 19:26:12 +00:00
|
|
|
networkIconPath: Theme.svg(network.iconUrl)
|
2024-07-11 21:00:15 +00:00
|
|
|
|
2024-10-03 18:15:24 +00:00
|
|
|
fiatFees: request.fiatMaxFees ? request.fiatMaxFees.toFixed() : ""
|
|
|
|
cryptoFees: request.ethMaxFees ? request.ethMaxFees.toFixed() : ""
|
|
|
|
estimatedTime: WalletUtils.getLabelForEstimatedTxTime(request.estimatedTimeCategory)
|
|
|
|
feesLoading: hasFees && (!fiatFees || !cryptoFees)
|
2024-07-11 21:00:15 +00:00
|
|
|
hasFees: signingTransaction
|
2024-10-03 18:15:24 +00:00
|
|
|
enoughFundsForTransaction: request.haveEnoughFunds
|
|
|
|
enoughFundsForFees: request.haveEnoughFees
|
2024-07-11 21:00:15 +00:00
|
|
|
|
2024-07-17 16:36:32 +00:00
|
|
|
signingTransaction: !!request.method && (request.method === SessionRequest.methods.signTransaction.name
|
|
|
|
|| request.method === SessionRequest.methods.sendTransaction.name)
|
2024-10-30 21:37:42 +00:00
|
|
|
requestPayload: {
|
|
|
|
try {
|
|
|
|
const data = JSON.parse(request.preparedData)
|
|
|
|
|
|
|
|
delete data.maxFeePerGas
|
|
|
|
delete data.maxPriorityFeePerGas
|
|
|
|
delete data.gasPrice
|
|
|
|
|
|
|
|
return JSON.stringify(data, null, 2)
|
|
|
|
} catch(_) {
|
|
|
|
return request.preparedData
|
|
|
|
}
|
|
|
|
}
|
2024-10-04 12:49:16 +00:00
|
|
|
expirationSeconds: request.expirationTimestamp ? request.expirationTimestamp - requestTimestamp.getTime() / 1000
|
|
|
|
: 0
|
|
|
|
hasExpiryDate: !!request.expirationTimestamp
|
2024-10-03 18:15:24 +00:00
|
|
|
|
2024-10-30 21:37:42 +00:00
|
|
|
onOpened: {
|
|
|
|
root.subscribeForFeeUpdates(request.topic, request.id)
|
|
|
|
}
|
|
|
|
|
2024-07-15 17:30:27 +00:00
|
|
|
onClosed: {
|
2024-10-03 18:15:24 +00:00
|
|
|
Qt.callLater(rejectRequest)
|
2024-07-15 17:30:27 +00:00
|
|
|
}
|
2024-05-31 09:58:47 +00:00
|
|
|
|
2024-07-11 21:00:15 +00:00
|
|
|
onAccepted: {
|
2024-07-15 17:30:27 +00:00
|
|
|
requestHandled = true
|
2024-10-03 18:15:24 +00:00
|
|
|
root.signRequestAccepted(request.topic, request.id)
|
2024-05-31 09:58:47 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 21:00:15 +00:00
|
|
|
onRejected: {
|
2024-07-15 17:30:27 +00:00
|
|
|
rejectRequest()
|
|
|
|
}
|
|
|
|
|
2024-09-17 07:42:01 +00:00
|
|
|
ModelEntry {
|
|
|
|
id: accountEntry
|
2024-10-03 18:15:24 +00:00
|
|
|
sourceModel: root.accountsModel
|
2024-09-17 07:42:01 +00:00
|
|
|
key: "address"
|
|
|
|
value: request.accountAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelEntry {
|
|
|
|
id: networkEntry
|
2024-10-03 18:15:24 +00:00
|
|
|
sourceModel: root.networksModel
|
2024-09-17 07:42:01 +00:00
|
|
|
key: "chainId"
|
|
|
|
value: request.chainId
|
|
|
|
}
|
2024-05-31 09:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-24 15:37:08 +00:00
|
|
|
// Used between transitioning from PairWCModal to ConnectDAppModal
|
|
|
|
Timer {
|
|
|
|
id: connectDappTimer
|
|
|
|
|
|
|
|
interval: 500
|
|
|
|
running: false
|
|
|
|
repeat: false
|
|
|
|
|
|
|
|
onTriggered: {
|
|
|
|
pairWCLoader.item.close()
|
|
|
|
connectDappLoader.active = true
|
|
|
|
}
|
|
|
|
}
|
2024-05-06 17:55:11 +00:00
|
|
|
}
|