Skip to content

Instantly share code, notes, and snippets.

@Nuhvi
Last active March 25, 2022 09:23
Show Gist options
  • Save Nuhvi/60291b111f93928d8110ff0ae9ecea25 to your computer and use it in GitHub Desktop.
Save Nuhvi/60291b111f93928d8110ff0ae9ecea25 to your computer and use it in GitHub Desktop.
// npm i hypercore@10.0.0-alpha.24 hyperswarm@3.0.1
// Run `node create-hypercore.js` and pass the resulting key to https://gist.github.com/Nazeh/721368b93729dcfa00049c9de230f2ad
const Hypercore = require('hypercore');
const Hyperswarm = require('hyperswarm');
const createCore = async () => {
const core = new Hypercore('origin', { valueEncoding: 'json' });
await core.ready();
// Add blocks here if you want.
const blocks = [{ foo: 0 }, { foo: 0, bar: 1 }];
await core.append(blocks);
const swarm = new Hyperswarm();
swarm.on('connection', (conn, info) => {
console.log('connected to peer', info);
core.replicate(conn);
});
const discovery = swarm.join(core.discoveryKey, {
server: true,
client: false,
});
await discovery.flushed();
console.log('Created core: ', core.key.toString('hex'));
process.once('SIGINT', () => {
swarm.destroy();
});
};
createCore();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment