Skip to content

Instantly share code, notes, and snippets.

@emilbayes
Last active November 14, 2018 11:43
Show Gist options
  • Save emilbayes/46e2a1191b43599b2f2cdce9f8184de6 to your computer and use it in GitHub Desktop.
Save emilbayes/46e2a1191b43599b2f2cdce9f8184de6 to your computer and use it in GitHub Desktop.
node_modules
remote
local
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()
swarm.on('connection', function (stream) {
const encryptedStream = noise(stream, true, {
pattern: 'XK',
staticKeyPair: client,
remoteStaticKey: publicKey
})
encryptedStream.on('error', (err) => {
console.error(err)
})
encryptedStream.write(Buffer.from('my-id'))
encryptedStream.once('readable', function () {
var hello = JSON.parse(encryptedStream.read().toString())
if (hello.error) {
console.log(hello.message)
process.exit
}
const feed = hypercore(`./local/test`, key)
pump(encryptedStream, feed.replicate({ live: true }), encryptedStream)
})
})
swarm.join(publicKey, {
announce: false,
lookup: true
})
{
"dependencies": {
"@hyperswarm/network": "0.0.4",
"duplex-json-stream": "^1.0.1",
"hypercore": "^6.21.0",
"hypercore-protocol": "^6.7.1",
"noise-peer": "^1.0.0",
"pump": "^3.0.0"
}
}
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.seedKeygen(Buffer.alloc(32, 'secret'))
console.log(server.publicKey.toString('hex'))
const swarm = network()
const hyperswarm = network()
const feeds = new Map()
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()
}
})
encryptedStream.on('error', (err) => {
console.error(err)
})
encryptedStream.once('readable', function () {
var id = encryptedStream.read()
if (!id.equals(Buffer.from('my-id'))) {
encryptedStream.end(JSON.stringify({
error: true,
message: 'Auth failed. Goodbye'
}))
return
}
console.log(id)
// do something with id
const feed = hypercore(`./remote/test`)
feed.ready((err) => {
if (encryptedStream.destroyed) return
if (err) return encryptedStream.destroy(err)
encryptedStream.write(JSON.stringify({
key: feed.key.toString('hex')
}))
pump(encryptedStream, feed.replicate({ live: true }), encryptedStream, function (err) {
console.log('replication ended', err)
})
const key = feed.key.toString('hex')
console.log(key)
feed.append('important history data')
})
})
})
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