2017-01-27 23:16:35 +01:00
|
|
|
const EventEmitter = require('events')
|
2017-01-28 01:11:59 +01:00
|
|
|
const ObservableStore = require('obs-store')
|
|
|
|
const createId = require('./random-id')
|
|
|
|
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2017-01-27 23:16:35 +01:00
|
|
|
module.exports = class MessageManager extends EventEmitter{
|
|
|
|
constructor (opts) {
|
|
|
|
super()
|
2017-01-28 01:11:59 +01:00
|
|
|
this.memStore = new ObservableStore({ messages: [] })
|
|
|
|
}
|
|
|
|
|
|
|
|
getState() {
|
|
|
|
return {
|
|
|
|
unapprovedMsgs: this.unapprovedMsgs(),
|
|
|
|
messages: this.getMsgList(),
|
|
|
|
}
|
2017-01-27 23:16:35 +01:00
|
|
|
}
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2017-01-27 23:16:35 +01:00
|
|
|
getMsgList () {
|
2017-01-28 01:11:59 +01:00
|
|
|
return this.memStore.getState().messages
|
2017-01-27 23:16:35 +01:00
|
|
|
}
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2017-01-28 01:11:59 +01:00
|
|
|
get unapprovedMsgCount () {
|
|
|
|
return Object.keys(this.unapprovedMsgs()).length
|
|
|
|
}
|
|
|
|
|
|
|
|
unapprovedMsgs () {
|
2017-01-27 23:16:35 +01:00
|
|
|
let messages = this.getMsgList()
|
2017-01-28 01:11:59 +01:00
|
|
|
return messages.filter(msg => msg.status === 'unapproved')
|
2017-01-27 23:16:35 +01:00
|
|
|
.reduce((result, msg) => { result[msg.id] = msg; return result }, {})
|
|
|
|
}
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2017-01-27 23:16:35 +01:00
|
|
|
_saveMsgList (msgList) {
|
2017-01-28 01:11:59 +01:00
|
|
|
this.emit('updateBadge')
|
|
|
|
let state = this.memStore.getState()
|
|
|
|
state.messages = msgList
|
|
|
|
this.memStore.putState(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
// keep the cb around for after approval (requires user interaction)
|
|
|
|
// This cb fires completion to the Dapp's write operation.
|
|
|
|
|
|
|
|
// signal update
|
|
|
|
this.emit('update')
|
|
|
|
return msgId
|
2017-01-27 23:16:35 +01:00
|
|
|
}
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2017-01-27 23:16:35 +01:00
|
|
|
addMsg (msg) {
|
|
|
|
let messages = this.getMsgList()
|
|
|
|
messages.push(msg)
|
|
|
|
this._saveMsgList(messages)
|
|
|
|
}
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2017-01-27 23:16:35 +01:00
|
|
|
getMsg (msgId) {
|
|
|
|
let messages = this.getMsgList()
|
|
|
|
let matching = messages.filter(msg => msg.id === msgId)
|
|
|
|
return matching.length > 0 ? matching[0] : null
|
|
|
|
}
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2017-01-28 01:11:59 +01:00
|
|
|
brodcastMessage (rawSig, msgId, status) {
|
|
|
|
this.emit(`${msgId}:finished`, {status, rawSig})
|
|
|
|
}
|
|
|
|
|
|
|
|
approveMessage (msgParams) {
|
|
|
|
this.setMessageApproved(msgParams.metamaskId)
|
|
|
|
return this.prepMsgForSigning(msgParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
setMessageApproved (msgId) {
|
|
|
|
this._setMsgStatus(msgId, 'approved')
|
|
|
|
}
|
|
|
|
prepMsgForSigning (msgParams) {
|
|
|
|
delete msgParams.metamaskId
|
|
|
|
return Promise.resolve(msgParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelMessage (msgId) {
|
|
|
|
// reject tx
|
|
|
|
// clean up
|
|
|
|
this.brodcastMessage(null, msgId, 'rejected')
|
|
|
|
this.rejectMsg(msgId)
|
2017-01-27 23:16:35 +01:00
|
|
|
}
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2017-01-27 23:16:35 +01:00
|
|
|
rejectMsg (msgId) {
|
|
|
|
this._setMsgStatus(msgId, 'rejected')
|
|
|
|
}
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2017-01-27 23:16:35 +01:00
|
|
|
_setMsgStatus (msgId, status) {
|
|
|
|
let msg = this.getMsg(msgId)
|
|
|
|
if (msg) msg.status = status
|
2017-01-28 01:11:59 +01:00
|
|
|
this._updateMsg(msg)
|
2017-01-27 23:16:35 +01:00
|
|
|
}
|
2016-05-03 23:32:22 +02:00
|
|
|
|
2017-01-28 01:11:59 +01:00
|
|
|
_updateMsg (msg) {
|
2017-01-27 23:16:35 +01:00
|
|
|
let messages = this.getMsgList()
|
|
|
|
let index = messages.findIndex((message) => message.id === msg.id)
|
|
|
|
if (index !== -1) {
|
|
|
|
messages[index] = msg
|
2016-05-03 23:32:22 +02:00
|
|
|
}
|
2017-01-27 23:16:35 +01:00
|
|
|
this._saveMsgList(messages)
|
2016-05-03 23:32:22 +02:00
|
|
|
}
|
|
|
|
}
|