From 8012ede12698477692b80769781096b559159a32 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 11 Jan 2017 19:04:19 -0800 Subject: background - introduce ObservableStore --- app/scripts/metamask-controller.js | 53 ++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 25 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 1fc97e81d..8e0eaf54c 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -6,26 +6,38 @@ const KeyringController = require('./keyring-controller') const NoticeController = require('./notice-controller') const messageManager = require('./lib/message-manager') const TxManager = require('./transaction-manager') -const HostStore = require('./lib/remote-store.js').HostStore const Web3 = require('web3') const ConfigManager = require('./lib/config-manager') const extension = require('./lib/extension') const autoFaucet = require('./lib/auto-faucet') const nodeify = require('./lib/nodeify') const IdStoreMigrator = require('./lib/idStore-migrator') +const ObservableStore = require('./lib/observable/') +const HostStore = require('./lib/observable/host') +const transformStore = require('./lib/observable/util/transform') const version = require('../manifest.json').version module.exports = class MetamaskController extends EventEmitter { constructor (opts) { super() - this.state = { network: 'loading' } this.opts = opts - this.configManager = new ConfigManager(opts) + this.state = { network: 'loading' } + + // observable state store + this.store = new ObservableStore(opts.initState) + // config manager + this.configManager = new ConfigManager({ + store: this.store, + }) + // key mgmt this.keyringController = new KeyringController({ configManager: this.configManager, getNetwork: this.getStateNetwork.bind(this), }) + this.keyringController.on('newAccount', (account) => { + autoFaucet(account) + }) // notices this.noticeController = new NoticeController({ configManager: this.configManager, @@ -228,29 +240,20 @@ module.exports = class MetamaskController extends EventEmitter { initPublicConfigStore () { // get init state - var initPublicState = configToPublic(this.configManager.getConfig()) - var publicConfigStore = new HostStore(initPublicState) - - // subscribe to changes - this.configManager.subscribe(function (state) { - storeSetFromObj(publicConfigStore, configToPublic(state)) - }) - - this.keyringController.on('newAccount', (account) => { - autoFaucet(account) - }) - - // config substate - function configToPublic (state) { - return { - selectedAccount: state.selectedAccount, + var initPublicState = this.store.get() + var publicConfigStore = new HostStore(initPublicState, { readOnly: true }) + + // sync publicConfigStore with transform + transformStore(this.store, publicConfigStore, selectPublicState) + + function selectPublicState(state) { + let result = { selectedAccount: undefined } + try { + result.selectedAccount = state.data.config.selectedAccount + } catch (err) { + console.warn('Error in "selectPublicState": ' + err.message) } - } - // dump obj into store - function storeSetFromObj (store, obj) { - Object.keys(obj).forEach(function (key) { - store.set(key, obj[key]) - }) + return result } return publicConfigStore -- cgit v1.2.3 From 3bc996878b467e1fa5fd63656bd465377daa137d Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 11 Jan 2017 22:47:56 -0800 Subject: background - move pojo migrator to outside of metamask controller --- app/scripts/metamask-controller.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 8e0eaf54c..e15844a56 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -14,7 +14,7 @@ const nodeify = require('./lib/nodeify') const IdStoreMigrator = require('./lib/idStore-migrator') const ObservableStore = require('./lib/observable/') const HostStore = require('./lib/observable/host') -const transformStore = require('./lib/observable/util/transform') +const synchronizeStore = require('./lib/observable/util/sync') const version = require('../manifest.json').version module.exports = class MetamaskController extends EventEmitter { @@ -244,12 +244,12 @@ module.exports = class MetamaskController extends EventEmitter { var publicConfigStore = new HostStore(initPublicState, { readOnly: true }) // sync publicConfigStore with transform - transformStore(this.store, publicConfigStore, selectPublicState) + synchronizeStore(this.store, publicConfigStore, selectPublicState) function selectPublicState(state) { let result = { selectedAccount: undefined } try { - result.selectedAccount = state.data.config.selectedAccount + result.selectedAccount = state.config.selectedAccount } catch (err) { console.warn('Error in "selectPublicState": ' + err.message) } -- cgit v1.2.3 From b52346388b8d4518ffb2eb34236c6d17579085f3 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 18 Jan 2017 15:17:08 -0800 Subject: Added new modular private key import system Now any strategy for importing a private key that can be described as a pure function can be very easily turned into a MetaMask import strategy. I've created a generic and reusable UI action called `importNewAccount(strategy, args)`. The `strategy` is a unique identifier defined in `app/scripts/account-import-strategies`, and the `args` will be passed to the member of the `strategies` array whose key matches the strategy string. Strategies return private key hex strings, and are used by the metamask-controller to create a new keyring, and select that new account, before calling back. This also implements @frankiebee's idea of showing the imported account when it's been imported (my oversight!). This commit only moves us to this architecture, keeping feature parity for private key import, but has some untested code for importing geth-style JSON files as well! --- app/scripts/metamask-controller.js | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 629216e42..7084bbc2d 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -13,6 +13,7 @@ const extension = require('./lib/extension') const autoFaucet = require('./lib/auto-faucet') const nodeify = require('./lib/nodeify') const IdStoreMigrator = require('./lib/idStore-migrator') +const accountImporter = require('./account-import-strategies') const version = require('../manifest.json').version module.exports = class MetamaskController extends EventEmitter { @@ -121,6 +122,16 @@ module.exports = class MetamaskController extends EventEmitter { .then((newState) => { cb(null, newState) }) .catch((reason) => { cb(reason) }) }, + importAccountWithStrategy: (strategy, args, cb) => { + accountImporter.importAccount(strategy, args) + .then((privateKey) => { + return keyringController.addNewKeyring('Simple Key Pair', [ privateKey ]) + }) + .then(keyring => keyring.getAccounts()) + .then((accounts) => keyringController.setSelectedAccount(accounts[0])) + .then(() => { cb(null, keyringController.fullUpdate()) }) + .catch((reason) => { cb(reason) }) + }, addNewAccount: nodeify(keyringController.addNewAccount).bind(keyringController), setSelectedAccount: nodeify(keyringController.setSelectedAccount).bind(keyringController), saveAccountLabel: nodeify(keyringController.saveAccountLabel).bind(keyringController), -- cgit v1.2.3 From 8642ced310890c7a3202a2826a2c74fad1fefca3 Mon Sep 17 00:00:00 2001 From: Frankie Date: Tue, 24 Jan 2017 12:06:59 -0800 Subject: Fix issue where generating a new account would put it in loose keys --- app/scripts/metamask-controller.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 629216e42..6b6424f2a 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1,5 +1,6 @@ const EventEmitter = require('events') const extend = require('xtend') +const promiseToCallback = require('promise-to-callback') const EthStore = require('./lib/eth-store') const MetaMaskProvider = require('web3-provider-engine/zero.js') const KeyringController = require('./keyring-controller') @@ -121,7 +122,11 @@ module.exports = class MetamaskController extends EventEmitter { .then((newState) => { cb(null, newState) }) .catch((reason) => { cb(reason) }) }, - addNewAccount: nodeify(keyringController.addNewAccount).bind(keyringController), + addNewAccount: (cb) => { + const primaryKeyring = keyringController.getKeyringsByType('HD Key Tree')[0] + if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found')) + promiseToCallback(keyringController.addNewAccount(primaryKeyring))(cb) + }, setSelectedAccount: nodeify(keyringController.setSelectedAccount).bind(keyringController), saveAccountLabel: nodeify(keyringController.saveAccountLabel).bind(keyringController), exportAccount: nodeify(keyringController.exportAccount).bind(keyringController), -- cgit v1.2.3 From 48ffea0142bf68a49f30887b3509786d7e751895 Mon Sep 17 00:00:00 2001 From: Frankie Date: Tue, 24 Jan 2017 12:28:05 -0800 Subject: Move the assumption of their only being one hd keyring when requesting seed words to metamaskController --- app/scripts/metamask-controller.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 6b6424f2a..a235be75b 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -107,7 +107,11 @@ module.exports = class MetamaskController extends EventEmitter { // forward directly to keyringController createNewVaultAndKeychain: nodeify(keyringController.createNewVaultAndKeychain).bind(keyringController), createNewVaultAndRestore: nodeify(keyringController.createNewVaultAndRestore).bind(keyringController), - placeSeedWords: nodeify(keyringController.placeSeedWords).bind(keyringController), + placeSeedWords: (cb) => { + const primaryKeyring = keyringController.getKeyringsByType('HD Key Tree')[0] + if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found')) + promiseToCallback(keyringController.placeSeedWords(primaryKeyring))(cb) + }, clearSeedWordCache: nodeify(keyringController.clearSeedWordCache).bind(keyringController), setLocked: nodeify(keyringController.setLocked).bind(keyringController), submitPassword: (password, cb) => { -- cgit v1.2.3 From 463a56ff54b0d850c86348e260e5f7c17b138ccb Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 24 Jan 2017 15:33:33 -0800 Subject: background controller - extract KeyringC.placeSeedWords to MetamaskC --- app/scripts/metamask-controller.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index a235be75b..a1bb9a923 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -107,10 +107,19 @@ module.exports = class MetamaskController extends EventEmitter { // forward directly to keyringController createNewVaultAndKeychain: nodeify(keyringController.createNewVaultAndKeychain).bind(keyringController), createNewVaultAndRestore: nodeify(keyringController.createNewVaultAndRestore).bind(keyringController), + // Adds the current vault's seed words to the UI's state tree. + // + // Used when creating a first vault, to allow confirmation. + // Also used when revealing the seed words in the confirmation view. placeSeedWords: (cb) => { const primaryKeyring = keyringController.getKeyringsByType('HD Key Tree')[0] if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found')) - promiseToCallback(keyringController.placeSeedWords(primaryKeyring))(cb) + primaryKeyring.serialize() + .then((serialized) => { + const seedWords = serialized.mnemonic + this.configManager.setSeedWords(seedWords) + promiseToCallback(this.keyringController.fullUpdate())(cb) + }) }, clearSeedWordCache: nodeify(keyringController.clearSeedWordCache).bind(keyringController), setLocked: nodeify(keyringController.setLocked).bind(keyringController), -- cgit v1.2.3 From 76ce348a04b83693eda0e8a40f9888c1f5fe7ef5 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 24 Jan 2017 19:47:00 -0800 Subject: obs-store - use published module --- app/scripts/metamask-controller.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index d3077817d..8f157a45e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1,6 +1,9 @@ const EventEmitter = require('events') const extend = require('xtend') const promiseToCallback = require('promise-to-callback') +const pipe = require('pump') +const ObservableStore = require('obs-store') +const storeTransform = require('obs-store/lib/transform') const EthStore = require('./lib/eth-store') const MetaMaskProvider = require('web3-provider-engine/zero.js') const KeyringController = require('./keyring-controller') @@ -13,9 +16,6 @@ const extension = require('./lib/extension') const autoFaucet = require('./lib/auto-faucet') const nodeify = require('./lib/nodeify') const IdStoreMigrator = require('./lib/idStore-migrator') -const ObservableStore = require('./lib/observable/') -const HostStore = require('./lib/observable/host') -const synchronizeStore = require('./lib/observable/util/sync') const accountImporter = require('./account-import-strategies') const version = require('../manifest.json').version @@ -258,18 +258,21 @@ module.exports = class MetamaskController extends EventEmitter { initPublicConfigStore () { // get init state - var initPublicState = this.store.get() - var publicConfigStore = new HostStore(initPublicState, { readOnly: true }) + const publicConfigStore = new ObservableStore() // sync publicConfigStore with transform - synchronizeStore(this.store, publicConfigStore, selectPublicState) + pipe( + this.store, + storeTransform(selectPublicState), + publicConfigStore + ) function selectPublicState(state) { - let result = { selectedAccount: undefined } + const result = { selectedAccount: undefined } try { result.selectedAccount = state.config.selectedAccount - } catch (err) { - console.warn('Error in "selectPublicState": ' + err.message) + } catch (_) { + // thats fine, im sure it will be there next time... } return result } @@ -314,9 +317,11 @@ module.exports = class MetamaskController extends EventEmitter { this.opts.showUnconfirmedMessage(msgParams, msgId) } - setupPublicConfig (stream) { - var storeStream = this.publicConfigStore.createStream() - stream.pipe(storeStream).pipe(stream) + setupPublicConfig (outStream) { + pipe( + this.publicConfigStore, + outStream + ) } // Log blocks -- cgit v1.2.3 From af115aa4c12849b147224cd0d167f5a57565ed6d Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 26 Jan 2017 15:09:31 -0800 Subject: background - organize metamask instantiation --- app/scripts/metamask-controller.js | 69 +++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 34 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 8f157a45e..347fa60dc 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -5,12 +5,12 @@ const pipe = require('pump') const ObservableStore = require('obs-store') const storeTransform = require('obs-store/lib/transform') const EthStore = require('./lib/eth-store') +const EthQuery = require('eth-query') const MetaMaskProvider = require('web3-provider-engine/zero.js') const KeyringController = require('./keyring-controller') const NoticeController = require('./notice-controller') const messageManager = require('./lib/message-manager') const TxManager = require('./transaction-manager') -const Web3 = require('web3') const ConfigManager = require('./lib/config-manager') const extension = require('./lib/extension') const autoFaucet = require('./lib/auto-faucet') @@ -29,30 +29,33 @@ module.exports = class MetamaskController extends EventEmitter { // observable state store this.store = new ObservableStore(opts.initState) + // config manager this.configManager = new ConfigManager({ 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 this.keyringController = new KeyringController({ + ethStore: this.ethStore, configManager: this.configManager, getNetwork: this.getStateNetwork.bind(this), }) this.keyringController.on('newAccount', (account) => { autoFaucet(account) }) - // notices - this.noticeController = new NoticeController({ - 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 + + // tx mgmt this.txManager = new TxManager({ txList: this.configManager.getTxList(), txHistoryLimit: 40, @@ -64,11 +67,18 @@ module.exports = class MetamaskController extends EventEmitter { provider: 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.configManager.setCurrentFiat(currentFiat) - this.configManager.updateConversionRate() + this.getNetwork() + this.messageManager = messageManager + this.publicConfigStore = this.initPublicConfigStore() this.checkTOSChange() @@ -79,6 +89,7 @@ module.exports = class MetamaskController extends EventEmitter { configManager: this.configManager, }) + // manual state subscriptions this.ethStore.on('update', this.sendUpdate.bind(this)) this.keyringController.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) { - const keyringController = this.keyringController - - var providerOpts = { + initializeProvider () { + let provider = MetaMaskProvider({ static: { eth_syncing: false, web3_clientVersion: `MetaMask/v${version}`, @@ -232,8 +241,8 @@ module.exports = class MetamaskController extends EventEmitter { rpcUrl: this.configManager.getCurrentRpcAddress(), // account mgmt getAccounts: (cb) => { - var selectedAccount = this.configManager.getSelectedAccount() - var result = selectedAccount ? [selectedAccount] : [] + let selectedAccount = this.configManager.getSelectedAccount() + let result = selectedAccount ? [selectedAccount] : [] cb(null, result) }, // tx signing @@ -241,18 +250,10 @@ module.exports = class MetamaskController extends EventEmitter { // msg signing approveMessage: this.newUnsignedMessage.bind(this), signMessage: (...args) => { - keyringController.signMessage(...args) + this.keyringController.signMessage(...args) 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 } @@ -449,7 +450,7 @@ module.exports = class MetamaskController extends EventEmitter { this.sendUpdate() } - this.web3.version.getNetwork((err, network) => { + this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => { if (err) { this.state.network = 'loading' return this.sendUpdate() -- cgit v1.2.3 From dc86ddebb77fed06a2857ef7a6031a45724740a9 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 26 Jan 2017 16:07:35 -0800 Subject: background - graceful publicConfigStore connection teardown --- app/scripts/metamask-controller.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 347fa60dc..3e6ce0a2e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -321,7 +321,9 @@ module.exports = class MetamaskController extends EventEmitter { setupPublicConfig (outStream) { pipe( this.publicConfigStore, - outStream + outStream, + // cleanup on disconnect + () => this.publicConfigStore.unpipe(outStream) ) } -- cgit v1.2.3 From 2f79781ab9fbdde018fc921da9be0b3411ed8cad Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 26 Jan 2017 20:17:36 -0800 Subject: metamask controller - adopt clearSeedWords from keyring controller --- app/scripts/metamask-controller.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 3e6ce0a2e..83dc2a9b9 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -146,7 +146,14 @@ module.exports = class MetamaskController extends EventEmitter { promiseToCallback(this.keyringController.fullUpdate())(cb) }) }, - clearSeedWordCache: nodeify(keyringController.clearSeedWordCache).bind(keyringController), + // ClearSeedWordCache + // + // Removes the primary account's seed words from the UI's state tree, + // ensuring they are only ever available in the background process. + clearSeedWordCache: (cb) => { + this.configManager.setSeedWords(null) + cb(null, this.configManager.getSelectedAccount()) + }, setLocked: nodeify(keyringController.setLocked).bind(keyringController), submitPassword: (password, cb) => { this.migrateOldVaultIfAny(password) @@ -321,9 +328,7 @@ module.exports = class MetamaskController extends EventEmitter { setupPublicConfig (outStream) { pipe( this.publicConfigStore, - outStream, - // cleanup on disconnect - () => this.publicConfigStore.unpipe(outStream) + outStream ) } -- cgit v1.2.3 From 2113979be7ee257c2650916c2c48f68c6390ef1f Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 26 Jan 2017 20:52:46 -0800 Subject: metamask controller - adopt setup*Communication from background --- app/scripts/metamask-controller.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 83dc2a9b9..1429c02ca 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -2,11 +2,13 @@ const EventEmitter = require('events') const extend = require('xtend') const promiseToCallback = require('promise-to-callback') const pipe = require('pump') +const Dnode = require('dnode') const ObservableStore = require('obs-store') const storeTransform = require('obs-store/lib/transform') const EthStore = require('./lib/eth-store') const EthQuery = require('eth-query') const MetaMaskProvider = require('web3-provider-engine/zero.js') +const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const KeyringController = require('./keyring-controller') const NoticeController = require('./notice-controller') const messageManager = require('./lib/message-manager') @@ -202,8 +204,35 @@ module.exports = class MetamaskController extends EventEmitter { } } - setupProviderConnection (stream, originDomain) { - stream.on('data', this.onRpcRequest.bind(this, stream, originDomain)) + setupUntrustedCommunication (connectionStream, originDomain) { + // setup multiplexing + var mx = setupMultiplex(connectionStream) + // connect features + this.setupProviderConnection(mx.createStream('provider'), originDomain) + this.setupPublicConfig(mx.createStream('publicConfig')) + } + + setupTrustedCommunication (connectionStream, originDomain) { + // setup multiplexing + var mx = setupMultiplex(connectionStream) + // connect features + this.setupControllerConnection(mx.createStream('controller')) + this.setupProviderConnection(mx.createStream('provider'), originDomain) + } + + setupControllerConnection (outStream) { + const api = this.getApi() + const dnode = Dnode(api) + outStream.pipe(dnode).pipe(outStream) + dnode.on('remote', (remote) => { + // push updates to popup + const sendUpdate = remote.sendUpdate.bind(remote) + this.on('update', sendUpdate) + }) + } + + setupProviderConnection (outStream, originDomain) { + outStream.on('data', this.onRpcRequest.bind(this, outStream, originDomain)) } onRpcRequest (stream, originDomain, request) { -- cgit v1.2.3 From 832772414e8dba08e0fcfefea59a4e24b1f4a0d3 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 26 Jan 2017 21:18:17 -0800 Subject: metamask - use web3-stream-provider/handler instead of manual wiring --- app/scripts/metamask-controller.js | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 1429c02ca..ec5fd498b 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -7,6 +7,7 @@ const ObservableStore = require('obs-store') const storeTransform = require('obs-store/lib/transform') const EthStore = require('./lib/eth-store') const EthQuery = require('eth-query') +const streamIntoProvider = require('web3-stream-provider/handler') const MetaMaskProvider = require('web3-provider-engine/zero.js') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const KeyringController = require('./keyring-controller') @@ -232,31 +233,15 @@ module.exports = class MetamaskController extends EventEmitter { } setupProviderConnection (outStream, originDomain) { - outStream.on('data', this.onRpcRequest.bind(this, outStream, originDomain)) - } - - onRpcRequest (stream, originDomain, request) { - // handle rpc request - this.provider.sendAsync(request, function onPayloadHandled (err, response) { - logger(err, request, response) - if (response) { - try { - stream.write(response) - } catch (err) { - logger(err) - } - } - }) - + streamIntoProvider(outStream, originDomain, logger) function logger (err, request, response) { if (err) return console.error(err) - if (!request.isMetamaskInternal) { - if (global.METAMASK_DEBUG) { - console.log(`RPC (${originDomain}):`, request, '->', response) - } - if (response.error) { - console.error('Error in RPC response:\n', response.error) - } + if (response.error) { + console.error('Error in RPC response:\n', response.error) + } + if (request.isMetamaskInternal) return + if (global.METAMASK_DEBUG) { + console.log(`RPC (${originDomain}):`, request, '->', response) } } } -- cgit v1.2.3 From fc1b11e373851ad4ab3a3f74e7d6850aebe6ba82 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 26 Jan 2017 21:19:09 -0800 Subject: metamask - organize into sections --- app/scripts/metamask-controller.js | 110 ++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 49 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index ec5fd498b..841e35db2 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -98,6 +98,63 @@ module.exports = class MetamaskController extends EventEmitter { this.txManager.on('update', this.sendUpdate.bind(this)) } + // + // Constructor helpers + // + + initializeProvider () { + let provider = MetaMaskProvider({ + static: { + eth_syncing: false, + web3_clientVersion: `MetaMask/v${version}`, + }, + rpcUrl: this.configManager.getCurrentRpcAddress(), + // account mgmt + getAccounts: (cb) => { + let selectedAccount = this.configManager.getSelectedAccount() + let result = selectedAccount ? [selectedAccount] : [] + cb(null, result) + }, + // tx signing + processTransaction: (txParams, cb) => this.newUnapprovedTransaction(txParams, cb), + // msg signing + approveMessage: this.newUnsignedMessage.bind(this), + signMessage: (...args) => { + this.keyringController.signMessage(...args) + this.sendUpdate() + }, + }) + return provider + } + + initPublicConfigStore () { + // get init state + const publicConfigStore = new ObservableStore() + + // sync publicConfigStore with transform + pipe( + this.store, + storeTransform(selectPublicState), + publicConfigStore + ) + + function selectPublicState(state) { + const result = { selectedAccount: undefined } + try { + result.selectedAccount = state.config.selectedAccount + } catch (_) { + // thats fine, im sure it will be there next time... + } + return result + } + + return publicConfigStore + } + + // + // Constructor helpers + // + getState () { return this.keyringController.getState() .then((keyringControllerState) => { @@ -114,6 +171,10 @@ module.exports = class MetamaskController extends EventEmitter { }) } + // + // Remote Features + // + getApi () { const keyringController = this.keyringController const txManager = this.txManager @@ -253,55 +314,6 @@ module.exports = class MetamaskController extends EventEmitter { }) } - initializeProvider () { - let provider = MetaMaskProvider({ - static: { - eth_syncing: false, - web3_clientVersion: `MetaMask/v${version}`, - }, - rpcUrl: this.configManager.getCurrentRpcAddress(), - // account mgmt - getAccounts: (cb) => { - let selectedAccount = this.configManager.getSelectedAccount() - let result = selectedAccount ? [selectedAccount] : [] - cb(null, result) - }, - // tx signing - processTransaction: (txParams, cb) => this.newUnapprovedTransaction(txParams, cb), - // msg signing - approveMessage: this.newUnsignedMessage.bind(this), - signMessage: (...args) => { - this.keyringController.signMessage(...args) - this.sendUpdate() - }, - }) - return provider - } - - initPublicConfigStore () { - // get init state - const publicConfigStore = new ObservableStore() - - // sync publicConfigStore with transform - pipe( - this.store, - storeTransform(selectPublicState), - publicConfigStore - ) - - function selectPublicState(state) { - const result = { selectedAccount: undefined } - try { - result.selectedAccount = state.config.selectedAccount - } catch (_) { - // thats fine, im sure it will be there next time... - } - return result - } - - return publicConfigStore - } - newUnapprovedTransaction (txParams, cb) { const self = this self.txManager.addUnapprovedTransaction(txParams, (err, txMeta) => { -- cgit v1.2.3 From 9ab836284360607277d33513e11857d683e0708a Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 26 Jan 2017 22:30:12 -0800 Subject: metamask controller - cleanup remote api --- app/scripts/metamask-controller.js | 177 +++++++++++++++++++++---------------- 1 file changed, 100 insertions(+), 77 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 841e35db2..3ff29b202 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -152,7 +152,7 @@ module.exports = class MetamaskController extends EventEmitter { } // - // Constructor helpers + // State Management // getState () { @@ -181,87 +181,49 @@ module.exports = class MetamaskController extends EventEmitter { const noticeController = this.noticeController return { - getState: nodeify(this.getState.bind(this)), - setRpcTarget: this.setRpcTarget.bind(this), - setProviderType: this.setProviderType.bind(this), - useEtherscanProvider: this.useEtherscanProvider.bind(this), - agreeToDisclaimer: this.agreeToDisclaimer.bind(this), - resetDisclaimer: this.resetDisclaimer.bind(this), - setCurrentFiat: this.setCurrentFiat.bind(this), - setTOSHash: this.setTOSHash.bind(this), - checkTOSChange: this.checkTOSChange.bind(this), - setGasMultiplier: this.setGasMultiplier.bind(this), - markAccountsFound: this.markAccountsFound.bind(this), - - // forward directly to keyringController - createNewVaultAndKeychain: nodeify(keyringController.createNewVaultAndKeychain).bind(keyringController), - createNewVaultAndRestore: nodeify(keyringController.createNewVaultAndRestore).bind(keyringController), - // Adds the current vault's seed words to the UI's state tree. - // - // Used when creating a first vault, to allow confirmation. - // Also used when revealing the seed words in the confirmation view. - placeSeedWords: (cb) => { - const primaryKeyring = keyringController.getKeyringsByType('HD Key Tree')[0] - if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found')) - primaryKeyring.serialize() - .then((serialized) => { - const seedWords = serialized.mnemonic - this.configManager.setSeedWords(seedWords) - promiseToCallback(this.keyringController.fullUpdate())(cb) - }) - }, - // ClearSeedWordCache - // - // Removes the primary account's seed words from the UI's state tree, - // ensuring they are only ever available in the background process. - clearSeedWordCache: (cb) => { - this.configManager.setSeedWords(null) - cb(null, this.configManager.getSelectedAccount()) - }, - setLocked: nodeify(keyringController.setLocked).bind(keyringController), - submitPassword: (password, cb) => { - this.migrateOldVaultIfAny(password) - .then(keyringController.submitPassword.bind(keyringController, password)) - .then((newState) => { cb(null, newState) }) - .catch((reason) => { cb(reason) }) - }, - addNewKeyring: (type, opts, cb) => { - keyringController.addNewKeyring(type, opts) - .then(() => keyringController.fullUpdate()) - .then((newState) => { cb(null, newState) }) - .catch((reason) => { cb(reason) }) - }, - addNewAccount: (cb) => { - const primaryKeyring = keyringController.getKeyringsByType('HD Key Tree')[0] - if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found')) - promiseToCallback(keyringController.addNewAccount(primaryKeyring))(cb) - }, - importAccountWithStrategy: (strategy, args, cb) => { - accountImporter.importAccount(strategy, args) - .then((privateKey) => { - return keyringController.addNewKeyring('Simple Key Pair', [ privateKey ]) - }) - .then(keyring => keyring.getAccounts()) - .then((accounts) => keyringController.setSelectedAccount(accounts[0])) - .then(() => { cb(null, keyringController.fullUpdate()) }) - .catch((reason) => { cb(reason) }) - }, - setSelectedAccount: nodeify(keyringController.setSelectedAccount).bind(keyringController), - saveAccountLabel: nodeify(keyringController.saveAccountLabel).bind(keyringController), - exportAccount: nodeify(keyringController.exportAccount).bind(keyringController), - - // signing methods - approveTransaction: txManager.approveTransaction.bind(txManager), - cancelTransaction: txManager.cancelTransaction.bind(txManager), - signMessage: keyringController.signMessage.bind(keyringController), - cancelMessage: keyringController.cancelMessage.bind(keyringController), - + // etc + getState: nodeify(this.getState.bind(this)), + setRpcTarget: this.setRpcTarget.bind(this), + setProviderType: this.setProviderType.bind(this), + useEtherscanProvider: this.useEtherscanProvider.bind(this), + agreeToDisclaimer: this.agreeToDisclaimer.bind(this), + resetDisclaimer: this.resetDisclaimer.bind(this), + setCurrentFiat: this.setCurrentFiat.bind(this), + setTOSHash: this.setTOSHash.bind(this), + checkTOSChange: this.checkTOSChange.bind(this), + setGasMultiplier: this.setGasMultiplier.bind(this), + markAccountsFound: this.markAccountsFound.bind(this), // coinbase buyEth: this.buyEth.bind(this), // shapeshift createShapeShiftTx: this.createShapeShiftTx.bind(this), + + // primary HD keyring management + addNewAccount: this.addNewAccount.bind(this), + placeSeedWords: this.placeSeedWords.bind(this), + clearSeedWordCache: this.clearSeedWordCache.bind(this), + importAccountWithStrategy: this.importAccountWithStrategy.bind(this), + + // vault management + submitPassword: this.submitPassword.bind(this), + + // KeyringController + setLocked: nodeify(keyringController.setLocked).bind(keyringController), + createNewVaultAndKeychain: nodeify(keyringController.createNewVaultAndKeychain).bind(keyringController), + createNewVaultAndRestore: nodeify(keyringController.createNewVaultAndRestore).bind(keyringController), + addNewKeyring: nodeify(keyringController.addNewKeyring).bind(keyringController), + setSelectedAccount: nodeify(keyringController.setSelectedAccount).bind(keyringController), + saveAccountLabel: nodeify(keyringController.saveAccountLabel).bind(keyringController), + exportAccount: nodeify(keyringController.exportAccount).bind(keyringController), + + // signing methods + approveTransaction: txManager.approveTransaction.bind(txManager), + cancelTransaction: txManager.cancelTransaction.bind(txManager), + signMessage: keyringController.signMessage.bind(keyringController), + cancelMessage: keyringController.cancelMessage.bind(keyringController), + // notices - checkNotices: noticeController.updateNoticesList.bind(noticeController), + checkNotices: noticeController.updateNoticesList.bind(noticeController), markNoticeRead: noticeController.markNoticeRead.bind(noticeController), } } @@ -314,6 +276,67 @@ module.exports = class MetamaskController extends EventEmitter { }) } + // + // Vault Management + // + + submitPassword (password, cb) { + this.migrateOldVaultIfAny(password) + .then(this.keyringController.submitPassword.bind(this.keyringController, password)) + .then((newState) => { cb(null, newState) }) + .catch((reason) => { cb(reason) }) + } + + // + // Opinionated Keyring Management + // + + 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) + } + + // Adds the current vault's seed words to the UI's state tree. + // + // Used when creating a first vault, to allow confirmation. + // Also used when revealing the seed words in the confirmation view. + placeSeedWords (cb) { + const primaryKeyring = this.keyringController.getKeyringsByType('HD Key Tree')[0] + if (!primaryKeyring) return cb(new Error('MetamaskController - No HD Key Tree found')) + primaryKeyring.serialize() + .then((serialized) => { + const seedWords = serialized.mnemonic + this.configManager.setSeedWords(seedWords) + promiseToCallback(this.keyringController.fullUpdate())(cb) + }) + } + + // ClearSeedWordCache + // + // Removes the primary account's seed words from the UI's state tree, + // ensuring they are only ever available in the background process. + clearSeedWordCache (cb) { + this.configManager.setSeedWords(null) + cb(null, this.configManager.getSelectedAccount()) + } + + importAccountWithStrategy (strategy, args, cb) { + accountImporter.importAccount(strategy, args) + .then((privateKey) => { + return this.keyringController.addNewKeyring('Simple Key Pair', [ privateKey ]) + }) + .then(keyring => keyring.getAccounts()) + .then((accounts) => this.keyringController.setSelectedAccount(accounts[0])) + .then(() => { cb(null, this.keyringController.fullUpdate()) }) + .catch((reason) => { cb(reason) }) + } + + + // + // Identity Management + // + newUnapprovedTransaction (txParams, cb) { const self = this self.txManager.addUnapprovedTransaction(txParams, (err, txMeta) => { -- cgit v1.2.3 From efcd22905a585d8c2ae55ec4e8bd6d9b027b740d Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 26 Jan 2017 23:03:11 -0800 Subject: metamask - fix setupProviderConnection --- app/scripts/metamask-controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 3ff29b202..3ce9c2373 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -256,7 +256,7 @@ module.exports = class MetamaskController extends EventEmitter { } setupProviderConnection (outStream, originDomain) { - streamIntoProvider(outStream, originDomain, logger) + streamIntoProvider(outStream, this.provider, logger) function logger (err, request, response) { if (err) return console.error(err) if (response.error) { -- cgit v1.2.3 From ad9531d521057da3540a47bfa592dee6fd225d2e Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 27 Jan 2017 19:35:03 -0800 Subject: metamask - organize methods --- app/scripts/metamask-controller.js | 246 +++++++++++++++++++------------------ 1 file changed, 126 insertions(+), 120 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 3ce9c2373..30fc61168 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -41,7 +41,7 @@ module.exports = class MetamaskController extends EventEmitter { // rpc provider this.provider = this.initializeProvider(opts) - this.provider.on('block', this.processBlock.bind(this)) + this.provider.on('block', this.logBlock.bind(this)) this.provider.on('error', this.getNetwork.bind(this)) // eth data query tools @@ -142,9 +142,7 @@ module.exports = class MetamaskController extends EventEmitter { const result = { selectedAccount: undefined } try { result.selectedAccount = state.config.selectedAccount - } catch (_) { - // thats fine, im sure it will be there next time... - } + } catch (_) {} return result } @@ -269,6 +267,13 @@ module.exports = class MetamaskController extends EventEmitter { } } + setupPublicConfig (outStream) { + pipe( + this.publicConfigStore, + outStream + ) + } + sendUpdate () { this.getState() .then((state) => { @@ -374,31 +379,90 @@ module.exports = class MetamaskController extends EventEmitter { this.opts.showUnconfirmedMessage(msgParams, msgId) } - setupPublicConfig (outStream) { - pipe( - this.publicConfigStore, - outStream - ) + + markAccountsFound (cb) { + this.configManager.setLostAccounts([]) + this.sendUpdate() + cb(null, this.getState()) } - // Log blocks - processBlock (block) { - if (global.METAMASK_DEBUG) { - console.log(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`) + // Migrate Old Vault If Any + // @string password + // + // returns Promise() + // + // Temporary step used when logging in. + // Checks if old style (pre-3.0.0) Metamask Vault exists. + // If so, persists that vault in the new vault format + // with the provided password, so the other unlock steps + // may be completed without interruption. + migrateOldVaultIfAny (password) { + + if (!this.checkIfShouldMigrate()) { + return Promise.resolve(password) } - this.verifyNetwork() + + const keyringController = this.keyringController + + return this.idStoreMigrator.migratedVaultForPassword(password) + .then(this.restoreOldVaultAccounts.bind(this)) + .then(this.restoreOldLostAccounts.bind(this)) + .then(keyringController.persistAllKeyrings.bind(keyringController, password)) + .then(() => password) } - verifyNetwork () { - // Check network when restoring connectivity: - if (this.state.network === 'loading') { - this.getNetwork() + checkIfShouldMigrate() { + return !!this.configManager.getWallet() && !this.configManager.getVault() + } + + restoreOldVaultAccounts(migratorOutput) { + const { serialized } = migratorOutput + return this.keyringController.restoreKeyring(serialized) + .then(() => migratorOutput) + } + + restoreOldLostAccounts(migratorOutput) { + const { lostAccounts } = migratorOutput + if (lostAccounts) { + this.configManager.setLostAccounts(lostAccounts.map(acct => acct.address)) + return this.importLostAccounts(migratorOutput) } + return Promise.resolve(migratorOutput) } - // config + // IMPORT LOST ACCOUNTS + // @Object with key lostAccounts: @Array accounts <{ address, privateKey }> + // Uses the array's private keys to create a new Simple Key Pair keychain + // and add it to the keyring controller. + importLostAccounts ({ lostAccounts }) { + const privKeys = lostAccounts.map(acct => acct.privateKey) + return this.keyringController.restoreKeyring({ + type: 'Simple Key Pair', + data: privKeys, + }) + } + + // + // disclaimer // + agreeToDisclaimer (cb) { + try { + this.configManager.setConfirmedDisclaimer(true) + cb() + } catch (err) { + cb(err) + } + } + + resetDisclaimer () { + try { + this.configManager.setConfirmedDisclaimer(false) + } catch (e) { + console.error(e) + } + } + setTOSHash (hash) { try { this.configManager.setTOSHash(hash) @@ -419,23 +483,16 @@ module.exports = class MetamaskController extends EventEmitter { } } - // disclaimer - - agreeToDisclaimer (cb) { - try { - this.configManager.setConfirmedDisclaimer(true) - cb() - } catch (err) { - cb(err) - } - } + // + // config + // - resetDisclaimer () { - try { - this.configManager.setConfirmedDisclaimer(false) - } catch (e) { - console.error(e) + // Log blocks + logBlock (block) { + if (global.METAMASK_DEBUG) { + console.log(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`) } + this.verifyNetwork() } setCurrentFiat (fiat, cb) { @@ -463,24 +520,6 @@ module.exports = class MetamaskController extends EventEmitter { }, 300000) } - // called from popup - setRpcTarget (rpcTarget) { - this.configManager.setRpcTarget(rpcTarget) - extension.runtime.reload() - this.getNetwork() - } - - setProviderType (type) { - this.configManager.setProviderType(type) - extension.runtime.reload() - this.getNetwork() - } - - useEtherscanProvider () { - this.configManager.useEtherscanProvider() - extension.runtime.reload() - } - buyEth (address, amount) { if (!amount) amount = '5' @@ -500,25 +539,6 @@ module.exports = class MetamaskController extends EventEmitter { this.configManager.createShapeShiftTx(depositAddress, depositType) } - getNetwork (err) { - if (err) { - this.state.network = 'loading' - this.sendUpdate() - } - - this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => { - if (err) { - this.state.network = 'loading' - return this.sendUpdate() - } - if (global.METAMASK_DEBUG) { - console.log('web3.getNetwork returned ' + network) - } - this.state.network = network - this.sendUpdate() - }) - } - setGasMultiplier (gasMultiplier, cb) { try { this.configManager.setGasMultiplier(gasMultiplier) @@ -528,69 +548,55 @@ module.exports = class MetamaskController extends EventEmitter { } } - getStateNetwork () { - return this.state.network - } - - markAccountsFound (cb) { - this.configManager.setLostAccounts([]) - this.sendUpdate() - cb(null, this.getState()) - } - - // Migrate Old Vault If Any - // @string password // - // returns Promise() + // network // - // Temporary step used when logging in. - // Checks if old style (pre-3.0.0) Metamask Vault exists. - // If so, persists that vault in the new vault format - // with the provided password, so the other unlock steps - // may be completed without interruption. - migrateOldVaultIfAny (password) { - if (!this.checkIfShouldMigrate()) { - return Promise.resolve(password) + verifyNetwork () { + // Check network when restoring connectivity: + if (this.state.network === 'loading') { + this.getNetwork() } + } - const keyringController = this.keyringController + setRpcTarget (rpcTarget) { + this.configManager.setRpcTarget(rpcTarget) + extension.runtime.reload() + this.getNetwork() + } - return this.idStoreMigrator.migratedVaultForPassword(password) - .then(this.restoreOldVaultAccounts.bind(this)) - .then(this.restoreOldLostAccounts.bind(this)) - .then(keyringController.persistAllKeyrings.bind(keyringController, password)) - .then(() => password) + setProviderType (type) { + this.configManager.setProviderType(type) + extension.runtime.reload() + this.getNetwork() } - checkIfShouldMigrate() { - return !!this.configManager.getWallet() && !this.configManager.getVault() + useEtherscanProvider () { + this.configManager.useEtherscanProvider() + extension.runtime.reload() } - restoreOldVaultAccounts(migratorOutput) { - const { serialized } = migratorOutput - return this.keyringController.restoreKeyring(serialized) - .then(() => migratorOutput) + getStateNetwork () { + return this.state.network } - restoreOldLostAccounts(migratorOutput) { - const { lostAccounts } = migratorOutput - if (lostAccounts) { - this.configManager.setLostAccounts(lostAccounts.map(acct => acct.address)) - return this.importLostAccounts(migratorOutput) + getNetwork (err) { + if (err) { + this.state.network = 'loading' + this.sendUpdate() } - return Promise.resolve(migratorOutput) - } - // IMPORT LOST ACCOUNTS - // @Object with key lostAccounts: @Array accounts <{ address, privateKey }> - // Uses the array's private keys to create a new Simple Key Pair keychain - // and add it to the keyring controller. - importLostAccounts ({ lostAccounts }) { - const privKeys = lostAccounts.map(acct => acct.privateKey) - return this.keyringController.restoreKeyring({ - type: 'Simple Key Pair', - data: privKeys, + this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => { + if (err) { + this.state.network = 'loading' + return this.sendUpdate() + } + if (global.METAMASK_DEBUG) { + console.log('web3.getNetwork returned ' + network) + } + this.state.network = network + this.sendUpdate() }) } + } -- cgit v1.2.3 From 1fd17467442fb60f308b116fea53f87a4cc16dca Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 27 Jan 2017 23:05:29 -0800 Subject: metamask - adopt shapeshift txs from keyringController --- app/scripts/metamask-controller.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 30fc61168..bef791ec9 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -162,7 +162,9 @@ module.exports = class MetamaskController extends EventEmitter { this.configManager.getConfig(), this.txManager.getState(), keyringControllerState, - this.noticeController.getState(), { + this.noticeController.getState(), + { + shapeShiftTxList: this.configManager.getShapeShiftTxList(), lostAccounts: this.configManager.getLostAccounts(), } ) -- cgit v1.2.3 From e9cdbf4f89085cc6c31991416eaac7ca91f9c1d4 Mon Sep 17 00:00:00 2001 From: kumavis Date: Sat, 28 Jan 2017 13:12:12 -0800 Subject: keyring - add and initialize obs-store --- app/scripts/metamask-controller.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index bef791ec9..f3da6f53e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -29,9 +29,10 @@ module.exports = class MetamaskController extends EventEmitter { super() this.opts = opts this.state = { network: 'loading' } + let initState = opts.initState || {} // observable state store - this.store = new ObservableStore(opts.initState) + this.store = new ObservableStore(initState) // config manager this.configManager = new ConfigManager({ @@ -50,6 +51,7 @@ module.exports = class MetamaskController extends EventEmitter { // key mgmt this.keyringController = new KeyringController({ + initState: initState.KeyringController, ethStore: this.ethStore, configManager: this.configManager, getNetwork: this.getStateNetwork.bind(this), -- cgit v1.2.3 From 8be68575bbef1dcc89b51355abaee90dbf018387 Mon Sep 17 00:00:00 2001 From: Frankie Date: Fri, 27 Jan 2017 16:11:59 -0800 Subject: Clean up message manger includes: Provider egine bump Remove presence of message manger in keyring controller Change the status wording fom conf to approved make Message manager a class fix messages not being apart of the badge re write message manger to better reflect controller pattern --- app/scripts/metamask-controller.js | 62 +++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 24 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 3ce9c2373..571968b65 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -12,7 +12,7 @@ const MetaMaskProvider = require('web3-provider-engine/zero.js') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const KeyringController = require('./keyring-controller') const NoticeController = require('./notice-controller') -const messageManager = require('./lib/message-manager') +const MessageManager = require('./lib/message-manager') const TxManager = require('./transaction-manager') const ConfigManager = require('./lib/config-manager') const extension = require('./lib/extension') @@ -32,7 +32,7 @@ module.exports = class MetamaskController extends EventEmitter { // observable state store this.store = new ObservableStore(opts.initState) - + // config manager this.configManager = new ConfigManager({ store: this.store, @@ -47,7 +47,7 @@ module.exports = class MetamaskController extends EventEmitter { // eth data query tools this.ethQuery = new EthQuery(this.provider) this.ethStore = new EthStore(this.provider) - + // key mgmt this.keyringController = new KeyringController({ ethStore: this.ethStore, @@ -70,7 +70,7 @@ module.exports = class MetamaskController extends EventEmitter { provider: this.provider, blockTracker: this.provider, }) - + // notices this.noticeController = new NoticeController({ configManager: this.configManager, @@ -80,7 +80,7 @@ module.exports = class MetamaskController extends EventEmitter { // this.noticeController.startPolling() this.getNetwork() - this.messageManager = messageManager + this.messageManager = new MessageManager() this.publicConfigStore = this.initPublicConfigStore() this.checkTOSChange() @@ -96,6 +96,7 @@ module.exports = class MetamaskController extends EventEmitter { this.ethStore.on('update', this.sendUpdate.bind(this)) this.keyringController.on('update', this.sendUpdate.bind(this)) this.txManager.on('update', this.sendUpdate.bind(this)) + this.messageManager.on('update', this.sendUpdate.bind(this)) } // @@ -118,11 +119,7 @@ module.exports = class MetamaskController extends EventEmitter { // tx signing processTransaction: (txParams, cb) => this.newUnapprovedTransaction(txParams, cb), // msg signing - approveMessage: this.newUnsignedMessage.bind(this), - signMessage: (...args) => { - this.keyringController.signMessage(...args) - this.sendUpdate() - }, + processMessage: this.newUnsignedMessage.bind(this), }) return provider } @@ -163,6 +160,7 @@ module.exports = class MetamaskController extends EventEmitter { this.ethStore.getState(), this.configManager.getConfig(), this.txManager.getState(), + this.messageManager.getState(), keyringControllerState, this.noticeController.getState(), { lostAccounts: this.configManager.getLostAccounts(), @@ -178,6 +176,7 @@ module.exports = class MetamaskController extends EventEmitter { getApi () { const keyringController = this.keyringController const txManager = this.txManager + const messageManager = this.messageManager const noticeController = this.noticeController return { @@ -197,7 +196,7 @@ module.exports = class MetamaskController extends EventEmitter { buyEth: this.buyEth.bind(this), // shapeshift createShapeShiftTx: this.createShapeShiftTx.bind(this), - + // primary HD keyring management addNewAccount: this.addNewAccount.bind(this), placeSeedWords: this.placeSeedWords.bind(this), @@ -219,8 +218,8 @@ module.exports = class MetamaskController extends EventEmitter { // signing methods approveTransaction: txManager.approveTransaction.bind(txManager), cancelTransaction: txManager.cancelTransaction.bind(txManager), - signMessage: keyringController.signMessage.bind(keyringController), - cancelMessage: keyringController.cancelMessage.bind(keyringController), + signMessage: this.signMessage.bind(this), + cancelMessage: messageManager.cancelMessage.bind(messageManager), // notices checkNotices: noticeController.updateNoticesList.bind(noticeController), @@ -358,20 +357,35 @@ module.exports = class MetamaskController extends EventEmitter { } newUnsignedMessage (msgParams, cb) { - var state = this.keyringController.getState() - if (!state.isUnlocked) { - this.keyringController.addUnconfirmedMessage(msgParams, cb) - this.opts.unlockAccountMessage() - } else { - this.addUnconfirmedMessage(msgParams, cb) + this.keyringController.getState() + .then((state) => { + let msgId = this.messageManager.addUnapprovedMessage(msgParams) this.sendUpdate() - } + state.isUnlocked ? this.opts.unlockAccountMessage() : this.opts.showUnconfirmedMessage() + this.messageManager.once(`${msgId}:finished`, (data) => { + switch (data.status) { + case 'approved': + return cb(null, data.rawSig) + case 'rejected': + return cb(new Error('MetaMask Tx Signature: User denied transaction signature.')) + default: + return cb(new Error(`MetaMask Tx Signature: Unknown problem: ${JSON.stringify(msgParams)}`)) + } + }) + }) } - addUnconfirmedMessage (msgParams, cb) { - const keyringController = this.keyringController - const msgId = keyringController.addUnconfirmedMessage(msgParams, cb) - this.opts.showUnconfirmedMessage(msgParams, msgId) + signMessage (msgParams, cb) { + const msgId = msgParams.metamaskId + return this.messageManager.approveMessage(msgParams) + .then((cleanMsgParams) => { + return this.keyringController.signMessage(cleanMsgParams) + }) + .then((rawSig) => { + this.messageManager.brodcastMessage(rawSig, msgId, 'approved') + }).then(() => { + cb() + }).catch((err) => cb(err)) } setupPublicConfig (outStream) { -- cgit v1.2.3 From 13e20034698ae0477240e88eab9a3483e5daccd6 Mon Sep 17 00:00:00 2001 From: kumavis Date: Sat, 28 Jan 2017 22:12:15 -0800 Subject: metamask - subscribe metamask store to keyringController substate --- app/scripts/metamask-controller.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index f3da6f53e..cf58d2477 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -98,6 +98,9 @@ module.exports = class MetamaskController extends EventEmitter { this.ethStore.on('update', this.sendUpdate.bind(this)) this.keyringController.on('update', this.sendUpdate.bind(this)) this.txManager.on('update', this.sendUpdate.bind(this)) + this.keyringController.store.subscribe((state) => { + this.store.updateState({ KeyringController: state }) + }) } // -- cgit v1.2.3 From 6480c2d8a612a966091aa69c8af5462274e8d04c Mon Sep 17 00:00:00 2001 From: kumavis Date: Sun, 29 Jan 2017 23:59:17 -0800 Subject: public config - get selectedAccount from keyring controller substate --- app/scripts/metamask-controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index cf58d2477..422fda3d7 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -146,7 +146,7 @@ module.exports = class MetamaskController extends EventEmitter { function selectPublicState(state) { const result = { selectedAccount: undefined } try { - result.selectedAccount = state.config.selectedAccount + result.selectedAccount = state.KeyringController.selectedAccount } catch (_) {} return result } -- cgit v1.2.3 From dd528d886f1d2e017cd55ba7e003493f0bc4579c Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 00:32:04 -0800 Subject: metamask - lookup selectedAccount via keyringController --- app/scripts/metamask-controller.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 422fda3d7..b9a94b0c9 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -65,7 +65,7 @@ module.exports = class MetamaskController extends EventEmitter { txList: this.configManager.getTxList(), txHistoryLimit: 40, setTxList: this.configManager.setTxList.bind(this.configManager), - getSelectedAccount: this.configManager.getSelectedAccount.bind(this.configManager), + getSelectedAccount: this.keyringController.getSelectedAccount.bind(this.keyringController), getGasMultiplier: this.configManager.getGasMultiplier.bind(this.configManager), getNetwork: this.getStateNetwork.bind(this), signTransaction: this.keyringController.signTransaction.bind(this.keyringController), @@ -116,7 +116,7 @@ module.exports = class MetamaskController extends EventEmitter { rpcUrl: this.configManager.getCurrentRpcAddress(), // account mgmt getAccounts: (cb) => { - let selectedAccount = this.configManager.getSelectedAccount() + let selectedAccount = this.keyringController.getSelectedAccount() let result = selectedAccount ? [selectedAccount] : [] cb(null, result) }, @@ -330,7 +330,7 @@ module.exports = class MetamaskController extends EventEmitter { // ensuring they are only ever available in the background process. clearSeedWordCache (cb) { this.configManager.setSeedWords(null) - cb(null, this.configManager.getSelectedAccount()) + cb(null, this.keyringController.getSelectedAccount()) } importAccountWithStrategy (strategy, args, cb) { -- cgit v1.2.3 From 0a1918f71a0a5768d7501e3ee799a3ed2e6d9f49 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 12:42:24 -0800 Subject: preferences - introduce preferences controller --- app/scripts/metamask-controller.js | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index b9a94b0c9..c0673dda4 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -11,6 +11,7 @@ const streamIntoProvider = require('web3-stream-provider/handler') const MetaMaskProvider = require('web3-provider-engine/zero.js') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const KeyringController = require('./keyring-controller') +const PreferencesController = require('./lib/controllers/preferences') const NoticeController = require('./notice-controller') const messageManager = require('./lib/message-manager') const TxManager = require('./transaction-manager') @@ -40,6 +41,11 @@ module.exports = class MetamaskController extends EventEmitter { }) this.configManager.updateConversionRate() + // preferences controller + this.prefencesController = new PreferencesController({ + initState: initState.PrefencesController, + }) + // rpc provider this.provider = this.initializeProvider(opts) this.provider.on('block', this.logBlock.bind(this)) @@ -101,6 +107,9 @@ module.exports = class MetamaskController extends EventEmitter { this.keyringController.store.subscribe((state) => { this.store.updateState({ KeyringController: state }) }) + this.prefencesController.store.subscribe((state) => { + this.store.updateState({ PrefencesController: state }) + }) } // -- cgit v1.2.3 From 4dafb09db20a8c7050a431e70eb773e2ac62d6c8 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 13:01:07 -0800 Subject: metamask - move selectedAccount set/get to preferencesController --- app/scripts/metamask-controller.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index c0673dda4..2f9e37cee 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -42,8 +42,8 @@ module.exports = class MetamaskController extends EventEmitter { this.configManager.updateConversionRate() // preferences controller - this.prefencesController = new PreferencesController({ - initState: initState.PrefencesController, + this.preferencesController = new PreferencesController({ + initState: initState.PreferencesController, }) // rpc provider @@ -71,7 +71,7 @@ module.exports = class MetamaskController extends EventEmitter { txList: this.configManager.getTxList(), txHistoryLimit: 40, setTxList: this.configManager.setTxList.bind(this.configManager), - getSelectedAccount: this.keyringController.getSelectedAccount.bind(this.keyringController), + getSelectedAccount: this.preferencesController.getSelectedAccount.bind(this.preferencesController), getGasMultiplier: this.configManager.getGasMultiplier.bind(this.configManager), getNetwork: this.getStateNetwork.bind(this), signTransaction: this.keyringController.signTransaction.bind(this.keyringController), @@ -107,8 +107,8 @@ module.exports = class MetamaskController extends EventEmitter { this.keyringController.store.subscribe((state) => { this.store.updateState({ KeyringController: state }) }) - this.prefencesController.store.subscribe((state) => { - this.store.updateState({ PrefencesController: state }) + this.preferencesController.store.subscribe((state) => { + this.store.updateState({ PreferencesController: state }) }) } @@ -125,7 +125,7 @@ module.exports = class MetamaskController extends EventEmitter { rpcUrl: this.configManager.getCurrentRpcAddress(), // account mgmt getAccounts: (cb) => { - let selectedAccount = this.keyringController.getSelectedAccount() + let selectedAccount = this.preferencesController.getSelectedAccount() let result = selectedAccount ? [selectedAccount] : [] cb(null, result) }, @@ -191,6 +191,7 @@ module.exports = class MetamaskController extends EventEmitter { getApi () { const keyringController = this.keyringController + const preferencesController = this.preferencesController const txManager = this.txManager const noticeController = this.noticeController @@ -221,12 +222,14 @@ module.exports = class MetamaskController extends EventEmitter { // vault management submitPassword: this.submitPassword.bind(this), + // PreferencesController + setSelectedAccount: nodeify(preferencesController.setSelectedAccount).bind(preferencesController), + // KeyringController setLocked: nodeify(keyringController.setLocked).bind(keyringController), createNewVaultAndKeychain: nodeify(keyringController.createNewVaultAndKeychain).bind(keyringController), createNewVaultAndRestore: nodeify(keyringController.createNewVaultAndRestore).bind(keyringController), addNewKeyring: nodeify(keyringController.addNewKeyring).bind(keyringController), - setSelectedAccount: nodeify(keyringController.setSelectedAccount).bind(keyringController), saveAccountLabel: nodeify(keyringController.saveAccountLabel).bind(keyringController), exportAccount: nodeify(keyringController.exportAccount).bind(keyringController), @@ -339,7 +342,7 @@ module.exports = class MetamaskController extends EventEmitter { // ensuring they are only ever available in the background process. clearSeedWordCache (cb) { this.configManager.setSeedWords(null) - cb(null, this.keyringController.getSelectedAccount()) + cb(null, this.preferencesController.getSelectedAccount()) } importAccountWithStrategy (strategy, args, cb) { @@ -348,7 +351,7 @@ module.exports = class MetamaskController extends EventEmitter { return this.keyringController.addNewKeyring('Simple Key Pair', [ privateKey ]) }) .then(keyring => keyring.getAccounts()) - .then((accounts) => this.keyringController.setSelectedAccount(accounts[0])) + .then((accounts) => this.preferencesController.setSelectedAccount(accounts[0])) .then(() => { cb(null, this.keyringController.fullUpdate()) }) .catch((reason) => { cb(reason) }) } -- cgit v1.2.3 From b1de2cdefa2341eeab8d0269a1b14b349fdd5d16 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 13:09:46 -0800 Subject: metamask - rename getSelectedAccount to getSelectedAddress --- app/scripts/metamask-controller.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 2f9e37cee..64deae426 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -71,7 +71,7 @@ module.exports = class MetamaskController extends EventEmitter { txList: this.configManager.getTxList(), txHistoryLimit: 40, setTxList: this.configManager.setTxList.bind(this.configManager), - getSelectedAccount: this.preferencesController.getSelectedAccount.bind(this.preferencesController), + getSelectedAddress: this.preferencesController.getSelectedAddress.bind(this.preferencesController), getGasMultiplier: this.configManager.getGasMultiplier.bind(this.configManager), getNetwork: this.getStateNetwork.bind(this), signTransaction: this.keyringController.signTransaction.bind(this.keyringController), @@ -125,7 +125,7 @@ module.exports = class MetamaskController extends EventEmitter { rpcUrl: this.configManager.getCurrentRpcAddress(), // account mgmt getAccounts: (cb) => { - let selectedAccount = this.preferencesController.getSelectedAccount() + let selectedAccount = this.preferencesController.getSelectedAddress() let result = selectedAccount ? [selectedAccount] : [] cb(null, result) }, @@ -342,7 +342,7 @@ module.exports = class MetamaskController extends EventEmitter { // ensuring they are only ever available in the background process. clearSeedWordCache (cb) { this.configManager.setSeedWords(null) - cb(null, this.preferencesController.getSelectedAccount()) + cb(null, this.preferencesController.getSelectedAddress()) } importAccountWithStrategy (strategy, args, cb) { -- cgit v1.2.3 From f9b31fe2c3a43339a4519761e4c8b2f3813e85e0 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 15:08:31 -0800 Subject: rename selectedAccount to selectedAddress --- app/scripts/metamask-controller.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 64deae426..e081188ce 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -62,8 +62,9 @@ module.exports = class MetamaskController extends EventEmitter { configManager: this.configManager, getNetwork: this.getStateNetwork.bind(this), }) - this.keyringController.on('newAccount', (account) => { - autoFaucet(account) + this.keyringController.on('newAccount', (address) => { + this.preferencesController.setSelectedAddress(address) + autoFaucet(address) }) // tx mgmt @@ -176,6 +177,7 @@ module.exports = class MetamaskController extends EventEmitter { this.configManager.getConfig(), this.txManager.getState(), keyringControllerState, + this.preferencesController.store.getState(), this.noticeController.getState(), { shapeShiftTxList: this.configManager.getShapeShiftTxList(), @@ -223,7 +225,7 @@ module.exports = class MetamaskController extends EventEmitter { submitPassword: this.submitPassword.bind(this), // PreferencesController - setSelectedAccount: nodeify(preferencesController.setSelectedAccount).bind(preferencesController), + setSelectedAddress: nodeify(preferencesController.setSelectedAddress).bind(preferencesController), // KeyringController setLocked: nodeify(keyringController.setLocked).bind(keyringController), @@ -351,7 +353,7 @@ module.exports = class MetamaskController extends EventEmitter { return this.keyringController.addNewKeyring('Simple Key Pair', [ privateKey ]) }) .then(keyring => keyring.getAccounts()) - .then((accounts) => this.preferencesController.setSelectedAccount(accounts[0])) + .then((accounts) => this.preferencesController.setSelectedAddress(accounts[0])) .then(() => { cb(null, this.keyringController.fullUpdate()) }) .catch((reason) => { cb(reason) }) } -- cgit v1.2.3 From 2fa60cfcbfdeb43599007f0dd26fd44a4beec8f1 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 15:25:12 -0800 Subject: continue rename selectedAccount to selectedAddress --- app/scripts/metamask-controller.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index e081188ce..fc7df5137 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -126,8 +126,8 @@ module.exports = class MetamaskController extends EventEmitter { rpcUrl: this.configManager.getCurrentRpcAddress(), // account mgmt getAccounts: (cb) => { - let selectedAccount = this.preferencesController.getSelectedAddress() - let result = selectedAccount ? [selectedAccount] : [] + let selectedAddress = this.preferencesController.getSelectedAddress() + let result = selectedAddress ? [selectedAddress] : [] cb(null, result) }, // tx signing @@ -154,9 +154,9 @@ module.exports = class MetamaskController extends EventEmitter { ) function selectPublicState(state) { - const result = { selectedAccount: undefined } + const result = { selectedAddress: undefined } try { - result.selectedAccount = state.KeyringController.selectedAccount + result.selectedAddress = state.KeyringController.selectedAddress } catch (_) {} return result } -- cgit v1.2.3 From 7ccaa263d265edc2f987dc66af0caf5fedb42296 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 30 Jan 2017 15:38:32 -0800 Subject: inpage - set web3.eth.defaultAccount correctly --- app/scripts/metamask-controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index fc7df5137..2c379f8d9 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -156,7 +156,7 @@ module.exports = class MetamaskController extends EventEmitter { function selectPublicState(state) { const result = { selectedAddress: undefined } try { - result.selectedAddress = state.KeyringController.selectedAddress + result.selectedAddress = state.PreferencesController.selectedAddress } catch (_) {} return result } -- cgit v1.2.3 From c0d3db6a8ce2fe0522fa4e3cba1e01d10469280f Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 31 Jan 2017 20:02:38 -0800 Subject: keyring - synchronous getState --- app/scripts/metamask-controller.js | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 2c379f8d9..17b5683a1 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -169,22 +169,19 @@ module.exports = class MetamaskController extends EventEmitter { // getState () { - return this.keyringController.getState() - .then((keyringControllerState) => { - return extend( - this.state, - this.ethStore.getState(), - this.configManager.getConfig(), - this.txManager.getState(), - keyringControllerState, - this.preferencesController.store.getState(), - this.noticeController.getState(), - { - shapeShiftTxList: this.configManager.getShapeShiftTxList(), - lostAccounts: this.configManager.getLostAccounts(), - } - ) - }) + return extend( + this.state, + this.ethStore.getState(), + this.configManager.getConfig(), + this.txManager.getState(), + this.keyringController.getState(), + this.preferencesController.store.getState(), + this.noticeController.getState(), + { + shapeShiftTxList: this.configManager.getShapeShiftTxList(), + lostAccounts: this.configManager.getLostAccounts(), + } + ) } // @@ -199,7 +196,7 @@ module.exports = class MetamaskController extends EventEmitter { return { // etc - getState: nodeify(this.getState.bind(this)), + getState: (cb) => cb(null, this.getState()), setRpcTarget: this.setRpcTarget.bind(this), setProviderType: this.setProviderType.bind(this), useEtherscanProvider: this.useEtherscanProvider.bind(this), @@ -295,11 +292,8 @@ module.exports = class MetamaskController extends EventEmitter { ) } - sendUpdate () { - this.getState() - .then((state) => { - this.emit('update', state) - }) + sendUpdate () { + this.emit('update', this.getState()) } // -- cgit v1.2.3 From ad060e267810dfd1217d5ddf40516976c8b2d68e Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 31 Jan 2017 22:35:11 -0800 Subject: metamask - inherit some configManager state from keyring controller --- app/scripts/metamask-controller.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 17b5683a1..23ced75f1 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -180,6 +180,9 @@ module.exports = class MetamaskController extends EventEmitter { { shapeShiftTxList: this.configManager.getShapeShiftTxList(), lostAccounts: this.configManager.getLostAccounts(), + currentFiat: this.configManager.getCurrentFiat(), + conversionRate: this.configManager.getConversionRate(), + conversionDate: this.configManager.getConversionDate(), } ) } -- cgit v1.2.3 From 1cb730144d6b1c05007323bbc31e4375373628fb Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 1 Feb 2017 00:31:26 -0800 Subject: metamask - adopt isInitialized from keyring controller --- app/scripts/metamask-controller.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 23ced75f1..222a1d618 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -169,14 +169,21 @@ module.exports = class MetamaskController extends EventEmitter { // getState () { + const wallet = this.configManager.getWallet() + const vault = this.keyringController.store.getState().vault + const isInitialized = (!!wallet || !!vault) return extend( + { + isInitialized, + }, this.state, this.ethStore.getState(), - this.configManager.getConfig(), this.txManager.getState(), this.keyringController.getState(), this.preferencesController.store.getState(), this.noticeController.getState(), + // config manager + this.configManager.getConfig(), { shapeShiftTxList: this.configManager.getShapeShiftTxList(), lostAccounts: this.configManager.getLostAccounts(), -- cgit v1.2.3 From 1b16b4624186265ccbb6f8106e1bf9ff997e2528 Mon Sep 17 00:00:00 2001 From: Frankie Date: Wed, 1 Feb 2017 11:54:01 -0800 Subject: code clan up and tests --- app/scripts/metamask-controller.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index bab005af2..38358f04b 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -224,7 +224,7 @@ module.exports = class MetamaskController extends EventEmitter { approveTransaction: txManager.approveTransaction.bind(txManager), cancelTransaction: txManager.cancelTransaction.bind(txManager), signMessage: this.signMessage.bind(this), - cancelMessage: messageManager.cancelMessage.bind(messageManager), + cancelMessage: messageManager.rejectMsg.bind(messageManager), // notices checkNotices: noticeController.updateNoticesList.bind(noticeController), @@ -369,31 +369,33 @@ module.exports = class MetamaskController extends EventEmitter { } newUnsignedMessage (msgParams, cb) { - this.keyringController.getState() - .then((state) => { - let msgId = this.messageManager.addUnapprovedMessage(msgParams) - this.sendUpdate() - state.isUnlocked ? this.opts.unlockAccountMessage() : this.opts.showUnconfirmedMessage() - this.messageManager.once(`${msgId}:finished`, (data) => { - switch (data.status) { - case 'approved': - return cb(null, data.rawSig) - case 'rejected': - return cb(new Error('MetaMask Tx Signature: User denied transaction signature.')) - default: - return cb(new Error(`MetaMask Tx Signature: Unknown problem: ${JSON.stringify(msgParams)}`)) - } - }) + let msgId = this.messageManager.addUnapprovedMessage(msgParams) + this.sendUpdate() + this.opts.showUnconfirmedMessage() + this.messageManager.once(`${msgId}:finished`, (data) => { + switch (data.status) { + case 'approved': + return cb(null, data.rawSig) + case 'rejected': + return cb(new Error('MetaMask Message Signature: User denied transaction signature.')) + default: + return cb(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`)) + } }) } signMessage (msgParams, cb) { const msgId = msgParams.metamaskId + // sets the status op the message to 'approved' + // and removes the metamaskId for signing return this.messageManager.approveMessage(msgParams) .then((cleanMsgParams) => { + // signs the message return this.keyringController.signMessage(cleanMsgParams) }) .then((rawSig) => { + // tells the listener that the message has been signed + // and can be returned to the dapp this.messageManager.brodcastMessage(rawSig, msgId, 'approved') }).then(() => { cb() -- cgit v1.2.3 From 4c0cb9d92e7d00da8219c82f24337e0a776e14f8 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 1 Feb 2017 16:38:09 -0800 Subject: metamask - memState - adopt configManager.isDisclaimerConfirmed from keyring controller --- app/scripts/metamask-controller.js | 1 + 1 file changed, 1 insertion(+) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index fb5234c24..a56203a88 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -189,6 +189,7 @@ module.exports = class MetamaskController extends EventEmitter { currentFiat: this.configManager.getCurrentFiat(), conversionRate: this.configManager.getConversionRate(), conversionDate: this.configManager.getConversionDate(), + isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), } ) } -- cgit v1.2.3 From 5c5aa6ea012b6a5d0cb8e8386ac0429f023c9e1a Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 16:54:16 -0800 Subject: keyring - rm config dep --- app/scripts/metamask-controller.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index a56203a88..695491c2a 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -59,7 +59,6 @@ module.exports = class MetamaskController extends EventEmitter { this.keyringController = new KeyringController({ initState: initState.KeyringController, ethStore: this.ethStore, - configManager: this.configManager, getNetwork: this.getStateNetwork.bind(this), }) this.keyringController.on('newAccount', (address) => { @@ -190,6 +189,7 @@ module.exports = class MetamaskController extends EventEmitter { conversionRate: this.configManager.getConversionRate(), conversionDate: this.configManager.getConversionDate(), isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), + seedWords: this.configManager.getSeedWords(), } ) } @@ -339,7 +339,7 @@ module.exports = class MetamaskController extends EventEmitter { .then((serialized) => { const seedWords = serialized.mnemonic this.configManager.setSeedWords(seedWords) - promiseToCallback(this.keyringController.fullUpdate())(cb) + cb() }) } -- cgit v1.2.3 From 79ed2e902fa3e1d9aadd9e3ecd88f627de5bb100 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 17:07:52 -0800 Subject: keyring - replace getState with memStore --- app/scripts/metamask-controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 695491c2a..02dd60528 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -177,7 +177,7 @@ module.exports = class MetamaskController extends EventEmitter { this.ethStore.getState(), this.txManager.getState(), this.messageManager.getState(), - this.keyringController.getState(), + this.keyringController.memStore.getState(), this.preferencesController.store.getState(), this.noticeController.getState(), // config manager -- cgit v1.2.3 From bcb86f38cbe91fb814d30b3129de76dba45bff66 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 18:21:22 -0800 Subject: messageManager - move view state to obs-store --- app/scripts/metamask-controller.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 02dd60528..b6a096488 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -100,17 +100,19 @@ module.exports = class MetamaskController extends EventEmitter { configManager: this.configManager, }) - // manual state subscriptions - this.ethStore.on('update', this.sendUpdate.bind(this)) - this.keyringController.on('update', this.sendUpdate.bind(this)) - this.txManager.on('update', this.sendUpdate.bind(this)) - this.messageManager.on('update', this.sendUpdate.bind(this)) + // manual disk state subscriptions this.keyringController.store.subscribe((state) => { this.store.updateState({ KeyringController: state }) }) this.preferencesController.store.subscribe((state) => { this.store.updateState({ PreferencesController: state }) }) + + // manual mem state subscriptions + this.ethStore.on('update', this.sendUpdate.bind(this)) + this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) + this.txManager.on('update', this.sendUpdate.bind(this)) + this.messageManager.memStore.subscribe(this.sendUpdate.bind(this)) } // @@ -176,7 +178,7 @@ module.exports = class MetamaskController extends EventEmitter { this.state, this.ethStore.getState(), this.txManager.getState(), - this.messageManager.getState(), + this.messageManager.memStore.getState(), this.keyringController.memStore.getState(), this.preferencesController.store.getState(), this.noticeController.getState(), -- cgit v1.2.3 From 99fa9ab13aaf69cb8602612816a4df49209fa8a6 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 20:20:13 -0800 Subject: migration 7 - break off TransactionManager substate --- app/scripts/metamask-controller.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index b6a096488..468e7d285 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -68,11 +68,9 @@ module.exports = class MetamaskController extends EventEmitter { // tx mgmt this.txManager = new TxManager({ - txList: this.configManager.getTxList(), + initState: initState.TxManager, txHistoryLimit: 40, - setTxList: this.configManager.setTxList.bind(this.configManager), getSelectedAddress: this.preferencesController.getSelectedAddress.bind(this.preferencesController), - getGasMultiplier: this.configManager.getGasMultiplier.bind(this.configManager), getNetwork: this.getStateNetwork.bind(this), signTransaction: this.keyringController.signTransaction.bind(this.keyringController), provider: this.provider, @@ -107,11 +105,14 @@ module.exports = class MetamaskController extends EventEmitter { this.preferencesController.store.subscribe((state) => { this.store.updateState({ PreferencesController: state }) }) + this.txManager.store.subscribe((state) => { + this.store.updateState({ TransactionManager: state }) + }) // manual mem state subscriptions this.ethStore.on('update', this.sendUpdate.bind(this)) this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) - this.txManager.on('update', this.sendUpdate.bind(this)) + this.txManager.memStore.subscribe(this.sendUpdate.bind(this)) this.messageManager.memStore.subscribe(this.sendUpdate.bind(this)) } @@ -177,7 +178,7 @@ module.exports = class MetamaskController extends EventEmitter { }, this.state, this.ethStore.getState(), - this.txManager.getState(), + this.txManager.memStore.getState(), this.messageManager.memStore.getState(), this.keyringController.memStore.getState(), this.preferencesController.store.getState(), @@ -245,11 +246,13 @@ module.exports = class MetamaskController extends EventEmitter { saveAccountLabel: nodeify(keyringController.saveAccountLabel).bind(keyringController), exportAccount: nodeify(keyringController.exportAccount).bind(keyringController), - // signing methods + // txManager approveTransaction: txManager.approveTransaction.bind(txManager), cancelTransaction: txManager.cancelTransaction.bind(txManager), - signMessage: this.signMessage.bind(this), - cancelMessage: messageManager.rejectMsg.bind(messageManager), + + // messageManager + signMessage: this.signMessage.bind(this), + cancelMessage: messageManager.rejectMsg.bind(messageManager), // notices checkNotices: noticeController.updateNoticesList.bind(noticeController), @@ -586,7 +589,7 @@ module.exports = class MetamaskController extends EventEmitter { setGasMultiplier (gasMultiplier, cb) { try { - this.configManager.setGasMultiplier(gasMultiplier) + this.txManager.setGasMultiplier(gasMultiplier) cb() } catch (err) { cb(err) -- cgit v1.2.3 From 0c6df24ecff566c84810a1b29316c1efa2c83870 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 20:59:47 -0800 Subject: metamask - introduce networkStore --- app/scripts/metamask-controller.js | 68 ++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 28 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 468e7d285..cc30a97e6 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -29,12 +29,14 @@ module.exports = class MetamaskController extends EventEmitter { constructor (opts) { super() this.opts = opts - this.state = { network: 'loading' } let initState = opts.initState || {} // observable state store this.store = new ObservableStore(initState) + // network store + this.networkStore = new ObservableStore({ network: 'loading' }) + // config manager this.configManager = new ConfigManager({ store: this.store, @@ -49,7 +51,7 @@ module.exports = class MetamaskController extends EventEmitter { // rpc provider this.provider = this.initializeProvider(opts) this.provider.on('block', this.logBlock.bind(this)) - this.provider.on('error', this.getNetwork.bind(this)) + this.provider.on('error', this.verifyNetwork.bind(this)) // eth data query tools this.ethQuery = new EthQuery(this.provider) @@ -59,7 +61,7 @@ module.exports = class MetamaskController extends EventEmitter { this.keyringController = new KeyringController({ initState: initState.KeyringController, ethStore: this.ethStore, - getNetwork: this.getStateNetwork.bind(this), + getNetwork: this.getNetworkState.bind(this), }) this.keyringController.on('newAccount', (address) => { this.preferencesController.setSelectedAddress(address) @@ -68,10 +70,11 @@ module.exports = class MetamaskController extends EventEmitter { // tx mgmt this.txManager = new TxManager({ - initState: initState.TxManager, + initState: initState.TransactionManager, + networkStore: this.networkStore, txHistoryLimit: 40, getSelectedAddress: this.preferencesController.getSelectedAddress.bind(this.preferencesController), - getNetwork: this.getStateNetwork.bind(this), + getNetwork: this.getNetworkState.bind(this), signTransaction: this.keyringController.signTransaction.bind(this.keyringController), provider: this.provider, blockTracker: this.provider, @@ -85,7 +88,7 @@ module.exports = class MetamaskController extends EventEmitter { // to be uncommented when retrieving notices from a remote server. // this.noticeController.startPolling() - this.getNetwork() + this.lookupNetwork() this.messageManager = new MessageManager() this.publicConfigStore = this.initPublicConfigStore() @@ -111,6 +114,7 @@ module.exports = class MetamaskController extends EventEmitter { // manual mem state subscriptions this.ethStore.on('update', this.sendUpdate.bind(this)) + this.networkStore.subscribe(this.sendUpdate.bind(this)) this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) this.txManager.memStore.subscribe(this.sendUpdate.bind(this)) this.messageManager.memStore.subscribe(this.sendUpdate.bind(this)) @@ -176,7 +180,7 @@ module.exports = class MetamaskController extends EventEmitter { { isInitialized, }, - this.state, + this.networkStore.getState(), this.ethStore.getState(), this.txManager.memStore.getState(), this.messageManager.memStore.getState(), @@ -571,16 +575,20 @@ module.exports = class MetamaskController extends EventEmitter { buyEth (address, amount) { if (!amount) amount = '5' - var network = this.state.network - var url = `https://buy.coinbase.com/?code=9ec56d01-7e81-5017-930c-513daa27bb6a&amount=${amount}&address=${address}&crypto_currency=ETH` + const network = this.getNetworkState() + let url + + switch (network) { + case '1': + url = `https://buy.coinbase.com/?code=9ec56d01-7e81-5017-930c-513daa27bb6a&amount=${amount}&address=${address}&crypto_currency=ETH` + break - if (network === '3') { - url = 'https://faucet.metamask.io/' + case '3': + url = 'https://faucet.metamask.io/' + break } - extension.tabs.create({ - url, - }) + if (url) extension.tabs.create({ url }) } createShapeShiftTx (depositAddress, depositType) { @@ -602,21 +610,19 @@ module.exports = class MetamaskController extends EventEmitter { verifyNetwork () { // Check network when restoring connectivity: - if (this.state.network === 'loading') { - this.getNetwork() - } + if (this.isNetworkLoading()) this.lookupNetwork() } setRpcTarget (rpcTarget) { this.configManager.setRpcTarget(rpcTarget) extension.runtime.reload() - this.getNetwork() + this.lookupNetwork() } setProviderType (type) { this.configManager.setProviderType(type) extension.runtime.reload() - this.getNetwork() + this.lookupNetwork() } useEtherscanProvider () { @@ -624,26 +630,32 @@ module.exports = class MetamaskController extends EventEmitter { extension.runtime.reload() } - getStateNetwork () { - return this.state.network + getNetworkState () { + return this.networkStore.getState().network + } + + setNetworkState (network) { + return this.networkStore.updateState({ network }) + } + + isNetworkLoading () { + return this.getNetworkState() === 'loading' } - getNetwork (err) { + lookupNetwork (err) { if (err) { - this.state.network = 'loading' - this.sendUpdate() + this.setNetworkState('loading') } this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => { if (err) { - this.state.network = 'loading' - return this.sendUpdate() + this.setNetworkState('loading') + return } if (global.METAMASK_DEBUG) { console.log('web3.getNetwork returned ' + network) } - this.state.network = network - this.sendUpdate() + this.setNetworkState(network) }) } -- cgit v1.2.3 From f08f40aee2614c9e72efce5d2d10f6e4b84d7a10 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 21:09:17 -0800 Subject: txManager - depend on preferencesStore --- app/scripts/metamask-controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index cc30a97e6..de6e08e0b 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -72,8 +72,8 @@ module.exports = class MetamaskController extends EventEmitter { this.txManager = new TxManager({ initState: initState.TransactionManager, networkStore: this.networkStore, + preferencesStore: this.preferencesController.store, txHistoryLimit: 40, - getSelectedAddress: this.preferencesController.getSelectedAddress.bind(this.preferencesController), getNetwork: this.getNetworkState.bind(this), signTransaction: this.keyringController.signTransaction.bind(this.keyringController), provider: this.provider, -- cgit v1.2.3 From 73edfc9f31b1cbd44ae8b5372e7bef5d1d5959ad Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 22:05:06 -0800 Subject: eth-store - convert to obs-store subclass --- app/scripts/metamask-controller.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index de6e08e0b..9f79cf038 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -55,7 +55,10 @@ module.exports = class MetamaskController extends EventEmitter { // eth data query tools this.ethQuery = new EthQuery(this.provider) - this.ethStore = new EthStore(this.provider) + this.ethStore = new EthStore({ + provider: this.provider, + blockTracker: this.provider, + }) // key mgmt this.keyringController = new KeyringController({ @@ -113,7 +116,7 @@ module.exports = class MetamaskController extends EventEmitter { }) // manual mem state subscriptions - this.ethStore.on('update', this.sendUpdate.bind(this)) + this.ethStore.subscribe(this.sendUpdate.bind(this)) this.networkStore.subscribe(this.sendUpdate.bind(this)) this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) this.txManager.memStore.subscribe(this.sendUpdate.bind(this)) -- cgit v1.2.3 From 8b5e3aa287ef5e803a68730db5ccb8bec0b6b254 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 22:23:45 -0800 Subject: migration #8 - break out NoticeController substate --- app/scripts/metamask-controller.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 9f79cf038..785bb7b33 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -85,7 +85,7 @@ module.exports = class MetamaskController extends EventEmitter { // notices this.noticeController = new NoticeController({ - configManager: this.configManager, + initState: initState.NoticeController, }) this.noticeController.updateNoticesList() // to be uncommented when retrieving notices from a remote server. @@ -189,7 +189,7 @@ module.exports = class MetamaskController extends EventEmitter { this.messageManager.memStore.getState(), this.keyringController.memStore.getState(), this.preferencesController.store.getState(), - this.noticeController.getState(), + this.noticeController.memStore.getState(), // config manager this.configManager.getConfig(), { -- cgit v1.2.3 From 9e4ef45b6ac460e6539e0f79ad5c78959fa1c4cb Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 23:32:24 -0800 Subject: migration #9 - break out CurrencyController substate --- app/scripts/metamask-controller.js | 47 ++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 25 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 785bb7b33..2f1623c7b 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -12,6 +12,7 @@ const MetaMaskProvider = require('web3-provider-engine/zero.js') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const KeyringController = require('./keyring-controller') const PreferencesController = require('./lib/controllers/preferences') +const CurrencyController = require('./lib/controllers/currency') const NoticeController = require('./notice-controller') const MessageManager = require('./lib/message-manager') const TxManager = require('./transaction-manager') @@ -41,13 +42,19 @@ module.exports = class MetamaskController extends EventEmitter { this.configManager = new ConfigManager({ store: this.store, }) - this.configManager.updateConversionRate() // preferences controller this.preferencesController = new PreferencesController({ initState: initState.PreferencesController, }) + // currency controller + this.currencyController = new CurrencyController({ + initState: initState.CurrencyController, + }) + this.currencyController.updateConversionRate() + this.currencyController.scheduleConversionInterval() + // rpc provider this.provider = this.initializeProvider(opts) this.provider.on('block', this.logBlock.bind(this)) @@ -97,8 +104,6 @@ module.exports = class MetamaskController extends EventEmitter { this.checkTOSChange() - this.scheduleConversionInterval() - // TEMPORARY UNTIL FULL DEPRECATION: this.idStoreMigrator = new IdStoreMigrator({ configManager: this.configManager, @@ -114,11 +119,15 @@ module.exports = class MetamaskController extends EventEmitter { this.txManager.store.subscribe((state) => { this.store.updateState({ TransactionManager: state }) }) + this.currencyController.store.subscribe((state) => { + this.store.updateState({ CurrencyController: state }) + }) // manual mem state subscriptions this.ethStore.subscribe(this.sendUpdate.bind(this)) this.networkStore.subscribe(this.sendUpdate.bind(this)) this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) + this.currencyController.store.subscribe(this.sendUpdate.bind(this)) this.txManager.memStore.subscribe(this.sendUpdate.bind(this)) this.messageManager.memStore.subscribe(this.sendUpdate.bind(this)) } @@ -189,15 +198,13 @@ module.exports = class MetamaskController extends EventEmitter { this.messageManager.memStore.getState(), this.keyringController.memStore.getState(), this.preferencesController.store.getState(), + this.currencyController.store.getState(), this.noticeController.memStore.getState(), // config manager this.configManager.getConfig(), { shapeShiftTxList: this.configManager.getShapeShiftTxList(), lostAccounts: this.configManager.getLostAccounts(), - currentFiat: this.configManager.getCurrentFiat(), - conversionRate: this.configManager.getConversionRate(), - conversionDate: this.configManager.getConversionDate(), isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), seedWords: this.configManager.getSeedWords(), } @@ -223,7 +230,7 @@ module.exports = class MetamaskController extends EventEmitter { useEtherscanProvider: this.useEtherscanProvider.bind(this), agreeToDisclaimer: this.agreeToDisclaimer.bind(this), resetDisclaimer: this.resetDisclaimer.bind(this), - setCurrentFiat: this.setCurrentFiat.bind(this), + setCurrentCurrency: this.setCurrentCurrency.bind(this), setTOSHash: this.setTOSHash.bind(this), checkTOSChange: this.checkTOSChange.bind(this), setGasMultiplier: this.setGasMultiplier.bind(this), @@ -550,29 +557,19 @@ module.exports = class MetamaskController extends EventEmitter { this.verifyNetwork() } - setCurrentFiat (fiat, cb) { + setCurrentCurrency (currencyCode, cb) { try { - this.configManager.setCurrentFiat(fiat) - this.configManager.updateConversionRate() - this.scheduleConversionInterval() + this.currencyController.setCurrentCurrency(currencyCode) + this.currencyController.updateConversionRate() const data = { - conversionRate: this.configManager.getConversionRate(), - currentFiat: this.configManager.getCurrentFiat(), - conversionDate: this.configManager.getConversionDate(), + conversionRate: this.currencyController.getConversionRate(), + currentFiat: this.currencyController.getCurrentCurrency(), + conversionDate: this.currencyController.getConversionDate(), } - cb(data) + cb(null, data) } catch (err) { - cb(null, err) - } - } - - scheduleConversionInterval () { - if (this.conversionInterval) { - clearInterval(this.conversionInterval) + cb(err) } - this.conversionInterval = setInterval(() => { - this.configManager.updateConversionRate() - }, 300000) } buyEth (address, amount) { -- cgit v1.2.3 From 970d4fd69519bab1de972839190a4ede888914bb Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 23:47:00 -0800 Subject: metamask - add missing diskState and memState subscriptions --- app/scripts/metamask-controller.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 2f1623c7b..c0910014f 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -110,26 +110,31 @@ module.exports = class MetamaskController extends EventEmitter { }) // manual disk state subscriptions + this.txManager.store.subscribe((state) => { + this.store.updateState({ TransactionManager: state }) + }) this.keyringController.store.subscribe((state) => { this.store.updateState({ KeyringController: state }) }) this.preferencesController.store.subscribe((state) => { this.store.updateState({ PreferencesController: state }) }) - this.txManager.store.subscribe((state) => { - this.store.updateState({ TransactionManager: state }) - }) this.currencyController.store.subscribe((state) => { this.store.updateState({ CurrencyController: state }) }) + this.noticeController.store.subscribe((state) => { + this.store.updateState({ NoticeController: state }) + }) // manual mem state subscriptions - this.ethStore.subscribe(this.sendUpdate.bind(this)) this.networkStore.subscribe(this.sendUpdate.bind(this)) - this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) - this.currencyController.store.subscribe(this.sendUpdate.bind(this)) + this.ethStore.subscribe(this.sendUpdate.bind(this)) this.txManager.memStore.subscribe(this.sendUpdate.bind(this)) this.messageManager.memStore.subscribe(this.sendUpdate.bind(this)) + this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) + this.preferencesController.store.subscribe(this.sendUpdate.bind(this)) + this.currencyController.store.subscribe(this.sendUpdate.bind(this)) + this.noticeController.memStore.subscribe(this.sendUpdate.bind(this)) } // -- cgit v1.2.3 From ff87b9dc7aa3a3bd0e6ca75ca76d538c5ecaf44a Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 3 Feb 2017 14:59:07 -0800 Subject: id mgmt - update to latest eth_sign spec --- app/scripts/metamask-controller.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index c0910014f..066e389e2 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -56,7 +56,7 @@ module.exports = class MetamaskController extends EventEmitter { this.currencyController.scheduleConversionInterval() // rpc provider - this.provider = this.initializeProvider(opts) + this.provider = this.initializeProvider() this.provider.on('block', this.logBlock.bind(this)) this.provider.on('error', this.verifyNetwork.bind(this)) @@ -418,7 +418,7 @@ module.exports = class MetamaskController extends EventEmitter { this.opts.showUnconfirmedMessage() this.messageManager.once(`${msgId}:finished`, (data) => { switch (data.status) { - case 'approved': + case 'signed': return cb(null, data.rawSig) case 'rejected': return cb(new Error('MetaMask Message Signature: User denied transaction signature.')) @@ -430,20 +430,20 @@ module.exports = class MetamaskController extends EventEmitter { signMessage (msgParams, cb) { const msgId = msgParams.metamaskId - // sets the status op the message to 'approved' - // and removes the metamaskId for signing - return this.messageManager.approveMessage(msgParams) - .then((cleanMsgParams) => { - // signs the message - return this.keyringController.signMessage(cleanMsgParams) - }) - .then((rawSig) => { - // tells the listener that the message has been signed - // and can be returned to the dapp - this.messageManager.brodcastMessage(rawSig, msgId, 'approved') - }).then(() => { - cb() - }).catch((err) => cb(err)) + promiseToCallback( + // sets the status op the message to 'approved' + // and removes the metamaskId for signing + this.messageManager.approveMessage(msgParams) + .then((cleanMsgParams) => { + // signs the message + return this.keyringController.signMessage(cleanMsgParams) + }) + .then((rawSig) => { + // tells the listener that the message has been signed + // and can be returned to the dapp + this.messageManager.setMsgStatusSigned(msgId, rawSig) + }) + )(cb) } -- cgit v1.2.3 From 13ee92909cce93b37eec2092757e4aab174a970e Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 3 Feb 2017 20:45:20 -0800 Subject: Mostly got shapeshift tx management into its own controller Rendering the list is still having issues, so this isn't done yet. --- app/scripts/metamask-controller.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'app/scripts/metamask-controller.js') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 066e389e2..fb2040c63 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -14,6 +14,7 @@ const KeyringController = require('./keyring-controller') const PreferencesController = require('./lib/controllers/preferences') const CurrencyController = require('./lib/controllers/currency') const NoticeController = require('./notice-controller') +const ShapeShiftController = require('./lib/controllers/shapeshift') const MessageManager = require('./lib/message-manager') const TxManager = require('./transaction-manager') const ConfigManager = require('./lib/config-manager') @@ -98,6 +99,10 @@ module.exports = class MetamaskController extends EventEmitter { // to be uncommented when retrieving notices from a remote server. // this.noticeController.startPolling() + this.shapeshiftController = new ShapeShiftController({ + initState: initState.ShapeShiftController, + }) + this.lookupNetwork() this.messageManager = new MessageManager() this.publicConfigStore = this.initPublicConfigStore() @@ -125,6 +130,9 @@ module.exports = class MetamaskController extends EventEmitter { this.noticeController.store.subscribe((state) => { this.store.updateState({ NoticeController: state }) }) + this.shapeshiftController.store.subscribe((state) => { + this.store.updateState({ ShapeShiftController: state }) + }) // manual mem state subscriptions this.networkStore.subscribe(this.sendUpdate.bind(this)) @@ -135,6 +143,7 @@ module.exports = class MetamaskController extends EventEmitter { this.preferencesController.store.subscribe(this.sendUpdate.bind(this)) this.currencyController.store.subscribe(this.sendUpdate.bind(this)) this.noticeController.memStore.subscribe(this.sendUpdate.bind(this)) + this.shapeshiftController.store.subscribe(this.sendUpdate.bind(this)) } // @@ -207,8 +216,8 @@ module.exports = class MetamaskController extends EventEmitter { this.noticeController.memStore.getState(), // config manager this.configManager.getConfig(), + this.shapeshiftController.store.getState(), { - shapeShiftTxList: this.configManager.getShapeShiftTxList(), lostAccounts: this.configManager.getLostAccounts(), isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), seedWords: this.configManager.getSeedWords(), @@ -327,7 +336,7 @@ module.exports = class MetamaskController extends EventEmitter { ) } - sendUpdate () { + sendUpdate () { this.emit('update', this.getState()) } @@ -597,7 +606,7 @@ module.exports = class MetamaskController extends EventEmitter { } createShapeShiftTx (depositAddress, depositType) { - this.configManager.createShapeShiftTx(depositAddress, depositType) + this.shapeshiftController.createShapeShiftTx(depositAddress, depositType) } setGasMultiplier (gasMultiplier, cb) { -- cgit v1.2.3