From 2f743772b748a5c148a45a60edcdeec77a53b384 Mon Sep 17 00:00:00 2001 From: shamb0t Date: Mon, 10 Sep 2018 18:42:49 +0400 Subject: [PATCH] Use latest ipfs-log --- src/OrbitDB.js | 22 ++++++++++++++++++---- src/access-controller.js | 19 +++++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/OrbitDB.js b/src/OrbitDB.js index 97efb71..32a0e15 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -9,6 +9,7 @@ const DocumentStore = require('orbit-db-docstore') const Pubsub = require('orbit-db-pubsub') const Cache = require('orbit-db-cache') const Keystore = require('orbit-db-keystore') +const IdentityProvider = require('orbit-db-identity-provider') const AccessController = require('./ipfs-access-controller') const OrbitDBAddress = require('./orbit-db-address') const createDBManifest = require('./db-manifest') @@ -37,10 +38,14 @@ class OrbitDB { this.stores = {} this.directory = directory || './orbitdb' 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 = {} } + async initialize(options = {}) { + this.identity = await IdentityProvider.createIdentity(this.keystore, this.id, options.identitySignerFn) + } + /* Databases */ async feed (address, options = {}) { options = Object.assign({ create: true, type: 'feed' }, options) @@ -112,8 +117,11 @@ class OrbitDB { /* Private methods */ async _createStore (type, address, options) { + if (!this.identity) + await this.initialize() // Get the type -> class mapping const Store = databaseTypes[type] + // this.identity = this.identity || await IdentityProvider.createIdentity(this.keystore, this.id, options.identitySignerFn) if (!Store) throw new Error(`Invalid database type '${type}'`) @@ -128,12 +136,11 @@ class OrbitDB { const opts = Object.assign({ replicate: true }, options, { accessController: accessController, - keystore: this.keystore, cache: cache, 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)) // ID of the store is the address as a string @@ -216,6 +223,9 @@ class OrbitDB { async create (name, type, options = {}) { logger.debug(`create()`) + if (!this.identity) + await this.initialize() + if (!OrbitDB.isValidType(type)) throw new Error(`Invalid database type '${type}'`) @@ -241,7 +251,7 @@ class OrbitDB { options.write.forEach(e => accessController.add('write', e)) } else { // 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 const accessControllerAddress = await accessController.save() @@ -280,6 +290,10 @@ class OrbitDB { */ async open (address, options = {}) { logger.debug(`open()`) + + if (!this.identity) + await this.initialize() + options = Object.assign({ localOnly: false, create: false }, options) logger.debug(`Open database '${address}'`) diff --git a/src/access-controller.js b/src/access-controller.js index 882e66b..36e5e04 100644 --- a/src/access-controller.js +++ b/src/access-controller.js @@ -2,9 +2,9 @@ class AccessController { constructor () { - this._access = { - admin: [], - write: [], + this._access = { + admin: [], + write: [], read: [], // Not used atm } } @@ -13,6 +13,17 @@ class AccessController { async load (address) {} 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 */ get admin () { return this._access.admin @@ -78,7 +89,7 @@ class AccessController { break default: break - } + } } }