diff options
author | Dan Finlay <flyswatter@users.noreply.github.com> | 2017-02-28 02:28:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-28 02:28:31 +0800 |
commit | ab01fef1c0ffddf09a20f90a910e5e068cf8c71e (patch) | |
tree | 0fe9d0322bb39f42d3170b56d0eca7a90985400c /app/scripts/lib/personal-message-manager.js | |
parent | 09c7b9d242f66db99c80f22fae00dfdb894e2adc (diff) | |
parent | c831043a5125c093a83857a335c7816627a7e291 (diff) | |
download | tangerine-wallet-browser-ab01fef1c0ffddf09a20f90a910e5e068cf8c71e.tar tangerine-wallet-browser-ab01fef1c0ffddf09a20f90a910e5e068cf8c71e.tar.gz tangerine-wallet-browser-ab01fef1c0ffddf09a20f90a910e5e068cf8c71e.tar.bz2 tangerine-wallet-browser-ab01fef1c0ffddf09a20f90a910e5e068cf8c71e.tar.lz tangerine-wallet-browser-ab01fef1c0ffddf09a20f90a910e5e068cf8c71e.tar.xz tangerine-wallet-browser-ab01fef1c0ffddf09a20f90a910e5e068cf8c71e.tar.zst tangerine-wallet-browser-ab01fef1c0ffddf09a20f90a910e5e068cf8c71e.zip |
Merge pull request #1149 from MetaMask/i1114-AddPersonalSign
Add personal_sign
Diffstat (limited to 'app/scripts/lib/personal-message-manager.js')
-rw-r--r-- | app/scripts/lib/personal-message-manager.js | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/app/scripts/lib/personal-message-manager.js b/app/scripts/lib/personal-message-manager.js new file mode 100644 index 000000000..3b8510767 --- /dev/null +++ b/app/scripts/lib/personal-message-manager.js @@ -0,0 +1,119 @@ +const EventEmitter = require('events') +const ObservableStore = require('obs-store') +const ethUtil = require('ethereumjs-util') +const createId = require('./random-id') + + +module.exports = class PersonalMessageManager extends EventEmitter{ + constructor (opts) { + super() + this.memStore = new ObservableStore({ + unapprovedPersonalMsgs: {}, + unapprovedPersonalMsgCount: 0, + }) + this.messages = [] + } + + get unapprovedPersonalMsgCount () { + return Object.keys(this.getUnapprovedMsgs()).length + } + + getUnapprovedMsgs () { + return this.messages.filter(msg => msg.status === 'unapproved') + .reduce((result, msg) => { result[msg.id] = msg; return result }, {}) + } + + addUnapprovedMessage (msgParams) { + msgParams.data = normalizeMsgData(msgParams.data) + // 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', + type: 'personal_sign', + } + this.addMsg(msgData) + + // signal update + this.emit('update') + return msgId + } + + addMsg (msg) { + this.messages.push(msg) + this._saveMsgList() + } + + getMsg (msgId) { + return this.messages.find(msg => msg.id === msgId) + } + + approveMessage (msgParams) { + this.setMsgStatusApproved(msgParams.metamaskId) + return this.prepMsgForSigning(msgParams) + } + + setMsgStatusApproved (msgId) { + this._setMsgStatus(msgId, 'approved') + } + + setMsgStatusSigned (msgId, rawSig) { + const msg = this.getMsg(msgId) + msg.rawSig = rawSig + this._updateMsg(msg) + this._setMsgStatus(msgId, 'signed') + } + + prepMsgForSigning (msgParams) { + delete msgParams.metamaskId + return Promise.resolve(msgParams) + } + + rejectMsg (msgId) { + this._setMsgStatus(msgId, 'rejected') + } + + // + // PRIVATE METHODS + // + + _setMsgStatus (msgId, status) { + const msg = this.getMsg(msgId) + if (!msg) throw new Error('PersonalMessageManager - Message not found for id: "${msgId}".') + msg.status = status + this._updateMsg(msg) + this.emit(`${msgId}:${status}`, msg) + if (status === 'rejected' || status === 'signed') { + this.emit(`${msgId}:finished`, msg) + } + } + + _updateMsg (msg) { + const index = this.messages.findIndex((message) => message.id === msg.id) + if (index !== -1) { + this.messages[index] = msg + } + this._saveMsgList() + } + + _saveMsgList () { + const unapprovedPersonalMsgs = this.getUnapprovedMsgs() + const unapprovedPersonalMsgCount = Object.keys(unapprovedPersonalMsgs).length + this.memStore.updateState({ unapprovedPersonalMsgs, unapprovedPersonalMsgCount }) + this.emit('updateBadge') + } + +} + +function normalizeMsgData(data) { + if (data.slice(0, 2) === '0x') { + // data is already hex + return data + } else { + // data is unicode, convert to hex + return ethUtil.bufferToHex(new Buffer(data, 'utf8')) + } +} |