{"size": 2182, "ext": "js", "max_stars_repo_path": "src/eth/accounts/setup.js", "max_stars_repo_name": "konstantinzolotarev/dai.js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/eth/accounts/setup.js", "max_issues_repo_name": "konstantinzolotarev/dai.js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/eth/accounts/setup.js", "max_forks_repo_name": "konstantinzolotarev/dai.js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.8904109589, "max_line_length": 86, "alphanum_fraction": 0.6727772686}
import ProviderType from '../web3/ProviderType';
import Web3ProviderEngine from 'web3-provider-engine/dist/es5';
import RpcSource from 'web3-provider-engine/dist/es5/subproviders/rpc';
import ProviderSubprovider from 'web3-provider-engine/dist/es5/subproviders/provider';

export async function setupEngine(settings) {
  const { provider: providerSettings } = settings.web3;
  const engine = new Web3ProviderEngine();
  const result = { engine };

  if (providerSettings.type === ProviderType.BROWSER || !providerSettings) {
    result.provider = await getBrowserProvider();
  } else {
    const rpcUrl = getRpcUrl(providerSettings);
    result.provider = new RpcSource({ rpcUrl });
  }

  engine.addProvider(result.provider);
  return result;
}

export async function getBrowserProvider() {
  if (typeof window === 'undefined') {
    throw new Error(
      'Cannot use ProviderType.BROWSER because window is undefined'
    );
  }

  const wrap = provider => {
    const subprovider = new ProviderSubprovider(provider);
    subprovider.isWindowProvider = true;
    return subprovider;
  };

  // If web3 is injected (old MetaMask)...
  if (typeof window.web3 !== 'undefined') {
    return wrap(window.web3.currentProvider);
  }

  // If web3 is not injected (new MetaMask)...
  return new Promise((resolve, reject) => {
    let resolved = false;

    window.addEventListener('message', ({ data }) => {
      if (data && data.type && data.type === 'ETHEREUM_PROVIDER_SUCCESS') {
        resolved = true;
        resolve(wrap(window.ethereum));
      }
    });

    // Request provider
    window.postMessage({ type: 'ETHEREUM_PROVIDER_REQUEST' }, '*');

    setTimeout(() => {
      if (!resolved) reject(new Error('Timed out waiting for provider'));
    }, 30000);
  });
}

function getRpcUrl(providerSettings) {
  const { network, infuraApiKey, type, url } = providerSettings;
  switch (type) {
    case ProviderType.HTTP:
      return url;
    case ProviderType.INFURA:
      return `https://${network}.infura.io/${infuraApiKey || ''}`;
    case ProviderType.TEST:
      return 'http://127.1:2000';
    default:
      throw new Error('Invalid web3 provider type: ' + type);
  }
}
