diff options
Diffstat (limited to 'app/scripts/lib')
-rw-r--r-- | app/scripts/lib/config-manager-singleton.js | 3 | ||||
-rw-r--r-- | app/scripts/lib/config-manager.js | 56 | ||||
-rw-r--r-- | app/scripts/lib/id-management.js | 6 | ||||
-rw-r--r-- | app/scripts/lib/idStore.js | 28 | ||||
-rw-r--r-- | app/scripts/lib/notifications.js | 126 |
5 files changed, 107 insertions, 112 deletions
diff --git a/app/scripts/lib/config-manager-singleton.js b/app/scripts/lib/config-manager-singleton.js deleted file mode 100644 index 5915c401b..000000000 --- a/app/scripts/lib/config-manager-singleton.js +++ /dev/null @@ -1,3 +0,0 @@ -var ConfigManager = require('./config-manager') - -module.exports = new ConfigManager() diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index a3ff0bdfb..0af82c89c 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -1,9 +1,7 @@ const Migrator = require('pojo-migrator') -const extend = require('xtend') const MetamaskConfig = require('../config.js') const migrations = require('./migrations') -const STORAGE_KEY = 'metamask-config' const TESTNET_RPC = MetamaskConfig.network.testnet const MAINNET_RPC = MetamaskConfig.network.mainnet @@ -15,7 +13,7 @@ const MAINNET_RPC = MetamaskConfig.network.mainnet * particular portions of the state. */ module.exports = ConfigManager -function ConfigManager () { +function ConfigManager (opts) { // ConfigManager is observable and will emit updates this._subs = [] @@ -37,12 +35,10 @@ function ConfigManager () { // How to load initial config. // Includes step on migrating pre-pojo-migrator data. - loadData: loadData, + loadData: opts.loadData, // How to persist migrated config. - setData: function (data) { - window.localStorage[STORAGE_KEY] = JSON.stringify(data) - }, + setData: opts.setData, }) } @@ -280,49 +276,3 @@ ConfigManager.prototype.getConfirmed = function () { return ('isConfirmed' in data) && data.isConfirmed } -function loadData () { - var oldData = getOldStyleData() - var newData - try { - newData = JSON.parse(window.localStorage[STORAGE_KEY]) - } catch (e) {} - - var data = extend({ - meta: { - version: 0, - }, - data: { - config: { - provider: { - type: 'testnet', - }, - }, - }, - }, oldData || null, newData || null) - return data -} - -function getOldStyleData () { - var config, wallet, seedWords - - var result = { - meta: { version: 0 }, - data: {}, - } - - try { - config = JSON.parse(window.localStorage['config']) - result.data.config = config - } catch (e) {} - try { - wallet = JSON.parse(window.localStorage['lightwallet']) - result.data.wallet = wallet - } catch (e) {} - try { - seedWords = window.localStorage['seedWords'] - result.data.seedWords = seedWords - } catch (e) {} - - return result -} - diff --git a/app/scripts/lib/id-management.js b/app/scripts/lib/id-management.js index cc50bd649..9b8ceb415 100644 --- a/app/scripts/lib/id-management.js +++ b/app/scripts/lib/id-management.js @@ -1,6 +1,5 @@ const ethUtil = require('ethereumjs-util') const Transaction = require('ethereumjs-tx') -const configManager = require('./config-manager-singleton') module.exports = IdManagement @@ -9,6 +8,7 @@ function IdManagement (opts) { this.keyStore = opts.keyStore this.derivedKey = opts.derivedKey + this.configManager = opts.configManager this.hdPathString = "m/44'/60'/0'/0" this.getAddresses = function () { @@ -32,9 +32,9 @@ function IdManagement (opts) { // Add the tx hash to the persisted meta-tx object var txHash = ethUtil.bufferToHex(tx.hash()) - var metaTx = configManager.getTx(txParams.metamaskId) + var metaTx = this.configManager.getTx(txParams.metamaskId) metaTx.hash = txHash - configManager.updateTx(metaTx) + this.configManager.updateTx(metaTx) // return raw serialized tx var rawTx = ethUtil.bufferToHex(tx.serialize()) diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index d9657dacf..f705c07a7 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -7,7 +7,6 @@ const extend = require('xtend') const createId = require('web3-provider-engine/util/random-id') const ethBinToOps = require('eth-bin-to-ops') const autoFaucet = require('./auto-faucet') -const configManager = require('./config-manager-singleton') const messageManager = require('./message-manager') const DEFAULT_RPC = 'https://testrpc.metamask.io/' const IdManagement = require('./id-management') @@ -20,6 +19,7 @@ function IdentityStore (opts = {}) { // we just use the ethStore to auto-add accounts this._ethStore = opts.ethStore + this.configManager = opts.configManager // lightwallet key store this._keyStore = null // lightwallet wrapper @@ -43,7 +43,10 @@ function IdentityStore (opts = {}) { IdentityStore.prototype.createNewVault = function (password, entropy, cb) { delete this._keyStore - configManager.clearWallet() + if (this.configManager) { + this.configManager.clearWallet() + } + this._createIdmgmt(password, null, entropy, (err) => { if (err) return cb(err) @@ -51,14 +54,14 @@ IdentityStore.prototype.createNewVault = function (password, entropy, cb) { this._didUpdate() this._autoFaucet() - configManager.setShowSeedWords(true) + this.configManager.setShowSeedWords(true) var seedWords = this._idmgmt.getSeed() cb(null, seedWords) }) } IdentityStore.prototype.recoverSeed = function (cb) { - configManager.setShowSeedWords(true) + this.configManager.setShowSeedWords(true) if (!this._idmgmt) return cb(new Error('Unauthenticated. Please sign in.')) var seedWords = this._idmgmt.getSeed() cb(null, seedWords) @@ -79,11 +82,13 @@ IdentityStore.prototype.setStore = function (store) { } IdentityStore.prototype.clearSeedWordCache = function (cb) { + const configManager = this.configManager configManager.setShowSeedWords(false) cb(null, configManager.getSelectedAccount()) } IdentityStore.prototype.getState = function () { + const configManager = this.configManager var seedWords = this.getSeedIfUnlocked() return clone(extend(this._currentState, { isInitialized: !!configManager.getWallet() && !seedWords, @@ -99,6 +104,7 @@ IdentityStore.prototype.getState = function () { } IdentityStore.prototype.getSeedIfUnlocked = function () { + const configManager = this.configManager var showSeed = configManager.getShouldShowSeedWords() var idmgmt = this._idmgmt var shouldShow = showSeed && !!idmgmt @@ -107,10 +113,12 @@ IdentityStore.prototype.getSeedIfUnlocked = function () { } IdentityStore.prototype.getSelectedAddress = function () { + const configManager = this.configManager return configManager.getSelectedAccount() } IdentityStore.prototype.setSelectedAddress = function (address, cb) { + const configManager = this.configManager if (!address) { var addresses = this._getAddresses() address = addresses[0] @@ -123,6 +131,7 @@ IdentityStore.prototype.setSelectedAddress = function (address, cb) { IdentityStore.prototype.revealAccount = function (cb) { const derivedKey = this._idmgmt.derivedKey const keyStore = this._keyStore + const configManager = this.configManager keyStore.setDefaultHdDerivationPath(this.hdPathString) keyStore.generateNewAddress(derivedKey, 1) @@ -158,6 +167,7 @@ IdentityStore.prototype.setLocked = function (cb) { } IdentityStore.prototype.submitPassword = function (password, cb) { + const configManager = this.configManager this.tryPassword(password, (err) => { if (err) return cb(err) // load identities before returning... @@ -177,6 +187,7 @@ IdentityStore.prototype.exportAccount = function (address, cb) { // comes from dapp via zero-client hooked-wallet provider IdentityStore.prototype.addUnconfirmedTransaction = function (txParams, onTxDoneCb, cb) { + const configManager = this.configManager var self = this // create txData obj with parameters and meta data var time = (new Date()).getTime() @@ -227,6 +238,7 @@ IdentityStore.prototype.addUnconfirmedTransaction = function (txParams, onTxDone // comes from metamask ui IdentityStore.prototype.approveTransaction = function (txId, cb) { + const configManager = this.configManager var approvalCb = this._unconfTxCbs[txId] || noop // accept tx @@ -240,6 +252,7 @@ IdentityStore.prototype.approveTransaction = function (txId, cb) { // comes from metamask ui IdentityStore.prototype.cancelTransaction = function (txId) { + const configManager = this.configManager var approvalCb = this._unconfTxCbs[txId] || noop // reject tx @@ -347,6 +360,7 @@ IdentityStore.prototype._isUnlocked = function () { // load identities from keyStoreet IdentityStore.prototype._loadIdentities = function () { + const configManager = this.configManager if (!this._isUnlocked()) throw new Error('not unlocked') var addresses = this._getAddresses() @@ -367,6 +381,7 @@ IdentityStore.prototype._loadIdentities = function () { } IdentityStore.prototype.saveAccountLabel = function (account, label, cb) { + const configManager = this.configManager configManager.setNicknameForWallet(account, label) this._loadIdentities() cb(null, label) @@ -379,6 +394,7 @@ IdentityStore.prototype.saveAccountLabel = function (account, label, cb) { // If there is no balance and it mayBeFauceting, // then it is in fact fauceting. IdentityStore.prototype._mayBeFauceting = function (i) { + const configManager = this.configManager var config = configManager.getProvider() if (i === 0 && config.type === 'rpc' && @@ -397,6 +413,7 @@ IdentityStore.prototype.tryPassword = function (password, cb) { } IdentityStore.prototype._createIdmgmt = function (password, seed, entropy, cb) { + const configManager = this.configManager var keyStore = null LightwalletKeyStore.deriveKeyFromPassword(password, (err, derivedKey) => { if (err) return cb(err) @@ -425,6 +442,7 @@ IdentityStore.prototype._createIdmgmt = function (password, seed, entropy, cb) { keyStore: keyStore, derivedKey: derivedKey, hdPathSTring: this.hdPathString, + configManager: this.configManager, }) cb() @@ -432,6 +450,7 @@ IdentityStore.prototype._createIdmgmt = function (password, seed, entropy, cb) { } IdentityStore.prototype._restoreFromSeed = function (password, seed, derivedKey) { + const configManager = this.configManager var keyStore = new LightwalletKeyStore(seed, derivedKey, this.hdPathString) keyStore.addHdDerivationPath(this.hdPathString, derivedKey, {curve: 'secp256k1', purpose: 'sign'}) keyStore.setDefaultHdDerivationPath(this.hdPathString) @@ -443,6 +462,7 @@ IdentityStore.prototype._restoreFromSeed = function (password, seed, derivedKey) } IdentityStore.prototype._createFirstWallet = function (entropy, derivedKey) { + const configManager = this.configManager var secretSeed = LightwalletKeyStore.generateRandomSeed(entropy) var keyStore = new LightwalletKeyStore(secretSeed, derivedKey, this.hdPathString) keyStore.addHdDerivationPath(this.hdPathString, derivedKey, {curve: 'secp256k1', purpose: 'sign'}) diff --git a/app/scripts/lib/notifications.js b/app/scripts/lib/notifications.js index 5762fd26b..a5746ae6e 100644 --- a/app/scripts/lib/notifications.js +++ b/app/scripts/lib/notifications.js @@ -1,10 +1,11 @@ const createId = require('hat') +const extend = require('xtend') const unmountComponentAtNode = require('react-dom').unmountComponentAtNode const findDOMNode = require('react-dom').findDOMNode const render = require('react-dom').render const h = require('react-hyperscript') -const uiUtils = require('../../../ui/app/util') -const renderPendingTx = require('../../../ui/app/components/pending-tx').prototype.renderGeneric +const PendingTxDetails = require('../../../ui/app/components/pending-tx-details') +const PendingMsgDetails = require('../../../ui/app/components/pending-msg-details') const MetaMaskUiCss = require('../../../ui/css') var notificationHandlers = {} @@ -56,29 +57,29 @@ function createTxNotification (opts) { // guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236 if (!chrome.notifications) return console.error('Chrome notifications API missing...') - renderTransactionNotificationSVG(opts, function(err, source){ + var state = { + title: 'New Unsigned Transaction', + imageifyIdenticons: false, + txData: { + txParams: opts.txParams, + time: (new Date()).getTime(), + }, + identities: { + + }, + accounts: { + + }, + onConfirm: opts.confirm, + onCancel: opts.cancel, + } + + renderTxNotificationSVG(state, function(err, notificationSvgSource){ if (err) throw err - - var imageUrl = 'data:image/svg+xml;utf8,' + encodeURIComponent(source) - - var id = createId() - chrome.notifications.create(id, { - type: 'image', - // requireInteraction: true, - iconUrl: '/images/icon-128.png', - imageUrl: imageUrl, - title: opts.title, - message: '', - buttons: [{ - title: 'confirm', - }, { - title: 'cancel', - }], - }) - notificationHandlers[id] = { - confirm: opts.confirm, - cancel: opts.cancel, - } + + showNotification(extend(state, { + imageUrl: toSvgUri(notificationSvgSource), + })) }) } @@ -86,19 +87,46 @@ function createTxNotification (opts) { function createMsgNotification (opts) { // guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236 if (!chrome.notifications) return console.error('Chrome notifications API missing...') - var message = [ - 'Submitted by ' + opts.msgParams.origin, - 'to be signed by: ' + uiUtils.addressSummary(opts.msgParams.from), - 'message:\n' + opts.msgParams.data, - ].join('\n') + + var state = { + title: 'New Unsigned Message', + imageifyIdenticons: false, + txData: { + msgParams: opts.msgParams, + time: (new Date()).getTime(), + }, + identities: { + + }, + accounts: { + + }, + onConfirm: opts.confirm, + onCancel: opts.cancel, + } + + renderMsgNotificationSVG(state, function(err, notificationSvgSource){ + if (err) throw err + + showNotification(extend(state, { + imageUrl: toSvgUri(notificationSvgSource), + })) + + }) +} + +function showNotification (state) { + // guard for chrome bug https://github.com/MetaMask/metamask-plugin/issues/236 + if (!chrome.notifications) return console.error('Chrome notifications API missing...') var id = createId() chrome.notifications.create(id, { - type: 'basic', + type: 'image', requireInteraction: true, iconUrl: '/images/icon-128.png', - title: opts.title, - message: message, + imageUrl: state.imageUrl, + title: state.title, + message: '', buttons: [{ title: 'confirm', }, { @@ -106,27 +134,23 @@ function createMsgNotification (opts) { }], }) notificationHandlers[id] = { - confirm: opts.confirm, - cancel: opts.cancel, + confirm: state.onConfirm, + cancel: state.onCancel, } -} -function renderTransactionNotificationSVG(opts, cb){ - var state = { - nonInteractive: true, - inlineIdenticons: true, - txData: { - txParams: opts.txParams, - time: (new Date()).getTime(), - }, - identities: { +} - }, - accounts: { +function renderTxNotificationSVG(state, cb){ + var content = h(PendingTxDetails, state) + renderNotificationSVG(content, cb) +} - }, - } +function renderMsgNotificationSVG(state, cb){ + var content = h(PendingMsgDetails, state) + renderNotificationSVG(content, cb) +} +function renderNotificationSVG(content, cb){ var container = document.createElement('div') var confirmView = h('div.app-primary', { style: { @@ -138,7 +162,7 @@ function renderTransactionNotificationSVG(opts, cb){ }, }, [ h('style', MetaMaskUiCss()), - renderPendingTx(h, state), + content, ]) render(confirmView, container, function ready(){ @@ -160,4 +184,8 @@ function svgWrapper(content){ </svg> ` return wrapperSource.split('{{content}}').join(content) +} + +function toSvgUri(content){ + return 'data:image/svg+xml;utf8,' + encodeURIComponent(content) }
\ No newline at end of file |