mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-06-27 06:41:50 +00:00
added create id from private key
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "peer-id",
|
"name": "peer-id",
|
||||||
"version": "0.3.4",
|
"version": "0.4.0",
|
||||||
"description": "IPFS Peer Id implementation in Node.js",
|
"description": "IPFS Peer Id implementation in Node.js",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -41,6 +41,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bs58": "^3.0.0",
|
"bs58": "^3.0.0",
|
||||||
"keypair": "^1.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 multihashing = require('multihashing')
|
||||||
var base58 = require('bs58')
|
var base58 = require('bs58')
|
||||||
var keypair = require('keypair')
|
var keypair = require('keypair')
|
||||||
|
var forge = require('node-forge')
|
||||||
|
|
||||||
exports = module.exports = Id
|
exports = module.exports = Id
|
||||||
|
|
||||||
@ -46,6 +47,10 @@ function Id (id, privKey, pubKey) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fix (str) {
|
||||||
|
return str.replace(/\r/g, '') + '\n'
|
||||||
|
}
|
||||||
|
|
||||||
// generation
|
// generation
|
||||||
|
|
||||||
exports.create = function () {
|
exports.create = function () {
|
||||||
@ -73,6 +78,13 @@ exports.createFromPubKey = function (pubKey) {
|
|||||||
return new Id(mhId, null, pubKey)
|
return new Id(mhId, null, pubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createFromPrivKey = function () {
|
exports.createFromPrivKey = function (privKey) {
|
||||||
// TODO(daviddias) derive PubKey from priv
|
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) {
|
test('Recreate from a Private Key', function (t) {
|
||||||
// TODO
|
var id = PeerId.createFromPrivKey(testId.privKey)
|
||||||
|
t.ok(id)
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user