From 2df6544c0d3020a730f16e80407510b5318e714b Mon Sep 17 00:00:00 2001 From: David Dias Date: Sat, 22 Jul 2017 13:53:06 -0700 Subject: [PATCH] feat: next libp2p-crypto (#90) * feat: next libp2p-crypto * chore: update deps --- package.json | 26 ++++++++++++------------- src/handshake/crypto.js | 6 +++--- test/index.spec.js | 43 +++++++++++++---------------------------- 3 files changed, 29 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index dc7a571..57245a8 100644 --- a/package.json +++ b/package.json @@ -25,28 +25,28 @@ "author": "Friedel Ziegelmayer ", "license": "MIT", "dependencies": { - "async": "^2.1.5", - "debug": "^2.6.3", + "async": "^2.5.0", + "debug": "^2.6.8", "interface-connection": "~0.3.2", - "libp2p-crypto": "~0.8.6", - "multihashing-async": "~0.4.4", - "peer-id": "~0.8.4", + "libp2p-crypto": "~0.9.4", + "multihashing-async": "~0.4.6", + "peer-id": "~0.9.0", "protocol-buffers": "^3.2.1", "pull-defer": "^0.2.2", "pull-handshake": "^1.1.4", - "pull-length-prefixed": "^1.2.0", - "pull-stream": "^3.5.0" + "pull-length-prefixed": "^1.3.0", + "pull-stream": "^3.6.0" }, "devDependencies": { - "aegir": "^11.0.0", - "benchmark": "^2.1.3", - "chai": "^3.5.0", - "dirty-chai": "^1.2.2", + "aegir": "^11.0.2", + "benchmark": "^2.1.4", + "chai": "^4.1.0", + "dirty-chai": "^2.0.1", "gulp": "^3.9.1", - "libp2p-websockets": "~0.9.4", + "libp2p-websockets": "~0.10.0", "multistream-select": "^0.13.5", "pre-commit": "^1.2.2", - "pull-goodbye": "0.0.1", + "pull-goodbye": "0.0.2", "pull-pair": "^1.1.0" }, "pre-commit": [ diff --git a/src/handshake/crypto.js b/src/handshake/crypto.js index 8d5a768..387b271 100644 --- a/src/handshake/crypto.js +++ b/src/handshake/crypto.js @@ -30,7 +30,7 @@ exports.createProposal = (state) => { } exports.createExchange = (state, callback) => { - crypto.generateEphemeralKeyPair(state.protocols.local.curveT, (err, res) => { + crypto.keys.generateEphemeralKeyPair(state.protocols.local.curveT, (err, res) => { if (err) { return callback(err) } @@ -67,7 +67,7 @@ exports.identify = (state, msg, callback) => { state.proposal.in = pbm.Propose.decode(msg) const pubkey = state.proposal.in.pubkey - state.key.remote = crypto.unmarshalPublicKey(pubkey) + state.key.remote = crypto.keys.unmarshalPublicKey(pubkey) PeerId.createFromPubKey(pubkey.toString('base64'), (err, remoteId) => { if (err) { return callback(err) @@ -156,7 +156,7 @@ exports.generateKeys = (state, callback) => { (secret, cb) => { state.shared.secret = secret - crypto.keyStretcher( + crypto.keys.keyStretcher( state.protocols.local.cipherT, state.protocols.local.hashT, state.shared.secret, diff --git a/test/index.spec.js b/test/index.spec.js index cf42824..2ab7610 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -11,6 +11,7 @@ const PeerId = require('peer-id') const crypto = require('libp2p-crypto') const parallel = require('async/parallel') const series = require('async/series') +const Buffer = require('safe-buffer').Buffer const ms = require('multistream-select') const pull = require('pull-stream') const Listener = ms.Listener @@ -20,23 +21,19 @@ const secio = require('../src') describe('libp2p-secio', () => { it('exports a tag', () => { - expect(secio.tag).to.be.eql('/secio/1.0.0') + expect(secio.tag).to.equal('/secio/1.0.0') }) it('upgrades a connection', (done) => { const p = pair() createSession(p[0], (err, local) => { - if (err) { - return done(err) - } + expect(err).to.not.exist() createSession(p[1], (err, remote) => { - if (err) { - return done(err) - } + expect(err).to.not.exist() pull( - pull.values([new Buffer('hello world')]), + pull.values([Buffer.from('hello world')]), local ) @@ -44,7 +41,7 @@ describe('libp2p-secio', () => { remote, pull.collect((err, chunks) => { expect(err).to.not.exist() - expect(chunks).to.be.eql([new Buffer('hello world')]) + expect(chunks).to.eql([Buffer.from('hello world')]) done() }) ) @@ -66,9 +63,7 @@ describe('libp2p-secio', () => { (cb) => { listener.addHandler('/banana/1.0.0', (protocol, conn) => { createSession(conn, (err, local) => { - if (err) { - return done(err) - } + expect(err).to.not.exist() pull( local, pull.collect((err, chunks) => { @@ -82,14 +77,10 @@ describe('libp2p-secio', () => { cb() }, (cb) => dialer.select('/banana/1.0.0', (err, conn) => { - if (err) { - return cb(err) - } + expect(err).to.not.exist() createSession(conn, (err, remote) => { - if (err) { - return cb(err) - } + expect(err).to.not.exist() pull( pull.values([new Buffer('hello world')]), remote @@ -97,24 +88,16 @@ describe('libp2p-secio', () => { cb() }) }) - ], (err) => { - if (err) { - throw err - } - }) + ], (err) => expect(err).to.not.exist()) }) }) function createSession (insecure, callback) { - crypto.generateKeyPair('RSA', 2048, (err, key) => { - if (err) { - return callback(err) - } + crypto.keys.generateKeyPair('RSA', 2048, (err, key) => { + expect(err).to.not.exist() key.public.hash((err, digest) => { - if (err) { - return callback(err) - } + expect(err).to.not.exist() callback(null, secio.encrypt(new PeerId(digest, key), key, insecure)) })