Skip to content

Instantly share code, notes, and snippets.

@fredguth
Forked from jackkleeman/init.js
Created April 1, 2018 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredguth/e330d1c85237267ece2ffefe21cee127 to your computer and use it in GitHub Desktop.
Save fredguth/e330d1c85237267ece2ffefe21cee127 to your computer and use it in GitHub Desktop.
function init(m, email, password) {
// generate the database encryption key
vendor.nCrypto
.pbkdf2(
Buffer.from(m.mnemonic, "utf8"),
Buffer.from(databaseKeySeed, "utf8"),
2048,
64,
"sha512"
)
.then(function(buffer) {
dbKey = buffer.slice(0, 32);
// setup unencrypted local DB changefeed
PouchDB.plugin(vendor.cryptoPouch);
db = new PouchDB(localDBPrefix + m.dbName);
db.crypto({
key: dbKey
});
db
.changes({
since: "now",
live: true,
include_docs: true
})
.on("change", function(change) {
if (change.doc && change.doc.message) {
changefeed(change.doc);
}
})
.on("error", console.log);
});
// setup remote DB
var remoteDB = new PouchDB(remoteDBAddress);
// setup local DB
var localDB = new PouchDB(localDBPrefix + m.dbName);
localDB.replicate
.from(remoteDB)
.on("complete", function() {
localDB
.sync(remoteDB, {
live: true,
retry: true
})
})
}
@fredguth
Copy link
Author

fredguth commented Apr 1, 2018

I am not sure if I understood. You created 2 "localDBPrefix+m.dbName" pouchDBs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment