Skip to content

Commit

Permalink
Merge pull request #8 from BCH-Consolidating-CoinJoin/unstable
Browse files Browse the repository at this point in the history
fix(connectToOrbitDB): Added connectToOrbitDB()
  • Loading branch information
christroutner committed Nov 28, 2018
2 parents 604d2ea + 3abd927 commit 063673d
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,9 @@ class CCNet {
console.log("IPFS is ready.");
this.ipfsIsReady = true;

const access = {
// Give write access to everyone
write: ["*"]
};

// Create OrbitDB instance
const orbitdb = new OrbitDB(ipfs);

// Instantiate the ccoinjoin database.
const db = await orbitdb.eventlog("ccoinjoin", access);

// Load any saved state from disk.
await db.load();

// Save local instances of ipfs and the db.
this.db = db;
this.ipfs = ipfs;

// React to DB update events.
db.events.on("replicated", () => {
console.log(`replication event fired`);
this.dbHasSynced = true;
});

return resolve(true)
});
} catch (err) {
Expand All @@ -97,6 +76,42 @@ class CCNet {
});
}

// Connects to an OrbitDB. Assumes IPFS has already established a connection.
async connectToOrbitDB(orbitAddr) {
// Create OrbitDB instance
const orbitdb = new OrbitDB(this.ipfs);
let db

if(!orbitAddr) {
const access = {
// Give write access to everyone
write: ["*"]
};

db = await orbitdb.eventlog("ccoinjoin", access);
console.log(`New ccoinjoin DB created. DB ID: ${db.id}`)
}

else {
console.log(`Connecting to OrbitDB ${orbitAddr}`)
db = await orbitdb.eventlog(orbitAddr);
}

// Load any saved state from disk.
await db.load();

// Save local instances of ipfs and the db.
this.db = db;

// React to DB update events.
db.events.on("replicated", () => {
console.log(`replication event fired`);
this.dbHasSynced = true;
});

return db
}

// Returns an array of the last 100 entries from the log DB.
// latest[0] should be the latest entry, with latest[99] being the oldest.
// Ordering is subject to network latency and CRDT algorithm.
Expand Down

0 comments on commit 063673d

Please sign in to comment.