diff options
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/background.js | 8 | ||||
-rw-r--r-- | app/scripts/lib/config-manager.js | 67 | ||||
-rw-r--r-- | app/scripts/lib/idStore.js | 27 | ||||
-rw-r--r-- | app/scripts/lib/message-manager.js | 61 |
4 files changed, 87 insertions, 76 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js index 3c46f4693..0f9ecc1c9 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -10,6 +10,7 @@ const IdentityStore = require('./lib/idStore') const createTxNotification = require('./lib/notifications.js').createTxNotification const createMsgNotification = require('./lib/notifications.js').createMsgNotification const configManager = require('./lib/config-manager-singleton') +const messageManager = require('./lib/message-manager') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const HostStore = require('./lib/remote-store.js').HostStore const Web3 = require('web3') @@ -175,6 +176,8 @@ function setupControllerConnection(stream){ setSelectedAddress: idStore.setSelectedAddress.bind(idStore), approveTransaction: idStore.approveTransaction.bind(idStore), cancelTransaction: idStore.cancelTransaction.bind(idStore), + signMessage: idStore.signMessage.bind(idStore), + cancelMessage: idStore.cancelMessage.bind(idStore), setLocked: idStore.setLocked.bind(idStore), clearSeedWordCache: idStore.clearSeedWordCache.bind(idStore), exportAccount: idStore.exportAccount.bind(idStore), @@ -206,7 +209,10 @@ idStore.on('update', updateBadge) function updateBadge(state){ var label = '' var unconfTxs = configManager.unconfirmedTxs() - var count = Object.keys(unconfTxs).length + var unconfTxLen = Object.keys(unconfTxs).length + var unconfMsgs = messageManager.unconfirmedMsgs() + var unconfMsgLen = Object.keys(unconfMsgs).length + var count = unconfTxLen + unconfMsgLen if (count) { label = String(count) } diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index 0e7454dfd..5bfb8befe 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -211,73 +211,6 @@ ConfigManager.prototype.updateTx = function(tx) { this._saveTxList(transactions) } -// -// Msg -// - -ConfigManager.prototype.getMsgList = function() { - var data = this.migrator.getData() - if (data.messages !== undefined) { - return data.messages - } else { - return [] - } -} - -ConfigManager.prototype.unconfirmedMsgs = function() { - var messages = this.getMsgList() - return messages.filter(msg => msg.status === 'unconfirmed') - .reduce((result, msg) => { result[msg.id] = msg; return result }, {}) -} - -ConfigManager.prototype._saveMsgList = function(msgList) { - var data = this.migrator.getData() - data.messages = msgList - this.setData(data) -} - -ConfigManager.prototype.addMsg = function(msg) { - var messages = this.getMsgList() - messages.push(msg) - this._saveMsgList(messages) -} - -ConfigManager.prototype.getMsg = function(msgId) { - var messages = this.getMsgList() - var matching = messages.filter(msg => msg.id === msgId) - return matching.length > 0 ? matching[0] : null -} - -ConfigManager.prototype.confirmMsg = function(msgId) { - this._setMsgStatus(msgId, 'confirmed') -} - -ConfigManager.prototype.rejectMsg = function(msgId) { - this._setMsgStatus(msgId, 'rejected') -} - -ConfigManager.prototype._setMsgStatus = function(msgId, status) { - var msg = this.getMsg(msgId) - msg.status = status - this.updateMsg(msg) -} - -ConfigManager.prototype.updateMsg = function(msg) { - var messages = this.getMsgList() - var found, index - messages.forEach((otherMsg, i) => { - if (otherMsg.id === msg.id) { - found = true - index = i - } - }) - if (found) { - messages[index] = msg - } - this._saveMsgList(messages) -} - - // observable diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index c25d83c9d..b8d825d8b 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -9,6 +9,7 @@ const extend = require('xtend') const createId = require('web3-provider-engine/util/random-id') const autoFaucet = require('./auto-faucet') const configManager = require('./config-manager-singleton') +const messageManager = require('./message-manager') const DEFAULT_RPC = 'https://testrpc.metamask.io/' @@ -32,6 +33,7 @@ function IdentityStore(opts = {}) { selectedAddress: null, identities: {}, } + // not part of serilized metamask state - only kept in memory this._unconfTxCbs = {} this._unconfMsgCbs = {} @@ -85,6 +87,8 @@ IdentityStore.prototype.getState = function(){ seedWords: seedWords, unconfTxs: configManager.unconfirmedTxs(), transactions: configManager.getTxList(), + unconfMsgs: messageManager.unconfirmedMsgs(), + messages: messageManager.getMsgList(), selectedAddress: configManager.getSelectedAccount(), })) } @@ -226,7 +230,7 @@ IdentityStore.prototype.addUnconfirmedMessage = function(msgParams, cb){ time: time, status: 'unconfirmed', } - configManager.addMsg(msgData) + messageManager.addMsg(msgData) console.log('addUnconfirmedMessage:', msgData) // keep the cb around for after approval (requires user interaction) @@ -241,27 +245,27 @@ IdentityStore.prototype.addUnconfirmedMessage = function(msgParams, cb){ // comes from metamask ui IdentityStore.prototype.approveMessage = function(msgId, cb){ - var msgData = configManager.getMsg(msgId) + var msgData = messageManager.getMsg(msgId) var approvalCb = this._unconfMsgCbs[msgId] || noop // accept msg cb() approvalCb(null, true) // clean up - configManager.confirmMsg(msgId) + messageManager.confirmMsg(msgId) delete this._unconfMsgCbs[msgId] this._didUpdate() } // comes from metamask ui IdentityStore.prototype.cancelMessage = function(msgId){ - var txData = configManager.getMsg(msgId) + var txData = messageManager.getMsg(msgId) var approvalCb = this._unconfMsgCbs[msgId] || noop // reject tx approvalCb(null, false) // clean up - configManager.rejectMsg(msgId) + messageManager.rejectMsg(msgId) delete this._unconfTxCbs[msgId] this._didUpdate() } @@ -271,7 +275,14 @@ IdentityStore.prototype.signMessage = function(msgParams, cb){ try { console.log('signing msg...', msgParams.data) var rawMsg = this._idmgmt.signMsg(msgParams.from, msgParams.data) - cb(null, rawMsg) + if ('metamaskId' in msgParams) { + var id = msgParams.metamaskId + delete msgParams.metamaskId + + this.approveMessage(id, cb) + } else { + cb(null, rawMsg) + } } catch (err) { cb(err) } @@ -426,7 +437,7 @@ function IdManagement(opts) { var privKeyHex = this.exportPrivateKey(txParams.from) var privKey = ethUtil.toBuffer(privKeyHex) tx.sign(privKey) - + // Add the tx hash to the persisted meta-tx object var txHash = ethUtil.bufferToHex(tx.hash()) var metaTx = configManager.getTx(txParams.metamaskId) @@ -472,4 +483,4 @@ function concatSig(v, r, s) { s = ethUtil.toUnsigned(s).toString('hex') v = ethUtil.stripHexPrefix(ethUtil.intToHex(v)) return ethUtil.addHexPrefix(r.concat(s, v).toString("hex")) -}
\ No newline at end of file +} diff --git a/app/scripts/lib/message-manager.js b/app/scripts/lib/message-manager.js new file mode 100644 index 000000000..91edb7759 --- /dev/null +++ b/app/scripts/lib/message-manager.js @@ -0,0 +1,61 @@ +module.exports = new MessageManager() + +function MessageManager(opts) { + this.messages = [] +} + +MessageManager.prototype.getMsgList = function() { + return this.messages +} + +MessageManager.prototype.unconfirmedMsgs = function() { + var messages = this.getMsgList() + return messages.filter(msg => msg.status === 'unconfirmed') + .reduce((result, msg) => { result[msg.id] = msg; return result }, {}) +} + +MessageManager.prototype._saveMsgList = function(msgList) { + this.messages = msgList +} + +MessageManager.prototype.addMsg = function(msg) { + var messages = this.getMsgList() + messages.push(msg) + this._saveMsgList(messages) +} + +MessageManager.prototype.getMsg = function(msgId) { + var messages = this.getMsgList() + var matching = messages.filter(msg => msg.id === msgId) + return matching.length > 0 ? matching[0] : null +} + +MessageManager.prototype.confirmMsg = function(msgId) { + this._setMsgStatus(msgId, 'confirmed') +} + +MessageManager.prototype.rejectMsg = function(msgId) { + this._setMsgStatus(msgId, 'rejected') +} + +MessageManager.prototype._setMsgStatus = function(msgId, status) { + var msg = this.getMsg(msgId) + if (msg) msg.status = status + this.updateMsg(msg) +} + +MessageManager.prototype.updateMsg = function(msg) { + var messages = this.getMsgList() + var found, index + messages.forEach((otherMsg, i) => { + if (otherMsg.id === msg.id) { + found = true + index = i + } + }) + if (found) { + messages[index] = msg + } + this._saveMsgList(messages) +} + |