Skip to content

Commit

Permalink
chore: Add readme & basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
lejeunerenard committed Feb 25, 2023
1 parent 50e6c13 commit d8f975b
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 3 deletions.
44 changes: 44 additions & 0 deletions examples/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { AutobaseManager } from '../index.js'
import Corestore from 'corestore'
import Hyperswarm from 'hyperswarm'
import Autobase from 'autobase'
import crypto from 'crypto'

const corestore = new Corestore('./basic-example')
await corestore.ready()

const mine = corestore.namespace('inputs').get({ name: 'input' })
const output = corestore.namespace('outputs').get({ name: 'output' })
await mine.ready()
await output.ready()
const base = new Autobase({
inputs: [mine],
localInput: mine,
localOutput: output
})

// Create manager & ensure its ready
const manager = new AutobaseManager(
// Autobase to manage
base,
// allow() to filter core keys
() => true,
// Get function for a hypercore given a key
corestore.get.bind(corestore),
// Storage for managing autobase keys
corestore.storage)
await manager.ready()

// Create connection to attach to
const swarm = new Hyperswarm()
swarm.on('connection', (conn, info) => {
console.log('found peer', info.publicKey.toString('hex').slice(-6))
const stream = corestore.replicate(conn)

// Attach manager
manager.attachStream(stream)
})
const topic = crypto.createHash('sha256')
.update('autobase-manager-test')
.digest()
swarm.join(topic)
Loading

0 comments on commit d8f975b

Please sign in to comment.