2016-09-12 20:07:25 +02:00
|
|
|
/* ID Management
|
|
|
|
*
|
|
|
|
* This module exists to hold the decrypted credentials for the current session.
|
|
|
|
* It therefore exposes sign methods, because it is able to perform these
|
|
|
|
* with noa dditional authentication, because its very instantiation
|
|
|
|
* means the vault is unlocked.
|
|
|
|
*/
|
|
|
|
|
2016-06-15 23:58:06 +02:00
|
|
|
const ethUtil = require('ethereumjs-util')
|
2016-06-21 09:39:27 +02:00
|
|
|
const Transaction = require('ethereumjs-tx')
|
2016-06-15 23:58:06 +02:00
|
|
|
|
|
|
|
module.exports = IdManagement
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
function IdManagement (opts) {
|
2016-06-15 23:58:06 +02:00
|
|
|
if (!opts) opts = {}
|
|
|
|
|
|
|
|
this.keyStore = opts.keyStore
|
|
|
|
this.derivedKey = opts.derivedKey
|
2016-06-25 00:52:56 +02:00
|
|
|
this.configManager = opts.configManager
|
2016-06-15 23:58:06 +02:00
|
|
|
this.hdPathString = "m/44'/60'/0'/0"
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
this.getAddresses = function () {
|
2016-06-21 22:56:04 +02:00
|
|
|
return this.keyStore.getAddresses(this.hdPathString).map(function (address) { return '0x' + address })
|
2016-06-15 23:58:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
this.signTx = function (txParams) {
|
2016-06-15 23:58:06 +02:00
|
|
|
// normalize values
|
|
|
|
txParams.to = ethUtil.addHexPrefix(txParams.to)
|
2016-10-06 00:26:26 +02:00
|
|
|
txParams.from = ethUtil.addHexPrefix(txParams.from.toLowerCase())
|
2016-06-15 23:58:06 +02:00
|
|
|
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())
|
2016-06-25 00:52:56 +02:00
|
|
|
var metaTx = this.configManager.getTx(txParams.metamaskId)
|
2016-06-15 23:58:06 +02:00
|
|
|
metaTx.hash = txHash
|
2016-06-25 00:52:56 +02:00
|
|
|
this.configManager.updateTx(metaTx)
|
2016-06-15 23:58:06 +02:00
|
|
|
|
|
|
|
// return raw serialized tx
|
|
|
|
var rawTx = ethUtil.bufferToHex(tx.serialize())
|
|
|
|
return rawTx
|
|
|
|
}
|
|
|
|
|
2016-06-16 20:46:35 +02:00
|
|
|
this.signMsg = function (address, message) {
|
2016-06-15 23:58:06 +02:00
|
|
|
// sign message
|
2016-10-06 00:26:26 +02:00
|
|
|
var privKeyHex = this.exportPrivateKey(address.toLowerCase())
|
2016-06-21 22:18:32 +02:00
|
|
|
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
|
|
|
|
}
|
2016-06-15 23:58:06 +02:00
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
this.getSeed = function () {
|
2016-06-15 23:58:06 +02:00
|
|
|
return this.keyStore.getSeed(this.derivedKey)
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
this.exportPrivateKey = function (address) {
|
2016-06-15 23:58:06 +02:00
|
|
|
var privKeyHex = ethUtil.addHexPrefix(this.keyStore.exportPrivateKey(address, this.derivedKey, this.hdPathString))
|
|
|
|
return privKeyHex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:56:04 +02:00
|
|
|
function padWithZeroes (number, length) {
|
|
|
|
var myString = '' + number
|
|
|
|
while (myString.length < length) {
|
|
|
|
myString = '0' + myString
|
2016-06-15 23:58:06 +02:00
|
|
|
}
|
2016-06-21 22:56:04 +02:00
|
|
|
return myString
|
2016-06-15 23:58:06 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
function concatSig (v, r, s) {
|
2016-06-22 02:13:02 +02:00
|
|
|
const rSig = ethUtil.fromSigned(r)
|
|
|
|
const sSig = ethUtil.fromSigned(s)
|
|
|
|
const vSig = ethUtil.bufferToInt(v)
|
|
|
|
const rStr = padWithZeroes(ethUtil.toUnsigned(rSig).toString('hex'), 64)
|
|
|
|
const sStr = padWithZeroes(ethUtil.toUnsigned(sSig).toString('hex'), 64)
|
|
|
|
const vStr = ethUtil.stripHexPrefix(ethUtil.intToHex(vSig))
|
|
|
|
return ethUtil.addHexPrefix(rStr.concat(sStr, vStr)).toString('hex')
|
2016-06-15 23:58:06 +02:00
|
|
|
}
|
2016-06-22 02:13:02 +02:00
|
|
|
|