aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r--app/scripts/controllers/address-book.js8
-rw-r--r--app/scripts/controllers/balance.js80
-rw-r--r--app/scripts/controllers/blacklist.js60
-rw-r--r--app/scripts/controllers/computed-balances.js77
-rw-r--r--app/scripts/controllers/currency.js14
-rw-r--r--app/scripts/controllers/infura.js43
-rw-r--r--app/scripts/controllers/network.js214
-rw-r--r--app/scripts/controllers/preferences.js80
-rw-r--r--app/scripts/controllers/recent-blocks.js110
-rw-r--r--app/scripts/controllers/transactions.js325
10 files changed, 992 insertions, 19 deletions
diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js
index c66eb2bd4..6fb4ee114 100644
--- a/app/scripts/controllers/address-book.js
+++ b/app/scripts/controllers/address-book.js
@@ -39,11 +39,11 @@ class AddressBookController {
// pushed object is an object of two fields. Current behavior does not set an
// upper limit to the number of addresses.
_addToAddressBook (address, name) {
- let addressBook = this._getAddressBook()
- let identities = this._getIdentities()
+ const addressBook = this._getAddressBook()
+ const identities = this._getIdentities()
- let addressBookIndex = addressBook.findIndex((element) => { return element.address.toLowerCase() === address.toLowerCase() || element.name === name })
- let identitiesIndex = Object.keys(identities).findIndex((element) => { return element.toLowerCase() === address.toLowerCase() })
+ const addressBookIndex = addressBook.findIndex((element) => { return element.address.toLowerCase() === address.toLowerCase() || element.name === name })
+ const identitiesIndex = Object.keys(identities).findIndex((element) => { return element.toLowerCase() === address.toLowerCase() })
// trigger this condition if we own this address--no need to overwrite.
if (identitiesIndex !== -1) {
return Promise.resolve(addressBook)
diff --git a/app/scripts/controllers/balance.js b/app/scripts/controllers/balance.js
new file mode 100644
index 000000000..f83f294cc
--- /dev/null
+++ b/app/scripts/controllers/balance.js
@@ -0,0 +1,80 @@
+const ObservableStore = require('obs-store')
+const PendingBalanceCalculator = require('../lib/pending-balance-calculator')
+const BN = require('ethereumjs-util').BN
+
+class BalanceController {
+
+ constructor (opts = {}) {
+ this._validateParams(opts)
+ const { address, accountTracker, txController, blockTracker } = opts
+
+ this.address = address
+ this.accountTracker = accountTracker
+ this.txController = txController
+ this.blockTracker = blockTracker
+
+ const initState = {
+ ethBalance: undefined,
+ }
+ this.store = new ObservableStore(initState)
+
+ this.balanceCalc = new PendingBalanceCalculator({
+ getBalance: () => this._getBalance(),
+ getPendingTransactions: this._getPendingTransactions.bind(this),
+ })
+
+ this._registerUpdates()
+ }
+
+ async updateBalance () {
+ const balance = await this.balanceCalc.getBalance()
+ this.store.updateState({
+ ethBalance: balance,
+ })
+ }
+
+ _registerUpdates () {
+ const update = this.updateBalance.bind(this)
+
+ this.txController.on('tx:status-update', (txId, status) => {
+ switch (status) {
+ case 'submitted':
+ case 'confirmed':
+ case 'failed':
+ update()
+ return
+ default:
+ return
+ }
+ })
+ this.accountTracker.store.subscribe(update)
+ this.blockTracker.on('block', update)
+ }
+
+ async _getBalance () {
+ const { accounts } = this.accountTracker.store.getState()
+ const entry = accounts[this.address]
+ const balance = entry.balance
+ return balance ? new BN(balance.substring(2), 16) : undefined
+ }
+
+ async _getPendingTransactions () {
+ const pending = this.txController.getFilteredTxList({
+ from: this.address,
+ status: 'submitted',
+ err: undefined,
+ })
+ return pending
+ }
+
+ _validateParams (opts) {
+ const { address, accountTracker, txController, blockTracker } = opts
+ if (!address || !accountTracker || !txController || !blockTracker) {
+ const error = 'Cannot construct a balance checker without address, accountTracker, txController, and blockTracker.'
+ throw new Error(error)
+ }
+ }
+
+}
+
+module.exports = BalanceController
diff --git a/app/scripts/controllers/blacklist.js b/app/scripts/controllers/blacklist.js
new file mode 100644
index 000000000..33c31dab9
--- /dev/null
+++ b/app/scripts/controllers/blacklist.js
@@ -0,0 +1,60 @@
+const ObservableStore = require('obs-store')
+const extend = require('xtend')
+const PhishingDetector = require('eth-phishing-detect/src/detector')
+
+// compute phishing lists
+const PHISHING_DETECTION_CONFIG = require('eth-phishing-detect/src/config.json')
+// every four minutes
+const POLLING_INTERVAL = 4 * 60 * 1000
+
+class BlacklistController {
+
+ constructor (opts = {}) {
+ const initState = extend({
+ phishing: PHISHING_DETECTION_CONFIG,
+ }, opts.initState)
+ this.store = new ObservableStore(initState)
+ // phishing detector
+ this._phishingDetector = null
+ this._setupPhishingDetector(initState.phishing)
+ // polling references
+ this._phishingUpdateIntervalRef = null
+ }
+
+ //
+ // PUBLIC METHODS
+ //
+
+ checkForPhishing (hostname) {
+ if (!hostname) return false
+ const { result } = this._phishingDetector.check(hostname)
+ return result
+ }
+
+ async updatePhishingList () {
+ const response = await fetch('https://api.infura.io/v2/blacklist')
+ const phishing = await response.json()
+ this.store.updateState({ phishing })
+ this._setupPhishingDetector(phishing)
+ return phishing
+ }
+
+ scheduleUpdates () {
+ if (this._phishingUpdateIntervalRef) return
+ this.updatePhishingList()
+ this._phishingUpdateIntervalRef = setInterval(() => {
+ this.updatePhishingList()
+ }, POLLING_INTERVAL)
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ _setupPhishingDetector (config) {
+ this._phishingDetector = new PhishingDetector(config)
+ }
+}
+
+module.exports = BlacklistController
+
diff --git a/app/scripts/controllers/computed-balances.js b/app/scripts/controllers/computed-balances.js
new file mode 100644
index 000000000..907b087cf
--- /dev/null
+++ b/app/scripts/controllers/computed-balances.js
@@ -0,0 +1,77 @@
+const ObservableStore = require('obs-store')
+const extend = require('xtend')
+const BalanceController = require('./balance')
+
+class ComputedbalancesController {
+
+ constructor (opts = {}) {
+ const { accountTracker, txController, blockTracker } = opts
+ this.accountTracker = accountTracker
+ this.txController = txController
+ this.blockTracker = blockTracker
+
+ const initState = extend({
+ computedBalances: {},
+ }, opts.initState)
+ this.store = new ObservableStore(initState)
+ this.balances = {}
+
+ this._initBalanceUpdating()
+ }
+
+ updateAllBalances () {
+ Object.keys(this.balances).forEach((balance) => {
+ const address = balance.address
+ this.balances[address].updateBalance()
+ })
+ }
+
+ _initBalanceUpdating () {
+ const store = this.accountTracker.store.getState()
+ this.syncAllAccountsFromStore(store)
+ this.accountTracker.store.subscribe(this.syncAllAccountsFromStore.bind(this))
+ }
+
+ syncAllAccountsFromStore (store) {
+ const upstream = Object.keys(store.accounts)
+ const balances = Object.keys(this.balances)
+ .map(address => this.balances[address])
+
+ // Follow new addresses
+ for (const address in balances) {
+ this.trackAddressIfNotAlready(address)
+ }
+
+ // Unfollow old ones
+ balances.forEach(({ address }) => {
+ if (!upstream.includes(address)) {
+ delete this.balances[address]
+ }
+ })
+ }
+
+ trackAddressIfNotAlready (address) {
+ const state = this.store.getState()
+ if (!(address in state.computedBalances)) {
+ this.trackAddress(address)
+ }
+ }
+
+ trackAddress (address) {
+ const updater = new BalanceController({
+ address,
+ accountTracker: this.accountTracker,
+ txController: this.txController,
+ blockTracker: this.blockTracker,
+ })
+ updater.store.subscribe((accountBalance) => {
+ const newState = this.store.getState()
+ newState.computedBalances[address] = accountBalance
+ this.store.updateState(newState)
+ })
+ this.balances[address] = updater
+ updater.updateBalance()
+ }
+}
+
+module.exports = ComputedbalancesController
diff --git a/app/scripts/controllers/currency.js b/app/scripts/controllers/currency.js
index c4904f8ac..25a7a942e 100644
--- a/app/scripts/controllers/currency.js
+++ b/app/scripts/controllers/currency.js
@@ -8,7 +8,7 @@ class CurrencyController {
constructor (opts = {}) {
const initState = extend({
- currentCurrency: 'USD',
+ currentCurrency: 'usd',
conversionRate: 0,
conversionDate: 'N/A',
}, opts.initState)
@@ -45,15 +45,17 @@ class CurrencyController {
updateConversionRate () {
const currentCurrency = this.getCurrentCurrency()
- return fetch(`https://www.cryptonator.com/api/ticker/eth-${currentCurrency}`)
+ return fetch(`https://api.infura.io/v1/ticker/eth${currentCurrency.toLowerCase()}`)
.then(response => response.json())
.then((parsedResponse) => {
- this.setConversionRate(Number(parsedResponse.ticker.price))
+ this.setConversionRate(Number(parsedResponse.bid))
this.setConversionDate(Number(parsedResponse.timestamp))
}).catch((err) => {
- console.warn('MetaMask - Failed to query currency conversion.')
- this.setConversionRate(0)
- this.setConversionDate('N/A')
+ if (err) {
+ console.warn('MetaMask - Failed to query currency conversion.')
+ this.setConversionRate(0)
+ this.setConversionDate('N/A')
+ }
})
}
diff --git a/app/scripts/controllers/infura.js b/app/scripts/controllers/infura.js
new file mode 100644
index 000000000..10adb1004
--- /dev/null
+++ b/app/scripts/controllers/infura.js
@@ -0,0 +1,43 @@
+const ObservableStore = require('obs-store')
+const extend = require('xtend')
+
+// every ten minutes
+const POLLING_INTERVAL = 10 * 60 * 1000
+
+class InfuraController {
+
+ constructor (opts = {}) {
+ const initState = extend({
+ infuraNetworkStatus: {},
+ }, opts.initState)
+ this.store = new ObservableStore(initState)
+ }
+
+ //
+ // PUBLIC METHODS
+ //
+
+ // Responsible for retrieving the status of Infura's nodes. Can return either
+ // ok, degraded, or down.
+ checkInfuraNetworkStatus () {
+ return fetch('https://api.infura.io/v1/status/metamask')
+ .then(response => response.json())
+ .then((parsedResponse) => {
+ this.store.updateState({
+ infuraNetworkStatus: parsedResponse,
+ })
+ return parsedResponse
+ })
+ }
+
+ scheduleInfuraNetworkCheck () {
+ if (this.conversionInterval) {
+ clearInterval(this.conversionInterval)
+ }
+ this.conversionInterval = setInterval(() => {
+ this.checkInfuraNetworkStatus()
+ }, POLLING_INTERVAL)
+ }
+}
+
+module.exports = InfuraController
diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js
new file mode 100644
index 000000000..617456cd7
--- /dev/null
+++ b/app/scripts/controllers/network.js
@@ -0,0 +1,214 @@
+const assert = require('assert')
+const EventEmitter = require('events')
+const createMetamaskProvider = require('web3-provider-engine/zero.js')
+const SubproviderFromProvider = require('web3-provider-engine/subproviders/web3.js')
+const createInfuraProvider = require('eth-json-rpc-infura/src/createProvider')
+const ObservableStore = require('obs-store')
+const ComposedStore = require('obs-store/lib/composed')
+const extend = require('xtend')
+const EthQuery = require('eth-query')
+const createEventEmitterProxy = require('../lib/events-proxy.js')
+const networkConfig = require('../config.js')
+const { OLD_UI_NETWORK_TYPE, DEFAULT_RPC } = networkConfig.enums
+const INFURA_PROVIDER_TYPES = ['ropsten', 'rinkeby', 'kovan', 'mainnet']
+
+module.exports = class NetworkController extends EventEmitter {
+
+ constructor (config) {
+ super()
+
+ this._networkEndpointVersion = OLD_UI_NETWORK_TYPE
+ this._networkEndpoints = this.getNetworkEndpoints(OLD_UI_NETWORK_TYPE)
+ this._defaultRpc = this._networkEndpoints[DEFAULT_RPC]
+
+ config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider)
+ this.networkStore = new ObservableStore('loading')
+ this.providerStore = new ObservableStore(config.provider)
+ this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore })
+ this._proxy = createEventEmitterProxy()
+
+ this.on('networkDidChange', this.lookupNetwork)
+ }
+
+ async setNetworkEndpoints (version) {
+ if (version === this._networkEndpointVersion) {
+ return
+ }
+
+ this._networkEndpointVersion = version
+ this._networkEndpoints = this.getNetworkEndpoints(version)
+ this._defaultRpc = this._networkEndpoints[DEFAULT_RPC]
+ const { type } = this.getProviderConfig()
+
+ return this.setProviderType(type, true)
+ }
+
+ getNetworkEndpoints (version = OLD_UI_NETWORK_TYPE) {
+ return networkConfig[version]
+ }
+
+ initializeProvider (_providerParams) {
+ this._baseProviderParams = _providerParams
+ const { type, rpcTarget } = this.providerStore.getState()
+ // map rpcTarget to rpcUrl
+ const opts = {
+ type,
+ rpcUrl: rpcTarget,
+ }
+ this._configureProvider(opts)
+ this._proxy.on('block', this._logBlock.bind(this))
+ this._proxy.on('error', this.verifyNetwork.bind(this))
+ this.ethQuery = new EthQuery(this._proxy)
+ this.lookupNetwork()
+ return this._proxy
+ }
+
+ verifyNetwork () {
+ // Check network when restoring connectivity:
+ if (this.isNetworkLoading()) this.lookupNetwork()
+ }
+
+ getNetworkState () {
+ return this.networkStore.getState()
+ }
+
+ setNetworkState (network) {
+ return this.networkStore.putState(network)
+ }
+
+ isNetworkLoading () {
+ return this.getNetworkState() === 'loading'
+ }
+
+ lookupNetwork () {
+ // Prevent firing when provider is not defined.
+ if (!this.ethQuery || !this.ethQuery.sendAsync) {
+ return log.warn('NetworkController - lookupNetwork aborted due to missing ethQuery')
+ }
+ 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({
+ type: 'rpc',
+ rpcTarget: rpcUrl,
+ })
+ this._switchNetwork({ rpcUrl })
+ }
+
+ getCurrentRpcAddress () {
+ const provider = this.getProviderConfig()
+ if (!provider) return null
+ return this.getRpcAddressForType(provider.type)
+ }
+
+ async setProviderType (type, forceUpdate = false) {
+ assert(type !== 'rpc', `NetworkController.setProviderType - cannot connect by type "rpc"`)
+ // skip if type already matches
+ if (type === this.getProviderConfig().type && !forceUpdate) {
+ return
+ }
+
+ const rpcTarget = this.getRpcAddressForType(type)
+ assert(rpcTarget, `NetworkController - unknown rpc address for type "${type}"`)
+ this.providerStore.updateState({ type, rpcTarget })
+ this._switchNetwork({ type })
+ }
+
+ getProviderConfig () {
+ return this.providerStore.getState()
+ }
+
+ getRpcAddressForType (type, provider = this.getProviderConfig()) {
+ if (this._networkEndpoints[type]) {
+ return this._networkEndpoints[type]
+ }
+
+ return provider && provider.rpcTarget ? provider.rpcTarget : this._defaultRpc
+ }
+
+ //
+ // Private
+ //
+
+ _switchNetwork (opts) {
+ this.setNetworkState('loading')
+ this._configureProvider(opts)
+ this.emit('networkDidChange')
+ }
+
+ _configureProvider (opts) {
+ // type-based rpc endpoints
+ const { type } = opts
+ if (type) {
+ // type-based infura rpc endpoints
+ const isInfura = INFURA_PROVIDER_TYPES.includes(type)
+ opts.rpcUrl = this.getRpcAddressForType(type)
+ if (isInfura) {
+ this._configureInfuraProvider(opts)
+ // other type-based rpc endpoints
+ } else {
+ this._configureStandardProvider(opts)
+ }
+ // url-based rpc endpoints
+ } else {
+ this._configureStandardProvider(opts)
+ }
+ }
+
+ _configureInfuraProvider (opts) {
+ log.info('_configureInfuraProvider', opts)
+ const infuraProvider = createInfuraProvider({
+ network: opts.type,
+ })
+ const infuraSubprovider = new SubproviderFromProvider(infuraProvider)
+ const providerParams = extend(this._baseProviderParams, {
+ rpcUrl: opts.rpcUrl,
+ engineParams: {
+ pollingInterval: 8000,
+ blockTrackerProvider: infuraProvider,
+ },
+ dataSubprovider: infuraSubprovider,
+ })
+ const provider = createMetamaskProvider(providerParams)
+ this._setProvider(provider)
+ }
+
+ _configureStandardProvider ({ rpcUrl }) {
+ const providerParams = extend(this._baseProviderParams, {
+ rpcUrl,
+ engineParams: {
+ pollingInterval: 8000,
+ },
+ })
+ const provider = createMetamaskProvider(providerParams)
+ this._setProvider(provider)
+ }
+
+ _setProvider (provider) {
+ // collect old block tracker events
+ const oldProvider = this._provider
+ let blockTrackerHandlers
+ if (oldProvider) {
+ // capture old block handlers
+ blockTrackerHandlers = oldProvider._blockTracker.proxyEventHandlers
+ // tear down
+ oldProvider.removeAllListeners()
+ oldProvider.stop()
+ }
+ // override block tracler
+ provider._blockTracker = createEventEmitterProxy(provider._blockTracker, blockTrackerHandlers)
+ // set as new provider
+ this._provider = provider
+ this._proxy.setTarget(provider)
+ }
+
+ _logBlock (block) {
+ log.info(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`)
+ this.verifyNetwork()
+ }
+}
diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js
index c7f675a41..39d15fd83 100644
--- a/app/scripts/controllers/preferences.js
+++ b/app/scripts/controllers/preferences.js
@@ -7,13 +7,22 @@ class PreferencesController {
constructor (opts = {}) {
const initState = extend({
frequentRpcList: [],
+ currentAccountTab: 'history',
+ tokens: [],
+ useBlockie: false,
+ featureFlags: {},
}, opts.initState)
this.store = new ObservableStore(initState)
}
+// PUBLIC METHODS
- //
- // PUBLIC METHODS
- //
+ setUseBlockie (val) {
+ this.store.updateState({ useBlockie: val })
+ }
+
+ getUseBlockie () {
+ return this.store.getState().useBlockie
+ }
setSelectedAddress (_address) {
return new Promise((resolve, reject) => {
@@ -23,10 +32,44 @@ class PreferencesController {
})
}
- getSelectedAddress (_address) {
+ getSelectedAddress () {
return this.store.getState().selectedAddress
}
+ async addToken (rawAddress, symbol, decimals) {
+ const address = normalizeAddress(rawAddress)
+ const newEntry = { address, symbol, decimals }
+
+ const tokens = this.store.getState().tokens
+ const previousEntry = tokens.find((token, index) => {
+ return token.address === address
+ })
+ const previousIndex = tokens.indexOf(previousEntry)
+
+ if (previousEntry) {
+ tokens[previousIndex] = newEntry
+ } else {
+ tokens.push(newEntry)
+ }
+
+ this.store.updateState({ tokens })
+
+ return Promise.resolve(tokens)
+ }
+
+ removeToken (rawAddress) {
+ const tokens = this.store.getState().tokens
+
+ const updatedTokens = tokens.filter(token => token.address !== rawAddress)
+
+ this.store.updateState({ tokens: updatedTokens })
+ return Promise.resolve(updatedTokens)
+ }
+
+ getTokens () {
+ return this.store.getState().tokens
+ }
+
updateFrequentRpcList (_url) {
return this.addToFrequentRpcList(_url)
.then((rpcList) => {
@@ -35,9 +78,16 @@ class PreferencesController {
})
}
+ setCurrentAccountTab (currentAccountTab) {
+ return new Promise((resolve, reject) => {
+ this.store.updateState({ currentAccountTab })
+ resolve()
+ })
+ }
+
addToFrequentRpcList (_url) {
- let rpcList = this.getFrequentRpcList()
- let index = rpcList.findIndex((element) => { return element === _url })
+ const rpcList = this.getFrequentRpcList()
+ const index = rpcList.findIndex((element) => { return element === _url })
if (index !== -1) {
rpcList.splice(index, 1)
}
@@ -54,12 +104,24 @@ class PreferencesController {
return this.store.getState().frequentRpcList
}
- //
- // PRIVATE METHODS
- //
+ setFeatureFlag (feature, activated) {
+ const currentFeatureFlags = this.store.getState().featureFlags
+ const updatedFeatureFlags = {
+ ...currentFeatureFlags,
+ [feature]: activated,
+ }
+ this.store.updateState({ featureFlags: updatedFeatureFlags })
+ return Promise.resolve(updatedFeatureFlags)
+ }
+ getFeatureFlags () {
+ return this.store.getState().featureFlags
+ }
+ //
+ // PRIVATE METHODS
+ //
}
module.exports = PreferencesController
diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js
new file mode 100644
index 000000000..4ae3810eb
--- /dev/null
+++ b/app/scripts/controllers/recent-blocks.js
@@ -0,0 +1,110 @@
+const ObservableStore = require('obs-store')
+const extend = require('xtend')
+const BN = require('ethereumjs-util').BN
+const EthQuery = require('eth-query')
+
+class RecentBlocksController {
+
+ constructor (opts = {}) {
+ const { blockTracker, provider } = opts
+ this.blockTracker = blockTracker
+ this.ethQuery = new EthQuery(provider)
+ this.historyLength = opts.historyLength || 40
+
+ const initState = extend({
+ recentBlocks: [],
+ }, opts.initState)
+ this.store = new ObservableStore(initState)
+
+ this.blockTracker.on('block', this.processBlock.bind(this))
+ this.backfill()
+ }
+
+ resetState () {
+ this.store.updateState({
+ recentBlocks: [],
+ })
+ }
+
+ processBlock (newBlock) {
+ const block = this.mapTransactionsToPrices(newBlock)
+
+ const state = this.store.getState()
+ state.recentBlocks.push(block)
+
+ while (state.recentBlocks.length > this.historyLength) {
+ state.recentBlocks.shift()
+ }
+
+ this.store.updateState(state)
+ }
+
+ backfillBlock (newBlock) {
+ const block = this.mapTransactionsToPrices(newBlock)
+
+ const state = this.store.getState()
+
+ if (state.recentBlocks.length < this.historyLength) {
+ state.recentBlocks.unshift(block)
+ }
+
+ this.store.updateState(state)
+ }
+
+ mapTransactionsToPrices (newBlock) {
+ const block = extend(newBlock, {
+ gasPrices: newBlock.transactions.map((tx) => {
+ return tx.gasPrice
+ }),
+ })
+ delete block.transactions
+ return block
+ }
+
+ async backfill() {
+ this.blockTracker.once('block', async (block) => {
+ let blockNum = block.number
+ let recentBlocks
+ let state = this.store.getState()
+ recentBlocks = state.recentBlocks
+
+ while (recentBlocks.length < this.historyLength) {
+ try {
+ let blockNumBn = new BN(blockNum.substr(2), 16)
+ const newNum = blockNumBn.subn(1).toString(10)
+ const newBlock = await this.getBlockByNumber(newNum)
+
+ if (newBlock) {
+ this.backfillBlock(newBlock)
+ blockNum = newBlock.number
+ }
+
+ state = this.store.getState()
+ recentBlocks = state.recentBlocks
+ } catch (e) {
+ log.error(e)
+ }
+ await this.wait()
+ }
+ })
+ }
+
+ async wait () {
+ return new Promise((resolve) => {
+ setTimeout(resolve, 100)
+ })
+ }
+
+ async getBlockByNumber (number) {
+ const bn = new BN(number)
+ return new Promise((resolve, reject) => {
+ this.ethQuery.getBlockByNumber('0x' + bn.toString(16), true, (err, block) => {
+ if (err) reject(err)
+ resolve(block)
+ })
+ })
+ }
+
+}
+
+module.exports = RecentBlocksController
diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js
new file mode 100644
index 000000000..ef5578d5a
--- /dev/null
+++ b/app/scripts/controllers/transactions.js
@@ -0,0 +1,325 @@
+const EventEmitter = require('events')
+const ObservableStore = require('obs-store')
+const ethUtil = require('ethereumjs-util')
+const Transaction = require('ethereumjs-tx')
+const EthQuery = require('ethjs-query')
+const TransactionStateManger = require('../lib/tx-state-manager')
+const TxGasUtil = require('../lib/tx-gas-utils')
+const PendingTransactionTracker = require('../lib/pending-tx-tracker')
+const createId = require('../lib/random-id')
+const NonceTracker = require('../lib/nonce-tracker')
+
+/*
+ Transaction Controller is an aggregate of sub-controllers and trackers
+ composing them in a way to be exposed to the metamask controller
+ - txStateManager
+ responsible for the state of a transaction and
+ storing the transaction
+ - pendingTxTracker
+ watching blocks for transactions to be include
+ and emitting confirmed events
+ - txGasUtil
+ gas calculations and safety buffering
+ - nonceTracker
+ calculating nonces
+*/
+
+module.exports = class TransactionController extends EventEmitter {
+ constructor (opts) {
+ super()
+ this.networkStore = opts.networkStore || new ObservableStore({})
+ this.preferencesStore = opts.preferencesStore || new ObservableStore({})
+ this.provider = opts.provider
+ this.blockTracker = opts.blockTracker
+ this.signEthTx = opts.signTransaction
+ this.getGasPrice = opts.getGasPrice
+
+ this.memStore = new ObservableStore({})
+ this.query = new EthQuery(this.provider)
+ this.txGasUtil = new TxGasUtil(this.provider)
+
+ this.txStateManager = new TransactionStateManger({
+ initState: opts.initState,
+ txHistoryLimit: opts.txHistoryLimit,
+ getNetwork: this.getNetwork.bind(this),
+ })
+
+ this.txStateManager.getFilteredTxList({
+ status: 'unapproved',
+ loadingDefaults: true,
+ }).forEach((tx) => {
+ this.addTxDefaults(tx)
+ .then((txMeta) => {
+ txMeta.loadingDefaults = false
+ this.txStateManager.updateTx(txMeta, 'transactions: gas estimation for tx on boot')
+ }).catch((error) => {
+ this.txStateManager.setTxStatusFailed(tx.id, error)
+ })
+ })
+
+ this.txStateManager.getFilteredTxList({
+ status: 'approved',
+ }).forEach((txMeta) => {
+ const txSignError = new Error('Transaction found as "approved" during boot - possibly stuck during signing')
+ this.txStateManager.setTxStatusFailed(txMeta.id, txSignError)
+ })
+
+
+ this.store = this.txStateManager.store
+ this.txStateManager.on('tx:status-update', this.emit.bind(this, 'tx:status-update'))
+ this.nonceTracker = new NonceTracker({
+ provider: this.provider,
+ getPendingTransactions: this.txStateManager.getPendingTransactions.bind(this.txStateManager),
+ getConfirmedTransactions: (address) => {
+ return this.txStateManager.getFilteredTxList({
+ from: address,
+ status: 'confirmed',
+ err: undefined,
+ })
+ },
+ })
+
+ this.pendingTxTracker = new PendingTransactionTracker({
+ provider: this.provider,
+ nonceTracker: this.nonceTracker,
+ publishTransaction: (rawTx) => this.query.sendRawTransaction(rawTx),
+ getPendingTransactions: this.txStateManager.getPendingTransactions.bind(this.txStateManager),
+ getCompletedTransactions: this.txStateManager.getConfirmedTransactions.bind(this.txStateManager),
+ })
+
+ this.txStateManager.store.subscribe(() => this.emit('update:badge'))
+
+ this.pendingTxTracker.on('tx:warning', (txMeta) => {
+ this.txStateManager.updateTx(txMeta, 'transactions/pending-tx-tracker#event: tx:warning')
+ })
+ this.pendingTxTracker.on('tx:failed', this.txStateManager.setTxStatusFailed.bind(this.txStateManager))
+ this.pendingTxTracker.on('tx:confirmed', this.txStateManager.setTxStatusConfirmed.bind(this.txStateManager))
+ this.pendingTxTracker.on('tx:block-update', (txMeta, latestBlockNumber) => {
+ if (!txMeta.firstRetryBlockNumber) {
+ txMeta.firstRetryBlockNumber = latestBlockNumber
+ this.txStateManager.updateTx(txMeta, 'transactions/pending-tx-tracker#event: tx:block-update')
+ }
+ })
+ this.pendingTxTracker.on('tx:retry', (txMeta) => {
+ if (!('retryCount' in txMeta)) txMeta.retryCount = 0
+ txMeta.retryCount++
+ this.txStateManager.updateTx(txMeta, 'transactions/pending-tx-tracker#event: tx:retry')
+ })
+
+ this.blockTracker.on('block', this.pendingTxTracker.checkForTxInBlock.bind(this.pendingTxTracker))
+ // this is a little messy but until ethstore has been either
+ // removed or redone this is to guard against the race condition
+ this.blockTracker.on('latest', this.pendingTxTracker.resubmitPendingTxs.bind(this.pendingTxTracker))
+ this.blockTracker.on('sync', this.pendingTxTracker.queryPendingTxs.bind(this.pendingTxTracker))
+ // memstore is computed from a few different stores
+ this._updateMemstore()
+ this.txStateManager.store.subscribe(() => this._updateMemstore())
+ this.networkStore.subscribe(() => this._updateMemstore())
+ this.preferencesStore.subscribe(() => this._updateMemstore())
+ }
+
+ getState () {
+ return this.memStore.getState()
+ }
+
+ getNetwork () {
+ return this.networkStore.getState()
+ }
+
+ getSelectedAddress () {
+ return this.preferencesStore.getState().selectedAddress
+ }
+
+ getUnapprovedTxCount () {
+ return Object.keys(this.txStateManager.getUnapprovedTxList()).length
+ }
+
+ getPendingTxCount (account) {
+ return this.txStateManager.getPendingTransactions(account).length
+ }
+
+ getFilteredTxList (opts) {
+ return this.txStateManager.getFilteredTxList(opts)
+ }
+
+ getChainId () {
+ const networkState = this.networkStore.getState()
+ const getChainId = parseInt(networkState)
+ if (Number.isNaN(getChainId)) {
+ return 0
+ } else {
+ return getChainId
+ }
+ }
+
+ wipeTransactions (address) {
+ this.txStateManager.wipeTransactions(address)
+ }
+
+ // Adds a tx to the txlist
+ addTx (txMeta) {
+ this.txStateManager.addTx(txMeta)
+ this.emit(`${txMeta.id}:unapproved`, txMeta)
+ }
+
+ async newUnapprovedTransaction (txParams) {
+ log.debug(`MetaMaskController newUnapprovedTransaction ${JSON.stringify(txParams)}`)
+ const initialTxMeta = await this.addUnapprovedTransaction(txParams)
+ // listen for tx completion (success, fail)
+ return new Promise((resolve, reject) => {
+ this.txStateManager.once(`${initialTxMeta.id}:finished`, (finishedTxMeta) => {
+ switch (finishedTxMeta.status) {
+ case 'submitted':
+ return resolve(finishedTxMeta.hash)
+ case 'rejected':
+ return reject(new Error('MetaMask Tx Signature: User denied transaction signature.'))
+ case 'failed':
+ return reject(new Error(finishedTxMeta.err.message))
+ default:
+ return reject(new Error(`MetaMask Tx Signature: Unknown problem: ${JSON.stringify(finishedTxMeta.txParams)}`))
+ }
+ })
+ })
+ }
+
+ async addUnapprovedTransaction (txParams) {
+ // validate
+ await this.txGasUtil.validateTxParams(txParams)
+ // construct txMeta
+ const txMeta = {
+ id: createId(),
+ time: (new Date()).getTime(),
+ status: 'unapproved',
+ metamaskNetworkId: this.getNetwork(),
+ txParams: txParams,
+ loadingDefaults: true,
+ }
+ this.addTx(txMeta)
+ this.emit('newUnapprovedTx', txMeta)
+ // add default tx params
+ try {
+ await this.addTxDefaults(txMeta)
+ } catch (error) {
+ console.log(error)
+ this.txStateManager.setTxStatusFailed(txMeta.id, error)
+ throw error
+ }
+ txMeta.loadingDefaults = false
+ // save txMeta
+ this.txStateManager.updateTx(txMeta)
+
+ return txMeta
+ }
+
+ async addTxDefaults (txMeta) {
+ const txParams = txMeta.txParams
+ // ensure value
+ txMeta.gasPriceSpecified = Boolean(txParams.gasPrice)
+ txMeta.nonceSpecified = Boolean(txParams.nonce)
+ let gasPrice = txParams.gasPrice
+ if (!gasPrice) {
+ gasPrice = this.getGasPrice ? this.getGasPrice() : await this.query.gasPrice()
+ }
+ txParams.gasPrice = ethUtil.addHexPrefix(gasPrice.toString(16))
+ txParams.value = txParams.value || '0x0'
+ // set gasLimit
+ return await this.txGasUtil.analyzeGasUsage(txMeta)
+ }
+
+ async retryTransaction (txId) {
+ this.txStateManager.setTxStatusUnapproved(txId)
+ const txMeta = this.txStateManager.getTx(txId)
+ txMeta.lastGasPrice = txMeta.txParams.gasPrice
+ this.txStateManager.updateTx(txMeta, 'retryTransaction: manual retry')
+ }
+
+ async updateTransaction (txMeta) {
+ this.txStateManager.updateTx(txMeta, 'confTx: user updated transaction')
+ }
+
+ async updateAndApproveTransaction (txMeta) {
+ this.txStateManager.updateTx(txMeta, 'confTx: user approved transaction')
+ await this.approveTransaction(txMeta.id)
+ }
+
+ async approveTransaction (txId) {
+ let nonceLock
+ try {
+ // approve
+ this.txStateManager.setTxStatusApproved(txId)
+ // get next nonce
+ const txMeta = this.txStateManager.getTx(txId)
+ const fromAddress = txMeta.txParams.from
+ // wait for a nonce
+ nonceLock = await this.nonceTracker.getNonceLock(fromAddress)
+ // add nonce to txParams
+ const nonce = txMeta.nonceSpecified ? txMeta.txParams.nonce : nonceLock.nextNonce
+ if (nonce > nonceLock.nextNonce) {
+ const message = `Specified nonce may not be larger than account's next valid nonce.`
+ throw new Error(message)
+ }
+ txMeta.txParams.nonce = ethUtil.addHexPrefix(nonce.toString(16))
+ // add nonce debugging information to txMeta
+ txMeta.nonceDetails = nonceLock.nonceDetails
+ this.txStateManager.updateTx(txMeta, 'transactions#approveTransaction')
+ // sign transaction
+ const rawTx = await this.signTransaction(txId)
+ await this.publishTransaction(txId, rawTx)
+ // must set transaction to submitted/failed before releasing lock
+ nonceLock.releaseLock()
+ } catch (err) {
+ this.txStateManager.setTxStatusFailed(txId, err)
+ // must set transaction to submitted/failed before releasing lock
+ if (nonceLock) nonceLock.releaseLock()
+ // continue with error chain
+ throw err
+ }
+ }
+
+ async signTransaction (txId) {
+ const txMeta = this.txStateManager.getTx(txId)
+ const txParams = txMeta.txParams
+ const fromAddress = txParams.from
+ // add network/chain id
+ txParams.chainId = ethUtil.addHexPrefix(this.getChainId().toString(16))
+ const ethTx = new Transaction(txParams)
+ await this.signEthTx(ethTx, fromAddress)
+ this.txStateManager.setTxStatusSigned(txMeta.id)
+ const rawTx = ethUtil.bufferToHex(ethTx.serialize())
+ return rawTx
+ }
+
+ async publishTransaction (txId, rawTx) {
+ const txMeta = this.txStateManager.getTx(txId)
+ txMeta.rawTx = rawTx
+ this.txStateManager.updateTx(txMeta, 'transactions#publishTransaction')
+ const txHash = await this.query.sendRawTransaction(rawTx)
+ this.setTxHash(txId, txHash)
+ this.txStateManager.setTxStatusSubmitted(txId)
+ }
+
+ async cancelTransaction (txId) {
+ this.txStateManager.setTxStatusRejected(txId)
+ }
+
+ // receives a txHash records the tx as signed
+ setTxHash (txId, txHash) {
+ // Add the tx hash to the persisted meta-tx object
+ const txMeta = this.txStateManager.getTx(txId)
+ txMeta.hash = txHash
+ this.txStateManager.updateTx(txMeta, 'transactions#setTxHash')
+ }
+
+//
+// PRIVATE METHODS
+//
+
+ _updateMemstore () {
+ const unapprovedTxs = this.txStateManager.getUnapprovedTxList()
+ const selectedAddressTxList = this.txStateManager.getFilteredTxList({
+ from: this.getSelectedAddress(),
+ metamaskNetworkId: this.getNetwork(),
+ })
+ this.memStore.updateState({ unapprovedTxs, selectedAddressTxList })
+ }
+}