Skip to content

Commit c1e18ee

Browse files
committed
Merge pull request #24 from diasdavid/feat/marshal-unmarshal
Feat/marshal unmarshal
2 parents e9b107f + b358530 commit c1e18ee

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed

package.json

+3-2
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",
@@ -31,7 +32,7 @@
3132
},
3233
"homepage": "https://github.com/diasdavid/js-peer-id",
3334
"devDependencies": {
34-
"aegir": "^2.1.1",
35+
"aegir": "^3.0.4",
3536
"buffer-loader": "0.0.1",
3637
"chai": "^3.5.0",
3738
"pre-commit": "^1.1.2"
@@ -46,7 +47,7 @@
4647
"type": "git",
4748
"url": "https://github.com/diasdavid/js-peer-id.git"
4849
},
49-
"dignified": {
50+
"aegir": {
5051
"webpack": {
5152
"resolve": {
5253
"alias": {

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

+39-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Id is an object representation of a peer Id. a peer Id is a multihash
33
*/
4+
45
'use strict'
56

67
const fs = require('fs')
@@ -10,17 +11,16 @@ const forge = require('node-forge')
1011
const protobuf = require('protocol-buffers')
1112
const path = require('path')
1213

13-
// protobuf read from file
14-
const messages = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto')))
14+
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')
@@ -52,23 +60,25 @@ function Id (id, privKey, pubKey) {
5260
}
5361

5462
// unwrap the private key protobuf
55-
function unmarshal (key) {
56-
return messages.PrivateKey.decode(key)
63+
function keyUnmarshal (key) {
64+
return pbCrypto.PrivateKey.decode(key)
5765
}
5866

5967
// create a public key protobuf to be base64 string stored in config
60-
function marshal (data, type) {
61-
var epb
68+
function keyMarshal (data, type) {
69+
const RSA = 0
70+
71+
let epb
6272
if (type === 'Public') {
63-
epb = messages.PublicKey.encode({
64-
Type: 0,
73+
epb = pbCrypto.PublicKey.encode({
74+
Type: RSA,
6575
Data: data
6676
})
6777
}
6878

6979
if (type === 'Private') {
70-
epb = messages.PrivateKey.encode({
71-
Type: 0,
80+
epb = pbCrypto.PrivateKey.encode({
81+
Type: RSA,
7282
Data: data
7383
})
7484
}
@@ -88,10 +98,10 @@ function formatKey (key, type) {
8898
const nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary')
8999

90100
// protobuf the new DER bytes to the PublicKey Data: field
91-
const marshalKey = marshal(nDerBuf, type)
101+
const marsheledKey = keyMarshal(nDerBuf, type)
92102

93103
// encode the protobuf public key to base64 string
94-
const b64 = marshalKey.toString('base64')
104+
const b64 = marsheledKey.toString('base64')
95105
return b64
96106
}
97107

@@ -120,26 +130,26 @@ exports.create = function (opts) {
120130

121131
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
122132

123-
return new Id(mhId, bufProtoPriv64, bufProtoPub64)
133+
return new PeerId(mhId, bufProtoPriv64, bufProtoPub64)
124134
}
125135

126136
exports.createFromHexString = function (str) {
127-
return new Id(new Buffer(str, 'hex'))
137+
return new PeerId(new Buffer(str, 'hex'))
128138
}
129139

130140
exports.createFromBytes = function (buf) {
131-
return new Id(buf)
141+
return new PeerId(buf)
132142
}
133143

134144
exports.createFromB58String = function (str) {
135-
return new Id(new Buffer(base58.decode(str)))
145+
return new PeerId(new Buffer(base58.decode(str)))
136146
}
137147

138148
// Public Key input will be a buffer
139149
exports.createFromPubKey = function (pubKey) {
140150
const buf = new Buffer(pubKey, 'base64')
141151
const mhId = multihashing(buf, 'sha2-256')
142-
return new Id(mhId, null, pubKey)
152+
return new PeerId(mhId, null, pubKey)
143153
}
144154

145155
// Private key input will be a string
@@ -148,7 +158,7 @@ exports.createFromPrivKey = function (privKey) {
148158
const buf = new Buffer(privKey, 'base64')
149159

150160
// get the private key data from the protobuf
151-
const mpk = unmarshal(buf)
161+
const mpk = keyUnmarshal(buf)
152162

153163
// create a forge buffer
154164
const fbuf = forge.util.createBuffer(mpk.Data.toString('binary'))
@@ -171,5 +181,12 @@ exports.createFromPrivKey = function (privKey) {
171181
// buffer the public key for consistency before storing
172182
const bufProtoPub64 = new Buffer(protoPublic64, 'base64')
173183
const mhId = multihashing(new Buffer(protoPublic64, 'base64'), 'sha2-256')
174-
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'))
175192
}

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)