Use DAGnode.create to get hash without saving to IPFS
This commit is contained in:
parent
2378b654a5
commit
c32033161d
6313
package-lock.json
generated
6313
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -14,6 +14,7 @@
|
|||||||
"main": "src/OrbitDB.js",
|
"main": "src/OrbitDB.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ipfs-pubsub-1on1": "~0.0.4",
|
"ipfs-pubsub-1on1": "~0.0.4",
|
||||||
|
"ipld-dag-pb": "0.14.11",
|
||||||
"logplease": "^1.2.14",
|
"logplease": "^1.2.14",
|
||||||
"multihashes": "^0.4.12",
|
"multihashes": "^0.4.12",
|
||||||
"orbit-db-cache": "~0.2.4",
|
"orbit-db-cache": "~0.2.4",
|
||||||
|
@ -203,45 +203,7 @@ class OrbitDB {
|
|||||||
delete this.stores[address]
|
delete this.stores[address]
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create and Open databases */
|
async _determineAddress(name, type, options = {}, onlyHash) {
|
||||||
|
|
||||||
/*
|
|
||||||
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 = {}) {
|
|
||||||
if (!OrbitDB.isValidType(type))
|
if (!OrbitDB.isValidType(type))
|
||||||
throw new Error(`Invalid database type '${type}'`)
|
throw new Error(`Invalid database type '${type}'`)
|
||||||
|
|
||||||
@ -266,15 +228,57 @@ class OrbitDB {
|
|||||||
accessController.add('write', this.key.getPublic('hex'))
|
accessController.add('write', this.key.getPublic('hex'))
|
||||||
}
|
}
|
||||||
// Save the Access Controller in IPFS
|
// Save the Access Controller in IPFS
|
||||||
const accessControllerAddress = await accessController.save()
|
const accessControllerAddress = await accessController.save(onlyHash)
|
||||||
|
|
||||||
// Save the manifest to IPFS
|
// 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
|
// Create the database address
|
||||||
return OrbitDBAddress.parse(path.join('/orbitdb', manifestHash, name))
|
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 = {
|
options = {
|
||||||
localOnly: false // if set to true, throws an error if database can't be found locally
|
localOnly: false // if set to true, throws an error if database can't be found locally
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const { DAGNode } = require('ipld-dag-pb')
|
||||||
|
|
||||||
// Creates a DB manifest file and saves it in IPFS
|
// 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 = {
|
const manifest = {
|
||||||
name: name,
|
name: name,
|
||||||
type: type,
|
type: type,
|
||||||
accessController: path.join('/ipfs', accessControllerAddress),
|
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()
|
return dag.toJSON().multihash.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const AccessController = require('./access-controller')
|
const AccessController = require('./access-controller')
|
||||||
|
const { DAGNode } = require('ipld-dag-pb')
|
||||||
|
|
||||||
|
|
||||||
class IPFSAccessController extends AccessController {
|
class IPFSAccessController extends AccessController {
|
||||||
constructor (ipfs) {
|
constructor (ipfs) {
|
||||||
@ -23,11 +25,18 @@ class IPFSAccessController extends AccessController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async save () {
|
async save (onlyHash) {
|
||||||
let hash
|
let hash
|
||||||
try {
|
try {
|
||||||
const access = JSON.stringify(this._access, null, 2)
|
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()
|
hash = dag.toJSON().multihash.toString()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("ACCESS ERROR:", e)
|
console.log("ACCESS ERROR:", e)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user