aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2017-02-22 06:25:47 +0800
committerDan Finlay <dan@danfinlay.com>2017-02-22 06:25:47 +0800
commit0584988688a471698e9b3ad05cb0597f0270ea9e (patch)
treebdcbb009636dcdea67abcc28238023dd8bf7e882 /app/scripts/lib
parent29f07238f443007a03c968349af4d3fca7e10522 (diff)
downloadtangerine-wallet-browser-0584988688a471698e9b3ad05cb0597f0270ea9e.tar.gz
tangerine-wallet-browser-0584988688a471698e9b3ad05cb0597f0270ea9e.tar.zst
tangerine-wallet-browser-0584988688a471698e9b3ad05cb0597f0270ea9e.zip
Move sigUtil and keyrings to external modules
These external modules now have their own test coverage and build enforcement. This allowed me to somewhat more easily add good tests around our personalSign strategy (held now in [eth-sig-util](https://github.com/flyswatter/eth-sig-util), and allow each of the keyrings to import that, etc.
Diffstat (limited to 'app/scripts/lib')
-rw-r--r--app/scripts/lib/config-manager.js2
-rw-r--r--app/scripts/lib/controllers/preferences.js2
-rw-r--r--app/scripts/lib/idStore-migrator.js2
-rw-r--r--app/scripts/lib/personal-message-manager.js118
-rw-r--r--app/scripts/lib/sig-util.js28
-rw-r--r--app/scripts/lib/tx-utils.js2
6 files changed, 122 insertions, 32 deletions
diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js
index 6267eab68..ea5e49b19 100644
--- a/app/scripts/lib/config-manager.js
+++ b/app/scripts/lib/config-manager.js
@@ -1,6 +1,6 @@
const MetamaskConfig = require('../config.js')
const ethUtil = require('ethereumjs-util')
-const normalize = require('./sig-util').normalize
+const normalize = require('eth-sig-util').normalize
const TESTNET_RPC = MetamaskConfig.network.testnet
const MAINNET_RPC = MetamaskConfig.network.mainnet
diff --git a/app/scripts/lib/controllers/preferences.js b/app/scripts/lib/controllers/preferences.js
index dc9464c4e..c5e93a5b9 100644
--- a/app/scripts/lib/controllers/preferences.js
+++ b/app/scripts/lib/controllers/preferences.js
@@ -1,5 +1,5 @@
const ObservableStore = require('obs-store')
-const normalizeAddress = require('../sig-util').normalize
+const normalizeAddress = require('eth-sig-util').normalize
class PreferencesController {
diff --git a/app/scripts/lib/idStore-migrator.js b/app/scripts/lib/idStore-migrator.js
index 655aed0af..1485beb48 100644
--- a/app/scripts/lib/idStore-migrator.js
+++ b/app/scripts/lib/idStore-migrator.js
@@ -1,6 +1,6 @@
const IdentityStore = require('./idStore')
const HdKeyring = require('../keyrings/hd')
-const sigUtil = require('./sig-util')
+const sigUtil = require('eth-sig-util')
const normalize = sigUtil.normalize
const denodeify = require('denodeify')
diff --git a/app/scripts/lib/personal-message-manager.js b/app/scripts/lib/personal-message-manager.js
new file mode 100644
index 000000000..72dd1da96
--- /dev/null
+++ b/app/scripts/lib/personal-message-manager.js
@@ -0,0 +1,118 @@
+const EventEmitter = require('events')
+const ObservableStore = require('obs-store')
+const ethUtil = require('ethereumjs-util')
+const createId = require('./random-id')
+
+
+module.exports = class MessageManager 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',
+ }
+ 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('MessageManager - 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'))
+ }
+}
diff --git a/app/scripts/lib/sig-util.js b/app/scripts/lib/sig-util.js
deleted file mode 100644
index 193dda381..000000000
--- a/app/scripts/lib/sig-util.js
+++ /dev/null
@@ -1,28 +0,0 @@
-const ethUtil = require('ethereumjs-util')
-
-module.exports = {
-
- concatSig: function (v, r, s) {
- const rSig = ethUtil.fromSigned(r)
- const sSig = ethUtil.fromSigned(s)
- const vSig = ethUtil.bufferToInt(v)
- const rStr = padWithZeroes(ethUtil.toUnsigned(rSig).toString('hex'), 64)
- const sStr = padWithZeroes(ethUtil.toUnsigned(sSig).toString('hex'), 64)
- const vStr = ethUtil.stripHexPrefix(ethUtil.intToHex(vSig))
- return ethUtil.addHexPrefix(rStr.concat(sStr, vStr)).toString('hex')
- },
-
- normalize: function (address) {
- if (!address) return
- return ethUtil.addHexPrefix(address.toLowerCase())
- },
-
-}
-
-function padWithZeroes (number, length) {
- var myString = '' + number
- while (myString.length < length) {
- myString = '0' + myString
- }
- return myString
-}
diff --git a/app/scripts/lib/tx-utils.js b/app/scripts/lib/tx-utils.js
index 5116cb93b..240a6ab47 100644
--- a/app/scripts/lib/tx-utils.js
+++ b/app/scripts/lib/tx-utils.js
@@ -2,7 +2,7 @@ const async = require('async')
const EthQuery = require('eth-query')
const ethUtil = require('ethereumjs-util')
const Transaction = require('ethereumjs-tx')
-const normalize = require('./sig-util').normalize
+const normalize = require('eth-sig-util').normalize
const BN = ethUtil.BN
/*