Use DAGnode.create to get hash without saving to IPFS

This commit is contained in:
Tyler Yasaka 2018-11-13 15:52:50 -08:00
parent 2378b654a5
commit c32033161d
No known key found for this signature in database
GPG Key ID: D3EC8FC9F3B711F4
5 changed files with 3404 additions and 3024 deletions

6313
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,7 @@
"main": "src/OrbitDB.js",
"dependencies": {
"ipfs-pubsub-1on1": "~0.0.4",
"ipld-dag-pb": "0.14.11",
"logplease": "^1.2.14",
"multihashes": "^0.4.12",
"orbit-db-cache": "~0.2.4",

View File

@ -203,45 +203,7 @@ class OrbitDB {
delete this.stores[address]
}
/* Create and Open databases */
/*
options = {
admin: [], // array of keys that are the admins of this database (same as write access)
write: [], // array of keys that can write to this database
directory: './orbitdb', // directory in which to place the database files
overwrite: false, // whether we should overwrite the existing database if it exists
}
*/
async create (name, type, options = {}) {
logger.debug(`create()`)
// The directory to look databases from can be passed in as an option
const directory = options.directory || this.directory
logger.debug(`Creating database '${name}' as ${type} in '${directory}'`)
// 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 = {}) {
async _determineAddress(name, type, options = {}, onlyHash) {
if (!OrbitDB.isValidType(type))
throw new Error(`Invalid database type '${type}'`)
@ -266,15 +228,57 @@ class OrbitDB {
accessController.add('write', this.key.getPublic('hex'))
}
// Save the Access Controller in IPFS
const accessControllerAddress = await accessController.save()
const accessControllerAddress = await accessController.save(onlyHash)
// Save the manifest to IPFS
const manifestHash = await createDBManifest(this._ipfs, name, type, accessControllerAddress)
const manifestHash = await createDBManifest(this._ipfs, name, type, accessControllerAddress, onlyHash)
// Create the database address
return OrbitDBAddress.parse(path.join('/orbitdb', manifestHash, name))
}
/* Create and Open databases */
/*
options = {
admin: [], // array of keys that are the admins of this database (same as write access)
write: [], // array of keys that can write to this database
directory: './orbitdb', // directory in which to place the database files
overwrite: false, // whether we should overwrite the existing database if it exists
}
*/
async create (name, type, options = {}) {
logger.debug(`create()`)
// The directory to look databases from can be passed in as an option
const directory = options.directory || this.directory
logger.debug(`Creating database '${name}' as ${type} in '${directory}'`)
// 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 = {}) {
return this._determineAddress(name, type, options, true)
}
/*
options = {
localOnly: false // if set to true, throws an error if database can't be found locally
@ -372,7 +376,7 @@ class OrbitDB {
if (!cache) {
return false
}
const data = await cache.get(path.join(dbAddress.toString(), '_manifest'))
const data = await cache.get(path.join(dbAddress.toString(), '_manifest'))
return data !== undefined && data !== null
}

View File

@ -1,13 +1,22 @@
const path = require('path')
const { DAGNode } = require('ipld-dag-pb')
// Creates a DB manifest file and saves it in IPFS
const createDBManifest = async (ipfs, name, type, accessControllerAddress) => {
const createDBManifest = async (ipfs, name, type, accessControllerAddress, onlyHash) => {
const manifest = {
name: name,
type: type,
accessController: path.join('/ipfs', accessControllerAddress),
}
const dag = await ipfs.object.put(Buffer.from(JSON.stringify(manifest)))
let dag
const manifestJSON = JSON.stringify(manifest)
if (onlyHash) {
dag = await new Promise(resolve => {
DAGNode.create(Buffer.from(manifestJSON), (err, n) => { resolve(n) })
})
} else {
dag = await ipfs.object.put(Buffer.from(manifestJSON))
}
return dag.toJSON().multihash.toString()
}

View File

@ -1,6 +1,8 @@
'use strict'
const AccessController = require('./access-controller')
const { DAGNode } = require('ipld-dag-pb')
class IPFSAccessController extends AccessController {
constructor (ipfs) {
@ -23,11 +25,18 @@ class IPFSAccessController extends AccessController {
}
}
async save () {
async save (onlyHash) {
let hash
try {
const access = JSON.stringify(this._access, null, 2)
const dag = await this._ipfs.object.put(new Buffer(access))
let dag
if (onlyHash) {
dag = await new Promise(resolve => {
DAGNode.create(Buffer.from(access), (err, n) => { resolve(n) })
})
} else {
dag = await this._ipfs.object.put(new Buffer(access))
}
hash = dag.toJSON().multihash.toString()
} catch (e) {
console.log("ACCESS ERROR:", e)