aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/scripts/background.js9
-rw-r--r--app/scripts/keyring-controller.js107
-rw-r--r--app/scripts/lib/message-manager.js148
-rw-r--r--app/scripts/metamask-controller.js67
-rw-r--r--app/scripts/transaction-manager.js2
5 files changed, 168 insertions, 165 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js
index da9c4f24b..2e5a992b9 100644
--- a/app/scripts/background.js
+++ b/app/scripts/background.js
@@ -8,7 +8,6 @@ const Migrator = require('./lib/migrator/')
const migrations = require('./migrations/')
const PortStream = require('./lib/port-stream.js')
const notification = require('./lib/notifications.js')
-const messageManager = require('./lib/message-manager')
const MetamaskController = require('./metamask-controller')
const extension = require('./lib/extension')
const firstTimeState = require('./first-time-state')
@@ -112,14 +111,14 @@ function setupController (initState) {
updateBadge()
controller.txManager.on('updateBadge', updateBadge)
+ controller.messageManager.on('updateBadge', updateBadge)
// plugin badge text
function updateBadge () {
var label = ''
var unapprovedTxCount = controller.txManager.unapprovedTxCount
- var unconfMsgs = messageManager.unconfirmedMsgs()
- var unconfMsgLen = Object.keys(unconfMsgs).length
- var count = unapprovedTxCount + unconfMsgLen
+ var unapprovedMsgCount = controller.messageManager.unapprovedMsgCount
+ var count = unapprovedTxCount + unapprovedMsgCount
if (count) {
label = String(count)
}
@@ -145,4 +144,4 @@ extension.runtime.onInstalled.addListener(function (details) {
if ((details.reason === 'install') && (!METAMASK_DEBUG)) {
extension.tabs.create({url: 'https://metamask.io/#how-it-works'})
}
-}) \ No newline at end of file
+})
diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js
index be54ab00b..12e3d2844 100644
--- a/app/scripts/keyring-controller.js
+++ b/app/scripts/keyring-controller.js
@@ -5,11 +5,7 @@ const EventEmitter = require('events').EventEmitter
const ObservableStore = require('obs-store')
const filter = require('promise-filter')
const encryptor = require('browser-passworder')
-const createId = require('./lib/random-id')
const normalizeAddress = require('./lib/sig-util').normalize
-const messageManager = require('./lib/message-manager')
-function noop () {}
-
// Keyrings:
const SimpleKeyring = require('./keyrings/simple')
const HdKeyring = require('./keyrings/hd')
@@ -18,7 +14,6 @@ const keyringTypes = [
HdKeyring,
]
-
class KeyringController extends EventEmitter {
// PUBLIC METHODS
@@ -42,9 +37,7 @@ class KeyringController extends EventEmitter {
this.ethStore = opts.ethStore
this.encryptor = encryptor
this.keyrings = []
-
- this._unconfMsgCbs = {}
-
+ this.identities = {} // Essentially a name hash
this.getNetwork = opts.getNetwork
}
@@ -78,6 +71,7 @@ class KeyringController extends EventEmitter {
// in this class, but will need to be Promisified when we move our
// persistence to an async model.
getState () {
+
// old wallet
const memState = this.memStore.getState()
const result = {
@@ -87,9 +81,6 @@ class KeyringController extends EventEmitter {
keyringTypes: memState.keyringTypes,
identities: memState.identities,
keyrings: memState.keyrings,
- // messageManager
- unconfMsgs: messageManager.unconfirmedMsgs(),
- messages: messageManager.getMsgList(),
// configManager
seedWords: this.configManager.getSeedWords(),
isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(),
@@ -153,6 +144,17 @@ class KeyringController extends EventEmitter {
.then(this.fullUpdate.bind(this))
}
+ // ClearSeedWordCache
+ //
+ // returns Promise( @string currentSelectedAccount )
+ //
+ // Removes the current vault's seed words from the UI's state tree,
+ // ensuring they are only ever available in the background process.
+ clearSeedWordCache () {
+ this.configManager.setSeedWords(null)
+ return Promise.resolve(this.configManager.getSelectedAccount())
+ }
+
// Set Locked
// returns Promise( @object state )
//
@@ -204,8 +206,8 @@ class KeyringController extends EventEmitter {
this.keyrings.push(keyring)
return this.setupAccounts(accounts)
})
- .then(() => this.persistAllKeyrings())
- .then(() => this.fullUpdate())
+ .then(() => { return this.password })
+ .then(this.persistAllKeyrings.bind(this))
.then(() => {
return keyring
})
@@ -284,86 +286,19 @@ class KeyringController extends EventEmitter {
return keyring.signTransaction(fromAddress, ethTx)
})
}
- // Add Unconfirmed Message
- // @object msgParams
- // @function cb
- //
- // Does not call back, only emits an `update` event.
- //
- // Adds the given `msgParams` and `cb` to a local cache,
- // for displaying to a user for approval before signing or canceling.
- addUnconfirmedMessage (msgParams, cb) {
- // create txData obj with parameters and meta data
- var time = (new Date()).getTime()
- var msgId = createId()
- var msgData = {
- id: msgId,
- msgParams: msgParams,
- time: time,
- status: 'unconfirmed',
- }
- messageManager.addMsg(msgData)
- console.log('addUnconfirmedMessage:', msgData)
-
- // keep the cb around for after approval (requires user interaction)
- // This cb fires completion to the Dapp's write operation.
- this._unconfMsgCbs[msgId] = cb
-
- // signal update
- this.emit('update')
- return msgId
- }
-
- // Cancel Message
- // @string msgId
- // @function cb (optional)
- //
- // Calls back to cached `unconfMsgCb`.
- // Calls back to `cb` if provided.
- //
- // Forgets any messages matching `msgId`.
- cancelMessage (msgId, cb) {
- var approvalCb = this._unconfMsgCbs[msgId] || noop
-
- // reject tx
- approvalCb(null, false)
- // clean up
- messageManager.rejectMsg(msgId)
- delete this._unconfTxCbs[msgId]
-
- if (cb && typeof cb === 'function') {
- cb()
- }
- }
// Sign Message
// @object msgParams
- // @function cb
//
// returns Promise(@buffer rawSig)
- // calls back @function cb with @buffer rawSig
- // calls back cached Dapp's @function unconfMsgCb.
//
// Attempts to sign the provided @object msgParams.
- signMessage (msgParams, cb) {
- try {
- const msgId = msgParams.metamaskId
- delete msgParams.metamaskId
- const approvalCb = this._unconfMsgCbs[msgId] || noop
-
- const address = normalizeAddress(msgParams.from)
- return this.getKeyringForAccount(address)
- .then((keyring) => {
- return keyring.signMessage(address, msgParams.data)
- }).then((rawSig) => {
- cb(null, rawSig)
- approvalCb(null, true)
- messageManager.confirmMsg(msgId)
- return rawSig
- })
- } catch (e) {
- cb(e)
- }
+ signMessage (msgParams) {
+ const address = normalizeAddress(msgParams.from)
+ return this.getKeyringForAccount(address)
+ .then((keyring) => {
+ return keyring.signMessage(address, msgParams.data)
+ })
}
// PRIVATE METHODS
diff --git a/app/scripts/lib/message-manager.js b/app/scripts/lib/message-manager.js
index b609b820e..490cd4d1c 100644
--- a/app/scripts/lib/message-manager.js
+++ b/app/scripts/lib/message-manager.js
@@ -1,61 +1,113 @@
-module.exports = new MessageManager()
+const EventEmitter = require('events')
+const ObservableStore = require('obs-store')
+const createId = require('./random-id')
-function MessageManager (opts) {
- this.messages = []
-}
-MessageManager.prototype.getMsgList = function () {
- return this.messages
-}
+module.exports = class MessageManager extends EventEmitter{
+ constructor (opts) {
+ super()
+ this.memStore = new ObservableStore({ 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 }, {})
-}
+ getState() {
+ return {
+ unapprovedMsgs: this.getUnapprovedMsgs(),
+ messages: this.getMsgList(),
+ }
+ }
-MessageManager.prototype._saveMsgList = function (msgList) {
- this.messages = msgList
-}
+ getMsgList () {
+ return this.memStore.getState().messages
+ }
-MessageManager.prototype.addMsg = function (msg) {
- var messages = this.getMsgList()
- messages.push(msg)
- this._saveMsgList(messages)
-}
+ get unapprovedMsgCount () {
+ return Object.keys(this.getUnapprovedMsgs()).length
+ }
-MessageManager.prototype.getMsg = function (msgId) {
- var messages = this.getMsgList()
- var matching = messages.filter(msg => msg.id === msgId)
- return matching.length > 0 ? matching[0] : null
-}
+ getUnapprovedMsgs () {
+ let messages = this.getMsgList()
+ return messages.filter(msg => msg.status === 'unapproved')
+ .reduce((result, msg) => { result[msg.id] = msg; return result }, {})
+ }
-MessageManager.prototype.confirmMsg = function (msgId) {
- this._setMsgStatus(msgId, 'confirmed')
-}
+ addUnapprovedMessage (msgParams) {
+ // create txData obj with parameters and meta data
+ var time = (new Date()).getTime()
+ var msgId = createId()
+ var msgData = {
+ id: msgId,
+ msgParams: msgParams,
+ time: time,
+ status: 'unapproved',
+ }
+ this.addMsg(msgData)
+ console.log('addUnapprovedMessage:', msgData)
-MessageManager.prototype.rejectMsg = function (msgId) {
- this._setMsgStatus(msgId, 'rejected')
-}
+ // keep the cb around for after approval (requires user interaction)
+ // This cb fires completion to the Dapp's write operation.
-MessageManager.prototype._setMsgStatus = function (msgId, status) {
- var msg = this.getMsg(msgId)
- if (msg) msg.status = status
- this.updateMsg(msg)
-}
+ // signal update
+ this.emit('update')
+ return msgId
+ }
+
+ addMsg (msg) {
+ let messages = this.getMsgList()
+ messages.push(msg)
+ this._saveMsgList(messages)
+ }
-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
+ getMsg (msgId) {
+ let messages = this.getMsgList()
+ let matching = messages.filter(msg => msg.id === msgId)
+ return matching.length > 0 ? matching[0] : null
+ }
+
+ approveMessage (msgParams) {
+ this.setMsgStatusApproved(msgParams.metamaskId)
+ return this.prepMsgForSigning(msgParams)
+ }
+
+ setMsgStatusApproved (msgId) {
+ this._setMsgStatus(msgId, 'approved')
+ }
+
+ prepMsgForSigning (msgParams) {
+ delete msgParams.metamaskId
+ return Promise.resolve(msgParams)
+ }
+
+ rejectMsg (msgId) {
+ this.brodcastMessage(null, msgId, 'rejected')
+ this._setMsgStatus(msgId, 'rejected')
+ }
+
+ brodcastMessage (rawSig, msgId, status) {
+ this.emit(`${msgId}:finished`, {status, rawSig})
+ }
+// PRIVATE METHODS
+
+ _setMsgStatus (msgId, status) {
+ let msg = this.getMsg(msgId)
+ if (msg) msg.status = status
+ this._updateMsg(msg)
+ }
+
+ _updateMsg (msg) {
+ let messages = this.getMsgList()
+ let index = messages.findIndex((message) => message.id === msg.id)
+ if (index !== -1) {
+ messages[index] = msg
}
- })
- if (found) {
- messages[index] = msg
+ this._saveMsgList(messages)
}
- this._saveMsgList(messages)
-}
+ _saveMsgList (msgList) {
+ this.emit('updateBadge')
+ let state = this.memStore.getState()
+ state.messages = msgList
+ this.memStore.putState(state)
+ }
+
+
+}
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index 222a1d618..fb5234c24 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -13,7 +13,7 @@ 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 MessageManager = require('./lib/message-manager')
const TxManager = require('./transaction-manager')
const ConfigManager = require('./lib/config-manager')
const extension = require('./lib/extension')
@@ -34,7 +34,7 @@ module.exports = class MetamaskController extends EventEmitter {
// observable state store
this.store = new ObservableStore(initState)
-
+
// config manager
this.configManager = new ConfigManager({
store: this.store,
@@ -54,7 +54,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({
initState: initState.KeyringController,
@@ -79,7 +79,7 @@ module.exports = class MetamaskController extends EventEmitter {
provider: this.provider,
blockTracker: this.provider,
})
-
+
// notices
this.noticeController = new NoticeController({
configManager: this.configManager,
@@ -89,7 +89,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()
@@ -105,6 +105,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))
this.keyringController.store.subscribe((state) => {
this.store.updateState({ KeyringController: state })
})
@@ -133,11 +134,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
}
@@ -169,6 +166,7 @@ module.exports = class MetamaskController extends EventEmitter {
//
getState () {
+
const wallet = this.configManager.getWallet()
const vault = this.keyringController.store.getState().vault
const isInitialized = (!!wallet || !!vault)
@@ -179,6 +177,7 @@ module.exports = class MetamaskController extends EventEmitter {
this.state,
this.ethStore.getState(),
this.txManager.getState(),
+ this.messageManager.getState(),
this.keyringController.getState(),
this.preferencesController.store.getState(),
this.noticeController.getState(),
@@ -202,6 +201,7 @@ module.exports = class MetamaskController extends EventEmitter {
const keyringController = this.keyringController
const preferencesController = this.preferencesController
const txManager = this.txManager
+ const messageManager = this.messageManager
const noticeController = this.noticeController
return {
@@ -221,7 +221,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),
@@ -245,8 +245,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.rejectMsg.bind(messageManager),
// notices
checkNotices: noticeController.updateNoticesList.bind(noticeController),
@@ -388,20 +388,37 @@ 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.sendUpdate()
- }
+ 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)}`))
+ }
+ })
}
- 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
+ // 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))
}
diff --git a/app/scripts/transaction-manager.js b/app/scripts/transaction-manager.js
index d69dab1d7..6fecdba39 100644
--- a/app/scripts/transaction-manager.js
+++ b/app/scripts/transaction-manager.js
@@ -28,7 +28,7 @@ module.exports = class TransactionManager extends EventEmitter {
var selectedAddress = this.getSelectedAddress()
return {
transactions: this.getTxList(),
- unconfTxs: this.getUnapprovedTxList(),
+ unapprovedTxs: this.getUnapprovedTxList(),
selectedAddressTxList: this.getFilteredTxList({metamaskNetworkId: this.getNetwork(), from: selectedAddress}),
}
}