Skip to content

Commit b8f93ea

Browse files
mfornosmichaelsbradleyjr
authored andcommitted
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.
1 parent df2aaab commit b8f93ea

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

packages/embarkjs/embarkjs/src/lib/blockchain.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,23 @@ Blockchain.doConnect = function(connectionList, opts, doneCb) {
208208
});
209209
};
210210

211-
Blockchain.enableEthereum = function() {
212-
if (typeof window !== 'undefined' && window.ethereum) {
213-
return ethereum.enable().then((accounts) => {
214-
this.blockchainConnector.setProvider(ethereum);
215-
this.blockchainConnector.setDefaultAccount(accounts[0]);
216-
contracts.forEach(contract => {
217-
contract.options.from = this.blockchainConnector.getDefaultAccount();
218-
});
219-
return accounts;
220-
});
211+
// See https://eips.ethereum.org/EIPS/eip-1102
212+
Blockchain.enableEthereum = async function() {
213+
if (typeof window === 'undefined' || !window.ethereum) {
214+
throw new Error('ethereum not available in this browser');
221215
}
216+
217+
await ethereum.enable();
218+
this.blockchainConnector.setProvider(ethereum);
219+
220+
const accounts = await this.blockchainConnector.getAccounts();
221+
this.blockchainConnector.setDefaultAccount(accounts[0]);
222+
223+
contracts.forEach(contract => {
224+
contract.options.from = this.blockchainConnector.getDefaultAccount();
225+
});
226+
227+
return accounts;
222228
};
223229

224230
Blockchain.execWhenReady = function(cb) {

0 commit comments

Comments
 (0)