Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Created November 12, 2018 15:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mafintosh/99a44acc2164f11f8dde58c94b972fed to your computer and use it in GitHub Desktop.
Save mafintosh/99a44acc2164f11f8dde58c94b972fed to your computer and use it in GitHub Desktop.
const noise = require('noise-peer')
const network = require('@hyperswarm/network')
const jsonStream = require('duplex-json-stream')
const hypercore = require('hypercore')
const pump = require('pump')
const swarm = network()
const hyperswarm = network()
const publicKey = Buffer.from(process.argv[2], 'hex')
const feeds = new Map()
const client = noise.keygen()
hyperswarm.on('connection', function (connection, info) {
if (!info.peer) {
// TODO: handle like we do on the server
return
}
const feed = feeds.get(info.peer.topic.toString('hex'))
if (!feed) return connection.destroy()
pump(connection, feed.replicate({ live: true }), connection)
})
swarm.on('connection', function (stream) {
const encryptedStream = noise(stream, true, {
pattern: 'XK',
staticKeyPair: client,
remoteStaticKey: publicKey
})
const json = jsonStream(encryptedStream)
json.once('data', function (handshake) {
const feed = hypercore(`./local/${handshake.hypercore}`, handshake.hypercore)
feed.ready(function (err) {
if (err) return json.destroy(err)
feeds.set(feed.discoveryKey.toString('hex'), feed)
hyperswarm.join(feed.discoveryKey, {
announce: true,
lookup: true
})
})
})
json.write({ userid: 'testuser' })
})
swarm.join(publicKey, {
announce: false,
lookup: true
})
const noise = require('noise-peer')
const network = require('@hyperswarm/network')
const jsonStream = require('duplex-json-stream')
const hypercore = require('hypercore')
const protocol = require('hypercore-protocol')
const pump = require('pump')
const server = noise.keygen()
console.log(server.publicKey.toString('hex'))
const swarm = network()
const hyperswarm = network()
const feeds = new Map()
hyperswarm.on('connection', function (stream, info) {
console.log('new connection ...')
const p = protocol()
pump(p, stream, p)
p.on('feed', function (discoveryKey) {
const feed = feeds.get(discoveryKey.toString('hex'))
if (!feed) return p.destroy()
feed.replicate({ stream: p })
})
})
swarm.on('connection', function (stream) {
const encryptedStream = noise(stream, false, {
pattern: 'XK',
staticKeyPair: server,
onstatickey: function (remoteKey, done) {
console.log('new client key:', remoteKey)
done()
}
})
const json = jsonStream(encryptedStream)
json.on('error', function () {})
json.once('data', function (handshake) {
// check if we already have a hypercore ...
const feed = hypercore(`./data/${handshake.userid}`)
feed.append('important data')
feed.ready(function (err) {
if (err) return json.destroy(err)
feeds.set(feed.discoveryKey.toString('hex'), feed)
json.write({ hypercore: feed.key.toString('hex') })
hyperswarm.join(feed.discoveryKey, {
announce: true,
lookup: false
})
})
})
})
swarm.join(server.publicKey, {
announce: true,
lookup: false
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment