emilbayes / noise-peer

Simple end-to-end encrypted, secure channels using Noise Protocol Framework and libsodium secretstream
ISC License
147 stars 13 forks source link

client crashes when no next tick set #5

Open robertkowalski opened 5 years ago

robertkowalski commented 5 years ago

my first noise server! :)

noise-network returns a noise stream on connect(), but it crashes when i do not use nextTick

first guess: probably we need one inside noise-peer to fix the issue.

server.js

'use strict'

const noise = require('noise-network')
const server = noise.createServer()
const stream = require('stream')
const hypercore = require('hypercore')
const { write } = require('length-prefixed-message')
const stats = require('./stats')

const feed = hypercore(`./test`)

server.on('connection', function (encryptedStream) {
  write(encryptedStream, JSON.stringify({
    key: feed.key.toString('hex')
  }))

  stream.pipeline(
    encryptedStream,
    feed.replicate({ live: true }),
    encryptedStream,
    (err) => {
      console.log('replication ended', err)
    }
  )
})

const keyPair = noise.seedKeygen(Buffer.alloc(32, 'secret'))

server.listen(keyPair, function () {
  console.log('Server is listening on:', server.publicKey.toString('hex'))
})

client.js


 const client = noise.connect('a9a5544b217d38cc0165d3fce9381e38464025d26d1e7e38c4ce4a729e73410b')
  // when no process.nextTick is used, client crashes
  // process.nextTick(function () { 
    read(client, (msg) => {
      msg = msg.toString()
      const p = JSON.parse(msg)

      const feed = hypercore(`./client-dbs/local-${testGroup}-${id}`, p.key)
      stream.pipeline(
        client,
        feed.replicate({ live: true }),
        client,
        (err) => {
          console.log('replication ended', err)
        }
      )
    })
 // })
emilbayes commented 5 years ago

This was fixed here: https://github.com/emilbayes/noise-peer/commit/f4c573c508cecdba092467427d9fc75d65bb5b60 but it is missing tests and update to noise-network