2021-09-30 09:43:29 +00:00
|
|
|
import QtQuick 2.13
|
|
|
|
import QtWebChannel 1.13
|
2021-12-07 23:15:17 +00:00
|
|
|
import QtQuick.Dialogs 1.2
|
2021-09-30 09:43:29 +00:00
|
|
|
|
|
|
|
import utils 1.0
|
2021-10-27 21:27:49 +00:00
|
|
|
import shared.controls 1.0
|
2021-09-30 09:43:29 +00:00
|
|
|
|
|
|
|
import "../stores"
|
|
|
|
|
|
|
|
QtObject {
|
|
|
|
id: provider
|
|
|
|
|
2021-12-07 23:15:17 +00:00
|
|
|
property var createAccessDialogComponent: function(){}
|
|
|
|
property var createSendTransactionModalComponent: function(){}
|
|
|
|
property var createSignMessageModalComponent: function(){}
|
|
|
|
property var showSendingError: function(){}
|
|
|
|
property var showSigningError: function(){}
|
|
|
|
property var showToastMessage: function(){}
|
2022-06-07 13:57:09 +00:00
|
|
|
property int chainId: (Web3ProviderStore && Web3ProviderStore.chainId) || 1
|
2021-10-25 20:29:36 +00:00
|
|
|
|
2021-12-07 23:15:17 +00:00
|
|
|
signal web3Response(string data);
|
|
|
|
|
2021-09-30 09:43:29 +00:00
|
|
|
function signValue(input){
|
|
|
|
if(Utils.isHex(input) && Utils.startsWith0x(input)){
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
return RootStore.getAscii2Hex(input)
|
|
|
|
}
|
|
|
|
|
2022-03-29 10:21:12 +00:00
|
|
|
property Connections conn: Connections {
|
|
|
|
target: Web3ProviderStore.web3ProviderInst
|
|
|
|
|
2023-01-18 09:25:36 +00:00
|
|
|
function onPostMessageResult(payloadMethod: string, result: string, chainId: string) {
|
2022-03-29 10:21:12 +00:00
|
|
|
web3Response(result)
|
|
|
|
const isSign = ["eth_sign", "personal_sign", "eth_signTypedData", "eth_signTypedData_v3"].indexOf(payloadMethod) > -1
|
|
|
|
const isTx = payloadMethod === "eth_sendTransaction"
|
|
|
|
try {
|
|
|
|
let responseObj = JSON.parse(result)
|
|
|
|
if (responseObj.error) {
|
|
|
|
throw new Error(responseObj.error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isTx) {
|
2022-12-08 15:56:02 +00:00
|
|
|
showToastMessage(responseObj.result.result, chainId)
|
2022-03-29 10:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
if (isTx) {
|
|
|
|
showSendingError(e.message)
|
|
|
|
} else if (isSign) {
|
2022-04-05 15:45:01 +00:00
|
|
|
showSigningError(e.message.message)
|
2022-03-29 10:21:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-04 15:54:07 +00:00
|
|
|
function postMessage(requestType, data) {
|
2021-09-30 09:43:29 +00:00
|
|
|
var request;
|
|
|
|
try {
|
|
|
|
request = JSON.parse(data)
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error parsing the message data", e)
|
|
|
|
return;
|
|
|
|
}
|
2022-03-04 09:09:58 +00:00
|
|
|
request.address = WalletStore.dappBrowserAccount.address
|
2022-06-07 13:57:09 +00:00
|
|
|
if (!request.payload) {
|
|
|
|
request.payload = {}
|
|
|
|
}
|
|
|
|
request.payload.chainId = provider.chainId
|
2021-09-30 09:43:29 +00:00
|
|
|
|
|
|
|
var ensAddr = Web3ProviderStore.urlENSDictionary[request.hostname];
|
|
|
|
if (ensAddr) {
|
|
|
|
request.hostname = ensAddr;
|
|
|
|
}
|
2021-12-07 23:15:17 +00:00
|
|
|
|
2022-02-09 10:22:24 +00:00
|
|
|
if (requestType === Constants.web3DisconnectAccount) {
|
2022-03-14 12:32:21 +00:00
|
|
|
RootStore.currentTabConnected = false
|
2022-02-09 10:22:24 +00:00
|
|
|
web3Response(JSON.stringify({type: Constants.web3DisconnectAccount}));
|
|
|
|
} else if (requestType === Constants.api_request) {
|
2022-03-04 09:09:58 +00:00
|
|
|
if (!Web3ProviderStore.hasPermission(request.hostname, request.address, request.permission)) {
|
2021-12-07 23:15:17 +00:00
|
|
|
RootStore.currentTabConnected = false
|
|
|
|
var dialog = createAccessDialogComponent()
|
2021-09-30 09:43:29 +00:00
|
|
|
dialog.request = request;
|
|
|
|
dialog.open();
|
|
|
|
} else {
|
2021-12-07 23:15:17 +00:00
|
|
|
RootStore.currentTabConnected = true
|
2022-03-29 10:21:12 +00:00
|
|
|
Web3ProviderStore.web3ProviderInst.postMessage("", requestType, JSON.stringify(request));
|
2021-09-30 09:43:29 +00:00
|
|
|
}
|
2022-02-04 15:54:07 +00:00
|
|
|
} else if (requestType === Constants.web3SendAsyncReadOnly &&
|
2021-09-30 09:43:29 +00:00
|
|
|
request.payload.method === "eth_sendTransaction") {
|
2022-02-04 15:54:07 +00:00
|
|
|
var acc = WalletStore.dappBrowserAccount
|
2021-09-30 09:43:29 +00:00
|
|
|
const value = RootStore.getWei2Eth(request.payload.params[0].value, 18);
|
2022-10-17 10:17:25 +00:00
|
|
|
const sendDialog = createSendTransactionModalComponent(request, requestType)
|
|
|
|
|
|
|
|
sendDialog.sendTransaction = function () {
|
|
|
|
if(sendDialog.bestRoutes.length === 1) {
|
|
|
|
let path = sendDialog.bestRoutes[0]
|
|
|
|
let eip1559Enabled = path.gasFees.eip1559Enabled
|
2022-11-30 12:59:21 +00:00
|
|
|
let maxFeePerGas = path.gasFees.maxFeePerGasM
|
2022-10-17 10:17:25 +00:00
|
|
|
let trx = request.payload.params[0]
|
|
|
|
// TODO: use bignumber instead of floats
|
|
|
|
trx.value = RootStore.getEth2Hex(parseFloat(value))
|
|
|
|
trx.gas = "0x" + parseInt(path.gasAmount, 10).toString(16)
|
|
|
|
trx.maxPriorityFeePerGas = RootStore.getGwei2Hex(parseFloat(eip1559Enabled ? path.gasFees.maxPriorityFeePerGas : "0"))
|
|
|
|
trx.maxFeePerGas = RootStore.getGwei2Hex(parseFloat(eip1559Enabled ? maxFeePerGas : path.gasFees.gasPrice))
|
|
|
|
|
|
|
|
request.payload.params[0] = trx
|
|
|
|
|
|
|
|
Web3ProviderStore.web3ProviderInst.authenticateToPostMessage(request.payload.method, requestType, JSON.stringify(request))
|
|
|
|
sendDialog.close()
|
|
|
|
}
|
2021-09-30 09:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sendDialog.open();
|
2022-02-04 15:54:07 +00:00
|
|
|
} else if (requestType === Constants.web3SendAsyncReadOnly && ["eth_sign", "personal_sign", "eth_signTypedData", "eth_signTypedData_v3"].indexOf(request.payload.method) > -1) {
|
2021-12-07 23:15:17 +00:00
|
|
|
const signDialog = createSignMessageModalComponent(request)
|
2021-09-30 09:43:29 +00:00
|
|
|
signDialog.web3Response = web3Response
|
|
|
|
signDialog.signMessage = function (enteredPassword) {
|
|
|
|
signDialog.interactedWith = true;
|
|
|
|
request.payload.password = enteredPassword;
|
2022-05-13 10:28:12 +00:00
|
|
|
request.payload.from = WalletStore.dappBrowserAccount.address;
|
2021-09-30 09:43:29 +00:00
|
|
|
switch(request.payload.method){
|
|
|
|
case Constants.personal_sign:
|
|
|
|
request.payload.params[0] = signValue(request.payload.params[0]);
|
|
|
|
case Constants.eth_sign:
|
|
|
|
request.payload.params[1] = signValue(request.payload.params[1]);
|
|
|
|
}
|
2022-03-29 10:21:12 +00:00
|
|
|
Web3ProviderStore.web3ProviderInst.postMessage(request.payload.method, requestType, JSON.stringify(request));
|
2021-09-30 09:43:29 +00:00
|
|
|
signDialog.close()
|
|
|
|
signDialog.destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
signDialog.open();
|
|
|
|
} else if (request.type === Constants.web3DisconnectAccount) {
|
|
|
|
web3Response(data);
|
|
|
|
} else {
|
2022-06-07 13:57:09 +00:00
|
|
|
Web3ProviderStore.web3ProviderInst.postMessage(request.payload.method, requestType, JSON.stringify(request));
|
2021-09-30 09:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-07 23:15:17 +00:00
|
|
|
|
|
|
|
WebChannel.id: "backend"
|
2021-09-30 09:43:29 +00:00
|
|
|
}
|