hyper-sdk
A Software Development Kit for the hypercore-protocol
Why use this?
Hypercore-protocol and it's ecosystem consists of a bunch of low level building blocks for working with data in distributed applications. Although this modularity makes it easy to mix and match pieces, it adds complexity when it comes to actually building something.
The Hyper SDK combines the lower level pieces of the Hyper stack into high level APIs that you can use across platforms so that you can focus on your application rather than the gritty details of how it works.
Goals
- High level API
- Cross-platform with same codebase
- ✔ Node
- ✔ Electron
- 🏗️ Web (PRs welcome)
Installation
Make sure you've set up Node.js.
npm install --save hyper-sdk
# or yarn
import * as SDK from "hyper-sdk"
API
SDK.create()
const sdk = await SDK.create({
storage: 'hyper-sdk',
autoJoin: true,
corestoreOpts: {},
swarmOpts: {},
})
sdk.publicKey
The public key used for identifying this peer in the hyperswarm network.
This is a 32 byte buffer which can be use in conjunction with sdk.joinPeer()
to connect two peers directly together.
sdk.connections
The list of active connections to other peers, taken from hyperswarm.
sdk.peers
The list of active peers.
Each peer has a publicKey
, and list of topics
You can find more docs in the hyperswarm repo.
sdk.cores
List of active Hypercores.
sdk.on('peer-add', peerInfo) / sdk.on('peer-remove', peerInfo)
You can listen on when a peer gets connected or disconnected with this event.
You can find more docs in the hyperswarm repo.
sdk.on('peer-add', (peerInfo) => {
console.log('Connected to', peerInfo.publicKey, 'on', peerInfo.topics)
})
sdk.on('peer-add', (peerInfo) => {
console.log('Disconnected from')
})
sdk.get()
You can initialize a Hypercore instanceby passing in a key, a name to derive a key from, or a URL containing either a key or a DNS name.
You can also pass additional options for whether the hypercore should be replicated as sparse or not.
Unlike corestore, you may not initialize a hypercore from a null
key since everything must be derivable or loadable.
Unless autoJoin
is set to false
, the peer discovery will be automatically started for the core.
const core = await sdk.get('example name')
const core = await sdk.get('hyper://example.mauve.moe')
const core = await sdk.get(b4a.alloc(32, 0))
const core = await sdk.get('hyper://0000000000000000000000000000000000000000000000000000000000000000')
const core = await sdk.get('hyper://yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy')
const core = await sdk.get('example', {sparse: false})
const core = await sdk.get('example', {autoJoin: false})
sdk.resolveDNSToKey()
You can manually resolve DNS addresses to hypercore keys on domains using the DNS Link spec with this method.
However, it's not mandatory to use DNS since sdk.get()
will automatically detect and perform resolutions of DNS for hyper://
URLs.
const key = await sdk.resolveDNSToKey('example.mauve.moe')
sdk.namespace()
Get back a namespaced Corestore instance which can be passed to things like Hyperdrive.
Note that cores initialized with a namespaced corestore will not be auto-joined and you will need to call sdk.join(core.discoveryKey)
on said cores.
import Hypderdrive from "hyperdrive"
const drive = new Hyperdrive(sdk.namespace('example'))
await drive.ready()
sdk.join(drive.publicKey)
sdk.join() / sdk.leave()
You can manually trigger peer discovery of hypercores as well as stop peer discovery.
This can be done by using the discoveryKey
of a hypercore, or any 32 byte buffer.
As well, you can use string names for topics in order to discover peers based on a human readable string.
When using string topics, they are converted to 32 byte buffers using the Hypercore Crypto namespace algorithm.
const core = await sdk.get('example', {autoJoin: false})
sdk.join(core.discoveryKey, {server: false})
sdk.join("cool cat videos")
sdk.leave(core.discoveryKey)
sdk.leave("cool cat videos")
sdk.joinPeer() / sdk.leavePeer()
const sdk1 = await SDK.create({persist: false})
const sdk2 = await SDK.create({persist: false})
sdk1.joinPeer(sdk2.publicKey)
sdk.close()
This will gracefully close connections, remove advertisements from the DHT, and close any open file handles.
Make sure you invoke this to keep the network fast and to avoid data corruption!