How to use the hypercore.discoveryKey function in hypercore

To help you get started, we’ve selected a few hypercore examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mafintosh / multifeed / index.js View on Github external
MultiFeed.prototype._addFeed = function (key) {
  if (key.equals(this.local.key)) {
    if (this.feeds.indexOf(this.local) > -1) return false
    this.feeds.push(this.local)
    this.emit('add-feed', this.local)
    return true
  }

  for (var i = 0; i < this.feeds.length; i++) {
    if (this.feeds[i].key.equals(key)) return false
  }

  var dk = hypercore.discoveryKey(key).toString('hex')
  var feed = this._createFeed('feeds/' + dk.slice(0, 2) + '/' + dk.slice(2, 4) + '/' + dk.slice(4), key)

  this.feeds.push(feed)
  this.emit('add-feed', feed)

  return true
}
github mafintosh / hypercore-archiver / index.js View on Github external
Archiver.prototype.replicate = function (opts) {
  if (!opts) opts = {}

  if (opts.discoveryKey) opts.discoveryKey = toBuffer(opts.discoveryKey, 'hex')
  if (opts.key) opts.discoveryKey = hypercore.discoveryKey(toBuffer(opts.key, 'hex'))

  var stream = protocol({live: true, id: this.changes.id, encrypt: opts.encrypt})
  var self = this

  stream.on('feed', add)
  if (opts.channel || opts.discoveryKey) add(opts.channel || opts.discoveryKey)

  function add (dk) {
    self.ready(function (err) {
      if (err) return stream.destroy(err)
      if (stream.destroyed) return

      var hex = dk.toString('hex')
      var changesHex = self.changes.discoveryKey.toString('hex')

      var archive = self.archives[hex]
github automerge / hypermerge / multicore.js View on Github external
Archiver.prototype.replicate = function (opts) {
  if (!opts) opts = {}

  if (opts.discoveryKey) opts.discoveryKey = toBuffer(opts.discoveryKey, 'hex')
  if (opts.key) opts.discoveryKey = hypercore.discoveryKey(toBuffer(opts.key, 'hex'))

  const protocolOpts = {
    live: true,
    id: this.changes.id,
    encrypt: opts.encrypt
  }
  if (opts.userData) {
    protocolOpts.userData = opts.userData
  }
  if (opts.timeout) {
    protocolOpts.timeout = opts.timeout
  }
  var stream = protocol(protocolOpts)
  var self = this

  stream.on('feed', add)
github automerge / hypermerge / multicore.js View on Github external
function storage (key) {
    var dk = hypercore.discoveryKey(key).toString('hex')
    var prefix = dk.slice(0, 2) + '/' + dk.slice(2, 4) + '/' + dk.slice(4) + '/'

    return function (name) {
      return self.storage.feeds(prefix + name)
    }
  }
}
github automerge / hypermerge / MultiCore.js View on Github external
function storage (key) {
    var dk = hypercore.discoveryKey(key).toString('hex')
    var prefix = dk.slice(0, 2) + '/' + dk.slice(2, 4) + '/' + dk.slice(4) + '/'

    return function (name) {
      return self.storage.feeds(prefix + name)
    }
  }
}
github inkandswitch / pushpin / src / hypermerge / multicore.js View on Github external
replicate(opts) {
    this._ensureReady()
    log('replicate')

    if (!opts) {
      opts = {}
    }

    if (opts.discoveryKey) {
      opts.discoveryKey = toBuffer(opts.discoveryKey, 'hex')
    }

    if (opts.key) {
      opts.discoveryKey = Hypercore.discoveryKey(toBuffer(opts.key, 'hex'))
    }

    const { archiver } = this

    const stream = HypercoreProtocol({
      live: true,
      id: archiver.changes.id,
      encrypt: opts.encrypt,
      extensions: ['hypermerge']
    })

    stream.on('feed', add)
    if (opts.channel || opts.discoveryKey) {
      add(opts.channel || opts.discoveryKey)
    }
github inkandswitch / capstone / src / modules / hypermerge / multicore.js View on Github external
_feedStorage(key) {
    const dk = Hypercore.discoveryKey(key).toString("hex")
    const prefix = `${dk.slice(0, 2)}/${dk.slice(2, 4)}/${dk.slice(4)}/`
    return name => this.archiver.storage.feeds(prefix + name)
  }
github inkandswitch / pushpin / src / hypermerge / multicore.js View on Github external
_feedStorage(key) {
    const dk = Hypercore.discoveryKey(key).toString('hex')
    const prefix = `${dk.slice(0, 2)}/${dk.slice(2, 4)}/${dk.slice(4)}/`
    return (name) => this.archiver.storage.feeds(prefix + name)
  }
github inkandswitch / capstone / src / node_modules / hypermerge / hypercore.ts View on Github external
export function discoveryKey(buf: Buffer): Buffer {
  return _hypercore.discoveryKey(buf)
}
github mafintosh / hyperdb / index.js View on Github external
function HyperDB (storage, key, opts) {
  if (!(this instanceof HyperDB)) return new HyperDB(storage, key, opts)
  events.EventEmitter.call(this)

  if (isOptions(key)) {
    opts = key
    key = null
  }

  opts = Object.assign({}, opts)
  if (opts.firstNode) opts.reduce = reduceFirst

  var checkout = opts.checkout

  this.key = typeof key === 'string' ? Buffer.from(key, 'hex') : key
  this.discoveryKey = this.key ? hypercore.discoveryKey(this.key) : null
  this.source = checkout ? checkout.source : null
  this.local = checkout ? checkout.local : null
  this.localContent = checkout ? checkout.localContent : null
  this.feeds = checkout ? checkout.feeds : []
  this.contentFeeds = checkout ? checkout.contentFeeds : (opts.contentFeed ? [] : null)
  this.ready = thunky(this._ready.bind(this))
  this.opened = false
  this.sparse = !!opts.sparse
  this.sparseContent = opts.sparseContent !== undefined ? !!opts.sparseContent : this.sparse
  this.id = Buffer.alloc(32)
  sodium.randombytes_buf(this.id)

  this._storage = createStorage(storage)
  this._contentStorage = typeof opts.contentFeed === 'function'
    ? opts.contentFeed
    : opts.contentFeed ? this._storage : null

hypercore

Hypercore is a secure, distributed append-only log

MIT
Latest version published 1 month ago

Package Health Score

84 / 100
Full package analysis

Similar packages