From f507f2a92710285679123c9429a37c9e330c7cac Mon Sep 17 00:00:00 2001 From: Dan Finlay <542863+danfinlay@users.noreply.github.com> Date: Mon, 25 Feb 2019 11:10:13 -0800 Subject: Feature Flag + Mobile Sync (#5955) --- app/scripts/metamask-controller.js | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 6555f179d..41c3e3642 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -56,6 +56,7 @@ const EthQuery = require('eth-query') const ethUtil = require('ethereumjs-util') const sigUtil = require('eth-sig-util') + module.exports = class MetamaskController extends EventEmitter { /** @@ -410,6 +411,9 @@ module.exports = class MetamaskController extends EventEmitter { checkHardwareStatus: nodeify(this.checkHardwareStatus, this), unlockHardwareWalletAccount: nodeify(this.unlockHardwareWalletAccount, this), + // mobile + fetchInfoToSync: nodeify(this.fetchInfoToSync, this), + // vault management submitPassword: nodeify(this.submitPassword, this), @@ -586,6 +590,60 @@ module.exports = class MetamaskController extends EventEmitter { }) } + /** + * Collects all the information that we want to share + * with the mobile client for syncing purposes + * @returns Promise Parts of the state that we want to syncx + */ + async fetchInfoToSync () { + // Preferences + const { + accountTokens, + currentLocale, + frequentRpcList, + identities, + selectedAddress, + tokens, + } = this.preferencesController.store.getState() + + const preferences = { + accountTokens, + currentLocale, + frequentRpcList, + identities, + selectedAddress, + tokens, + } + + // Accounts + const hdKeyring = this.keyringController.getKeyringsByType('HD Key Tree')[0] + const hdAccounts = await hdKeyring.getAccounts() + const accounts = { + hd: hdAccounts.filter((item, pos) => (hdAccounts.indexOf(item) === pos)).map(address => ethUtil.toChecksumAddress(address)), + simpleKeyPair: [], + ledger: [], + trezor: [], + } + + // transactions + + let transactions = this.txController.store.getState().transactions + // delete tx for other accounts that we're not importing + transactions = transactions.filter(tx => { + const checksummedTxFrom = ethUtil.toChecksumAddress(tx.txParams.from) + return ( + accounts.hd.includes(checksummedTxFrom) + ) + }) + + return { + accounts, + preferences, + transactions, + network: this.networkController.store.getState(), + } + } + /* * Submits the user's password and attempts to unlock the vault. * Also synchronizes the preferencesController, to ensure its schema -- cgit