From 63893ff57599cfc1fed33da5d0f8f227f59e4192 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Tue, 9 Feb 2021 08:38:25 +0100 Subject: [PATCH] Handle eth_accounts exception Signed-off-by: Andrea Maria Piana --- resources/js/provider.js | 30 +++++++++++++++++++++----- src/status_im/browser/core.cljs | 37 ++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/resources/js/provider.js b/resources/js/provider.js index b16608353d..9384e2c37c 100644 --- a/resources/js/provider.js +++ b/resources/js/provider.js @@ -84,6 +84,7 @@ } else if (data.isAllowed) { if (data.permission == 'web3') { window.statusAppcurrentAccountAddress = data.data[0]; + window.ethereum.emit("accountsChanged", data.data); } callback.resolve(data.data); } else { @@ -152,6 +153,30 @@ EthereumProvider.prototype.status = new StatusAPI(); EthereumProvider.prototype.isConnected = function () { return true; }; + EthereumProvider.prototype._events = {}; + + EthereumProvider.prototype.on = function(name, listener) { + if (!this._events[name]) { + this._events[name] = []; + } + this._events[name].push(listener); + } + + EthereumProvider.prototype.removeListener = function (name, listenerToRemove) { + if (!this._events[name]) { + return + } + + const filterListeners = (listener) => listener !== listenerToRemove; + this._events[name] = this._events[name].filter(filterListeners); + } + + EthereumProvider.prototype.emit = function (name, data) { + if (!this._events[name]) { + return + } + this._events[name].forEach(cb => cb(data)); + } EthereumProvider.prototype.enable = function () { return sendAPIrequest('web3'); }; @@ -160,11 +185,6 @@ return sendAPIrequest('qr-code', {regex: regex}); }; - EthereumProvider.prototype.on = function (type, handler) - { - console.log("Not supported by Status") - } - EthereumProvider.prototype.request = function (requestArguments) { if (!requestArguments) { diff --git a/src/status_im/browser/core.cljs b/src/status_im/browser/core.cljs index fa7fdc2370..27b1bcfc0a 100644 --- a/src/status_im/browser/core.cljs +++ b/src/status_im/browser/core.cljs @@ -403,18 +403,35 @@ :error %1 :result %2}])]})))) +(fx/defn handle-no-permissions [cofx {:keys [method id]} message-id] + (if (= method "eth_accounts") + ;; eth_accounts returns empty array for compatibility with meta-mask + (send-to-bridge cofx + {:type constants/web3-send-async-callback + :messageId message-id + :result {:jsonrpc "2.0" + :id (int id) + :result []}}) + (send-to-bridge cofx + {:type constants/web3-send-async-callback + :messageId message-id + :error {:code 4100}}))) + +(def permissioned-method + #{"eth_accounts" "eth_coinbase" "eth_sendTransaction" "eth_sign" + "keycard_signTypedData" + "eth_signTypedData" "personal_sign" "personal_ecRecover"}) + +(defn has-permissions? [{:dapps/keys [permissions]} dapp-name method] + (boolean + (and (permissioned-method method) + (not (some #{constants/dapp-permission-web3} (get-in permissions [dapp-name :permissions])))))) + (fx/defn web3-send-async-read-only [{:keys [db] :as cofx} dapp-name {:keys [method] :as payload} message-id] - (let [{:dapps/keys [permissions]} db] - (if (and (#{"eth_accounts" "eth_coinbase" "eth_sendTransaction" "eth_sign" - "keycard_signTypedData" - "eth_signTypedData" "personal_sign" "personal_ecRecover"} method) - (not (some #{constants/dapp-permission-web3} (get-in permissions [dapp-name :permissions])))) - (send-to-bridge cofx - {:type constants/web3-send-async-callback - :messageId message-id - :error {:code 4100}}) - (web3-send-async cofx payload message-id)))) + (if (has-permissions? db dapp-name method) + (handle-no-permissions cofx payload message-id) + (web3-send-async cofx payload message-id))) (fx/defn handle-scanned-qr-code [cofx data {:keys [dapp-name permission message-id]}]