From 82983e5effb236474c7eba2ac2ba62ea3fe58f5f Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 28 Apr 2016 14:16:24 -0700 Subject: idmgmt - eth_sign support + notifications --- app/scripts/lib/idStore.js | 99 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 3 deletions(-) (limited to 'app/scripts/lib/idStore.js') diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index b462d4ad5..c25d83c9d 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -34,6 +34,7 @@ function IdentityStore(opts = {}) { } // not part of serilized metamask state - only kept in memory this._unconfTxCbs = {} + this._unconfMsgCbs = {} } // @@ -140,6 +141,10 @@ IdentityStore.prototype.exportAccount = function(address, cb) { cb(null, privateKey) } +// +// Transactions +// + // comes from dapp via zero-client hooked-wallet provider IdentityStore.prototype.addUnconfirmedTransaction = function(txParams, cb){ @@ -170,7 +175,6 @@ IdentityStore.prototype.addUnconfirmedTransaction = function(txParams, cb){ // comes from metamask ui IdentityStore.prototype.approveTransaction = function(txId, cb){ var txData = configManager.getTx(txId) - var txParams = txData.txParams var approvalCb = this._unconfTxCbs[txId] || noop // accept tx @@ -206,6 +210,73 @@ IdentityStore.prototype.signTransaction = function(txParams, cb){ } } +// +// Messages +// + +// comes from dapp via zero-client hooked-wallet provider +IdentityStore.prototype.addUnconfirmedMessage = function(msgParams, cb){ + + // 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: 'unconfirmed', + } + configManager.addMsg(msgData) + console.log('addUnconfirmedMessage:', msgData) + + // keep the cb around for after approval (requires user interaction) + // This cb fires completion to the Dapp's write operation. + this._unconfMsgCbs[msgId] = cb + + // signal update + this._didUpdate() + + return msgId +} + +// comes from metamask ui +IdentityStore.prototype.approveMessage = function(msgId, cb){ + var msgData = configManager.getMsg(msgId) + var approvalCb = this._unconfMsgCbs[msgId] || noop + + // accept msg + cb() + approvalCb(null, true) + // clean up + configManager.confirmMsg(msgId) + delete this._unconfMsgCbs[msgId] + this._didUpdate() +} + +// comes from metamask ui +IdentityStore.prototype.cancelMessage = function(msgId){ + var txData = configManager.getMsg(msgId) + var approvalCb = this._unconfMsgCbs[msgId] || noop + + // reject tx + approvalCb(null, false) + // clean up + configManager.rejectMsg(msgId) + delete this._unconfTxCbs[msgId] + this._didUpdate() +} + +// performs the actual signing, no autofill of params +IdentityStore.prototype.signMessage = function(msgParams, cb){ + try { + console.log('signing msg...', msgParams.data) + var rawMsg = this._idmgmt.signMsg(msgParams.from, msgParams.data) + cb(null, rawMsg) + } catch (err) { + cb(err) + } +} + // // private // @@ -352,7 +423,7 @@ function IdManagement(opts) { var tx = new Transaction(txParams) // sign tx - var privKeyHex = ethUtil.addHexPrefix(this.keyStore.exportPrivateKey(txParams.from, this.derivedKey, this.hdPathString)) + var privKeyHex = this.exportPrivateKey(txParams.from) var privKey = ethUtil.toBuffer(privKeyHex) tx.sign(privKey) @@ -367,12 +438,23 @@ function IdManagement(opts) { return rawTx } + this.signMsg = function(address, message){ + // sign message + var privKeyHex = this.exportPrivateKey(address) + var privKey = ethUtil.toBuffer(privKeyHex) + var msgHash = ethUtil.sha3(message) + var msgSig = ethUtil.ecsign(msgHash, privKey) + var rawMsgSig = ethUtil.bufferToHex(concatSig(msgSig.v, msgSig.r, msgSig.s)) + return rawMsgSig + } + this.getSeed = function(){ return this.keyStore.getSeed(this.derivedKey) } this.exportPrivateKey = function(address) { - return this.keyStore.exportPrivateKey(address, this.derivedKey, this.hdPathString) + var privKeyHex = ethUtil.addHexPrefix(this.keyStore.exportPrivateKey(address, this.derivedKey, this.hdPathString)) + return privKeyHex } } @@ -380,3 +462,14 @@ function IdManagement(opts) { // util function noop(){} + + +function concatSig(v, r, s) { + r = ethUtil.fromSigned(r) + s = ethUtil.fromSigned(s) + v = ethUtil.bufferToInt(v) + r = ethUtil.toUnsigned(r).toString('hex') + s = ethUtil.toUnsigned(s).toString('hex') + v = ethUtil.stripHexPrefix(ethUtil.intToHex(v)) + return ethUtil.addHexPrefix(r.concat(s, v).toString("hex")) +} \ No newline at end of file -- cgit