id mgmt - update to latest eth_sign spec

feature/default_network_editable
kumavis 8 years ago
parent 77f8995568
commit ff87b9dc7a
  1. 11
      app/scripts/keyrings/hd.js
  2. 11
      app/scripts/keyrings/simple.js
  3. 35
      app/scripts/lib/message-manager.js
  4. 32
      app/scripts/metamask-controller.js
  5. 5
      package.json

@ -74,12 +74,13 @@ class HdKeyring extends EventEmitter {
} }
// For eth_sign, we need to sign transactions: // For eth_sign, we need to sign transactions:
signMessage (withAccount, data) { signMessage (withAccount, msgHex) {
const wallet = this._getWalletForAccount(withAccount) const wallet = this._getWalletForAccount(withAccount)
const message = ethUtil.stripHexPrefix(data) const privKey = wallet.getPrivateKey()
var privKey = wallet.getPrivateKey() const msgBuffer = ethUtil.toBuffer(msgHex)
var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey) const msgHash = ethUtil.hashPersonalMessage(msgBuffer)
var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s)) const msgSig = ethUtil.ecsign(msgHash, privKey)
const rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s))
return Promise.resolve(rawMsgSig) return Promise.resolve(rawMsgSig)
} }

@ -58,12 +58,13 @@ class SimpleKeyring extends EventEmitter {
} }
// For eth_sign, we need to sign transactions: // For eth_sign, we need to sign transactions:
signMessage (withAccount, data) { signMessage (withAccount, msgHex) {
const wallet = this._getWalletForAccount(withAccount) const wallet = this._getWalletForAccount(withAccount)
const message = ethUtil.stripHexPrefix(data) const privKey = wallet.getPrivateKey()
var privKey = wallet.getPrivateKey() const msgBuffer = ethUtil.toBuffer(msgHex)
var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey) const msgHash = ethUtil.hashPersonalMessage(msgBuffer)
var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s)) const msgSig = ethUtil.ecsign(msgHash, privKey)
const rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s))
return Promise.resolve(rawMsgSig) return Promise.resolve(rawMsgSig)
} }

@ -1,5 +1,6 @@
const EventEmitter = require('events') const EventEmitter = require('events')
const ObservableStore = require('obs-store') const ObservableStore = require('obs-store')
const ethUtil = require('ethereumjs-util')
const createId = require('./random-id') const createId = require('./random-id')
@ -23,6 +24,7 @@ module.exports = class MessageManager extends EventEmitter{
} }
addUnapprovedMessage (msgParams) { addUnapprovedMessage (msgParams) {
msgParams.data = normalizeMsgData(msgParams.data)
// create txData obj with parameters and meta data // create txData obj with parameters and meta data
var time = (new Date()).getTime() var time = (new Date()).getTime()
var msgId = createId() var msgId = createId()
@ -57,32 +59,39 @@ module.exports = class MessageManager extends EventEmitter{
this._setMsgStatus(msgId, 'approved') this._setMsgStatus(msgId, 'approved')
} }
setMsgStatusSigned (msgId, rawSig) {
const msg = this.getMsg(msgId)
msg.rawSig = rawSig
this._updateMsg(msg)
this._setMsgStatus(msgId, 'signed')
}
prepMsgForSigning (msgParams) { prepMsgForSigning (msgParams) {
delete msgParams.metamaskId delete msgParams.metamaskId
return Promise.resolve(msgParams) return Promise.resolve(msgParams)
} }
rejectMsg (msgId) { rejectMsg (msgId) {
this.brodcastMessage(null, msgId, 'rejected')
this._setMsgStatus(msgId, 'rejected') this._setMsgStatus(msgId, 'rejected')
} }
brodcastMessage (rawSig, msgId, status) {
this.emit(`${msgId}:finished`, {status, rawSig})
}
// //
// PRIVATE METHODS // PRIVATE METHODS
// //
_setMsgStatus (msgId, status) { _setMsgStatus (msgId, status) {
let msg = this.getMsg(msgId) const msg = this.getMsg(msgId)
if (msg) msg.status = status if (!msg) throw new Error('MessageManager - Message not found for id: "${msgId}".')
msg.status = status
this._updateMsg(msg) this._updateMsg(msg)
this.emit(`${msgId}:${status}`, msg)
if (status === 'rejected' || status === 'signed') {
this.emit(`${msgId}:finished`, msg)
}
} }
_updateMsg (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) { if (index !== -1) {
this.messages[index] = msg 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'))
}
}

@ -56,7 +56,7 @@ module.exports = class MetamaskController extends EventEmitter {
this.currencyController.scheduleConversionInterval() this.currencyController.scheduleConversionInterval()
// rpc provider // rpc provider
this.provider = this.initializeProvider(opts) this.provider = this.initializeProvider()
this.provider.on('block', this.logBlock.bind(this)) this.provider.on('block', this.logBlock.bind(this))
this.provider.on('error', this.verifyNetwork.bind(this)) this.provider.on('error', this.verifyNetwork.bind(this))
@ -418,7 +418,7 @@ module.exports = class MetamaskController extends EventEmitter {
this.opts.showUnconfirmedMessage() this.opts.showUnconfirmedMessage()
this.messageManager.once(`${msgId}:finished`, (data) => { this.messageManager.once(`${msgId}:finished`, (data) => {
switch (data.status) { switch (data.status) {
case 'approved': case 'signed':
return cb(null, data.rawSig) return cb(null, data.rawSig)
case 'rejected': case 'rejected':
return cb(new Error('MetaMask Message Signature: User denied transaction signature.')) return cb(new Error('MetaMask Message Signature: User denied transaction signature.'))
@ -430,20 +430,20 @@ module.exports = class MetamaskController extends EventEmitter {
signMessage (msgParams, cb) { signMessage (msgParams, cb) {
const msgId = msgParams.metamaskId const msgId = msgParams.metamaskId
// sets the status op the message to 'approved' promiseToCallback(
// and removes the metamaskId for signing // sets the status op the message to 'approved'
return this.messageManager.approveMessage(msgParams) // and removes the metamaskId for signing
.then((cleanMsgParams) => { this.messageManager.approveMessage(msgParams)
// signs the message .then((cleanMsgParams) => {
return this.keyringController.signMessage(cleanMsgParams) // signs the message
}) return this.keyringController.signMessage(cleanMsgParams)
.then((rawSig) => { })
// tells the listener that the message has been signed .then((rawSig) => {
// and can be returned to the dapp // tells the listener that the message has been signed
this.messageManager.brodcastMessage(rawSig, msgId, 'approved') // and can be returned to the dapp
}).then(() => { this.messageManager.setMsgStatusSigned(msgId, rawSig)
cb() })
}).catch((err) => cb(err)) )(cb)
} }

@ -55,7 +55,7 @@
"eth-lightwallet": "^2.3.3", "eth-lightwallet": "^2.3.3",
"eth-query": "^1.0.3", "eth-query": "^1.0.3",
"ethereumjs-tx": "^1.0.0", "ethereumjs-tx": "^1.0.0",
"ethereumjs-util": "^4.4.0", "ethereumjs-util": "ethereumjs/ethereumjs-util#ac5d0908536b447083ea422b435da27f26615de9",
"ethereumjs-wallet": "^0.6.0", "ethereumjs-wallet": "^0.6.0",
"express": "^4.14.0", "express": "^4.14.0",
"extension-link-enabler": "^1.0.0", "extension-link-enabler": "^1.0.0",
@ -96,6 +96,7 @@
"redux": "^3.0.5", "redux": "^3.0.5",
"redux-logger": "^2.3.1", "redux-logger": "^2.3.1",
"redux-thunk": "^1.0.2", "redux-thunk": "^1.0.2",
"request-promise": "^4.1.1",
"sandwich-expando": "^1.0.5", "sandwich-expando": "^1.0.5",
"semaphore": "^1.0.5", "semaphore": "^1.0.5",
"textarea-caret": "^3.0.1", "textarea-caret": "^3.0.1",
@ -103,7 +104,7 @@
"through2": "^2.0.1", "through2": "^2.0.1",
"valid-url": "^1.0.9", "valid-url": "^1.0.9",
"vreme": "^3.0.2", "vreme": "^3.0.2",
"web3": "0.17.0-beta", "web3": "0.18.2",
"web3-provider-engine": "^8.5.0", "web3-provider-engine": "^8.5.0",
"web3-stream-provider": "^2.0.6", "web3-stream-provider": "^2.0.6",
"xtend": "^4.0.1" "xtend": "^4.0.1"

Loading…
Cancel
Save