From b8f93ea2ad78f1affc2d1159c992fdfa700f0b42 Mon Sep 17 00:00:00 2001 From: Marc Date: Wed, 22 Jan 2020 20:11:37 +0100 Subject: [PATCH] fix(@embark/embarkjs): change enableEthereum to not rely on returned accounts array Some ethereum providers (e.g. Opera implementation) do not return the accounts array in the 'enable' call. --- .../embarkjs/embarkjs/src/lib/blockchain.js | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/embarkjs/embarkjs/src/lib/blockchain.js b/packages/embarkjs/embarkjs/src/lib/blockchain.js index b35a6913d..a34202c95 100644 --- a/packages/embarkjs/embarkjs/src/lib/blockchain.js +++ b/packages/embarkjs/embarkjs/src/lib/blockchain.js @@ -208,17 +208,23 @@ Blockchain.doConnect = function(connectionList, opts, doneCb) { }); }; -Blockchain.enableEthereum = function() { - if (typeof window !== 'undefined' && window.ethereum) { - return ethereum.enable().then((accounts) => { - this.blockchainConnector.setProvider(ethereum); - this.blockchainConnector.setDefaultAccount(accounts[0]); - contracts.forEach(contract => { - contract.options.from = this.blockchainConnector.getDefaultAccount(); - }); - return accounts; - }); +// See https://eips.ethereum.org/EIPS/eip-1102 +Blockchain.enableEthereum = async function() { + if (typeof window === 'undefined' || !window.ethereum) { + throw new Error('ethereum not available in this browser'); } + + await ethereum.enable(); + this.blockchainConnector.setProvider(ethereum); + + const accounts = await this.blockchainConnector.getAccounts(); + this.blockchainConnector.setDefaultAccount(accounts[0]); + + contracts.forEach(contract => { + contract.options.from = this.blockchainConnector.getDefaultAccount(); + }); + + return accounts; }; Blockchain.execWhenReady = function(cb) {