Skip to content

Instantly share code, notes, and snippets.

@doctorzhivago
Last active May 13, 2021 00:54
Show Gist options
  • Save doctorzhivago/6cb5288ffede42d4aa5dc3d2b0d98cb5 to your computer and use it in GitHub Desktop.
Save doctorzhivago/6cb5288ffede42d4aa5dc3d2b0d98cb5 to your computer and use it in GitHub Desktop.
Establish an IPNS name that points to a file containing an OrbitDB address
const createClient = require('ipfs-http-client');
const OrbitDB = require('orbit-db');
require('buffer');
const toBuffer = require('it-to-buffer');
const LOG='orbit*';
// NOTE: This runs via node.js 14.x +
// This was only tested with a go-ipfs node running on the same
// device on port 5001
// The go-ips node's config looks like this:
// ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"*\"]"
// ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\"true\"]"
// The go-ipfs node is run like so:
// ipfs daemon --enable-pubsub-experiment
class DBManager {
constructor (dbName) {
if (!dbName) {
throw new Error("dbName is required");
}
this.dbName = dbName;
this.node = createClient('/ip4/127.0.0.1/tcp/5001');
this.createDb();
}
async createDb () {
this.orbitdb = await OrbitDB.createInstance(this.node);
this.defaultOptions = {
// NOTE: everyone has write access!
// TODO: add configuration to pass an array of nodeIds to allow write access
accessController: { write: ['*'] }
};
this.docStoreOptions = {
...this.defaultOptions,
indexBy: 'hash',
};
// NOTE: this is for a docstore db
// TODO: add configuration options for other db types
this.DB = await this.orbitdb.docstore(this.dbName, this.docStoreOptions);
// Load the DB
await this.DB.load();
await this.saveAddressToIPFS();
await this.publish();
}
get dbAddress () {
console.log(this.DB.address.toString());
return this.DB.address;
}
async saveAddressToIPFS () {
console.log('Store the db address in IPFS, pinned...');
this.hash = await this.node.add(this.DB.address.toString(), { pin: true });
console.log('Here is the ipfs CID:');
console.log(this.hash.path);
console.log('Please wait...');
}
async publish () {
this.ipnsName = await this.node.name.publish(this.hash.path);
console.log(`ipns address:`);
console.log(this.ipnsName);
console.log(`The ipns address above will contain a single string that is the OrbitDB address that your orbit clients need to replicate.:`);
this.exampleOpenDB();
}
exampleOpenDB () {
(async () => {
const bufferedContents = await toBuffer(
this.node.cat(`/ipns/${this.ipnsName.name}`)
);
const decoder = new TextDecoder();
const stringContents = decoder.decode(bufferedContents);
console.log(stringContents);
// TODO: Remove pinned db address newline (if any)!
const dbAddress = stringContents.trim();
this.newDB = await this.orbitdb.docstore(
dbAddress, this.docStoreOptions
);
await this.newDB.load();
console.log('The new DB is now running from the address with pinned & named:');
console.log(this.newDB.address);
})();
}
}
new DBManager(`foo-${Math.random()}`);
module.exports = DBManager;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment