refactor: crypto and pnet (#469)

* feat: add initial plaintext 2 module

* refactor: initial refactor of pnet

* chore: fix lint

* fix: update plaintext api usage

* test: use plaintext for test crypto

* chore: update deps

test: update dialer suite scope

* feat: add connection protection to the upgrader

* refactor: cleanup and lint fix

* chore: remove unncessary transforms

* chore: temporarily disable bundlesize

* chore: add missing dep

* fix: use it-handshake to prevent overreading

* chore(fix): PR feedback updates

* chore: apply suggestions from code review

Co-Authored-By: Vasco Santos <vasco.santos@moxy.studio>
This commit is contained in:
Jacob Heun
2019-11-04 14:05:58 +01:00
parent af364b070b
commit 138bb0bbae
19 changed files with 577 additions and 309 deletions

View File

@ -13,12 +13,14 @@ const pipe = require('it-pipe')
const { collect } = require('streaming-iterables')
const pSettle = require('p-settle')
const Transport = require('libp2p-websockets')
const Crypto = require('../../src/insecure/plaintext')
const Protector = require('../../src/pnet')
const swarmKeyBuffer = Buffer.from(require('../fixtures/swarm.key'))
const Libp2p = require('../../src')
const Upgrader = require('../../src/upgrader')
const { codes } = require('../../src/errors')
const mockCrypto = require('../utils/mockCrypto')
const mockMultiaddrConnPair = require('../utils/mockMultiaddrConn')
const Peers = require('../fixtures/peers')
const addrs = [
@ -63,7 +65,7 @@ describe('Upgrader', () => {
sinon.stub(localUpgrader, 'muxers').value(muxers)
sinon.stub(remoteUpgrader, 'muxers').value(muxers)
const cryptos = new Map([[mockCrypto.tag, mockCrypto]])
const cryptos = new Map([[Crypto.protocol, Crypto]])
sinon.stub(localUpgrader, 'cryptos').value(cryptos)
sinon.stub(remoteUpgrader, 'cryptos').value(cryptos)
@ -85,7 +87,7 @@ describe('Upgrader', () => {
sinon.stub(localUpgrader, 'muxers').value(muxers)
sinon.stub(remoteUpgrader, 'muxers').value(muxers)
const cryptos = new Map([[mockCrypto.tag, mockCrypto]])
const cryptos = new Map([[Crypto.protocol, Crypto]])
sinon.stub(localUpgrader, 'cryptos').value(cryptos)
sinon.stub(remoteUpgrader, 'cryptos').value(cryptos)
@ -114,6 +116,48 @@ describe('Upgrader', () => {
expect(result).to.eql([hello])
})
it('should use a private connection protector when provided', async () => {
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })
const muxers = new Map([[Muxer.multicodec, Muxer]])
sinon.stub(localUpgrader, 'muxers').value(muxers)
sinon.stub(remoteUpgrader, 'muxers').value(muxers)
const cryptos = new Map([[Crypto.protocol, Crypto]])
sinon.stub(localUpgrader, 'cryptos').value(cryptos)
sinon.stub(remoteUpgrader, 'cryptos').value(cryptos)
const protector = new Protector(swarmKeyBuffer)
sinon.stub(localUpgrader, 'protector').value(protector)
sinon.stub(remoteUpgrader, 'protector').value(protector)
sinon.spy(protector, 'protect')
const connections = await Promise.all([
localUpgrader.upgradeOutbound(outbound),
remoteUpgrader.upgradeInbound(inbound)
])
expect(connections).to.have.length(2)
const { stream, protocol } = await connections[0].newStream('/echo/1.0.0')
expect(protocol).to.equal('/echo/1.0.0')
const hello = Buffer.from('hello there!')
const result = await pipe(
[hello],
stream,
function toBuffer (source) {
return (async function * () {
for await (const val of source) yield val.slice()
})()
},
collect
)
expect(result).to.eql([hello])
expect(protector.protect.callCount).to.eql(2)
})
it('should fail if crypto fails', async () => {
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })
@ -153,7 +197,7 @@ describe('Upgrader', () => {
sinon.stub(localUpgrader, 'muxers').value(muxersLocal)
sinon.stub(remoteUpgrader, 'muxers').value(muxersRemote)
const cryptos = new Map([[mockCrypto.tag, mockCrypto]])
const cryptos = new Map([[Crypto.protocol, Crypto]])
sinon.stub(localUpgrader, 'cryptos').value(cryptos)
sinon.stub(remoteUpgrader, 'cryptos').value(cryptos)
@ -178,7 +222,7 @@ describe('Upgrader', () => {
sinon.stub(localUpgrader, 'muxers').value(muxers)
sinon.stub(remoteUpgrader, 'muxers').value(muxers)
const cryptos = new Map([[mockCrypto.tag, mockCrypto]])
const cryptos = new Map([[Crypto.protocol, Crypto]])
sinon.stub(localUpgrader, 'cryptos').value(cryptos)
sinon.stub(remoteUpgrader, 'cryptos').value(cryptos)
@ -212,7 +256,7 @@ describe('Upgrader', () => {
sinon.stub(localUpgrader, 'muxers').value(muxers)
sinon.stub(remoteUpgrader, 'muxers').value(muxers)
const cryptos = new Map([[mockCrypto.tag, mockCrypto]])
const cryptos = new Map([[Crypto.protocol, Crypto]])
sinon.stub(localUpgrader, 'cryptos').value(cryptos)
sinon.stub(remoteUpgrader, 'cryptos').value(cryptos)
@ -246,7 +290,7 @@ describe('Upgrader', () => {
sinon.stub(localUpgrader, 'muxers').value(muxers)
sinon.stub(remoteUpgrader, 'muxers').value(muxers)
const cryptos = new Map([[mockCrypto.tag, mockCrypto]])
const cryptos = new Map([[Crypto.protocol, Crypto]])
sinon.stub(localUpgrader, 'cryptos').value(cryptos)
sinon.stub(remoteUpgrader, 'cryptos').value(cryptos)
@ -288,18 +332,21 @@ describe('libp2p.upgrader', () => {
})
it('should create an Upgrader', () => {
const protector = new Protector(swarmKeyBuffer)
libp2p = new Libp2p({
peerInfo: peers[0],
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [mockCrypto]
connEncryption: [Crypto],
connProtector: protector
}
})
expect(libp2p.upgrader).to.exist()
expect(libp2p.upgrader.muxers).to.eql(new Map([[Muxer.multicodec, Muxer]]))
expect(libp2p.upgrader.cryptos).to.eql(new Map([[mockCrypto.tag, mockCrypto]]))
expect(libp2p.upgrader.cryptos).to.eql(new Map([[Crypto.protocol, Crypto]]))
expect(libp2p.upgrader.protector).to.equal(protector)
// Ensure the transport manager also has the upgrader
expect(libp2p.upgrader).to.equal(libp2p.transportManager.upgrader)
})
@ -310,7 +357,7 @@ describe('libp2p.upgrader', () => {
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [mockCrypto]
connEncryption: [Crypto]
}
})
@ -335,14 +382,14 @@ describe('libp2p.upgrader', () => {
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [mockCrypto]
connEncryption: [Crypto]
}
})
const remoteUpgrader = new Upgrader({
localPeer: remotePeer.id,
muxers: new Map([[Muxer.multicodec, Muxer]]),
cryptos: new Map([[mockCrypto.tag, mockCrypto]])
cryptos: new Map([[Crypto.protocol, Crypto]])
})
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer: remotePeer.id })