mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-06-25 21:01:36 +00:00
add toJSON and fromJSOn and cli to quickly generate
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
"version": "0.6.6",
|
||||
"description": "IPFS Peer Id implementation in Node.js",
|
||||
"main": "lib/index.js",
|
||||
"bin": "src/bin.js",
|
||||
"jsnext:main": "src/index.js",
|
||||
"scripts": {
|
||||
"lint": "aegir-lint",
|
||||
|
7
src/bin.js
Executable file
7
src/bin.js
Executable file
@ -0,0 +1,7 @@
|
||||
#!/usr/local/bin/node
|
||||
|
||||
'use strict'
|
||||
|
||||
const PeerId = require('./index.js')
|
||||
|
||||
console.log(JSON.stringify(PeerId.create().toJSON(), null, ' '))
|
33
src/index.js
33
src/index.js
@ -13,14 +13,14 @@ const path = require('path')
|
||||
|
||||
const pbCrypto = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto')))
|
||||
|
||||
exports = module.exports = Id
|
||||
exports = module.exports = PeerId
|
||||
|
||||
exports.Buffer = Buffer
|
||||
|
||||
function Id (id, privKey, pubKey) {
|
||||
function PeerId (id, privKey, pubKey) {
|
||||
const self = this
|
||||
|
||||
if (!(self instanceof Id)) {
|
||||
if (!(self instanceof PeerId)) {
|
||||
throw new Error('Id must be called with new')
|
||||
}
|
||||
|
||||
@ -37,6 +37,14 @@ function Id (id, privKey, pubKey) {
|
||||
}
|
||||
}
|
||||
|
||||
self.toJSON = function () {
|
||||
return {
|
||||
id: self.id.toString('hex'),
|
||||
privKey: self.privKey.toString('hex'),
|
||||
pubKey: self.pubKey.toString('hex')
|
||||
}
|
||||
}
|
||||
|
||||
// encode/decode functions
|
||||
self.toHexString = function () {
|
||||
return self.id.toString('hex')
|
||||
@ -122,26 +130,26 @@ exports.create = function (opts) {
|
||||
|
||||
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
||||
|
||||
return new Id(mhId, bufProtoPriv64, bufProtoPub64)
|
||||
return new PeerId(mhId, bufProtoPriv64, bufProtoPub64)
|
||||
}
|
||||
|
||||
exports.createFromHexString = function (str) {
|
||||
return new Id(new Buffer(str, 'hex'))
|
||||
return new PeerId(new Buffer(str, 'hex'))
|
||||
}
|
||||
|
||||
exports.createFromBytes = function (buf) {
|
||||
return new Id(buf)
|
||||
return new PeerId(buf)
|
||||
}
|
||||
|
||||
exports.createFromB58String = function (str) {
|
||||
return new Id(new Buffer(base58.decode(str)))
|
||||
return new PeerId(new Buffer(base58.decode(str)))
|
||||
}
|
||||
|
||||
// Public Key input will be a buffer
|
||||
exports.createFromPubKey = function (pubKey) {
|
||||
const buf = new Buffer(pubKey, 'base64')
|
||||
const mhId = multihashing(buf, 'sha2-256')
|
||||
return new Id(mhId, null, pubKey)
|
||||
return new PeerId(mhId, null, pubKey)
|
||||
}
|
||||
|
||||
// Private key input will be a string
|
||||
@ -173,5 +181,12 @@ exports.createFromPrivKey = function (privKey) {
|
||||
// buffer the public key for consistency before storing
|
||||
const bufProtoPub64 = new Buffer(protoPublic64, 'base64')
|
||||
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
|
||||
return new Id(mhId, privKey, bufProtoPub64)
|
||||
return new PeerId(mhId, privKey, bufProtoPub64)
|
||||
}
|
||||
|
||||
exports.createFromJSON = function (obj) {
|
||||
return new PeerId(
|
||||
new Buffer(obj.id, 'hex'),
|
||||
new Buffer(obj.privKey, 'hex'),
|
||||
new Buffer(obj.pubKey, 'hex'))
|
||||
}
|
||||
|
@ -93,4 +93,12 @@ describe('id', function (done) {
|
||||
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex'))
|
||||
done()
|
||||
})
|
||||
|
||||
it('toJSON', (done) => {
|
||||
const id = PeerId.create()
|
||||
expect(id.toB58String()).to.equal(PeerId.createFromJSON(id.toJSON()).toB58String())
|
||||
expect(id.privKey).to.deep.equal(PeerId.createFromJSON(id.toJSON()).privKey)
|
||||
expect(id.pubKey).to.deep.equal(PeerId.createFromJSON(id.toJSON()).pubKey)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user