From ff87b9dc7aa3a3bd0e6ca75ca76d538c5ecaf44a Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 3 Feb 2017 14:59:07 -0800 Subject: id mgmt - update to latest eth_sign spec --- app/scripts/keyrings/hd.js | 11 ++++++----- app/scripts/keyrings/simple.js | 11 ++++++----- app/scripts/lib/message-manager.js | 35 +++++++++++++++++++++++++++-------- app/scripts/metamask-controller.js | 32 ++++++++++++++++---------------- 4 files changed, 55 insertions(+), 34 deletions(-) (limited to 'app') 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) } -- cgit