mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
background - organize metamask instantiation
This commit is contained in:
parent
d30612a216
commit
af115aa4c1
@ -41,17 +41,6 @@ module.exports = class KeyringController extends EventEmitter {
|
|||||||
this.getNetwork = opts.getNetwork
|
this.getNetwork = opts.getNetwork
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set Store
|
|
||||||
//
|
|
||||||
// Allows setting the ethStore after the constructor.
|
|
||||||
// This is currently required because of the initialization order
|
|
||||||
// of the ethStore and this class.
|
|
||||||
//
|
|
||||||
// Eventually would be nice to be able to add this in the constructor.
|
|
||||||
setStore (ethStore) {
|
|
||||||
this.ethStore = ethStore
|
|
||||||
}
|
|
||||||
|
|
||||||
// Full Update
|
// Full Update
|
||||||
// returns Promise( @object state )
|
// returns Promise( @object state )
|
||||||
//
|
//
|
||||||
@ -651,7 +640,7 @@ module.exports = class KeyringController extends EventEmitter {
|
|||||||
clearKeyrings () {
|
clearKeyrings () {
|
||||||
let accounts
|
let accounts
|
||||||
try {
|
try {
|
||||||
accounts = Object.keys(this.ethStore._currentState.accounts)
|
accounts = Object.keys(this.ethStore.getState())
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
accounts = []
|
accounts = []
|
||||||
}
|
}
|
||||||
|
@ -266,7 +266,7 @@ ConfigManager.prototype.setCurrentFiat = function (currency) {
|
|||||||
|
|
||||||
ConfigManager.prototype.getCurrentFiat = function () {
|
ConfigManager.prototype.getCurrentFiat = function () {
|
||||||
var data = this.getData()
|
var data = this.getData()
|
||||||
return data.fiatCurrency
|
return data.fiatCurrency || 'USD'
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigManager.prototype.updateConversionRate = function () {
|
ConfigManager.prototype.updateConversionRate = function () {
|
||||||
|
@ -5,12 +5,12 @@ const pipe = require('pump')
|
|||||||
const ObservableStore = require('obs-store')
|
const ObservableStore = require('obs-store')
|
||||||
const storeTransform = require('obs-store/lib/transform')
|
const storeTransform = require('obs-store/lib/transform')
|
||||||
const EthStore = require('./lib/eth-store')
|
const EthStore = require('./lib/eth-store')
|
||||||
|
const EthQuery = require('eth-query')
|
||||||
const MetaMaskProvider = require('web3-provider-engine/zero.js')
|
const MetaMaskProvider = require('web3-provider-engine/zero.js')
|
||||||
const KeyringController = require('./keyring-controller')
|
const KeyringController = require('./keyring-controller')
|
||||||
const NoticeController = require('./notice-controller')
|
const NoticeController = require('./notice-controller')
|
||||||
const messageManager = require('./lib/message-manager')
|
const messageManager = require('./lib/message-manager')
|
||||||
const TxManager = require('./transaction-manager')
|
const TxManager = require('./transaction-manager')
|
||||||
const Web3 = require('web3')
|
|
||||||
const ConfigManager = require('./lib/config-manager')
|
const ConfigManager = require('./lib/config-manager')
|
||||||
const extension = require('./lib/extension')
|
const extension = require('./lib/extension')
|
||||||
const autoFaucet = require('./lib/auto-faucet')
|
const autoFaucet = require('./lib/auto-faucet')
|
||||||
@ -29,30 +29,33 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
|
|
||||||
// observable state store
|
// observable state store
|
||||||
this.store = new ObservableStore(opts.initState)
|
this.store = new ObservableStore(opts.initState)
|
||||||
|
|
||||||
// config manager
|
// config manager
|
||||||
this.configManager = new ConfigManager({
|
this.configManager = new ConfigManager({
|
||||||
store: this.store,
|
store: this.store,
|
||||||
})
|
})
|
||||||
|
this.configManager.updateConversionRate()
|
||||||
|
|
||||||
|
// rpc provider
|
||||||
|
this.provider = this.initializeProvider(opts)
|
||||||
|
this.provider.on('block', this.processBlock.bind(this))
|
||||||
|
this.provider.on('error', this.getNetwork.bind(this))
|
||||||
|
|
||||||
|
// eth data query tools
|
||||||
|
this.ethQuery = new EthQuery(this.provider)
|
||||||
|
this.ethStore = new EthStore(this.provider)
|
||||||
|
|
||||||
// key mgmt
|
// key mgmt
|
||||||
this.keyringController = new KeyringController({
|
this.keyringController = new KeyringController({
|
||||||
|
ethStore: this.ethStore,
|
||||||
configManager: this.configManager,
|
configManager: this.configManager,
|
||||||
getNetwork: this.getStateNetwork.bind(this),
|
getNetwork: this.getStateNetwork.bind(this),
|
||||||
})
|
})
|
||||||
this.keyringController.on('newAccount', (account) => {
|
this.keyringController.on('newAccount', (account) => {
|
||||||
autoFaucet(account)
|
autoFaucet(account)
|
||||||
})
|
})
|
||||||
// notices
|
|
||||||
this.noticeController = new NoticeController({
|
// tx mgmt
|
||||||
configManager: this.configManager,
|
|
||||||
})
|
|
||||||
this.noticeController.updateNoticesList()
|
|
||||||
// to be uncommented when retrieving notices from a remote server.
|
|
||||||
// this.noticeController.startPolling()
|
|
||||||
this.provider = this.initializeProvider(opts)
|
|
||||||
this.ethStore = new EthStore(this.provider)
|
|
||||||
this.keyringController.setStore(this.ethStore)
|
|
||||||
this.getNetwork()
|
|
||||||
this.messageManager = messageManager
|
|
||||||
this.txManager = new TxManager({
|
this.txManager = new TxManager({
|
||||||
txList: this.configManager.getTxList(),
|
txList: this.configManager.getTxList(),
|
||||||
txHistoryLimit: 40,
|
txHistoryLimit: 40,
|
||||||
@ -64,11 +67,18 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
provider: this.provider,
|
provider: this.provider,
|
||||||
blockTracker: this.provider,
|
blockTracker: this.provider,
|
||||||
})
|
})
|
||||||
this.publicConfigStore = this.initPublicConfigStore()
|
|
||||||
|
// notices
|
||||||
|
this.noticeController = new NoticeController({
|
||||||
|
configManager: this.configManager,
|
||||||
|
})
|
||||||
|
this.noticeController.updateNoticesList()
|
||||||
|
// to be uncommented when retrieving notices from a remote server.
|
||||||
|
// this.noticeController.startPolling()
|
||||||
|
|
||||||
var currentFiat = this.configManager.getCurrentFiat() || 'USD'
|
this.getNetwork()
|
||||||
this.configManager.setCurrentFiat(currentFiat)
|
this.messageManager = messageManager
|
||||||
this.configManager.updateConversionRate()
|
this.publicConfigStore = this.initPublicConfigStore()
|
||||||
|
|
||||||
this.checkTOSChange()
|
this.checkTOSChange()
|
||||||
|
|
||||||
@ -79,6 +89,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
configManager: this.configManager,
|
configManager: this.configManager,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// manual state subscriptions
|
||||||
this.ethStore.on('update', this.sendUpdate.bind(this))
|
this.ethStore.on('update', this.sendUpdate.bind(this))
|
||||||
this.keyringController.on('update', this.sendUpdate.bind(this))
|
this.keyringController.on('update', this.sendUpdate.bind(this))
|
||||||
this.txManager.on('update', this.sendUpdate.bind(this))
|
this.txManager.on('update', this.sendUpdate.bind(this))
|
||||||
@ -221,10 +232,8 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
initializeProvider (opts) {
|
initializeProvider () {
|
||||||
const keyringController = this.keyringController
|
let provider = MetaMaskProvider({
|
||||||
|
|
||||||
var providerOpts = {
|
|
||||||
static: {
|
static: {
|
||||||
eth_syncing: false,
|
eth_syncing: false,
|
||||||
web3_clientVersion: `MetaMask/v${version}`,
|
web3_clientVersion: `MetaMask/v${version}`,
|
||||||
@ -232,8 +241,8 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
rpcUrl: this.configManager.getCurrentRpcAddress(),
|
rpcUrl: this.configManager.getCurrentRpcAddress(),
|
||||||
// account mgmt
|
// account mgmt
|
||||||
getAccounts: (cb) => {
|
getAccounts: (cb) => {
|
||||||
var selectedAccount = this.configManager.getSelectedAccount()
|
let selectedAccount = this.configManager.getSelectedAccount()
|
||||||
var result = selectedAccount ? [selectedAccount] : []
|
let result = selectedAccount ? [selectedAccount] : []
|
||||||
cb(null, result)
|
cb(null, result)
|
||||||
},
|
},
|
||||||
// tx signing
|
// tx signing
|
||||||
@ -241,18 +250,10 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
// msg signing
|
// msg signing
|
||||||
approveMessage: this.newUnsignedMessage.bind(this),
|
approveMessage: this.newUnsignedMessage.bind(this),
|
||||||
signMessage: (...args) => {
|
signMessage: (...args) => {
|
||||||
keyringController.signMessage(...args)
|
this.keyringController.signMessage(...args)
|
||||||
this.sendUpdate()
|
this.sendUpdate()
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
|
|
||||||
var provider = MetaMaskProvider(providerOpts)
|
|
||||||
var web3 = new Web3(provider)
|
|
||||||
this.web3 = web3
|
|
||||||
keyringController.web3 = web3
|
|
||||||
provider.on('block', this.processBlock.bind(this))
|
|
||||||
provider.on('error', this.getNetwork.bind(this))
|
|
||||||
|
|
||||||
return provider
|
return provider
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -449,7 +450,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
this.sendUpdate()
|
this.sendUpdate()
|
||||||
}
|
}
|
||||||
|
|
||||||
this.web3.version.getNetwork((err, network) => {
|
this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
this.state.network = 'loading'
|
this.state.network = 'loading'
|
||||||
return this.sendUpdate()
|
return this.sendUpdate()
|
||||||
|
Loading…
Reference in New Issue
Block a user