1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00

Merge pull request #182 from MetaMask/EasilyConfigurableProviders

Easily configurable providers
This commit is contained in:
Dan Finlay 2016-05-10 15:44:47 -07:00
commit 30831be137
7 changed files with 84 additions and 14 deletions

View File

@ -2,6 +2,9 @@
## Current Master ## Current Master
- Initial usage of scalable blockchain backend.
- Made official providers more easily configurable for us internally.
## 1.8.0 2016-05-10 ## 1.8.0 2016-05-10
- Add support for calls to `eth.sign`. - Add support for calls to `eth.sign`.

View File

@ -168,6 +168,7 @@ function setupControllerConnection(stream){
var dnode = Dnode({ var dnode = Dnode({
getState: function(cb){ cb(null, getState()) }, getState: function(cb){ cb(null, getState()) },
setRpcTarget: setRpcTarget, setRpcTarget: setRpcTarget,
setProviderType: setProviderType,
useEtherscanProvider: useEtherscanProvider, useEtherscanProvider: useEtherscanProvider,
// forward directly to idStore // forward directly to idStore
createNewVault: idStore.createNewVault.bind(idStore), createNewVault: idStore.createNewVault.bind(idStore),
@ -255,6 +256,12 @@ function setRpcTarget(rpcTarget){
idStore.getNetwork(3) // 3 retry attempts idStore.getNetwork(3) // 3 retry attempts
} }
function setProviderType(type) {
configManager.setProviderType(type)
chrome.runtime.reload()
idStore.getNetwork(3)
}
function useEtherscanProvider() { function useEtherscanProvider() {
configManager.useEtherscanProvider() configManager.useEtherscanProvider()
chrome.runtime.reload() chrome.runtime.reload()

View File

@ -2,7 +2,8 @@ const Migrator = require('pojo-migrator')
const extend = require('xtend') const extend = require('xtend')
const STORAGE_KEY = 'metamask-config' 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') const migrations = require('./migrations')
@ -59,8 +60,7 @@ ConfigManager.prototype.getConfig = function() {
} else { } else {
return { return {
provider: { provider: {
type: 'rpc', type: 'testnet',
rpcTarget: DEFAULT_RPC,
} }
} }
} }
@ -75,6 +75,14 @@ ConfigManager.prototype.setRpcTarget = function(rpcUrl) {
this.setConfig(config) this.setConfig(config)
} }
ConfigManager.prototype.setProviderType = function(type) {
var config = this.getConfig()
config.provider = {
type: type,
}
this.setConfig(config)
}
ConfigManager.prototype.useEtherscanProvider = function() { ConfigManager.prototype.useEtherscanProvider = function() {
var config = this.getConfig() var config = this.getConfig()
config.provider = { config.provider = {
@ -130,9 +138,19 @@ ConfigManager.prototype.getShouldShowSeedWords = function() {
} }
ConfigManager.prototype.getCurrentRpcAddress = function() { ConfigManager.prototype.getCurrentRpcAddress = function() {
var config = this.getConfig() var provider = this.getProvider()
if (!config) return null if (!provider) return null
return config.provider && config.provider.rpcTarget ? config.provider.rpcTarget : DEFAULT_RPC 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() { ConfigManager.prototype.clearWallet = function() {
@ -246,7 +264,9 @@ function loadData() {
}, },
data: { data: {
config: { config: {
rpcTarget: DEFAULT_RPC, provider: {
type: 'testnet',
}
} }
} }
}, oldData ? oldData : null, newData ? newData : null) }, oldData ? oldData : null, newData ? newData : null)

View File

@ -36,7 +36,8 @@ describe ('config view actions', function() {
} }
var result = reducers(initialState, action) var result = reducers(initialState, action)
assert.equal(result.metamask.rpcTarget, action.value) assert.equal(result.metamask.provider.type, 'rpc')
assert.equal(result.metamask.provider.rpcTarget, action.value)
}) })
}) })
}) })

View File

@ -77,10 +77,12 @@ var actions = {
// config screen // config screen
SHOW_CONFIG_PAGE: 'SHOW_CONFIG_PAGE', SHOW_CONFIG_PAGE: 'SHOW_CONFIG_PAGE',
SET_RPC_TARGET: 'SET_RPC_TARGET', SET_RPC_TARGET: 'SET_RPC_TARGET',
SET_PROVIDER_TYPE: 'SET_PROVIDER_TYPE',
USE_ETHERSCAN_PROVIDER: 'USE_ETHERSCAN_PROVIDER', USE_ETHERSCAN_PROVIDER: 'USE_ETHERSCAN_PROVIDER',
useEtherscanProvider: useEtherscanProvider, useEtherscanProvider: useEtherscanProvider,
showConfigPage: showConfigPage, showConfigPage: showConfigPage,
setRpcTarget: setRpcTarget, setRpcTarget: setRpcTarget,
setProviderType: setProviderType,
// hacky - need a way to get a reference to account manager // hacky - need a way to get a reference to account manager
_setAccountManager: _setAccountManager, _setAccountManager: _setAccountManager,
// loading overlay // 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() { function useEtherscanProvider() {
_accountManager.useEtherscanProvider() _accountManager.useEtherscanProvider()
return { return {

View File

@ -84,7 +84,7 @@ ConfigScreen.prototype.render = function() {
}, },
onClick(event) { onClick(event) {
event.preventDefault() event.preventDefault()
state.dispatch(actions.setRpcTarget('https://rpc.metamask.io/')) state.dispatch(actions.setProviderType('mainnet'))
} }
}, 'Use Main Network') }, 'Use Main Network')
]), ]),
@ -96,7 +96,7 @@ ConfigScreen.prototype.render = function() {
}, },
onClick(event) { onClick(event) {
event.preventDefault() event.preventDefault()
state.dispatch(actions.setRpcTarget('https://testrpc.metamask.io/')) state.dispatch(actions.setProviderType('testnet'))
} }
}, 'Use Morden Test Network') }, 'Use Morden Test Network')
]), ]),
@ -120,9 +120,28 @@ ConfigScreen.prototype.render = function() {
} }
function currentProviderDisplay(metamaskState) { 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', [ return h('div', [
h('span', {style: { fontWeight: 'bold', paddingRight: '10px'}}, 'Current RPC'), h('span', {style: { fontWeight: 'bold', paddingRight: '10px'}}, title),
h('span', rpc) h('span', value)
]) ])
} }

View File

@ -38,7 +38,17 @@ function reduceMetamask(state, action) {
case actions.SET_RPC_TARGET: case actions.SET_RPC_TARGET:
return extend(metamaskState, { 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: case actions.COMPLETED_TX: