fix(browser-provider): fix browser provider not asking for authorization

Fixes #4721
This commit is contained in:
Jonathan Rainville 2022-02-04 10:54:07 -05:00
parent 268d7fcd36
commit d4410c1aa7
6 changed files with 56 additions and 50 deletions

View File

@ -1,4 +1,4 @@
import tables import strutils
import controller_interface import controller_interface
import io_interface import io_interface
@ -45,7 +45,7 @@ method disconnect*(self: Controller) =
discard self.dappPermissionsService.revoke("web3".toPermission()) discard self.dappPermissionsService.revoke("web3".toPermission())
method postMessage*(self: Controller, requestType: string, message: string): string = 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 = method hasPermission*(self: Controller, hostname: string, permission: string): bool =
return self.dappPermissionsService.hasPermission(hostname, permission.toPermission()) return self.dappPermissionsService.hasPermission(hostname, permission.toPermission())

View File

@ -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

View File

@ -21,39 +21,6 @@ export service_interface
logScope: logScope:
topics = "provider-service" 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 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 SIGN_METHODS = toHashSet(["eth_sign", "personal_sign", "eth_signTypedData", "eth_signTypedData_v3"])
const ACC_METHODS = toHashSet(["eth_accounts", "eth_coinbase"]) const ACC_METHODS = toHashSet(["eth_accounts", "eth_coinbase"])
@ -289,8 +256,8 @@ proc process(self: Service, data: APIRequest): string =
"data": value "data": value
} }
method postMessage*(self: Service, message: string): string = method postMessage*(self: Service, requestType: RequestTypes, message: string): string =
case message.requestType(): case requestType:
of RequestTypes.Web3SendAsyncReadOnly: self.process(message.toWeb3SendAsyncReadOnly()) of RequestTypes.Web3SendAsyncReadOnly: self.process(message.toWeb3SendAsyncReadOnly())
of RequestTypes.HistoryStateChanged: """{"type":"TODO-IMPLEMENT-THIS"}""" ############# TODO: of RequestTypes.HistoryStateChanged: """{"type":"TODO-IMPLEMENT-THIS"}""" ############# TODO:
of RequestTypes.APIRequest: self.process(message.toAPIRequest()) of RequestTypes.APIRequest: self.process(message.toAPIRequest())

View File

@ -1,3 +1,6 @@
import dto
export dto
type type
ServiceInterface* {.pure inheritable.} = ref object of RootObj ServiceInterface* {.pure inheritable.} = ref object of RootObj
## Abstract class for this service access. ## Abstract class for this service access.
@ -8,7 +11,7 @@ method delete*(self: ServiceInterface) {.base.} =
method init*(self: ServiceInterface) {.base.} = method init*(self: ServiceInterface) {.base.} =
raise newException(ValueError, "No implementation available") 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") raise newException(ValueError, "No implementation available")
method ensResourceURL*(self: ServiceInterface, ens: string, url: string): (string, string, string, string, bool) = method ensResourceURL*(self: ServiceInterface, ens: string, url: string): (string, string, string, string, bool) =

View File

@ -33,7 +33,7 @@ StatusModal {
interactedWith = true interactedWith = true
request.isAllowed = isAllowed; request.isAllowed = isAllowed;
RootStore.currentTabConnected = isAllowed RootStore.currentTabConnected = isAllowed
web3Response(Web3ProviderStore.web3ProviderInst.postMessage(JSON.stringify(request))) web3Response(Web3ProviderStore.web3ProviderInst.postMessage(Constants.api_request, JSON.stringify(request)))
} }
onClosed: { onClosed: {

View File

@ -27,7 +27,7 @@ QtObject {
return RootStore.getAscii2Hex(input) return RootStore.getAscii2Hex(input)
} }
function postMessage(data) { function postMessage(requestType, data) {
var request; var request;
try { try {
request = JSON.parse(data) request = JSON.parse(data)
@ -41,7 +41,7 @@ QtObject {
request.hostname = ensAddr; request.hostname = ensAddr;
} }
if (request.type === Constants.api_request) { if (requestType === Constants.api_request) {
if (!Web3ProviderStore.web3ProviderInst.hasPermission(request.hostname, request.permission)) { if (!Web3ProviderStore.web3ProviderInst.hasPermission(request.hostname, request.permission)) {
RootStore.currentTabConnected = false RootStore.currentTabConnected = false
var dialog = createAccessDialogComponent() var dialog = createAccessDialogComponent()
@ -49,15 +49,15 @@ QtObject {
dialog.open(); dialog.open();
} else { } else {
RootStore.currentTabConnected = true RootStore.currentTabConnected = true
request.isAllowed = true; request.isAllowed = true
web3Response(Web3ProviderStore.web3ProviderInst.postMessage(JSON.stringify(request))); web3Response(Web3ProviderStore.web3ProviderInst.postMessage(requestType, JSON.stringify(request)));
} }
} else if (request.type === Constants.web3SendAsyncReadOnly && } else if (requestType === Constants.web3SendAsyncReadOnly &&
request.payload.method === "eth_sendTransaction") { request.payload.method === "eth_sendTransaction") {
var acc = WalletStore.dappBrowserAccount
const value = RootStore.getWei2Eth(request.payload.params[0].value, 18); const value = RootStore.getWei2Eth(request.payload.params[0].value, 18);
const sendDialog = createSendTransactionModalComponent(request) const sendDialog = createSendTransactionModalComponent(request)
// TODO change sendTransaction function to the postMessage one
sendDialog.sendTransaction = function (selectedGasLimit, selectedGasPrice, selectedTipLimit, selectedOverallLimit, enteredPassword) { sendDialog.sendTransaction = function (selectedGasLimit, selectedGasPrice, selectedTipLimit, selectedOverallLimit, enteredPassword) {
request.payload.selectedGasLimit = selectedGasLimit request.payload.selectedGasLimit = selectedGasLimit
request.payload.selectedGasPrice = selectedGasPrice request.payload.selectedGasPrice = selectedGasPrice
@ -66,7 +66,7 @@ QtObject {
request.payload.password = enteredPassword request.payload.password = enteredPassword
request.payload.params[0].value = value 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) provider.web3Response(response)
let responseObj let responseObj
@ -76,6 +76,7 @@ QtObject {
if (responseObj.error) { if (responseObj.error) {
throw new Error(responseObj.error) throw new Error(responseObj.error)
} }
showToastMessage(responseObj.result.result) showToastMessage(responseObj.result.result)
} catch (e) { } catch (e) {
if (Utils.isInvalidPasswordMessage(e.message)){ if (Utils.isInvalidPasswordMessage(e.message)){
@ -92,7 +93,7 @@ QtObject {
sendDialog.open(); sendDialog.open();
WalletStore.getGasPrice() 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) const signDialog = createSignMessageModalComponent(request)
signDialog.web3Response = web3Response signDialog.web3Response = web3Response
signDialog.signMessage = function (enteredPassword) { signDialog.signMessage = function (enteredPassword) {
@ -104,12 +105,12 @@ QtObject {
case Constants.eth_sign: case Constants.eth_sign:
request.payload.params[1] = signValue(request.payload.params[1]); 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); provider.web3Response(response);
try { try {
let responseObj = JSON.parse(response) let responseObj = JSON.parse(response)
if (responseObj.error) { if (responseObj.error) {
throw new Error(responseObj.error) throw new Error(responseObj.error.message)
} }
} catch (e) { } catch (e) {
if (Utils.isInvalidPasswordMessage(e.message)){ if (Utils.isInvalidPasswordMessage(e.message)){
@ -128,7 +129,7 @@ QtObject {
} else if (request.type === Constants.web3DisconnectAccount) { } else if (request.type === Constants.web3DisconnectAccount) {
web3Response(data); web3Response(data);
} else { } else {
web3Response(Web3ProviderStore.web3ProviderInst.postMessage(data)); web3Response(Web3ProviderStore.web3ProviderInst.postMessage(requestType, data));
} }
} }