|
@ -1,12 +1,14 @@ |
|
|
import OrbitDB from 'orbit-db'; |
|
|
import OrbitDB from 'orbit-db'; |
|
|
import Keystore from 'orbit-db-keystore'; |
|
|
import Identities from 'orbit-db-identity-provider'; |
|
|
import path from 'path'; |
|
|
|
|
|
import IPFS from 'ipfs'; |
|
|
import IPFS from 'ipfs'; |
|
|
import store from '../redux/store'; |
|
|
import store from '../redux/store'; |
|
|
import { DATABASES_LOADED, IPFS_INITIALIZED, updateDatabases } from '../redux/actions/orbitActions'; |
|
|
import { DATABASES_LOADED, IPFS_INITIALIZED, updateDatabases } from '../redux/actions/orbitActions'; |
|
|
import ipfsOptions from '../config/ipfsOptions'; |
|
|
import ipfsOptions from '../config/ipfsOptions'; |
|
|
|
|
|
import EthereumIdentityProvider from './EthereumIdentityProvider'; |
|
|
|
|
|
import { getPrivateKey, setKeyPair } from './levelUtils'; |
|
|
|
|
|
|
|
|
function initIPFS() { |
|
|
function initIPFS() { |
|
|
|
|
|
Identities.addIdentityProvider(EthereumIdentityProvider); |
|
|
const ipfs = new IPFS(ipfsOptions); |
|
|
const ipfs = new IPFS(ipfsOptions); |
|
|
ipfs.on('error', (error) => console.error(`IPFS error: ${error}`)); |
|
|
ipfs.on('error', (error) => console.error(`IPFS error: ${error}`)); |
|
|
ipfs.on('ready', async () => { |
|
|
ipfs.on('ready', async () => { |
|
@ -24,7 +26,9 @@ function initIPFS() { |
|
|
async function createTempDatabases() { |
|
|
async function createTempDatabases() { |
|
|
console.debug('Creating temporary databases...'); |
|
|
console.debug('Creating temporary databases...'); |
|
|
const ipfs = getIPFS(); |
|
|
const ipfs = getIPFS(); |
|
|
const orbitdb = new OrbitDB(ipfs); |
|
|
const identity = await Identities.createIdentity({type: 'ethereum'}); |
|
|
|
|
|
const orbitdb = await OrbitDB.createInstance(ipfs, {identity}); |
|
|
|
|
|
console.dir(orbitdb) |
|
|
const topicsDB = await orbitdb.keyvalue('topics'); |
|
|
const topicsDB = await orbitdb.keyvalue('topics'); |
|
|
const postsDB = await orbitdb.keyvalue('posts'); |
|
|
const postsDB = await orbitdb.keyvalue('posts'); |
|
|
return { orbitdb, topicsDB, postsDB }; |
|
|
return { orbitdb, topicsDB, postsDB }; |
|
@ -33,19 +37,25 @@ async function createTempDatabases() { |
|
|
async function createDatabases() { |
|
|
async function createDatabases() { |
|
|
console.debug('Creating databases...'); |
|
|
console.debug('Creating databases...'); |
|
|
const ipfs = getIPFS(); |
|
|
const ipfs = getIPFS(); |
|
|
const orbitdb = new OrbitDB(ipfs); |
|
|
const identity = await Identities.createIdentity({type: 'ethereum'}); |
|
|
const topicsDB = await orbitdb.keyvalue('topics'); |
|
|
const orbitdb = await OrbitDB.createInstance(ipfs, {identity}); |
|
|
const postsDB = await orbitdb.keyvalue('posts'); |
|
|
const options = { |
|
|
|
|
|
// Give write access to ourselves
|
|
|
const orbitKey = orbitdb.keystore.getKey(orbitdb.id); |
|
|
accessController: { |
|
|
|
|
|
write: ['*'] |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
const topicsDB = await orbitdb.keyvalue('topics', options); |
|
|
|
|
|
const postsDB = await orbitdb.keyvalue('posts', options); |
|
|
|
|
|
const privateKey = await getPrivateKey(identity.id); |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
identityId: 'Tempus', |
|
|
identityId: identity.id, |
|
|
identityPublicKey: 'edax', |
|
|
identityPublicKey: identity.publicKey, |
|
|
identityPrivateKey: 'rerum', |
|
|
identityPrivateKey: privateKey, |
|
|
orbitdb: orbitdb, |
|
|
orbitdb: orbitdb, |
|
|
orbitPublicKey: orbitKey.getPublic('hex'), |
|
|
orbitPublicKey: "eeeee", |
|
|
orbitPrivateKey: orbitKey.getPrivate('hex'), |
|
|
orbitPrivateKey: "fffffff", |
|
|
topicsDB: topicsDB.address.root, |
|
|
topicsDB: topicsDB.address.root, |
|
|
postsDB: postsDB.address.root |
|
|
postsDB: postsDB.address.root |
|
|
}; |
|
|
}; |
|
@ -54,30 +64,28 @@ async function createDatabases() { |
|
|
async function loadDatabases(identityId, identityPublicKey, identityPrivateKey, |
|
|
async function loadDatabases(identityId, identityPublicKey, identityPrivateKey, |
|
|
orbitId, orbitPublicKey, orbitPrivateKey, |
|
|
orbitId, orbitPublicKey, orbitPrivateKey, |
|
|
topicsDBId, postsDBId) { |
|
|
topicsDBId, postsDBId) { |
|
|
const directory = './orbitdb'; |
|
|
console.debug('Loading databases...'); |
|
|
const keystore = Keystore.create(path.join(directory, orbitId, '/keystore')); |
|
|
const ipfs = getIPFS(); |
|
|
|
|
|
await setKeyPair(identityId, identityPublicKey, identityPrivateKey); |
|
|
|
|
|
const identity = await Identities.createIdentity({type: 'ethereum' }); |
|
|
|
|
|
const orbitdb = await OrbitDB.createInstance(ipfs, {identity}); |
|
|
|
|
|
|
|
|
keystore._storage.setItem(orbitId, JSON.stringify({ |
|
|
console.dir(orbitdb) |
|
|
publicKey: orbitPublicKey, |
|
|
|
|
|
privateKey: orbitPrivateKey |
|
|
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
const ipfs = getIPFS(); |
|
|
const topicsDB = await orbitdb.keyvalue('topics') |
|
|
const orbitdb = new OrbitDB(ipfs, directory, |
|
|
|
|
|
{ |
|
|
|
|
|
peerId: orbitId, keystore |
|
|
|
|
|
}); |
|
|
|
|
|
const topicsDB = await orbitdb.keyvalue(`/orbitdb/${topicsDBId}/topics`) |
|
|
|
|
|
.catch((error) => console.error(`TopicsDB init error: ${error}`)); |
|
|
.catch((error) => console.error(`TopicsDB init error: ${error}`)); |
|
|
const postsDB = await orbitdb.keyvalue(`/orbitdb/${postsDBId}/posts`) |
|
|
|
|
|
.catch((error) => console.error(`PostsDB init error: ${error}`)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.dir(topicsDB) |
|
|
|
|
|
|
|
|
|
|
|
const postsDB = await orbitdb.keyvalue('posts') |
|
|
|
|
|
.catch((error) => console.error(`PostsDB init error: ${error}`)); |
|
|
|
|
|
console.dir(topicsDB) |
|
|
await topicsDB.load().catch((error) => console.error(`TopicsDB loading error: ${error}`)); |
|
|
await topicsDB.load().catch((error) => console.error(`TopicsDB loading error: ${error}`)); |
|
|
await postsDB.load().catch((error) => console.error(`PostsDB loading error: ${error}`)); |
|
|
await postsDB.load().catch((error) => console.error(`PostsDB loading error: ${error}`)); |
|
|
|
|
|
|
|
|
//It is possible that we lack our own data and need to replicate them from somewhere else
|
|
|
//It is possible that we lack our own data and need to replicate them from somewhere else
|
|
|
topicsDB.events.on('replicate', (address) => { |
|
|
topicsDB.events.on('replicate', (address) => { |
|
|
console.log(`TopicsDB Replicating (${address}).`); |
|
|
console.log(`TopicsDB replicating (${address}).`); |
|
|
}); |
|
|
}); |
|
|
topicsDB.events.on('replicated', (address) => { |
|
|
topicsDB.events.on('replicated', (address) => { |
|
|
console.log(`TopicsDB replicated (${address}).`); |
|
|
console.log(`TopicsDB replicated (${address}).`); |
|
@ -89,7 +97,7 @@ async function loadDatabases(identityId, identityPublicKey, identityPrivateKey, |
|
|
console.log(`PostsDB replicated (${address}).`); |
|
|
console.log(`PostsDB replicated (${address}).`); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
console.debug('Orbit databases loaded successfully.'); |
|
|
console.debug('Databases loaded successfully.'); |
|
|
store.dispatch(updateDatabases(DATABASES_LOADED, orbitdb, topicsDB, postsDB)); |
|
|
store.dispatch(updateDatabases(DATABASES_LOADED, orbitdb, topicsDB, postsDB)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|