added create id from private key

This commit is contained in:
David Dias
2015-11-05 18:51:53 +00:00
committed by nginnever
parent e3cc273a14
commit 5cb511c646
3 changed files with 19 additions and 5 deletions

View File

@ -5,6 +5,7 @@
var multihashing = require('multihashing')
var base58 = require('bs58')
var keypair = require('keypair')
var forge = require('node-forge')
exports = module.exports = Id
@ -46,6 +47,10 @@ function Id (id, privKey, pubKey) {
}
}
function fix (str) {
return str.replace(/\r/g, '') + '\n'
}
// generation
exports.create = function () {
@ -73,6 +78,13 @@ exports.createFromPubKey = function (pubKey) {
return new Id(mhId, null, pubKey)
}
exports.createFromPrivKey = function () {
// TODO(daviddias) derive PubKey from priv
exports.createFromPrivKey = function (privKey) {
var privateKey = forge.pki.privateKeyFromPem(privKey)
var publicKey = {
n: privateKey.n,
e: privateKey.e
}
var pubKey = fix(forge.pki.publicKeyToRSAPublicKeyPem(publicKey, 72))
var mhId = multihashing(pubKey, 'sha2-256')
return new Id(mhId, privKey, pubKey)
}