Skip to content

Commit b358530

Browse files
committed
add toJSON and fromJSOn and cli to quickly generate
1 parent 4afbedb commit b358530

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.6.6",
44
"description": "IPFS Peer Id implementation in Node.js",
55
"main": "lib/index.js",
6+
"bin": "src/bin.js",
67
"jsnext:main": "src/index.js",
78
"scripts": {
89
"lint": "aegir-lint",

src/bin.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/local/bin/node
2+
3+
'use strict'
4+
5+
const PeerId = require('./index.js')
6+
7+
console.log(JSON.stringify(PeerId.create().toJSON(), null, ' '))

src/index.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ const path = require('path')
1313

1414
const pbCrypto = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto')))
1515

16-
exports = module.exports = Id
16+
exports = module.exports = PeerId
1717

1818
exports.Buffer = Buffer
1919

20-
function Id (id, privKey, pubKey) {
20+
function PeerId (id, privKey, pubKey) {
2121
const self = this
2222

23-
if (!(self instanceof Id)) {
23+
if (!(self instanceof PeerId)) {
2424
throw new Error('Id must be called with new')
2525
}
2626

@@ -37,6 +37,14 @@ function Id (id, privKey, pubKey) {
3737
}
3838
}
3939

40+
self.toJSON = function () {
41+
return {
42+
id: self.id.toString('hex'),
43+
privKey: self.privKey.toString('hex'),
44+
pubKey: self.pubKey.toString('hex')
45+
}
46+
}
47+
4048
// encode/decode functions
4149
self.toHexString = function () {
4250
return self.id.toString('hex')
@@ -122,26 +130,26 @@ exports.create = function (opts) {
122130

123131
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
124132

125-
return new Id(mhId, bufProtoPriv64, bufProtoPub64)
133+
return new PeerId(mhId, bufProtoPriv64, bufProtoPub64)
126134
}
127135

128136
exports.createFromHexString = function (str) {
129-
return new Id(new Buffer(str, 'hex'))
137+
return new PeerId(new Buffer(str, 'hex'))
130138
}
131139

132140
exports.createFromBytes = function (buf) {
133-
return new Id(buf)
141+
return new PeerId(buf)
134142
}
135143

136144
exports.createFromB58String = function (str) {
137-
return new Id(new Buffer(base58.decode(str)))
145+
return new PeerId(new Buffer(base58.decode(str)))
138146
}
139147

140148
// Public Key input will be a buffer
141149
exports.createFromPubKey = function (pubKey) {
142150
const buf = new Buffer(pubKey, 'base64')
143151
const mhId = multihashing(buf, 'sha2-256')
144-
return new Id(mhId, null, pubKey)
152+
return new PeerId(mhId, null, pubKey)
145153
}
146154

147155
// Private key input will be a string
@@ -173,5 +181,12 @@ exports.createFromPrivKey = function (privKey) {
173181
// buffer the public key for consistency before storing
174182
const bufProtoPub64 = new Buffer(protoPublic64, 'base64')
175183
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
176-
return new Id(mhId, privKey, bufProtoPub64)
184+
return new PeerId(mhId, privKey, bufProtoPub64)
185+
}
186+
187+
exports.createFromJSON = function (obj) {
188+
return new PeerId(
189+
new Buffer(obj.id, 'hex'),
190+
new Buffer(obj.privKey, 'hex'),
191+
new Buffer(obj.pubKey, 'hex'))
177192
}

test/index.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,12 @@ describe('id', function (done) {
9393
expect(id.toBytes().toString('hex')).to.equal(testIdBytes.toString('hex'))
9494
done()
9595
})
96+
97+
it('toJSON', (done) => {
98+
const id = PeerId.create()
99+
expect(id.toB58String()).to.equal(PeerId.createFromJSON(id.toJSON()).toB58String())
100+
expect(id.privKey).to.deep.equal(PeerId.createFromJSON(id.toJSON()).privKey)
101+
expect(id.pubKey).to.deep.equal(PeerId.createFromJSON(id.toJSON()).pubKey)
102+
done()
103+
})
96104
})

0 commit comments

Comments
 (0)