From ed1a6bf1a4c782de117652f6762aadd0c844dcb4 Mon Sep 17 00:00:00 2001 From: Joel Torstensson Date: Tue, 23 Apr 2019 17:46:14 +0200 Subject: [PATCH] feat: Accept identity as an option when opening a DB --- src/OrbitDB.js | 3 ++- test/create-open.test.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/OrbitDB.js b/src/OrbitDB.js index 800569b..49b2c89 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -168,8 +168,9 @@ let databaseTypes = { cache: cache, onClose: this._onClose.bind(this), }) + const identity = options.identity || this.identity - const store = new Store(this._ipfs, this.identity, address, opts) + const store = new Store(this._ipfs, identity, address, opts) store.events.on('write', this._onWrite.bind(this)) // ID of the store is the address as a string const addr = address.toString() diff --git a/test/create-open.test.js b/test/create-open.test.js index 02dfcbe..95c8709 100644 --- a/test/create-open.test.js +++ b/test/create-open.test.js @@ -9,6 +9,7 @@ const levelup = require('levelup') const leveldown = require('leveldown') const OrbitDB = require('../src/OrbitDB') const OrbitDBAddress = require('../src/orbit-db-address') +const Identities = require('orbit-db-identity-provider') const io = require('orbit-db-io') // Include test utilities const { @@ -258,6 +259,15 @@ Object.keys(testAPIs).forEach(API => { assert.equal(db.address.toString().indexOf('abc'), 59) }) + it('opens a database - with a different identity', async () => { + const identity = await Identities.createIdentity({ id: 'test-id' }) + db = await orbitdb.open('abc', { create: true, type: 'feed', overwrite: true, identity }) + assert.equal(db.address.toString().indexOf('/orbitdb'), 0) + assert.equal(db.address.toString().indexOf('zd'), 9) + assert.equal(db.address.toString().indexOf('abc'), 59) + assert.equal(db.identity, identity) + }) + it('opens the same database - from an address', async () => { db = await orbitdb.open(db.address) assert.equal(db.address.toString().indexOf('/orbitdb'), 0)