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(){}
|
2021-10-25 20:29:36 +00:00
|
|
|
property int networkId: (Web3ProviderStore && Web3ProviderStore.networkId) || -1
|
|
|
|
|
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-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
|
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-02-04 15:54:07 +00:00
|
|
|
web3Response(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);
|
2021-12-07 23:15:17 +00:00
|
|
|
const sendDialog = createSendTransactionModalComponent(request)
|
2021-09-30 09:43:29 +00:00
|
|
|
|
|
|
|
sendDialog.sendTransaction = function (selectedGasLimit, selectedGasPrice, selectedTipLimit, selectedOverallLimit, enteredPassword) {
|
2022-02-09 10:22:24 +00:00
|
|
|
let trx = request.payload.params[0]
|
|
|
|
// TODO: use bignumber instead of floats
|
|
|
|
trx.value = RootStore.getEth2Hex(parseFloat(value))
|
|
|
|
trx.gas = "0x" + parseInt(selectedGasLimit, 10).toString(16)
|
2022-02-28 12:30:36 +00:00
|
|
|
if (WalletStore.isEIP1559Enabled()) {
|
2022-02-09 10:22:24 +00:00
|
|
|
trx.maxPriorityFeePerGas = RootStore.getGwei2Hex(parseFloat(selectedTipLimit))
|
|
|
|
trx.maxFeePerGas = RootStore.getGwei2Hex(parseFloat(selectedOverallLimit))
|
|
|
|
} else {
|
|
|
|
trx.gasPrice = RootStore.getGwei2Hex(parseFloat(selectedGasPrice))
|
|
|
|
}
|
|
|
|
|
2021-09-30 09:43:29 +00:00
|
|
|
request.payload.password = enteredPassword
|
2022-02-09 10:22:24 +00:00
|
|
|
request.payload.params[0] = trx
|
2021-09-30 09:43:29 +00:00
|
|
|
|
2022-02-04 15:54:07 +00:00
|
|
|
const response = Web3ProviderStore.web3ProviderInst.postMessage(requestType, JSON.stringify(request))
|
2021-09-30 09:43:29 +00:00
|
|
|
provider.web3Response(response)
|
|
|
|
|
|
|
|
let responseObj
|
|
|
|
try {
|
|
|
|
responseObj = JSON.parse(response)
|
|
|
|
|
|
|
|
if (responseObj.error) {
|
|
|
|
throw new Error(responseObj.error)
|
|
|
|
}
|
2022-02-04 15:54:07 +00:00
|
|
|
|
2021-12-07 23:15:17 +00:00
|
|
|
showToastMessage(responseObj.result.result)
|
2021-09-30 09:43:29 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (Utils.isInvalidPasswordMessage(e.message)){
|
|
|
|
//% "Wrong password"
|
|
|
|
sendDialog.transactionSigner.validationError = qsTrId("wrong-password")
|
|
|
|
return
|
|
|
|
}
|
2021-12-07 23:15:17 +00:00
|
|
|
return showSendingError(e.message)
|
2021-09-30 09:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sendDialog.close()
|
|
|
|
sendDialog.destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
sendDialog.open();
|
|
|
|
WalletStore.getGasPrice()
|
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;
|
|
|
|
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-02-04 15:54:07 +00:00
|
|
|
const response = Web3ProviderStore.web3ProviderInst.postMessage(requestType, JSON.stringify(request));
|
2021-09-30 09:43:29 +00:00
|
|
|
provider.web3Response(response);
|
|
|
|
try {
|
|
|
|
let responseObj = JSON.parse(response)
|
|
|
|
if (responseObj.error) {
|
2022-02-04 15:54:07 +00:00
|
|
|
throw new Error(responseObj.error.message)
|
2021-09-30 09:43:29 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (Utils.isInvalidPasswordMessage(e.message)){
|
|
|
|
//% "Wrong password"
|
|
|
|
signDialog.transactionSigner.validationError = qsTrId("wrong-password")
|
|
|
|
return
|
|
|
|
}
|
2021-12-07 23:15:17 +00:00
|
|
|
return showSigningError(e.message)
|
2021-09-30 09:43:29 +00:00
|
|
|
}
|
|
|
|
signDialog.close()
|
|
|
|
signDialog.destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
signDialog.open();
|
|
|
|
} else if (request.type === Constants.web3DisconnectAccount) {
|
|
|
|
web3Response(data);
|
|
|
|
} else {
|
2022-02-04 15:54:07 +00:00
|
|
|
web3Response(Web3ProviderStore.web3ProviderInst.postMessage(requestType, data));
|
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
|
|
|
}
|