Skip to content

hypercore-protocol extension to authenticate peers

License

Notifications You must be signed in to change notification settings

Telios-org/hypercore-peer-auth

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hypercore-peer-auth

Authenticate a hypercore-protocol connection by signing the NOISE public keys with a static key pair.

See test.js for an example.

Example

const hyperswarm = require('hyperswarm')
const Protocol = require('hypercore-protocol')
const crypto = require('hypercore-crypto')
const auth = require('.')

// each peer/device has a keypair that is stored (or derived)
// this could also be the keypair from an existing hypercore feed
const IDENTITY = crypto.keyPair()
// console.log('my key', IDENTITY.publicKey.toString('hex'))

// it also maintains a list of the pubkeys of peers it wants to connect with
const ALLOWED_KEYS = []

const swarm = hyperswarm()
swarm.on('connection', onconnection)
function onconnection (socket, details) {
  const isInitiator = !!details.client
  const protocol = new Protocol(!!details.client)

  pump(socket, protocol, socket)

  auth(protocol, {
    authKeyPair: IDENTITY
    onauthenticate (peerAuthKey, cb) {
      for (const key of ALLOWED_KEYS) {
        if (key.equals(peerAuthKey)) return cb(null, true)
      }
      cb(null, false)
    },
    onprotocol (protocol) {
      // if this is called, the peer has proven:
      // - it has the secret key to the peerAuthKey above
      // - the peerAuthKey passed the onauthenticate hook
      // so here you'd start replicating feeds:
      // feed.replicate(isInitiator, { stream: protocol })
    }
  })
}

About

hypercore-protocol extension to authenticate peers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%