Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ipfs.on('ready', async () => {
console.log(`ipfs ready.`)
let db
await ipfs.swarm.connect(MASTER_MULTIADDR)
try {
const orbitdb = await OrbitDB.createInstance(ipfs, {
directory: "./orbitdb/examples/eventlog"
});
// Make the DB public, so that anyone can write.
const options = {
accessController: {
write: ["*"]
}
}
db = await orbitdb.eventlog(DB_ADDRESS, options);
await db.load();
} catch (e) {
console.error(e)
process.exit(1)
async _init (opts) {
this._pubsub = new Pubsub(this.ipfs, (await this.ipfs.id()).id)
this._orbitdb = await OrbitDB.createInstance(this.ipfs, { directory: opts.orbitPath })
this._pubsub.subscribe(PINNING_ROOM, (topic, data) => {
// console.log('message', topic, data)
this.events.emit('pinning-room-message', topic, data)
}, (topic, peer) => {
// console.log('peer', topic, peer)
this.events.emit('pinning-room-peer', topic, peer)
})
}
export default async function startIpfs() {
// FIXME: Doesn't work on brave
console.log('Starting ipfs and orbitdb...');
window.ipfsNode = await IPFS.create(ipfsOptions);
const options = { id: 'local-id' };
const identity = await Identities.createIdentity(options);
const orbitdb = await OrbitDB.createInstance(window.ipfsNode, { identity });
// each user has their own metadata database
window.metadataDb = await orbitdb.docs('metadataDb', { indexBy: 'fileId' });
await window.metadataDb.load();
// TODO: Sharding of public database
// Create Public Metadata Registry
// const publicDb = {
// // Give write access to everyone
// accessController: {
// write: ['*'],
// },
// };
// const metadataRegistry = await orbitdb.eventlog('metadataRegistry', publicDb);
// Connect to Public Metadata Registry
const metadataRegistry = await orbitdb.open('/orbitdb/zdpuAwxsL6bPpUkmzRFuRzv513htstdndWHsfjgCHmaBuJw99/metadataRegistry');
logger.debug(`Going online: ${ipfsConfig.start}`)
if (ipfsConfig.start) logger.info(`IPFS going online!`)
const ipfs = await startIpfs(ipfsConfig)
const peerId = await ipfs.config.get('Identity.PeerID')
logger.debug("PeerID:", peerId)
// Try connecting immediately to peer ID given as 'from' argument
if (argv.from) {
console.log(">>", argv.from)
ipfs.swarm.connect(argv.from)
}
const directory = process.env.ORBITDB_PATH || config.defaultDatabaseDir
const orbitdb = await OrbitDB.createInstance(ipfs, {
directory: directory,
peerId: peerId })
logger.debug(`Loading database '${database}'`)
const db = await orbitdb.open(database, {
maxHistory: -1,
indexBy: argv.indexBy,
replicate: replicate || false,
type: openAsType,
create: openAsType ? true : false,
key: argv.key,
sync: replicate,
localOnly: argv.localOnly || false
})
const createDatabase = async (database, type, argv) => {
// TODO: add database path config: { repo: path.join('./.orbitdb') }
const ipfs = await startIpfs(config.ipfsConfig)
const peerId = await ipfs.config.get('Identity.PeerID')
// We need to pass the IPFS ID since we're not starting IPFS
const directory = process.env.ORBITDB_PATH || config.defaultDatabaseDir
const orbitdb = await OrbitDB.createInstance(ipfs, {
directory: directory,
peerId: peerId })
const db = await orbitdb.create(database, type, {
indexBy: argv.indexBy,
replicate: false,
key: argv.key,
})
await db.saveSnapshot()
return db
}
ipfs.on("ready", async () => {
let db;
try {
const orbitdb = await OrbitDB.createInstance(ipfs, {
directory: "./orbitdb/examples/eventlog"
});
const options = {
accessController: {
write: ["*"]
}
}
db = await orbitdb.eventlog(DB_NAME, options);
await db.load();
console.log(`db id: ${db.id}`);
} catch (e) {
console.error(e);
process.exit(1);
}
const profile = {
name: defaultIdentityProvider ? credentials : credentials.username,
location: 'Earth',
image: null
}
const identityKeysPath = path.join('./orbitdb', this._options.directory || '', 'keystore') || credentials.keystorePath
const newCredentials = Object.assign({}, credentials, { identityKeysPath })
const identity = await Identities.createIdentity(newCredentials)
this._user = new OrbitUser(identity, profile)
this._orbitdb = await OrbitDB.createInstance(
this._ipfs,
Object.assign(
{},
this._options.dbOptions,
{
directory: this._options.directory,
identity: this.user.identity
}
)
)
this._startPollingForPeers()
logger.info(`Connected to Orbit as "${this.user.profile.name}"`)
this.events.emit('connected', this.user)
exports.handler = async (argv) => {
const ipfsConfig = Object.assign({}, config.ipfsConfig)
const ipfs = await startIpfs(ipfsConfig)
const directory = process.env.ORBITDB_PATH || config.defaultDatabaseDir
const orbitdb = await OrbitDB.createInstance(ipfs, { directory: directory })
process.stdout.write(`${orbitdb.key.getPublic('hex')}\n`)
process.exit(0)
}
Swarm: [
'/dnsaddr/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star',
'/dnsaddr/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star',
]
}
}
}
if (orbitdb_dir) orbitdb_opts = Object.assign({'directory': orbitdb_dir}, orbitdb_opts)
ipfs_opts = Object.assign(ipfs_defaults, ipfs_opts)
ipfs = await new Promise((resolve, reject) => {
var node = new Ipfs(ipfs_opts)
node.on("ready", () => {
resolve(node)
})
}).catch((ex) => {throw ex})
orbitdb = await OrbitDB.createInstance(ipfs, orbitdb_opts)
dbm = new DBManager(orbitdb)
orbitdb_api = new OrbitApi(dbm, server_opts)
return orbitdb_api
}
async function api_factory(ipfs_host, ipfs_port, orbitdb_dir, orbitdb_opts, server_opts) {
let ipfs
let orbitdb
let dbm
let orbitdb_api
if (orbitdb_dir) orbitdb_opts = Object.assign({'directory': orbitdb_dir}, orbitdb_opts)
ipfs = new IpfsApi(ipfs_host, ipfs_port)
orbitdb = await OrbitDB.createInstance(ipfs, orbitdb_opts)
dbm = new DBManager(orbitdb)
orbitdb_api = new OrbitApi(dbm, server_opts)
return orbitdb_api
}