From 941c078da597cde2f9994dda864f0de7c500b1b4 Mon Sep 17 00:00:00 2001 From: Tyler Yasaka Date: Tue, 13 Nov 2018 08:17:22 -0800 Subject: [PATCH] Add a determineAddress method Because the generated address is deterministic, it can be predicted if you know the name, type, and write-access keys. This method makes it straightforward to do so. --- src/OrbitDB.js | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/OrbitDB.js b/src/OrbitDB.js index 4072915..e20bb5d 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -226,6 +226,28 @@ class OrbitDB { if (OrbitDBAddress.isValid(name)) throw new Error(`Given database name is an address. Please give only the name of the database!`) + // Create the database address + const dbAddress = await this.determineAddress(name, type, options) + + // Load the locally saved database information + const cache = await this._loadCache(directory, dbAddress) + + // Check if we have the database locally + const haveDB = await this._haveLocalData(cache, dbAddress) + + if (haveDB && !options.overwrite) + throw new Error(`Database '${dbAddress}' already exists!`) + + // Save the database locally + await this._saveDBManifest(directory, dbAddress) + + logger.debug(`Created database '${dbAddress}'`) + + // Open the database + return this.open(dbAddress, options) + } + + async determineAddress(name, type, options = {}) { // Create an AccessController const accessController = new AccessController(this._ipfs) /* Disabled temporarily until we do something with the admin keys */ @@ -250,24 +272,7 @@ class OrbitDB { const manifestHash = await createDBManifest(this._ipfs, name, type, accessControllerAddress) // Create the database address - const dbAddress = OrbitDBAddress.parse(path.join('/orbitdb', manifestHash, name)) - - // Load the locally saved database information - const cache = await this._loadCache(directory, dbAddress) - - // Check if we have the database locally - const haveDB = await this._haveLocalData(cache, dbAddress) - - if (haveDB && !options.overwrite) - throw new Error(`Database '${dbAddress}' already exists!`) - - // Save the database locally - await this._saveDBManifest(directory, dbAddress) - - logger.debug(`Created database '${dbAddress}'`) - - // Open the database - return this.open(dbAddress, options) + return OrbitDBAddress.parse(path.join('/orbitdb', manifestHash, name)) } /*