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(-) 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