feat: next libp2p-crypto (#90)

* feat: next libp2p-crypto

* chore: update deps
This commit is contained in:
David Dias 2017-07-22 13:53:06 -07:00 committed by GitHub
parent 5b39bf3bff
commit 2df6544c0d
3 changed files with 29 additions and 46 deletions

View File

@ -25,28 +25,28 @@
"author": "Friedel Ziegelmayer <dignifiedquire@gmail.com>", "author": "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"async": "^2.1.5", "async": "^2.5.0",
"debug": "^2.6.3", "debug": "^2.6.8",
"interface-connection": "~0.3.2", "interface-connection": "~0.3.2",
"libp2p-crypto": "~0.8.6", "libp2p-crypto": "~0.9.4",
"multihashing-async": "~0.4.4", "multihashing-async": "~0.4.6",
"peer-id": "~0.8.4", "peer-id": "~0.9.0",
"protocol-buffers": "^3.2.1", "protocol-buffers": "^3.2.1",
"pull-defer": "^0.2.2", "pull-defer": "^0.2.2",
"pull-handshake": "^1.1.4", "pull-handshake": "^1.1.4",
"pull-length-prefixed": "^1.2.0", "pull-length-prefixed": "^1.3.0",
"pull-stream": "^3.5.0" "pull-stream": "^3.6.0"
}, },
"devDependencies": { "devDependencies": {
"aegir": "^11.0.0", "aegir": "^11.0.2",
"benchmark": "^2.1.3", "benchmark": "^2.1.4",
"chai": "^3.5.0", "chai": "^4.1.0",
"dirty-chai": "^1.2.2", "dirty-chai": "^2.0.1",
"gulp": "^3.9.1", "gulp": "^3.9.1",
"libp2p-websockets": "~0.9.4", "libp2p-websockets": "~0.10.0",
"multistream-select": "^0.13.5", "multistream-select": "^0.13.5",
"pre-commit": "^1.2.2", "pre-commit": "^1.2.2",
"pull-goodbye": "0.0.1", "pull-goodbye": "0.0.2",
"pull-pair": "^1.1.0" "pull-pair": "^1.1.0"
}, },
"pre-commit": [ "pre-commit": [

View File

@ -30,7 +30,7 @@ exports.createProposal = (state) => {
} }
exports.createExchange = (state, callback) => { exports.createExchange = (state, callback) => {
crypto.generateEphemeralKeyPair(state.protocols.local.curveT, (err, res) => { crypto.keys.generateEphemeralKeyPair(state.protocols.local.curveT, (err, res) => {
if (err) { if (err) {
return callback(err) return callback(err)
} }
@ -67,7 +67,7 @@ exports.identify = (state, msg, callback) => {
state.proposal.in = pbm.Propose.decode(msg) state.proposal.in = pbm.Propose.decode(msg)
const pubkey = state.proposal.in.pubkey 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) => { PeerId.createFromPubKey(pubkey.toString('base64'), (err, remoteId) => {
if (err) { if (err) {
return callback(err) return callback(err)
@ -156,7 +156,7 @@ exports.generateKeys = (state, callback) => {
(secret, cb) => { (secret, cb) => {
state.shared.secret = secret state.shared.secret = secret
crypto.keyStretcher( crypto.keys.keyStretcher(
state.protocols.local.cipherT, state.protocols.local.cipherT,
state.protocols.local.hashT, state.protocols.local.hashT,
state.shared.secret, state.shared.secret,

View File

@ -11,6 +11,7 @@ const PeerId = require('peer-id')
const crypto = require('libp2p-crypto') const crypto = require('libp2p-crypto')
const parallel = require('async/parallel') const parallel = require('async/parallel')
const series = require('async/series') const series = require('async/series')
const Buffer = require('safe-buffer').Buffer
const ms = require('multistream-select') const ms = require('multistream-select')
const pull = require('pull-stream') const pull = require('pull-stream')
const Listener = ms.Listener const Listener = ms.Listener
@ -20,23 +21,19 @@ const secio = require('../src')
describe('libp2p-secio', () => { describe('libp2p-secio', () => {
it('exports a tag', () => { 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) => { it('upgrades a connection', (done) => {
const p = pair() const p = pair()
createSession(p[0], (err, local) => { createSession(p[0], (err, local) => {
if (err) { expect(err).to.not.exist()
return done(err)
}
createSession(p[1], (err, remote) => { createSession(p[1], (err, remote) => {
if (err) { expect(err).to.not.exist()
return done(err)
}
pull( pull(
pull.values([new Buffer('hello world')]), pull.values([Buffer.from('hello world')]),
local local
) )
@ -44,7 +41,7 @@ describe('libp2p-secio', () => {
remote, remote,
pull.collect((err, chunks) => { pull.collect((err, chunks) => {
expect(err).to.not.exist() expect(err).to.not.exist()
expect(chunks).to.be.eql([new Buffer('hello world')]) expect(chunks).to.eql([Buffer.from('hello world')])
done() done()
}) })
) )
@ -66,9 +63,7 @@ describe('libp2p-secio', () => {
(cb) => { (cb) => {
listener.addHandler('/banana/1.0.0', (protocol, conn) => { listener.addHandler('/banana/1.0.0', (protocol, conn) => {
createSession(conn, (err, local) => { createSession(conn, (err, local) => {
if (err) { expect(err).to.not.exist()
return done(err)
}
pull( pull(
local, local,
pull.collect((err, chunks) => { pull.collect((err, chunks) => {
@ -82,14 +77,10 @@ describe('libp2p-secio', () => {
cb() cb()
}, },
(cb) => dialer.select('/banana/1.0.0', (err, conn) => { (cb) => dialer.select('/banana/1.0.0', (err, conn) => {
if (err) { expect(err).to.not.exist()
return cb(err)
}
createSession(conn, (err, remote) => { createSession(conn, (err, remote) => {
if (err) { expect(err).to.not.exist()
return cb(err)
}
pull( pull(
pull.values([new Buffer('hello world')]), pull.values([new Buffer('hello world')]),
remote remote
@ -97,24 +88,16 @@ describe('libp2p-secio', () => {
cb() cb()
}) })
}) })
], (err) => { ], (err) => expect(err).to.not.exist())
if (err) {
throw err
}
})
}) })
}) })
function createSession (insecure, callback) { function createSession (insecure, callback) {
crypto.generateKeyPair('RSA', 2048, (err, key) => { crypto.keys.generateKeyPair('RSA', 2048, (err, key) => {
if (err) { expect(err).to.not.exist()
return callback(err)
}
key.public.hash((err, digest) => { key.public.hash((err, digest) => {
if (err) { expect(err).to.not.exist()
return callback(err)
}
callback(null, secio.encrypt(new PeerId(digest, key), key, insecure)) callback(null, secio.encrypt(new PeerId(digest, key), key, insecure))
}) })