Skip to content

Instantly share code, notes, and snippets.

@ninabreznik
Last active June 18, 2021 22:34
Show Gist options
  • Save ninabreznik/b80eb8cd46f77a913b597633969d4300 to your computer and use it in GitHub Desktop.
Save ninabreznik/b80eb8cd46f77a913b597633969d4300 to your computer and use it in GitHub Desktop.
hyperswarm/dht-5
/*--------------------------------------
PEER 1
---------------------------------------- */
const DHT = require('@hyperswarm/dht')
start()
async function start () {
const node = new DHT()
const keyPair = DHT.keyPair()
const server = node.createServer()
server.on('connection', (encryptedSocket) => {
console.log('from', encryptedSocket.remotePublicKey)
encryptedSocket.on('data', (data) => {
console.log('Got data', {data})
})
})
await server.listen(keyPair)
console.log(keyPair.publicKey.toString('hex'))
}
/*--------------------------------------
PEER 2
---------------------------------------- */
const DHT = require('@hyperswarm/dht')
start()
async function start () {
const node = new DHT()
const pubkey = 'f7c9996b3c3c49b717657ecd289a32280769aa4757fc9bcafc92046eee0780ee' // copy over from keyPair.publicKey from above
const encryptedSocket = node.connect(Buffer.from(pubkey, 'hex'))
encryptedSocket.on('open', function () {
// encryptedSocket fully open with the other peer
})
// pipe it somewhere like any duplex stream
process.stdin.pipe(noiseSocket).pipe(process.stdout)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment