diff --git a/app/scripts/keyrings/hd.js b/app/scripts/keyrings/hd.js index 1b9796e07..2e3b74192 100644 --- a/app/scripts/keyrings/hd.js +++ b/app/scripts/keyrings/hd.js @@ -74,12 +74,13 @@ class HdKeyring extends EventEmitter { } // For eth_sign, we need to sign transactions: - signMessage (withAccount, data) { + signMessage (withAccount, msgHex) { const wallet = this._getWalletForAccount(withAccount) - const message = ethUtil.stripHexPrefix(data) - var privKey = wallet.getPrivateKey() - var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey) - var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s)) + const privKey = wallet.getPrivateKey() + const msgBuffer = ethUtil.toBuffer(msgHex) + const msgHash = ethUtil.hashPersonalMessage(msgBuffer) + const msgSig = ethUtil.ecsign(msgHash, privKey) + const rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s)) return Promise.resolve(rawMsgSig) } diff --git a/app/scripts/keyrings/simple.js b/app/scripts/keyrings/simple.js index 46687fcaf..fa8e9fd78 100644 --- a/app/scripts/keyrings/simple.js +++ b/app/scripts/keyrings/simple.js @@ -58,12 +58,13 @@ class SimpleKeyring extends EventEmitter { } // For eth_sign, we need to sign transactions: - signMessage (withAccount, data) { + signMessage (withAccount, msgHex) { const wallet = this._getWalletForAccount(withAccount) - const message = ethUtil.stripHexPrefix(data) - var privKey = wallet.getPrivateKey() - var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey) - var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s)) + const privKey = wallet.getPrivateKey() + const msgBuffer = ethUtil.toBuffer(msgHex) + const msgHash = ethUtil.hashPersonalMessage(msgBuffer) + const msgSig = ethUtil.ecsign(msgHash, privKey) + const rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s)) return Promise.resolve(rawMsgSig) } diff --git a/app/scripts/lib/message-manager.js b/app/scripts/lib/message-manager.js index 38fa42017..ceaf8ee2f 100644 --- a/app/scripts/lib/message-manager.js +++ b/app/scripts/lib/message-manager.js @@ -1,5 +1,6 @@ const EventEmitter = require('events') const ObservableStore = require('obs-store') +const ethUtil = require('ethereumjs-util') const createId = require('./random-id') @@ -23,6 +24,7 @@ module.exports = class MessageManager extends EventEmitter{ } addUnapprovedMessage (msgParams) { + msgParams.data = normalizeMsgData(msgParams.data) // create txData obj with parameters and meta data var time = (new Date()).getTime() var msgId = createId() @@ -57,32 +59,39 @@ module.exports = class MessageManager extends EventEmitter{ 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.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 + 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) { - let index = this.messages.findIndex((message) => message.id === msg.id) + const index = this.messages.findIndex((message) => message.id === msg.id) if (index !== -1) { this.messages[index] = msg } @@ -97,3 +106,13 @@ module.exports = class MessageManager extends EventEmitter{ } } + +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')) + } +} \ No newline at end of file diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index c0910014f..066e389e2 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -56,7 +56,7 @@ module.exports = class MetamaskController extends EventEmitter { this.currencyController.scheduleConversionInterval() // rpc provider - this.provider = this.initializeProvider(opts) + this.provider = this.initializeProvider() this.provider.on('block', this.logBlock.bind(this)) this.provider.on('error', this.verifyNetwork.bind(this)) @@ -418,7 +418,7 @@ module.exports = class MetamaskController extends EventEmitter { this.opts.showUnconfirmedMessage() this.messageManager.once(`${msgId}:finished`, (data) => { switch (data.status) { - case 'approved': + case 'signed': return cb(null, data.rawSig) case 'rejected': return cb(new Error('MetaMask Message Signature: User denied transaction signature.')) @@ -430,20 +430,20 @@ module.exports = class MetamaskController extends EventEmitter { 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)) + promiseToCallback( + // sets the status op the message to 'approved' + // and removes the metamaskId for signing + 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.setMsgStatusSigned(msgId, rawSig) + }) + )(cb) } diff --git a/package.json b/package.json index 3337f4ac5..16246f77c 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "eth-lightwallet": "^2.3.3", "eth-query": "^1.0.3", "ethereumjs-tx": "^1.0.0", - "ethereumjs-util": "^4.4.0", + "ethereumjs-util": "ethereumjs/ethereumjs-util#ac5d0908536b447083ea422b435da27f26615de9", "ethereumjs-wallet": "^0.6.0", "express": "^4.14.0", "extension-link-enabler": "^1.0.0", @@ -96,6 +96,7 @@ "redux": "^3.0.5", "redux-logger": "^2.3.1", "redux-thunk": "^1.0.2", + "request-promise": "^4.1.1", "sandwich-expando": "^1.0.5", "semaphore": "^1.0.5", "textarea-caret": "^3.0.1", @@ -103,7 +104,7 @@ "through2": "^2.0.1", "valid-url": "^1.0.9", "vreme": "^3.0.2", - "web3": "0.17.0-beta", + "web3": "0.18.2", "web3-provider-engine": "^8.5.0", "web3-stream-provider": "^2.0.6", "xtend": "^4.0.1"