From 4afbedb525f499f14a771f841200aa6bae687f3a Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 23 May 2016 14:16:30 +0100 Subject: [PATCH 1/2] update dependencies --- package.json | 4 ++-- src/index.js | 28 +++++++++++++++------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 772690e..a93cc1d 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "homepage": "https://github.com/diasdavid/js-peer-id", "devDependencies": { - "aegir": "^2.1.1", + "aegir": "^3.0.4", "buffer-loader": "0.0.1", "chai": "^3.5.0", "pre-commit": "^1.1.2" @@ -46,7 +46,7 @@ "type": "git", "url": "https://github.com/diasdavid/js-peer-id.git" }, - "dignified": { + "aegir": { "webpack": { "resolve": { "alias": { diff --git a/src/index.js b/src/index.js index d8df1a2..5805f7a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ /* * Id is an object representation of a peer Id. a peer Id is a multihash */ + 'use strict' const fs = require('fs') @@ -10,8 +11,7 @@ const forge = require('node-forge') const protobuf = require('protocol-buffers') const path = require('path') -// protobuf read from file -const messages = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto'))) +const pbCrypto = protobuf(fs.readFileSync(path.resolve(__dirname, '../protos/crypto.proto'))) exports = module.exports = Id @@ -52,23 +52,25 @@ function Id (id, privKey, pubKey) { } // unwrap the private key protobuf -function unmarshal (key) { - return messages.PrivateKey.decode(key) +function keyUnmarshal (key) { + return pbCrypto.PrivateKey.decode(key) } // create a public key protobuf to be base64 string stored in config -function marshal (data, type) { - var epb +function keyMarshal (data, type) { + const RSA = 0 + + let epb if (type === 'Public') { - epb = messages.PublicKey.encode({ - Type: 0, + epb = pbCrypto.PublicKey.encode({ + Type: RSA, Data: data }) } if (type === 'Private') { - epb = messages.PrivateKey.encode({ - Type: 0, + epb = pbCrypto.PrivateKey.encode({ + Type: RSA, Data: data }) } @@ -88,10 +90,10 @@ function formatKey (key, type) { const nDerBuf = new Buffer(fDerBuf.getBytes(), 'binary') // protobuf the new DER bytes to the PublicKey Data: field - const marshalKey = marshal(nDerBuf, type) + const marsheledKey = keyMarshal(nDerBuf, type) // encode the protobuf public key to base64 string - const b64 = marshalKey.toString('base64') + const b64 = marsheledKey.toString('base64') return b64 } @@ -148,7 +150,7 @@ exports.createFromPrivKey = function (privKey) { const buf = new Buffer(privKey, 'base64') // get the private key data from the protobuf - const mpk = unmarshal(buf) + const mpk = keyUnmarshal(buf) // create a forge buffer const fbuf = forge.util.createBuffer(mpk.Data.toString('binary')) From b358530dfb8ce0f86fa0058cec8df57a4f399353 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 23 May 2016 15:25:30 +0100 Subject: [PATCH 2/2] add toJSON and fromJSOn and cli to quickly generate --- package.json | 1 + src/bin.js | 7 +++++++ src/index.js | 33 ++++++++++++++++++++++++--------- test/index.spec.js | 8 ++++++++ 4 files changed, 40 insertions(+), 9 deletions(-) create mode 100755 src/bin.js diff --git a/package.json b/package.json index a93cc1d..e3b1bac 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/bin.js b/src/bin.js new file mode 100755 index 0000000..b17b525 --- /dev/null +++ b/src/bin.js @@ -0,0 +1,7 @@ +#!/usr/local/bin/node + +'use strict' + +const PeerId = require('./index.js') + +console.log(JSON.stringify(PeerId.create().toJSON(), null, ' ')) diff --git a/src/index.js b/src/index.js index 5805f7a..9d32d71 100644 --- a/src/index.js +++ b/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')) } diff --git a/test/index.spec.js b/test/index.spec.js index 0cf653b..c61fd93 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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() + }) })