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

@ -1,6 +1,6 @@
{
"name": "peer-id",
"version": "0.3.4",
"version": "0.4.0",
"description": "IPFS Peer Id implementation in Node.js",
"main": "src/index.js",
"scripts": {
@ -41,6 +41,7 @@
"dependencies": {
"bs58": "^3.0.0",
"keypair": "^1.0.0",
"multihashing": "^0.2.0"
"multihashing": "^0.2.0",
"node-forge": "^0.6.38"
}
}

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)
}

View File

@ -44,6 +44,7 @@ test('Recreate from a Public Key', function (t) {
})
test('Recreate from a Private Key', function (t) {
// TODO
var id = PeerId.createFromPrivKey(testId.privKey)
t.ok(id)
t.end()
})