From d8bee4f5992c5a1d0851e87c49e13c97d9279411 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 10 May 2016 15:37:13 -0700 Subject: [PATCH] Make default providers more easiliy configurable for metamask devs No longer do our `mainnet` and `testnet` buttons set specific RPC urls. Now they set `provider.type`, which gets interpreted with code. Currently the provider types of `mainnet` and `testnet` point to our new scalable backends, but these could be re-interpreted to use any other provider, be it etherscan, peer to peer, or otherwise. Makes it easier for us to upgrade our infrastructure without incorporating migration logic into the program. --- CHANGELOG.md | 3 +++ app/scripts/background.js | 7 +++++++ app/scripts/lib/config-manager.js | 34 ++++++++++++++++++++++++------- ui/app/actions.js | 10 +++++++++ ui/app/config.js | 29 +++++++++++++++++++++----- ui/app/reducers/metamask.js | 12 ++++++++++- 6 files changed, 82 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e03bec32d..bf8a3c6ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Current Master +- Initial usage of scalable blockchain backend. +- Made official providers more easily configurable for us internally. + ## 1.8.0 2016-05-10 - Add support for calls to `eth.sign`. diff --git a/app/scripts/background.js b/app/scripts/background.js index 0f9ecc1c9..a52eab2d3 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -168,6 +168,7 @@ function setupControllerConnection(stream){ var dnode = Dnode({ getState: function(cb){ cb(null, getState()) }, setRpcTarget: setRpcTarget, + setProviderType: setProviderType, useEtherscanProvider: useEtherscanProvider, // forward directly to idStore createNewVault: idStore.createNewVault.bind(idStore), @@ -255,6 +256,12 @@ function setRpcTarget(rpcTarget){ idStore.getNetwork(3) // 3 retry attempts } +function setProviderType(type) { + configManager.setProviderType(type) + chrome.runtime.reload() + idStore.getNetwork(3) +} + function useEtherscanProvider() { configManager.useEtherscanProvider() chrome.runtime.reload() diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index 5bfb8befe..fd48432b5 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -2,7 +2,8 @@ const Migrator = require('pojo-migrator') const extend = require('xtend') const STORAGE_KEY = 'metamask-config' -const DEFAULT_RPC = 'https://testrpc.metamask.io/' +const TESTNET_RPC = 'http://morden.infura.io' +const MAINNET_RPC = 'http://mainnet.infura.io/' const migrations = require('./migrations') @@ -59,8 +60,7 @@ ConfigManager.prototype.getConfig = function() { } else { return { provider: { - type: 'rpc', - rpcTarget: DEFAULT_RPC, + type: 'testnet', } } } @@ -75,6 +75,14 @@ ConfigManager.prototype.setRpcTarget = function(rpcUrl) { this.setConfig(config) } +ConfigManager.prototype.setProviderType = function(type) { + var config = this.getConfig() + config.provider = { + type: type, + } + this.setConfig(config) +} + ConfigManager.prototype.useEtherscanProvider = function() { var config = this.getConfig() config.provider = { @@ -130,9 +138,19 @@ ConfigManager.prototype.getShouldShowSeedWords = function() { } ConfigManager.prototype.getCurrentRpcAddress = function() { - var config = this.getConfig() - if (!config) return null - return config.provider && config.provider.rpcTarget ? config.provider.rpcTarget : DEFAULT_RPC + var provider = this.getProvider() + if (!provider) return null + switch (provider.type) { + + case 'mainnet': + return MAINNET_RPC + + case 'testnet': + return TESTNET_RPC + + default: + return provider && provider.rpcTarget ? provider.rpcTarget : TESTNET_RPC + } } ConfigManager.prototype.clearWallet = function() { @@ -246,7 +264,9 @@ function loadData() { }, data: { config: { - rpcTarget: DEFAULT_RPC, + provider: { + type: 'testnet', + } } } }, oldData ? oldData : null, newData ? newData : null) diff --git a/ui/app/actions.js b/ui/app/actions.js index 5c5805c29..dbcf3e577 100644 --- a/ui/app/actions.js +++ b/ui/app/actions.js @@ -77,10 +77,12 @@ var actions = { // config screen SHOW_CONFIG_PAGE: 'SHOW_CONFIG_PAGE', SET_RPC_TARGET: 'SET_RPC_TARGET', + SET_PROVIDER_TYPE: 'SET_PROVIDER_TYPE', USE_ETHERSCAN_PROVIDER: 'USE_ETHERSCAN_PROVIDER', useEtherscanProvider: useEtherscanProvider, showConfigPage: showConfigPage, setRpcTarget: setRpcTarget, + setProviderType: setProviderType, // hacky - need a way to get a reference to account manager _setAccountManager: _setAccountManager, // loading overlay @@ -369,6 +371,14 @@ function setRpcTarget(newRpc) { } } +function setProviderType(type) { + _accountManager.setProviderType(type) + return { + type: this.SET_PROVIDER_TYPE, + value: type, + } +} + function useEtherscanProvider() { _accountManager.useEtherscanProvider() return { diff --git a/ui/app/config.js b/ui/app/config.js index ded065bf8..ddf158325 100644 --- a/ui/app/config.js +++ b/ui/app/config.js @@ -84,7 +84,7 @@ ConfigScreen.prototype.render = function() { }, onClick(event) { event.preventDefault() - state.dispatch(actions.setRpcTarget('https://rpc.metamask.io/')) + state.dispatch(actions.setProviderType('mainnet')) } }, 'Use Main Network') ]), @@ -96,7 +96,7 @@ ConfigScreen.prototype.render = function() { }, onClick(event) { event.preventDefault() - state.dispatch(actions.setRpcTarget('https://testrpc.metamask.io/')) + state.dispatch(actions.setProviderType('testnet')) } }, 'Use Morden Test Network') ]), @@ -120,9 +120,28 @@ ConfigScreen.prototype.render = function() { } function currentProviderDisplay(metamaskState) { - var rpc = metamaskState.provider.rpcTarget + var provider = metamaskState.provider + var title, value + + switch (provider.type) { + + case 'mainnet': + title = 'Current Network' + value = 'Main Ethereum Network' + break + + case 'testnet': + title = 'Current Network' + value = 'Morden Test Network' + break + + default: + title = 'Current RPC' + value = metamaskState.provider.rpcTarget + } + return h('div', [ - h('span', {style: { fontWeight: 'bold', paddingRight: '10px'}}, 'Current RPC'), - h('span', rpc) + h('span', {style: { fontWeight: 'bold', paddingRight: '10px'}}, title), + h('span', value) ]) } diff --git a/ui/app/reducers/metamask.js b/ui/app/reducers/metamask.js index 2fe96c453..9398f1497 100644 --- a/ui/app/reducers/metamask.js +++ b/ui/app/reducers/metamask.js @@ -38,7 +38,17 @@ function reduceMetamask(state, action) { case actions.SET_RPC_TARGET: return extend(metamaskState, { - rpcTarget: action.value, + provider: { + type: 'rpc', + rpcTarget: action.value, + }, + }) + + case actions.SET_PROVIDER_TYPE: + return extend(metamaskState, { + provider: { + type: action.value, + }, }) case actions.COMPLETED_TX: