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 startOrbitDbServer = async ipfs => {
// Remap the peer connected to ours which will wait before exchanging heads
// with the same peer
const orbitGlobal = new OrbitDB(ipfs, config.ORBIT_DB_PATH, {
keystore: new InsertOnlyKeystore()
})
orbitGlobal._onPeerConnected = _onPeerConnected
orbitGlobal.keystore.registerSignVerify(
config.GLOBAL_KEYS,
undefined,
verifyRegistrySignature,
message => {
handleGlobalRegistryWrite(orbitGlobal, message.payload)
}
)
const globalRegistry = await orbitGlobal.kvstore(config.GLOBAL_KEYS, {
write: ['*']
ipfs.on("ready", async () => {
// remap the peer connected to ours which will wait before exchanging heads with the same peer
const orbit_global = new OrbitDB(ipfs, "odb/Main", {keystore:new InsertOnlyKeystore()})
orbit_global._onPeerConnected = _onPeerConnected
ipfs.setMaxListeners(MAX_IPFS_CONNECTIONS)
orbit_global.keystore.registerSignVerify(GLOBAL_KEYS, undefined, verifyRegistrySignature, message => {
handleGlobalRegistryWrite(orbit_global, message.payload)
})
const global_registry = await orbit_global.kvstore(GLOBAL_KEYS, { write: ['*'] })
rebroadcastOnReplicate(orbit_global, global_registry)
orbit_global.keystore.registerSignVerify(CONV_INIT_PREFIX, undefined, verifyConversationSignature(global_registry),
message => {
const eth_address = message.id.substr(-42) //hopefully the last 42 is the eth address
onConverse(orbit_global, eth_address, message.payload)
})
ipfs.on('ready', async () => {
// Create a database
const orbitdb = new OrbitDB(ipfs)
const db = await orbitdb.docstore('obligatron.test')
resolve(db)
})
})
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)