How to use hypercore - 10 common examples

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 inkandswitch / capstone / src / node_modules / discovery-cloud / test2.ts View on Github external
process.env["DEBUG"] = "*"

import DiscoveryCloudClient from "./Client"
import { Stream } from "stream"
import * as Debug from "debug"
let hypercore = require("hypercore")
let crypto = require("hypercore/lib/crypto")
let ram = require("random-access-memory")

const log1 = Debug("discovery-cloud:test:client1")
const log2 = Debug("discovery-cloud:test:client2")

const url = "wss://discovery-cloud.herokuapp.com"
// const url = "ws://0.0.0.0:8080"
const keys = crypto.keyPair()

const feed1 = hypercore(ram, keys.publicKey, { secretKey: keys.secretKey })
const feed2 = hypercore(ram, keys.publicKey)

const client1 = new DiscoveryCloudClient({
  id: crypto.randomBytes(32),
  url,
  stream: info => feed1.replicate(info),
})

feed1.on("ready", () => {
  client1.join(feed1.discoveryKey)
  log1("joining")
  feed1.append("foo")
  feed1.append("bar")
  feed1.append("baz")
github inkandswitch / capstone / src / modules / discovery-cloud / test3.ts View on Github external
import { FrontendHandle } from "../../modules/hypermerge/frontend"
import { Doc, AnyEditDoc, EditDoc, AnyDoc } from "automerge/frontend"
import * as Debug from "debug"
const log = Debug("discovery-cloud:test")
let ram = require("random-access-memory")

const hm = new Hypermerge({ storage: ram })

interface Model {
  counter1?: number
  counter2?: number
}

type Handle = FrontendHandle | null

const buffers = keyPair()
const keys = {
  publicKey: Base58.encode(buffers.publicKey),
  secretKey: Base58.encode(buffers.secretKey),
}

const input = process.argv[2]

const docId = input || keys.publicKey

log("DOCID", docId)

hm.ready.then(hm => {
  swarm(hm, {
    id: hm.core.archiver.changes.id,
    url: "wss://discovery-cloud.herokuapp.com",
    // url: "ws://0.0.0.0:8080",
github inkandswitch / capstone / src / node_modules / discovery-cloud / test2.ts View on Github external
let hypercore = require("hypercore")
let crypto = require("hypercore/lib/crypto")
let ram = require("random-access-memory")

const log1 = Debug("discovery-cloud:test:client1")
const log2 = Debug("discovery-cloud:test:client2")

const url = "wss://discovery-cloud.herokuapp.com"
// const url = "ws://0.0.0.0:8080"
const keys = crypto.keyPair()

const feed1 = hypercore(ram, keys.publicKey, { secretKey: keys.secretKey })
const feed2 = hypercore(ram, keys.publicKey)

const client1 = new DiscoveryCloudClient({
  id: crypto.randomBytes(32),
  url,
  stream: info => feed1.replicate(info),
})

feed1.on("ready", () => {
  client1.join(feed1.discoveryKey)
  log1("joining")
  feed1.append("foo")
  feed1.append("bar")
  feed1.append("baz")
})

const client2 = new DiscoveryCloudClient({
  id: crypto.randomBytes(32),
  url,
  stream: info => feed2.replicate(info),
github automerge / hypermerge / MultiCore.js View on Github external
Archiver.prototype.createFeed = function (key, opts) {
  const self = this
  opts = opts || {}
  if (!key) {
    // create key pair
    const keyPair = crypto.keyPair()
    key = keyPair.publicKey
    opts.secretKey = keyPair.secretKey
  }
  const dk = hypercore.discoveryKey(toBuffer(key, 'hex')).toString('hex')

  if (this.feeds[dk]) {
    return this.feeds[dk]
  }

  opts.sparse = this.sparse
  const feed = hypercore(storage(key), key, opts)
  this.feeds[dk] = feed

  this.changes.append({type: 'add', key: key.toString('hex')})
  this.emit('add', feed)
github inkandswitch / pushpin / src / hypermerge / multicore.js View on Github external
createFeed(key, opts = {}) {
    this._ensureReady()
    log('createFeed', key && key.toString('hex'))

    // Create a key pair if we're making a feed from scratch.
    if (!key) {
      const keyPair = crypto.keyPair()
      key = keyPair.publicKey
      opts.secretKey = keyPair.secretKey
    }

    const dk = Hypercore.discoveryKey(toBuffer(key, 'hex')).toString('hex')

    if (this.archiver.feeds[dk]) {
      return this.archiver.feeds[dk]
    }

    opts.sparse = this.archiver.sparse
    const feed = Hypercore(this._feedStorage(key), key, opts)
    this.archiver.feeds[dk] = feed

    this.archiver.changes.append({ type: 'add', key: key.toString('hex') })
    this.archiver.emit('add', feed)
github fsteff / hypercore-encrypted / index.js View on Github external
if (!opts) opts = {}

  // note: internal encoding (of the unerlying hypercore) is always binary
  this.encoding = opts.valueEncoding || 'binary'
  // copy to avoid problems with internal changes
  opts = Object.assign({}, opts)

  const self = this
  if (Buffer.isBuffer(key)) {
    key = key.toString('hex')
  }

  let registerBook = findCryptoBook()

  hypercore.call(this, createStorage, key, opts)

  this._byteLengthOffset = 0
  this.on('append', () => {
    this._byteLengthOffset = 0
  })

  if (registerBook) {
    this.on('ready', () => {
      CryptoLib.getInstance().addBook(self.key.toString('hex'), self.cryptoKeyBook)
    })
  }

  function findCryptoBook () {
    let registerBook = false
    if (typeof opts.encryptionKeyBook === 'undefined' && (typeof key === 'string' && !opts.secretKey) && !opts.noEncryption) {
      self.cryptoKeyBook = CryptoLib.getInstance().getBook(key)
github mafintosh / hyperdb / test / basic.js View on Github external
tape('create with precreated keypair', function (t) {
  var crypto = require('hypercore/lib/crypto')
  var keyPair = crypto.keyPair()

  var db = create.one(keyPair.publicKey, {secretKey: keyPair.secretKey})
  db.put('hello', 'world', function (err, node) {
    t.same(node.value, 'world')
    t.error(err, 'no error')
    t.same(db.key, keyPair.publicKey, 'pubkey matches')
    db.source._storage.secretKey.read(0, keyPair.secretKey.length, function (err, secretKey) {
      t.error(err, 'no error')
      t.same(secretKey, keyPair.secretKey, 'secret key is stored')
    })
    db.get('hello', function (err, node) {
      t.error(err, 'no error')
      t.same(node.value, 'world', 'same value')
      t.end()
    })
  })
github inkandswitch / capstone / src / node_modules / hypermerge / __test__ / data1.ts View on Github external
t.joinSwarm(client)

//const keys = crypto.keyPair()
//const docId = Base58.encode(keys.publicKey)

interface Foo {
  foo: string
  bar: string
  counter1: number
  counter2: number
}

const front: FrontendManager = docId
  ? t.openDocumentFrontend(docId)
  : t.createDocumentFrontend(crypto.keyPair())

let i = 1
front.on("doc", doc => {
  log("DOC", doc)
})
if (!docId) {
  front.change(doc => {
    doc.foo = "bar"
    doc.bar = "baz"
    doc.counter1 = 0
  })
}
setInterval(() => {
  front.change(doc => {
    doc.counter1 += 1
  })
github inkandswitch / capstone / src / node_modules / hypermerge / __test__ / data2.ts View on Github external
t.joinSwarm(client)

//const keys = crypto.keyPair()
//const docId = Base58.encode(keys.publicKey)

interface Foo {
  foo: string
  bar: string
  counter1: number
  counter2: number
}

log("about to open", docId)
const front: FrontendManager = docId
  ? t.openDocumentFrontend(docId)
  : t.createDocumentFrontend(crypto.keyPair())

let i = 1
front.on("doc", doc => {
  log("DOC", doc)
})
/*
if (!docId) {
  front.change(doc => {
    doc.foo = "bar"
    doc.bar = "baz"
    doc.counter2 = 0
  })
}
*/
setInterval(() => {
  front.change(doc => {
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
}

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