mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-06-26 20:41:53 +00:00
added create id from private key
This commit is contained in:
@ -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"
|
||||
}
|
||||
}
|
||||
|
16
src/index.js
16
src/index.js
@ -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)
|
||||
}
|
||||
|
@ -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()
|
||||
})
|
||||
|
Reference in New Issue
Block a user