diff --git a/app/scripts/config.js b/app/scripts/config.js new file mode 100644 index 000000000..f26e6778d --- /dev/null +++ b/app/scripts/config.js @@ -0,0 +1,12 @@ +const MAINET_RPC_URL = 'https://mainnet.infura.io/' +const TESTNET_RPC_URL = 'https://morden.infura.io/' +const DEFAULT_RPC_URL = TESTNET_RPC_URL + +module.exports = { + network: { + default: DEFAULT_RPC_URL, + mainnet: MAINET_RPC_URL, + testnet: TESTNET_RPC_URL, + }, +} + diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 33e2c9358..91d782a32 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -4,6 +4,7 @@ const StreamProvider = require('./lib/stream-provider.js') const LocalMessageDuplexStream = require('./lib/local-message-stream.js') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const RemoteStore = require('./lib/remote-store.js').RemoteStore +const MetamaskConfig = require('./config.js') const Web3 = require('web3') const once = require('once') restoreContextAfterImports() @@ -12,8 +13,6 @@ restoreContextAfterImports() delete window.Web3 window.MetamaskWeb3 = Web3 -const DEFAULT_RPC_URL = 'https://rpc.metamask.io/' - // // setup plugin communication @@ -93,15 +92,33 @@ publicConfigStore.subscribe(function(state){ }) // setup sync http provider -var providerConfig = publicConfigStore.get('provider') || {} -var providerUrl = providerConfig.rpcTarget ? providerConfig.rpcTarget : DEFAULT_RPC_URL -var syncProvider = new Web3.providers.HttpProvider(providerUrl) -publicConfigStore.subscribe(function(state){ - if (!state.provider) return - if (!state.provider.rpcTarget || state.provider.rpcTarget === providerUrl) return - providerUrl = state.provider.rpcTarget - syncProvider = new Web3.providers.HttpProvider(providerUrl) -}) +updateProvider({ provider: publicConfigStore.get('provider') }) +publicConfigStore.subscribe(updateProvider) + +var syncProvider = null +var syncProviderUrl = null + +function updateProvider(state){ + var providerConfig = state.provider || {} + var newSyncProviderUrl = undefined + + if (providerConfig.rpcTarget) { + newSyncProviderUrl = providerConfig.rpcTarget + } else { + switch(providerConfig.type) { + case 'testnet': + newSyncProviderUrl = MetamaskConfig.network.testnet + break + case 'mainnet': + newSyncProviderUrl = MetamaskConfig.network.mainnet + break + default: + newSyncProviderUrl = MetamaskConfig.network.default + } + } + if (newSyncProviderUrl === syncProviderUrl) return + syncProvider = new Web3.providers.HttpProvider(newSyncProviderUrl) +} // handle sync methods remoteProvider.send = function(payload){ diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index 3c9326db9..847d85a20 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -1,11 +1,12 @@ const Migrator = require('pojo-migrator') const extend = require('xtend') +const MetamaskConfig = require('./config.js') +const migrations = require('./migrations') const STORAGE_KEY = 'metamask-config' -const TESTNET_RPC = 'https://morden.infura.io' -const MAINNET_RPC = 'https://mainnet.infura.io/' +const TESTNET_RPC = MetamaskConfig.network.testnet +const MAINNET_RPC = MetamaskConfig.network.mainnet -const migrations = require('./migrations') /* The config-manager is a convenience object * wrapping a pojo-migrator.