diff options
author | hackyminer <hackyminer@gmail.com> | 2018-10-26 19:37:35 +0800 |
---|---|---|
committer | hackyminer <hackyminer@gmail.com> | 2018-10-26 19:37:35 +0800 |
commit | bc7f8d0a5bf42b73ee1d1102362fc0a94dc80c0d (patch) | |
tree | 96fc1c969b49c66081ff1a042a6e2e3e787723c7 /app/scripts/controllers/network | |
parent | b62d07f3a5bdfde7e295f17b7f601c6f2d5314ef (diff) | |
parent | 54a8ade2669cb5f8f046509873bc2a9c25425847 (diff) | |
download | tangerine-wallet-browser-bc7f8d0a5bf42b73ee1d1102362fc0a94dc80c0d.tar tangerine-wallet-browser-bc7f8d0a5bf42b73ee1d1102362fc0a94dc80c0d.tar.gz tangerine-wallet-browser-bc7f8d0a5bf42b73ee1d1102362fc0a94dc80c0d.tar.bz2 tangerine-wallet-browser-bc7f8d0a5bf42b73ee1d1102362fc0a94dc80c0d.tar.lz tangerine-wallet-browser-bc7f8d0a5bf42b73ee1d1102362fc0a94dc80c0d.tar.xz tangerine-wallet-browser-bc7f8d0a5bf42b73ee1d1102362fc0a94dc80c0d.tar.zst tangerine-wallet-browser-bc7f8d0a5bf42b73ee1d1102362fc0a94dc80c0d.zip |
Merge branch 'develop' into eth_chainid
Diffstat (limited to 'app/scripts/controllers/network')
-rw-r--r-- | app/scripts/controllers/network/network.js | 62 |
1 files changed, 52 insertions, 10 deletions
diff --git a/app/scripts/controllers/network/network.js b/app/scripts/controllers/network/network.js index 6a5369f06..c21e9c764 100644 --- a/app/scripts/controllers/network/network.js +++ b/app/scripts/controllers/network/network.js @@ -11,6 +11,8 @@ const createInfuraClient = require('./createInfuraClient') const createJsonRpcClient = require('./createJsonRpcClient') const createLocalhostClient = require('./createLocalhostClient') const { createSwappableProxy, createEventEmitterProxy } = require('swappable-obj-proxy') +const extend = require('extend') +const networks = { networkList: {} } const { ROPSTEN, @@ -29,6 +31,10 @@ const defaultProviderConfig = { type: testMode ? RINKEBY : MAINNET, } +const defaultNetworkConfig = { + ticker: 'ETH', +} + module.exports = class NetworkController extends EventEmitter { constructor (opts = {}) { @@ -39,7 +45,8 @@ module.exports = class NetworkController extends EventEmitter { // create stores this.providerStore = new ObservableStore(providerConfig) this.networkStore = new ObservableStore('loading') - this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore }) + this.networkConfig = new ObservableStore(defaultNetworkConfig) + this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore, settings: this.networkConfig }) this.on('networkDidChange', this.lookupNetwork) // provider and block tracker this._provider = null @@ -51,8 +58,8 @@ module.exports = class NetworkController extends EventEmitter { initializeProvider (providerParams) { this._baseProviderParams = providerParams - const { type, rpcTarget } = this.providerStore.getState() - this._configureProvider({ type, rpcTarget }) + const { type, rpcTarget, chainId, ticker, nickname } = this.providerStore.getState() + this._configureProvider({ type, rpcTarget, chainId, ticker, nickname }) this.lookupNetwork() } @@ -72,7 +79,20 @@ module.exports = class NetworkController extends EventEmitter { return this.networkStore.getState() } - setNetworkState (network) { + getNetworkConfig () { + return this.networkConfig.getState() + } + + setNetworkState (network, type) { + if (network === 'loading') { + return this.networkStore.putState(network) + } + + // type must be defined + if (!type) { + return + } + network = networks.networkList[type] && networks.networkList[type].chainId ? networks.networkList[type].chainId : network return this.networkStore.putState(network) } @@ -85,6 +105,7 @@ module.exports = class NetworkController extends EventEmitter { if (!this._provider) { return log.warn('NetworkController - lookupNetwork aborted due to missing provider') } + var { type } = this.providerStore.getState() const ethQuery = new EthQuery(this._provider) // first attempt to perform lookup via eth_chainId ethQuery.sendAsync({ method: 'eth_chainId' }, (err, chainIdHex) => { @@ -93,20 +114,23 @@ module.exports = class NetworkController extends EventEmitter { ethQuery.sendAsync({ method: 'net_version' }, (err, network) => { if (err) return this.setNetworkState('loading') log.info(`net_version returned ${network}`) - this.setNetworkState(network) + this.setNetworkState(network, type) }) return } const chainId = Number.parseInt(chainIdHex, 16) log.info(`net_version returned ${chainId}`) - this.setNetworkState(chainId) + this.setNetworkState(chainId, type) }) } - setRpcTarget (rpcTarget) { + setRpcTarget (rpcTarget, chainId, ticker = 'ETH', nickname = '') { const providerConfig = { type: 'rpc', rpcTarget, + chainId, + ticker, + nickname, } this.providerConfig = providerConfig } @@ -142,7 +166,7 @@ module.exports = class NetworkController extends EventEmitter { } _configureProvider (opts) { - const { type, rpcTarget } = opts + const { type, rpcTarget, chainId, ticker, nickname } = opts // infura type-based endpoints const isInfura = INFURA_PROVIDER_TYPES.includes(type) if (isInfura) { @@ -152,7 +176,7 @@ module.exports = class NetworkController extends EventEmitter { this._configureLocalhostProvider() // url-based rpc endpoints } else if (type === 'rpc') { - this._configureStandardProvider({ rpcUrl: rpcTarget }) + this._configureStandardProvider({ rpcUrl: rpcTarget, chainId, ticker, nickname }) } else { throw new Error(`NetworkController - _configureProvider - unknown type "${type}"`) } @@ -162,6 +186,11 @@ module.exports = class NetworkController extends EventEmitter { log.info('NetworkController - configureInfuraProvider', type) const networkClient = createInfuraClient({ network: type }) this._setNetworkClient(networkClient) + // setup networkConfig + var settings = { + ticker: 'ETH', + } + this.networkConfig.putState(settings) } _configureLocalhostProvider () { @@ -170,9 +199,22 @@ module.exports = class NetworkController extends EventEmitter { this._setNetworkClient(networkClient) } - _configureStandardProvider ({ rpcUrl }) { + _configureStandardProvider ({ rpcUrl, chainId, ticker, nickname }) { log.info('NetworkController - configureStandardProvider', rpcUrl) const networkClient = createJsonRpcClient({ rpcUrl }) + // hack to add a 'rpc' network with chainId + networks.networkList['rpc'] = { + chainId: chainId, + rpcUrl, + ticker: ticker || 'ETH', + nickname, + } + // setup networkConfig + var settings = { + network: chainId, + } + settings = extend(settings, networks.networkList['rpc']) + this.networkConfig.putState(settings) this._setNetworkClient(networkClient) } |