diff options
author | Kevin Serrano <kevin.serrano@consensys.net> | 2017-10-27 07:22:08 +0800 |
---|---|---|
committer | Kevin Serrano <kevin.serrano@consensys.net> | 2017-10-27 07:22:08 +0800 |
commit | 1e9c0a9db2d58e61361bff7430d3a74006ef131c (patch) | |
tree | 1d1f09bfe2e1985c42d5a117e8e63d19c71a360b /app/scripts | |
parent | a387def701303e56f721fdc7c716e72641bfaf8f (diff) | |
parent | 19afb638194a11367250153a710d77011665132a (diff) | |
download | tangerine-wallet-browser-1e9c0a9db2d58e61361bff7430d3a74006ef131c.tar tangerine-wallet-browser-1e9c0a9db2d58e61361bff7430d3a74006ef131c.tar.gz tangerine-wallet-browser-1e9c0a9db2d58e61361bff7430d3a74006ef131c.tar.bz2 tangerine-wallet-browser-1e9c0a9db2d58e61361bff7430d3a74006ef131c.tar.lz tangerine-wallet-browser-1e9c0a9db2d58e61361bff7430d3a74006ef131c.tar.xz tangerine-wallet-browser-1e9c0a9db2d58e61361bff7430d3a74006ef131c.tar.zst tangerine-wallet-browser-1e9c0a9db2d58e61361bff7430d3a74006ef131c.zip |
Resolve merge conflicts
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/contentscript.js | 7 | ||||
-rw-r--r-- | app/scripts/controllers/balance.js | 10 | ||||
-rw-r--r-- | app/scripts/controllers/computed-balances.js | 29 | ||||
-rw-r--r-- | app/scripts/controllers/network.js | 6 | ||||
-rw-r--r-- | app/scripts/inpage.js | 10 | ||||
-rw-r--r-- | app/scripts/lib/account-tracker.js | 23 | ||||
-rw-r--r-- | app/scripts/lib/createLoggerMiddleware.js | 4 | ||||
-rw-r--r-- | app/scripts/lib/createOriginMiddleware.js | 4 | ||||
-rw-r--r-- | app/scripts/lib/createProviderMiddleware.js | 5 | ||||
-rw-r--r-- | app/scripts/lib/events-proxy.js | 4 | ||||
-rw-r--r-- | app/scripts/lib/nodeify.js | 4 | ||||
-rw-r--r-- | app/scripts/lib/pending-balance-calculator.js | 2 | ||||
-rw-r--r-- | app/scripts/lib/pending-tx-tracker.js | 12 | ||||
-rw-r--r-- | app/scripts/lib/port-stream.js | 2 | ||||
-rw-r--r-- | app/scripts/lib/tx-gas-utils.js | 2 | ||||
-rw-r--r-- | app/scripts/lib/tx-state-history-helper.js | 10 | ||||
-rw-r--r-- | app/scripts/lib/tx-state-manager.js | 2 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 59 |
18 files changed, 132 insertions, 63 deletions
diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index b4708189e..ffbbc73cc 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -7,7 +7,9 @@ const ObjectMultiplex = require('obj-multiplex') const extension = require('extensionizer') const PortStream = require('./lib/port-stream.js') -const inpageText = fs.readFileSync(path.join(__dirname, 'inpage.js')).toString() +const inpageContent = fs.readFileSync(path.join(__dirname, '..', '..', 'dist', 'chrome', 'scripts', 'inpage.js')).toString() +const inpageSuffix = '//# sourceURL=' + extension.extension.getURL('scripts/inpage.js') + '\n' +const inpageBundle = inpageContent + inpageSuffix // Eventually this streaming injection could be replaced with: // https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.exportFunction @@ -25,8 +27,7 @@ function setupInjection () { try { // inject in-page script var scriptTag = document.createElement('script') - scriptTag.src = extension.extension.getURL('scripts/inpage.js') - scriptTag.textContent = inpageText + scriptTag.textContent = inpageBundle scriptTag.onload = function () { this.parentNode.removeChild(this) } var container = document.head || document.documentElement // append as first child diff --git a/app/scripts/controllers/balance.js b/app/scripts/controllers/balance.js index 4fa4c78fe..f83f294cc 100644 --- a/app/scripts/controllers/balance.js +++ b/app/scripts/controllers/balance.js @@ -5,7 +5,9 @@ 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 @@ -65,6 +67,14 @@ class BalanceController { 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/computed-balances.js b/app/scripts/controllers/computed-balances.js index 2479e1b3a..907b087cf 100644 --- a/app/scripts/controllers/computed-balances.js +++ b/app/scripts/controllers/computed-balances.js @@ -20,23 +20,34 @@ class ComputedbalancesController { } updateAllBalances () { - for (let address in this.accountTracker.store.getState().accounts) { + Object.keys(this.balances).forEach((balance) => { + const address = balance.address this.balances[address].updateBalance() - } + }) } _initBalanceUpdating () { const store = this.accountTracker.store.getState() - this.addAnyAccountsFromStore(store) - this.accountTracker.store.subscribe(this.addAnyAccountsFromStore.bind(this)) + this.syncAllAccountsFromStore(store) + this.accountTracker.store.subscribe(this.syncAllAccountsFromStore.bind(this)) } - addAnyAccountsFromStore(store) { - const balances = store.accounts + syncAllAccountsFromStore (store) { + const upstream = Object.keys(store.accounts) + const balances = Object.keys(this.balances) + .map(address => this.balances[address]) - for (let address in balances) { + // 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) { @@ -47,14 +58,14 @@ class ComputedbalancesController { } trackAddress (address) { - let updater = new BalanceController({ + const updater = new BalanceController({ address, accountTracker: this.accountTracker, txController: this.txController, blockTracker: this.blockTracker, }) updater.store.subscribe((accountBalance) => { - let newState = this.store.getState() + const newState = this.store.getState() newState.computedBalances[address] = accountBalance this.store.updateState(newState) }) diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js index 0f9db4d53..23afdc12b 100644 --- a/app/scripts/controllers/network.js +++ b/app/scripts/controllers/network.js @@ -51,6 +51,10 @@ module.exports = class NetworkController extends EventEmitter { } lookupNetwork () { + // Prevent firing when provider is not defined. + if (!this.ethQuery || !this.ethQuery.sendAsync) { + return + } this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => { if (err) return this.setNetworkState('loading') log.info('web3.getNetwork returned ' + network) @@ -101,7 +105,7 @@ module.exports = class NetworkController extends EventEmitter { this.emit('networkDidChange') } - _configureStandardProvider(_providerParams) { + _configureStandardProvider (_providerParams) { const providerParams = extend(this._baseProviderParams, _providerParams) const provider = createMetamaskProvider(providerParams) this._setProvider(provider) diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index 9e98c044b..b6889b00f 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.js @@ -1,6 +1,7 @@ /*global Web3*/ cleanContextForImports() require('web3/dist/web3.min.js') +const log = require('loglevel') const LocalMessageDuplexStream = require('post-message-stream') // const PingStream = require('ping-pong-stream/ping') // const endOfStream = require('end-of-stream') @@ -8,6 +9,10 @@ const setupDappAutoReload = require('./lib/auto-reload.js') const MetamaskInpageProvider = require('./lib/inpage-provider.js') restoreContextAfterImports() +const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG' +window.log = log +log.setDefaultLevel(METAMASK_DEBUG ? 'debug' : 'warn') + // // setup plugin communication @@ -28,9 +33,9 @@ var inpageProvider = new MetamaskInpageProvider(metamaskStream) var web3 = new Web3(inpageProvider) web3.setProvider = function () { - console.log('MetaMask - overrode web3.setProvider') + log.debug('MetaMask - overrode web3.setProvider') } -console.log('MetaMask - injected web3') +log.debug('MetaMask - injected web3') // export global web3, with usage-detection setupDappAutoReload(web3, inpageProvider.publicConfigStore) @@ -65,4 +70,3 @@ function restoreContextAfterImports () { console.warn('MetaMask - global.define could not be overwritten.') } } - diff --git a/app/scripts/lib/account-tracker.js b/app/scripts/lib/account-tracker.js index cdc21282d..ce6642150 100644 --- a/app/scripts/lib/account-tracker.js +++ b/app/scripts/lib/account-tracker.js @@ -38,6 +38,29 @@ class AccountTracker extends EventEmitter { // public // + syncWithAddresses (addresses) { + const accounts = this.store.getState().accounts + const locals = Object.keys(accounts) + + const toAdd = [] + addresses.forEach((upstream) => { + if (!locals.includes(upstream)) { + toAdd.push(upstream) + } + }) + + const toRemove = [] + locals.forEach((local) => { + if (!addresses.includes(local)) { + toRemove.push(local) + } + }) + + toAdd.forEach(upstream => this.addAccount(upstream)) + toRemove.forEach(local => this.removeAccount(local)) + this._updateAccounts() + } + addAccount (address) { const accounts = this.store.getState().accounts accounts[address] = {} diff --git a/app/scripts/lib/createLoggerMiddleware.js b/app/scripts/lib/createLoggerMiddleware.js index b92a965de..2707cbd9e 100644 --- a/app/scripts/lib/createLoggerMiddleware.js +++ b/app/scripts/lib/createLoggerMiddleware.js @@ -1,7 +1,7 @@ // log rpc activity module.exports = createLoggerMiddleware -function createLoggerMiddleware({ origin }) { +function createLoggerMiddleware ({ origin }) { return function loggerMiddleware (req, res, next, end) { next((cb) => { if (res.error) { @@ -12,4 +12,4 @@ function createLoggerMiddleware({ origin }) { cb() }) } -}
\ No newline at end of file +} diff --git a/app/scripts/lib/createOriginMiddleware.js b/app/scripts/lib/createOriginMiddleware.js index e1e097cc4..f8bdb2dc2 100644 --- a/app/scripts/lib/createOriginMiddleware.js +++ b/app/scripts/lib/createOriginMiddleware.js @@ -1,9 +1,9 @@ // append dapp origin domain to request module.exports = createOriginMiddleware -function createOriginMiddleware({ origin }) { +function createOriginMiddleware ({ origin }) { return function originMiddleware (req, res, next, end) { req.origin = origin next() } -}
\ No newline at end of file +} diff --git a/app/scripts/lib/createProviderMiddleware.js b/app/scripts/lib/createProviderMiddleware.js index 6dd192411..4e667bac2 100644 --- a/app/scripts/lib/createProviderMiddleware.js +++ b/app/scripts/lib/createProviderMiddleware.js @@ -1,8 +1,7 @@ - module.exports = createProviderMiddleware // forward requests to provider -function createProviderMiddleware({ provider }) { +function createProviderMiddleware ({ provider }) { return (req, res, next, end) => { provider.sendAsync(req, (err, _res) => { if (err) return end(err) @@ -10,4 +9,4 @@ function createProviderMiddleware({ provider }) { end() }) } -}
\ No newline at end of file +} diff --git a/app/scripts/lib/events-proxy.js b/app/scripts/lib/events-proxy.js index d1199a278..c0a490b05 100644 --- a/app/scripts/lib/events-proxy.js +++ b/app/scripts/lib/events-proxy.js @@ -1,4 +1,4 @@ -module.exports = function createEventEmitterProxy(eventEmitter, listeners) { +module.exports = function createEventEmitterProxy (eventEmitter, listeners) { let target = eventEmitter const eventHandlers = listeners || {} const proxy = new Proxy({}, { @@ -28,4 +28,4 @@ module.exports = function createEventEmitterProxy(eventEmitter, listeners) { } if (listeners) proxy.setTarget(eventEmitter) return proxy -}
\ No newline at end of file +} diff --git a/app/scripts/lib/nodeify.js b/app/scripts/lib/nodeify.js index d24e92206..9b595d93c 100644 --- a/app/scripts/lib/nodeify.js +++ b/app/scripts/lib/nodeify.js @@ -1,8 +1,8 @@ const promiseToCallback = require('promise-to-callback') -const noop = function(){} +const noop = function () {} module.exports = function nodeify (fn, context) { - return function(){ + return function () { const args = [].slice.call(arguments) const lastArg = args[args.length - 1] const lastArgIsCallback = typeof lastArg === 'function' diff --git a/app/scripts/lib/pending-balance-calculator.js b/app/scripts/lib/pending-balance-calculator.js index cea642f1a..6ae526463 100644 --- a/app/scripts/lib/pending-balance-calculator.js +++ b/app/scripts/lib/pending-balance-calculator.js @@ -13,7 +13,7 @@ class PendingBalanceCalculator { this.getNetworkBalance = getBalance } - async getBalance() { + async getBalance () { const results = await Promise.all([ this.getNetworkBalance(), this.getPendingTransactions(), diff --git a/app/scripts/lib/pending-tx-tracker.js b/app/scripts/lib/pending-tx-tracker.js index df504c126..0d7c6a92c 100644 --- a/app/scripts/lib/pending-tx-tracker.js +++ b/app/scripts/lib/pending-tx-tracker.js @@ -81,14 +81,14 @@ module.exports = class PendingTransactionTracker extends EventEmitter { const errorMessage = err.message.toLowerCase() const isKnownTx = ( // geth - errorMessage.includes('replacement transaction underpriced') - || errorMessage.includes('known transaction') + errorMessage.includes('replacement transaction underpriced') || + errorMessage.includes('known transaction') || // parity - || errorMessage.includes('gas price too low to replace') - || errorMessage.includes('transaction with the same hash was already imported') + errorMessage.includes('gas price too low to replace') || + errorMessage.includes('transaction with the same hash was already imported') || // other - || errorMessage.includes('gateway timeout') - || errorMessage.includes('nonce too low') + errorMessage.includes('gateway timeout') || + errorMessage.includes('nonce too low') ) // ignore resubmit warnings, return early if (isKnownTx) return diff --git a/app/scripts/lib/port-stream.js b/app/scripts/lib/port-stream.js index 648d88087..a9716fb00 100644 --- a/app/scripts/lib/port-stream.js +++ b/app/scripts/lib/port-stream.js @@ -1,6 +1,6 @@ const Duplex = require('readable-stream').Duplex const inherits = require('util').inherits -const noop = function(){} +const noop = function () {} module.exports = PortDuplexStream diff --git a/app/scripts/lib/tx-gas-utils.js b/app/scripts/lib/tx-gas-utils.js index 41f67e230..7e72ea71d 100644 --- a/app/scripts/lib/tx-gas-utils.js +++ b/app/scripts/lib/tx-gas-utils.js @@ -81,4 +81,4 @@ module.exports = class txProvideUtil { throw new Error(`Invalid transaction value of ${txParams.value} not a positive number.`) } } -}
\ No newline at end of file +} diff --git a/app/scripts/lib/tx-state-history-helper.js b/app/scripts/lib/tx-state-history-helper.js index db6e3bc9f..94c7b6792 100644 --- a/app/scripts/lib/tx-state-history-helper.js +++ b/app/scripts/lib/tx-state-history-helper.js @@ -9,7 +9,7 @@ module.exports = { } -function migrateFromSnapshotsToDiffs(longHistory) { +function migrateFromSnapshotsToDiffs (longHistory) { return ( longHistory // convert non-initial history entries into diffs @@ -20,22 +20,22 @@ function migrateFromSnapshotsToDiffs(longHistory) { ) } -function generateHistoryEntry(previousState, newState, note) { +function generateHistoryEntry (previousState, newState, note) { const entry = jsonDiffer.compare(previousState, newState) // Add a note to the first op, since it breaks if we append it to the entry if (note && entry[0]) entry[0].note = note return entry } -function replayHistory(_shortHistory) { +function replayHistory (_shortHistory) { const shortHistory = clone(_shortHistory) return shortHistory.reduce((val, entry) => jsonDiffer.applyPatch(val, entry).newDocument) } -function snapshotFromTxMeta(txMeta) { +function snapshotFromTxMeta (txMeta) { // create txMeta snapshot for history const snapshot = clone(txMeta) // dont include previous history in this snapshot delete snapshot.history return snapshot -}
\ No newline at end of file +} diff --git a/app/scripts/lib/tx-state-manager.js b/app/scripts/lib/tx-state-manager.js index 2250403f6..0fd6bed4b 100644 --- a/app/scripts/lib/tx-state-manager.js +++ b/app/scripts/lib/tx-state-manager.js @@ -91,7 +91,7 @@ module.exports = class TransactionStateManger extends EventEmitter { updateTx (txMeta, note) { if (txMeta.txParams) { Object.keys(txMeta.txParams).forEach((key) => { - let value = txMeta.txParams[key] + const value = txMeta.txParams[key] if (typeof value !== 'string') console.error(`${key}: ${value} in txParams is not a string`) if (!ethUtil.isHexPrefixed(value)) console.error('is not hex prefixed, anything on txParams must be hex prefixed') }) diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 727f48f1c..ad42a39fb 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1,6 +1,5 @@ const EventEmitter = require('events') const extend = require('xtend') -const promiseToCallback = require('promise-to-callback') const pump = require('pump') const Dnode = require('dnode') const ObservableStore = require('obs-store') @@ -96,25 +95,20 @@ module.exports = class MetamaskController extends EventEmitter { // key mgmt this.keyringController = new KeyringController({ initState: initState.KeyringController, - accountTracker: this.accountTracker, getNetwork: this.networkController.getNetworkState.bind(this.networkController), encryptor: opts.encryptor || undefined, }) // If only one account exists, make sure it is selected. - this.keyringController.store.subscribe((state) => { - const addresses = Object.keys(state.walletNicknames || {}) + this.keyringController.memStore.subscribe((state) => { + const addresses = state.keyrings.reduce((res, keyring) => { + return res.concat(keyring.accounts) + }, []) if (addresses.length === 1) { const address = addresses[0] this.preferencesController.setSelectedAddress(address) } - }) - this.keyringController.on('newAccount', (address) => { - this.preferencesController.setSelectedAddress(address) - this.accountTracker.addAccount(address) - }) - this.keyringController.on('removedAccount', (address) => { - this.accountTracker.removeAccount(address) + this.accountTracker.syncWithAddresses(addresses) }) // address book controller @@ -329,13 +323,13 @@ module.exports = class MetamaskController extends EventEmitter { createShapeShiftTx: this.createShapeShiftTx.bind(this), // primary HD keyring management - addNewAccount: this.addNewAccount.bind(this), + addNewAccount: nodeify(this.addNewAccount, this), placeSeedWords: this.placeSeedWords.bind(this), clearSeedWordCache: this.clearSeedWordCache.bind(this), importAccountWithStrategy: this.importAccountWithStrategy.bind(this), // vault management - submitPassword: this.submitPassword.bind(this), + submitPassword: nodeify(keyringController.submitPassword, keyringController), // network management setProviderType: nodeify(networkController.setProviderType, networkController), @@ -351,8 +345,8 @@ module.exports = class MetamaskController extends EventEmitter { // KeyringController setLocked: nodeify(keyringController.setLocked, keyringController), - createNewVaultAndKeychain: nodeify(keyringController.createNewVaultAndKeychain, keyringController), - createNewVaultAndRestore: nodeify(keyringController.createNewVaultAndRestore, keyringController), + createNewVaultAndKeychain: nodeify(this.createNewVaultAndKeychain, this), + createNewVaultAndRestore: nodeify(this.createNewVaultAndRestore, this), addNewKeyring: nodeify(keyringController.addNewKeyring, keyringController), saveAccountLabel: nodeify(keyringController.saveAccountLabel, keyringController), exportAccount: nodeify(keyringController.exportAccount, keyringController), @@ -473,20 +467,43 @@ module.exports = class MetamaskController extends EventEmitter { // Vault Management // - submitPassword (password, cb) { - return this.keyringController.submitPassword(password) - .then((newState) => { cb(null, newState) }) - .catch((reason) => { cb(reason) }) + async createNewVaultAndKeychain (password, cb) { + const vault = await this.keyringController.createNewVaultAndKeychain(password) + this.selectFirstIdentity(vault) + return vault + } + + async createNewVaultAndRestore (password, seed, cb) { + const vault = await this.keyringController.createNewVaultAndRestore(password, seed) + this.selectFirstIdentity(vault) + return vault + } + + selectFirstIdentity (vault) { + const { identities } = vault + const address = Object.keys(identities)[0] + this.preferencesController.setSelectedAddress(address) } // // Opinionated Keyring Management // - addNewAccount (cb) { + async addNewAccount (cb) { const primaryKeyring = this.keyringController.getKeyringsByType('HD Key Tree')[0] if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found')) - promiseToCallback(this.keyringController.addNewAccount(primaryKeyring))(cb) + const keyringController = this.keyringController + const oldAccounts = await keyringController.getAccounts() + const keyState = await keyringController.addNewAccount(primaryKeyring) + const newAccounts = await keyringController.getAccounts() + + newAccounts.forEach((address) => { + if (!oldAccounts.includes(address)) { + this.preferencesController.setSelectedAddress(address) + } + }) + + return keyState } // Adds the current vault's seed words to the UI's state tree. |