Support new Metamask/Web3 privacy mode #97

Merged
raucao merged 1 commits from feature/87-metamask_privacy_mode into master 2019-04-04 23:24:03 +00:00
+37 -14
View File
@@ -33,20 +33,10 @@ export default Service.extend({
// this is called in the routes beforeModel(). So it is initialized before everything else // this is called in the routes beforeModel(). So it is initialized before everything else
// and we can rely on the ethProvider and the potential currentUserAccounts to be available // and we can rely on the ethProvider and the potential currentUserAccounts to be available
getEthProvider: function() { getEthProvider: function() {
return new RSVP.Promise((resolve) => { let ethProvider;
let ethProvider;
if (typeof window.web3 !== 'undefined') { return new RSVP.Promise(async (resolve) => {
console.debug('[kredits] Using user-provided instance, e.g. from Mist browser or Metamask'); function instantiateWithoutAccount () {
ethProvider = new ethers.providers.Web3Provider(window.web3.currentProvider);
ethProvider.listAccounts().then((accounts) => {
this.set('currentUserAccounts', accounts);
const ethSigner = accounts.length === 0 ? null : ethProvider.getSigner();
resolve({
ethProvider,
ethSigner
});
});
} else {
console.debug('[kredits] Creating new instance from npm module class'); console.debug('[kredits] Creating new instance from npm module class');
console.debug(`[kredits] providerURL: ${config.web3ProviderUrl}`); console.debug(`[kredits] providerURL: ${config.web3ProviderUrl}`);
ethProvider = new ethers.providers.JsonRpcProvider(config.web3ProviderUrl); ethProvider = new ethers.providers.JsonRpcProvider(config.web3ProviderUrl);
@@ -55,6 +45,39 @@ export default Service.extend({
ethSigner: null ethSigner: null
}); });
} }
function instantiateWithAccount (web3, context) {
console.debug('[kredits] Using user-provided instance, e.g. from Mist browser or Metamask');
ethProvider = new ethers.providers.Web3Provider(web3.currentProvider);
ethProvider.listAccounts().then(accounts => {
context.set('currentUserAccounts', accounts);
bumi commented 2019-04-04 10:41:01 +00:00 (Migrated from github.com)
Review

do those functions need to be in that RSVP.Promise ?
or could those be general functions that return a Promise that resolves the ethProvider?

something like:

getEthProvider: function() {
  if (window.ethereum) {
    return window.ethereum.enable().then(() => {
      return instantiateWithAccount(...)
    })
  }
}
do those functions need to be in that RSVP.Promise ? or could those be general functions that return a Promise that resolves the ethProvider? something like: ```js getEthProvider: function() { if (window.ethereum) { return window.ethereum.enable().then(() => { return instantiateWithAccount(...) }) } } ```
raucao commented 2019-04-04 10:43:41 +00:00 (Migrated from github.com)
Review

It's easier this way. Anyone wanting to refactor it more can do so in the future. PRs welcome.

It's easier this way. Anyone wanting to refactor it more can do so in the future. PRs welcome.
const ethSigner = accounts.length === 0 ? null : ethProvider.getSigner();
resolve({
ethProvider,
ethSigner
});
});
}
if (window.ethereum) {
window.web3 = new window.Web3(window.ethereum);
try {
// Request account access if needed
await window.ethereum.enable();
// Acccounts now exposed
instantiateWithAccount(window.web3, this);
} catch (error) {
instantiateWithoutAccount();
}
}
// Legacy dapp browsers...
else if (window.web3) {
instantiateWithAccount(window.web3, this);
}
// Non-dapp browsers...
else {
instantiateWithoutAccount();
}
}); });
}, },