1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 21:00:13 +01:00
metamask-extension/app/scripts/controllers/network.js

131 lines
3.9 KiB
JavaScript
Raw Normal View History

const EventEmitter = require('events')
const MetaMaskProvider = require('web3-provider-engine/zero.js')
const ObservableStore = require('obs-store')
2017-05-23 08:12:28 +02:00
const ComposedStore = require('obs-store/lib/composed')
const extend = require('xtend')
const EthQuery = require('eth-query')
2017-05-23 07:56:10 +02:00
const RPC_ADDRESS_LIST = require('../config.js').network
2017-05-23 08:12:28 +02:00
const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby']
module.exports = class NetworkController extends EventEmitter {
2017-05-23 08:12:28 +02:00
constructor (config) {
super()
2017-05-23 08:12:28 +02:00
this.networkStore = new ObservableStore('loading')
config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider)
this.providerStore = new ObservableStore(config.provider)
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore })
this._providerListeners = {}
2017-05-23 07:56:10 +02:00
2017-05-23 08:12:28 +02:00
this.on('networkDidChange', this.lookupNetwork)
this.providerStore.subscribe((state) => this.switchNetwork({rpcUrl: state.rpcTarget}))
2017-05-23 07:56:10 +02:00
}
get provider () {
return this._proxy
}
set provider (provider) {
this._provider = provider
}
initializeProvider (opts, providerContructor = MetaMaskProvider) {
2017-05-23 08:12:28 +02:00
this.providerInit = opts
this._provider = providerContructor(opts)
2017-05-23 07:56:10 +02:00
this._proxy = new Proxy(this._provider, {
get: (obj, name) => {
if (name === 'on') return this._on.bind(this)
return this._provider[name]
},
set: (obj, name, value) => {
this._provider[name] = value
return value
2017-05-23 07:56:10 +02:00
},
})
this.provider.on('block', this._logBlock.bind(this))
this.provider.on('error', this.verifyNetwork.bind(this))
this.ethQuery = new EthQuery(this.provider)
this.lookupNetwork()
2017-05-23 07:56:10 +02:00
return this.provider
}
2017-05-23 07:56:10 +02:00
2017-05-23 08:12:28 +02:00
switchNetwork (providerInit) {
this.setNetworkState('loading')
const newInit = extend(this.providerInit, providerInit)
this.providerInit = newInit
2017-05-23 07:56:10 +02:00
2017-05-23 08:12:28 +02:00
this._provider.removeAllListeners()
2017-05-24 17:52:18 +02:00
this._provider.stop()
2017-05-23 08:12:28 +02:00
this.provider = MetaMaskProvider(newInit)
2017-05-23 07:56:10 +02:00
// apply the listners created by other controllers
2017-05-23 08:12:28 +02:00
Object.keys(this._providerListeners).forEach((key) => {
this._providerListeners[key].forEach((handler) => this._provider.addListener(key, handler))
2017-05-23 07:56:10 +02:00
})
2017-05-23 08:12:28 +02:00
this.emit('networkDidChange')
}
verifyNetwork () {
// Check network when restoring connectivity:
if (this.isNetworkLoading()) this.lookupNetwork()
}
getNetworkState () {
2017-05-23 08:12:28 +02:00
return this.networkStore.getState()
}
setNetworkState (network) {
2017-05-23 08:12:28 +02:00
return this.networkStore.putState(network)
}
isNetworkLoading () {
return this.getNetworkState() === 'loading'
}
2017-05-23 08:12:28 +02:00
lookupNetwork () {
this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => {
if (err) return this.setNetworkState('loading')
log.info('web3.getNetwork returned ' + network)
this.setNetworkState(network)
})
}
setRpcTarget (rpcUrl) {
this.providerStore.updateState({
2017-05-23 08:12:28 +02:00
type: 'rpc',
rpcTarget: rpcUrl,
})
}
getCurrentRpcAddress () {
2017-05-23 08:12:28 +02:00
const provider = this.getProviderConfig()
if (!provider) return null
return this.getRpcAddressForType(provider.type)
}
setProviderType (type) {
2017-05-23 08:12:28 +02:00
if (type === this.getProviderConfig().type) return
const rpcTarget = this.getRpcAddressForType(type)
2017-05-23 08:12:28 +02:00
this.providerStore.updateState({type, rpcTarget})
}
2017-05-23 08:12:28 +02:00
getProviderConfig () {
return this.providerStore.getState()
}
2017-05-23 08:12:28 +02:00
getRpcAddressForType (type, provider = this.getProviderConfig()) {
if (RPC_ADDRESS_LIST[type]) return RPC_ADDRESS_LIST[type]
return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC
2017-05-23 07:56:10 +02:00
}
2017-05-23 07:56:10 +02:00
_logBlock (block) {
log.info(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`)
this.verifyNetwork()
}
2017-05-23 07:56:10 +02:00
_on (event, handler) {
2017-05-23 08:12:28 +02:00
if (!this._providerListeners[event]) this._providerListeners[event] = []
this._providerListeners[event].push(handler)
2017-05-23 07:56:10 +02:00
this._provider.on(event, handler)
}
}