From ae3993b6d752207115a4767247caddbb42c197e3 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 15 Jun 2016 14:58:06 -0700 Subject: Factor idManagement into its own module --- app/scripts/lib/id-management.js | 79 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 app/scripts/lib/id-management.js (limited to 'app/scripts/lib/id-management.js') diff --git a/app/scripts/lib/id-management.js b/app/scripts/lib/id-management.js new file mode 100644 index 000000000..0edd2e336 --- /dev/null +++ b/app/scripts/lib/id-management.js @@ -0,0 +1,79 @@ +const ethUtil = require('ethereumjs-util') + +module.exports = IdManagement + +function IdManagement(opts) { + if (!opts) opts = {} + + this.keyStore = opts.keyStore + this.derivedKey = opts.derivedKey + this.hdPathString = "m/44'/60'/0'/0" + + this.getAddresses = function(){ + return keyStore.getAddresses(this.hdPathString).map(function(address){ return '0x'+address }) + } + + this.signTx = function(txParams){ + // normalize values + txParams.to = ethUtil.addHexPrefix(txParams.to) + txParams.from = ethUtil.addHexPrefix(txParams.from) + txParams.value = ethUtil.addHexPrefix(txParams.value) + txParams.data = ethUtil.addHexPrefix(txParams.data) + txParams.gasLimit = ethUtil.addHexPrefix(txParams.gasLimit || txParams.gas) + txParams.nonce = ethUtil.addHexPrefix(txParams.nonce) + var tx = new Transaction(txParams) + + // sign tx + var privKeyHex = this.exportPrivateKey(txParams.from) + var privKey = ethUtil.toBuffer(privKeyHex) + tx.sign(privKey) + + // Add the tx hash to the persisted meta-tx object + var txHash = ethUtil.bufferToHex(tx.hash()) + var metaTx = configManager.getTx(txParams.metamaskId) + metaTx.hash = txHash + configManager.updateTx(metaTx) + + // return raw serialized tx + var rawTx = ethUtil.bufferToHex(tx.serialize()) + 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 msgBuffer = new Buffer(message.replace('0x',''), 'hex') + var msgSig = ethUtil.ecsign(msgBuffer, 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) { + var privKeyHex = ethUtil.addHexPrefix(this.keyStore.exportPrivateKey(address, this.derivedKey, this.hdPathString)) + return privKeyHex + } +} + +function pad_with_zeroes(number, length){ + var my_string = '' + number; + while (my_string.length < length) { + my_string = '0' + my_string; + } + return my_string; +} + +function concatSig(v, r, s) { + r = pad_with_zeroes(ethUtil.fromSigned(r), 64) + s = pad_with_zeroes(ethUtil.fromSigned(s), 64) + 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")) +} -- cgit From 408addb1b2a795515149dbde8131fa40cba721f4 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 16 Jun 2016 11:46:35 -0700 Subject: Fixed signing of hashes Signing now always takes a 64 digit hex string, and returns a message signature which appropriately pads r, s, and v with zeroes. Need to verify with Denis that this is the behavior he requires. --- app/scripts/lib/id-management.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'app/scripts/lib/id-management.js') diff --git a/app/scripts/lib/id-management.js b/app/scripts/lib/id-management.js index 0edd2e336..30a3141f1 100644 --- a/app/scripts/lib/id-management.js +++ b/app/scripts/lib/id-management.js @@ -39,16 +39,14 @@ function IdManagement(opts) { return rawTx } - this.signMsg = function(address, message){ + this.signMsg = function (address, message) { // sign message - var privKeyHex = this.exportPrivateKey(address) - var privKey = ethUtil.toBuffer(privKeyHex) - var msgHash = ethUtil.sha3(message) - var msgBuffer = new Buffer(message.replace('0x',''), 'hex') - var msgSig = ethUtil.ecsign(msgBuffer, privKey) - var rawMsgSig = ethUtil.bufferToHex(concatSig(msgSig.v, msgSig.r, msgSig.s)) - return rawMsgSig - } + var privKeyHex = this.exportPrivateKey(address); + var privKey = ethUtil.toBuffer(privKeyHex); + var msgSig = ethUtil.ecsign(new Buffer(message.replace('0x',''), 'hex'), privKey); + var rawMsgSig = ethUtil.bufferToHex(concatSig(msgSig.v, msgSig.r, msgSig.s)); + return rawMsgSig; + }; this.getSeed = function(){ return this.keyStore.getSeed(this.derivedKey) @@ -71,9 +69,8 @@ function pad_with_zeroes(number, length){ function concatSig(v, r, s) { r = pad_with_zeroes(ethUtil.fromSigned(r), 64) s = pad_with_zeroes(ethUtil.fromSigned(s), 64) - v = ethUtil.bufferToInt(v) - r = ethUtil.toUnsigned(r).toString('hex') - s = ethUtil.toUnsigned(s).toString('hex') + r = ethUtil.stripHexPrefix(r.toString('hex')) + s = ethUtil.stripHexPrefix(s.toString('hex')) v = ethUtil.stripHexPrefix(ethUtil.intToHex(v)) - return ethUtil.addHexPrefix(r.concat(s, v).toString("hex")) + return ethUtil.addHexPrefix(r.concat(s, v)) } -- cgit