From d4410c1aa7ba3c187fa53b67695c283efbe62622 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 4 Feb 2022 10:54:07 -0500 Subject: [PATCH] fix(browser-provider): fix browser provider not asking for authorization Fixes #4721 --- .../browser_section/provider/controller.nim | 4 +- src/app_service/service/provider/dto.nim | 35 ++++++++++++++++++ src/app_service/service/provider/service.nim | 37 +------------------ .../service/provider/service_interface.nim | 5 ++- .../Browser/popups/BrowserConnectionModal.qml | 2 +- .../Browser/views/WebProviderObj.qml | 23 ++++++------ 6 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 src/app_service/service/provider/dto.nim diff --git a/src/app/modules/main/browser_section/provider/controller.nim b/src/app/modules/main/browser_section/provider/controller.nim index c173e99f45..eb630e6c00 100644 --- a/src/app/modules/main/browser_section/provider/controller.nim +++ b/src/app/modules/main/browser_section/provider/controller.nim @@ -1,4 +1,4 @@ -import tables +import strutils import controller_interface import io_interface @@ -45,7 +45,7 @@ method disconnect*(self: Controller) = discard self.dappPermissionsService.revoke("web3".toPermission()) method postMessage*(self: Controller, requestType: string, message: string): string = - return self.providerService.postMessage(requestType, message) + return self.providerService.postMessage(parseEnum[RequestTypes](requestType), message) method hasPermission*(self: Controller, hostname: string, permission: string): bool = return self.dappPermissionsService.hasPermission(hostname, permission.toPermission()) diff --git a/src/app_service/service/provider/dto.nim b/src/app_service/service/provider/dto.nim new file mode 100644 index 0000000000..17b72a2650 --- /dev/null +++ b/src/app_service/service/provider/dto.nim @@ -0,0 +1,35 @@ +import json +import ../dapp_permissions/service as dapp_permissions_service + +const HTTPS_SCHEME* = "https" +const IPFS_GATEWAY* = ".infura.status.im" +const SWARM_GATEWAY* = "swarm-gateways.net" + +type + RequestTypes* {.pure.} = enum + Web3SendAsyncReadOnly = "web3-send-async-read-only", + HistoryStateChanged = "history-state-changed", + APIRequest = "api-request" + Unknown = "unknown" + + ResponseTypes* {.pure.} = enum + Web3SendAsyncCallback = "web3-send-async-callback", + APIResponse = "api-response", + Web3ResponseError = "web3-response-error" + +type + Payload* = ref object + id*: JsonNode + rpcMethod*: string + + Web3SendAsyncReadOnly* = ref object + messageId*: JsonNode + payload*: Payload + request*: string + hostname*: string + + APIRequest* = ref object + isAllowed*: bool + messageId*: JsonNode + permission*: Permission + hostname*: string \ No newline at end of file diff --git a/src/app_service/service/provider/service.nim b/src/app_service/service/provider/service.nim index e5dcfc934d..5add04dbe4 100644 --- a/src/app_service/service/provider/service.nim +++ b/src/app_service/service/provider/service.nim @@ -21,39 +21,6 @@ export service_interface logScope: topics = "provider-service" -const HTTPS_SCHEME* = "https" -const IPFS_GATEWAY* = ".infura.status.im" -const SWARM_GATEWAY* = "swarm-gateways.net" - -type - RequestTypes {.pure.} = enum - Web3SendAsyncReadOnly = "web3-send-async-read-only", - HistoryStateChanged = "history-state-changed", - APIRequest = "api-request" - Unknown = "unknown" - - ResponseTypes {.pure.} = enum - Web3SendAsyncCallback = "web3-send-async-callback", - APIResponse = "api-response", - Web3ResponseError = "web3-response-error" - -type - Payload = ref object - id: JsonNode - rpcMethod: string - - Web3SendAsyncReadOnly = ref object - messageId: JsonNode - payload: Payload - request: string - hostname: string - - APIRequest = ref object - isAllowed: bool - messageId: JsonNode - permission: Permission - hostname: string - const AUTH_METHODS = toHashSet(["eth_accounts", "eth_coinbase", "eth_sendTransaction", "eth_sign", "keycard_signTypedData", "eth_signTypedData", "eth_signTypedData_v3", "personal_sign", "personal_ecRecover"]) const SIGN_METHODS = toHashSet(["eth_sign", "personal_sign", "eth_signTypedData", "eth_signTypedData_v3"]) const ACC_METHODS = toHashSet(["eth_accounts", "eth_coinbase"]) @@ -289,8 +256,8 @@ proc process(self: Service, data: APIRequest): string = "data": value } -method postMessage*(self: Service, message: string): string = - case message.requestType(): +method postMessage*(self: Service, requestType: RequestTypes, message: string): string = + case requestType: of RequestTypes.Web3SendAsyncReadOnly: self.process(message.toWeb3SendAsyncReadOnly()) of RequestTypes.HistoryStateChanged: """{"type":"TODO-IMPLEMENT-THIS"}""" ############# TODO: of RequestTypes.APIRequest: self.process(message.toAPIRequest()) diff --git a/src/app_service/service/provider/service_interface.nim b/src/app_service/service/provider/service_interface.nim index 4fabb60406..5d24a43252 100644 --- a/src/app_service/service/provider/service_interface.nim +++ b/src/app_service/service/provider/service_interface.nim @@ -1,3 +1,6 @@ +import dto +export dto + type ServiceInterface* {.pure inheritable.} = ref object of RootObj ## Abstract class for this service access. @@ -8,7 +11,7 @@ method delete*(self: ServiceInterface) {.base.} = method init*(self: ServiceInterface) {.base.} = raise newException(ValueError, "No implementation available") -method postMessage*(self: ServiceInterface, requestType: string, message: string): string {.base.} = +method postMessage*(self: ServiceInterface, requestType: RequestTypes, message: string): string {.base.} = raise newException(ValueError, "No implementation available") method ensResourceURL*(self: ServiceInterface, ens: string, url: string): (string, string, string, string, bool) = diff --git a/ui/app/AppLayouts/Browser/popups/BrowserConnectionModal.qml b/ui/app/AppLayouts/Browser/popups/BrowserConnectionModal.qml index 5cd92247d9..7f8645765e 100644 --- a/ui/app/AppLayouts/Browser/popups/BrowserConnectionModal.qml +++ b/ui/app/AppLayouts/Browser/popups/BrowserConnectionModal.qml @@ -33,7 +33,7 @@ StatusModal { interactedWith = true request.isAllowed = isAllowed; RootStore.currentTabConnected = isAllowed - web3Response(Web3ProviderStore.web3ProviderInst.postMessage(JSON.stringify(request))) + web3Response(Web3ProviderStore.web3ProviderInst.postMessage(Constants.api_request, JSON.stringify(request))) } onClosed: { diff --git a/ui/app/AppLayouts/Browser/views/WebProviderObj.qml b/ui/app/AppLayouts/Browser/views/WebProviderObj.qml index 9e61516a0b..fafdad0fd0 100644 --- a/ui/app/AppLayouts/Browser/views/WebProviderObj.qml +++ b/ui/app/AppLayouts/Browser/views/WebProviderObj.qml @@ -27,7 +27,7 @@ QtObject { return RootStore.getAscii2Hex(input) } - function postMessage(data) { + function postMessage(requestType, data) { var request; try { request = JSON.parse(data) @@ -41,7 +41,7 @@ QtObject { request.hostname = ensAddr; } - if (request.type === Constants.api_request) { + if (requestType === Constants.api_request) { if (!Web3ProviderStore.web3ProviderInst.hasPermission(request.hostname, request.permission)) { RootStore.currentTabConnected = false var dialog = createAccessDialogComponent() @@ -49,15 +49,15 @@ QtObject { dialog.open(); } else { RootStore.currentTabConnected = true - request.isAllowed = true; - web3Response(Web3ProviderStore.web3ProviderInst.postMessage(JSON.stringify(request))); + request.isAllowed = true + web3Response(Web3ProviderStore.web3ProviderInst.postMessage(requestType, JSON.stringify(request))); } - } else if (request.type === Constants.web3SendAsyncReadOnly && + } else if (requestType === Constants.web3SendAsyncReadOnly && request.payload.method === "eth_sendTransaction") { + var acc = WalletStore.dappBrowserAccount const value = RootStore.getWei2Eth(request.payload.params[0].value, 18); const sendDialog = createSendTransactionModalComponent(request) - // TODO change sendTransaction function to the postMessage one sendDialog.sendTransaction = function (selectedGasLimit, selectedGasPrice, selectedTipLimit, selectedOverallLimit, enteredPassword) { request.payload.selectedGasLimit = selectedGasLimit request.payload.selectedGasPrice = selectedGasPrice @@ -66,7 +66,7 @@ QtObject { request.payload.password = enteredPassword request.payload.params[0].value = value - const response = Web3ProviderStore.web3ProviderInst.postMessage(JSON.stringify(request)) + const response = Web3ProviderStore.web3ProviderInst.postMessage(requestType, JSON.stringify(request)) provider.web3Response(response) let responseObj @@ -76,6 +76,7 @@ QtObject { if (responseObj.error) { throw new Error(responseObj.error) } + showToastMessage(responseObj.result.result) } catch (e) { if (Utils.isInvalidPasswordMessage(e.message)){ @@ -92,7 +93,7 @@ QtObject { sendDialog.open(); WalletStore.getGasPrice() - } else if (request.type === Constants.web3SendAsyncReadOnly && ["eth_sign", "personal_sign", "eth_signTypedData", "eth_signTypedData_v3"].indexOf(request.payload.method) > -1) { + } else if (requestType === Constants.web3SendAsyncReadOnly && ["eth_sign", "personal_sign", "eth_signTypedData", "eth_signTypedData_v3"].indexOf(request.payload.method) > -1) { const signDialog = createSignMessageModalComponent(request) signDialog.web3Response = web3Response signDialog.signMessage = function (enteredPassword) { @@ -104,12 +105,12 @@ QtObject { case Constants.eth_sign: request.payload.params[1] = signValue(request.payload.params[1]); } - const response = Web3ProviderStore.web3ProviderInst.postMessage(JSON.stringify(request)); + const response = Web3ProviderStore.web3ProviderInst.postMessage(requestType, JSON.stringify(request)); provider.web3Response(response); try { let responseObj = JSON.parse(response) if (responseObj.error) { - throw new Error(responseObj.error) + throw new Error(responseObj.error.message) } } catch (e) { if (Utils.isInvalidPasswordMessage(e.message)){ @@ -128,7 +129,7 @@ QtObject { } else if (request.type === Constants.web3DisconnectAccount) { web3Response(data); } else { - web3Response(Web3ProviderStore.web3ProviderInst.postMessage(data)); + web3Response(Web3ProviderStore.web3ProviderInst.postMessage(requestType, data)); } }