Use latest ipfs-log

This commit is contained in:
shamb0t 2018-09-10 18:42:49 +04:00
parent 5e3d0c5e81
commit 2f743772b7
2 changed files with 33 additions and 8 deletions

View File

@ -9,6 +9,7 @@ const DocumentStore = require('orbit-db-docstore')
const Pubsub = require('orbit-db-pubsub') const Pubsub = require('orbit-db-pubsub')
const Cache = require('orbit-db-cache') const Cache = require('orbit-db-cache')
const Keystore = require('orbit-db-keystore') const Keystore = require('orbit-db-keystore')
const IdentityProvider = require('orbit-db-identity-provider')
const AccessController = require('./ipfs-access-controller') const AccessController = require('./ipfs-access-controller')
const OrbitDBAddress = require('./orbit-db-address') const OrbitDBAddress = require('./orbit-db-address')
const createDBManifest = require('./db-manifest') const createDBManifest = require('./db-manifest')
@ -37,10 +38,14 @@ class OrbitDB {
this.stores = {} this.stores = {}
this.directory = directory || './orbitdb' this.directory = directory || './orbitdb'
this.keystore = options.keystore || Keystore.create(path.join(this.directory, this.id, '/keystore')) this.keystore = options.keystore || Keystore.create(path.join(this.directory, this.id, '/keystore'))
this.key = this.keystore.getKey(this.id) || this.keystore.createKey(this.id) this.identity = options.identity
this._directConnections = {} this._directConnections = {}
} }
async initialize(options = {}) {
this.identity = await IdentityProvider.createIdentity(this.keystore, this.id, options.identitySignerFn)
}
/* Databases */ /* Databases */
async feed (address, options = {}) { async feed (address, options = {}) {
options = Object.assign({ create: true, type: 'feed' }, options) options = Object.assign({ create: true, type: 'feed' }, options)
@ -112,8 +117,11 @@ class OrbitDB {
/* Private methods */ /* Private methods */
async _createStore (type, address, options) { async _createStore (type, address, options) {
if (!this.identity)
await this.initialize()
// Get the type -> class mapping // Get the type -> class mapping
const Store = databaseTypes[type] const Store = databaseTypes[type]
// this.identity = this.identity || await IdentityProvider.createIdentity(this.keystore, this.id, options.identitySignerFn)
if (!Store) if (!Store)
throw new Error(`Invalid database type '${type}'`) throw new Error(`Invalid database type '${type}'`)
@ -128,12 +136,11 @@ class OrbitDB {
const opts = Object.assign({ replicate: true }, options, { const opts = Object.assign({ replicate: true }, options, {
accessController: accessController, accessController: accessController,
keystore: this.keystore,
cache: cache, cache: cache,
onClose: this._onClose.bind(this), onClose: this._onClose.bind(this),
}) })
const store = new Store(this._ipfs, this.id, address, opts) const store = new Store(this._ipfs, this.identity, address, opts)
store.events.on('write', this._onWrite.bind(this)) store.events.on('write', this._onWrite.bind(this))
// ID of the store is the address as a string // ID of the store is the address as a string
@ -216,6 +223,9 @@ class OrbitDB {
async create (name, type, options = {}) { async create (name, type, options = {}) {
logger.debug(`create()`) logger.debug(`create()`)
if (!this.identity)
await this.initialize()
if (!OrbitDB.isValidType(type)) if (!OrbitDB.isValidType(type))
throw new Error(`Invalid database type '${type}'`) throw new Error(`Invalid database type '${type}'`)
@ -241,7 +251,7 @@ class OrbitDB {
options.write.forEach(e => accessController.add('write', e)) options.write.forEach(e => accessController.add('write', e))
} else { } else {
// Default is to add ourselves as the admin of the database // Default is to add ourselves as the admin of the database
accessController.add('write', this.key.getPublic('hex')) accessController.add('write', this.identity.publicKey)
} }
// Save the Access Controller in IPFS // Save the Access Controller in IPFS
const accessControllerAddress = await accessController.save() const accessControllerAddress = await accessController.save()
@ -280,6 +290,10 @@ class OrbitDB {
*/ */
async open (address, options = {}) { async open (address, options = {}) {
logger.debug(`open()`) logger.debug(`open()`)
if (!this.identity)
await this.initialize()
options = Object.assign({ localOnly: false, create: false }, options) options = Object.assign({ localOnly: false, create: false }, options)
logger.debug(`Open database '${address}'`) logger.debug(`Open database '${address}'`)

View File

@ -2,9 +2,9 @@
class AccessController { class AccessController {
constructor () { constructor () {
this._access = { this._access = {
admin: [], admin: [],
write: [], write: [],
read: [], // Not used atm read: [], // Not used atm
} }
} }
@ -13,6 +13,17 @@ class AccessController {
async load (address) {} async load (address) {}
async save () {} async save () {}
async canAppend(entry, identityProvider){
//verify identity?
if (this._access.write.includes('*'))
return true
if (this._access.write.includes(entry.identity.publicKey))
return true
return false
}
/* Properties */ /* Properties */
get admin () { get admin () {
return this._access.admin return this._access.admin
@ -78,7 +89,7 @@ class AccessController {
break break
default: default:
break break
} }
} }
} }