A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/app/scripts/lib/id-management.js

79 lines
2.5 KiB

const ethUtil = require('ethereumjs-util')
const Transaction = require('ethereumjs-tx')
const configManager = require('./config-manager-singleton')
module.exports = IdManagement
9 years ago
function IdManagement (opts) {
if (!opts) opts = {}
this.keyStore = opts.keyStore
this.derivedKey = opts.derivedKey
this.hdPathString = "m/44'/60'/0'/0"
9 years ago
this.getAddresses = function () {
9 years ago
return this.keyStore.getAddresses(this.hdPathString).map(function (address) { return '0x' + address })
}
9 years ago
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
9 years ago
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
}
9 years ago
this.getSeed = function () {
return this.keyStore.getSeed(this.derivedKey)
}
9 years ago
this.exportPrivateKey = function (address) {
var privKeyHex = ethUtil.addHexPrefix(this.keyStore.exportPrivateKey(address, this.derivedKey, this.hdPathString))
return privKeyHex
}
}
9 years ago
function padWithZeroes (number, length) {
var myString = '' + number
while (myString.length < length) {
myString = '0' + myString
}
9 years ago
return myString
}
9 years ago
function concatSig (v, r, s) {
9 years ago
r = padWithZeroes(ethUtil.fromSigned(r), 64)
s = padWithZeroes(ethUtil.fromSigned(s), 64)
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))
}