From 0bf95891c9f75e90d559b6c849d758017b353b09 Mon Sep 17 00:00:00 2001 From: Esteban Mino Date: Mon, 9 Sep 2019 15:20:33 -0300 Subject: [PATCH] add enable to constructor --- app/core/InpageBridge.js | 77 ++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/app/core/InpageBridge.js b/app/core/InpageBridge.js index 59e9863ba20..a6bb87330a6 100644 --- a/app/core/InpageBridge.js +++ b/app/core/InpageBridge.js @@ -148,6 +148,44 @@ class InpageBridge { this._network = undefined; // INITIAL_NETWORK this._selectedAddress = undefined; // INITIAL_SELECTED_ADDRESS this._subscribe(); + + /** + * Called by dapps to request access to user accounts + * + * @param {object} params - Configuration object for account access + * @returns {Promise>} - Promise resolving to array of user accounts + */ + this.enable = params => + new Promise((resolve, reject) => { + // Temporary fix for peepeth calling + // ethereum.enable with the wrong context + const self = this || window.ethereum; + try { + self.sendAsync({ method: 'eth_requestAccounts', params }, (error, result) => { + if (error) { + reject(error); + return; + } + resolve(result); + }); + } catch (e) { + if (e.toString().indexOf('Bridge not ready') !== -1) { + // Wait 1s and retry + + setTimeout(() => { + self.sendAsync({ method: 'eth_requestAccounts', params }, (error, result) => { + if (error) { + reject(error); + return; + } + resolve(result); + }); + }, 1000); + } else { + throw e; + } + } + }); } /** @@ -212,45 +250,6 @@ class InpageBridge { }); } - /** - * Called by dapps to request access to user accounts - * - * @param {object} params - Configuration object for account access - * @returns {Promise>} - Promise resolving to array of user accounts - */ - enable(params) { - return new Promise((resolve, reject) => { - // Temporary fix for peepeth calling - // ethereum.enable with the wrong context - const self = this || window.ethereum; - try { - self.sendAsync({ method: 'eth_requestAccounts', params }, (error, result) => { - if (error) { - reject(error); - return; - } - resolve(result); - }); - } catch (e) { - if (e.toString().indexOf('Bridge not ready') !== -1) { - // Wait 1s and retry - - setTimeout(() => { - self.sendAsync({ method: 'eth_requestAccounts', params }, (error, result) => { - if (error) { - reject(error); - return; - } - resolve(result); - }); - }, 1000); - } else { - throw e; - } - } - }); - } - /** * Called by dapps to use the QR scanner *