From 5bbc567dddadfcd5b7a275c0496f9da59c7516e9 Mon Sep 17 00:00:00 2001 From: Zach Ferland Date: Tue, 7 May 2019 13:34:19 -0400 Subject: [PATCH 01/10] feat: upgrade orbitdb and ipfs --- package.json | 12 +++++++++--- src/3box.js | 16 ++++++++------- src/3id/index.js | 2 +- src/3id/keyring.js | 13 ++++++++++++- src/3id/orbitProvider.js | 42 ++++++++++++++++++++++++++++++++++++++++ src/keyValueStore.js | 14 +++++++++----- src/thread.js | 10 ++++++---- 7 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 src/3id/orbitProvider.js diff --git a/package.json b/package.json index 8341b22f..f700c42e 100644 --- a/package.json +++ b/package.json @@ -43,19 +43,25 @@ }, "homepage": "https://github.com/3box/3box-js#readme", "dependencies": { - "@babel/runtime": "^7.1.2", + "@babel/runtime": "^7.4.4", "did-jwt": "^0.1.1", + "elliptic": "^6.4.1", "ethers": "^4.0.20", "graphql-request": "^1.8.2", "https-did-resolver": "^0.1.0", - "ipfs": "^0.33.1", + "idb-readable-stream": "0.0.4", + "ipfs": "^0.34.4", "ipfs-mini": "^1.1.5", "ipfs-postmsg-proxy": "^3.1.1", "js-sha256": "^0.9.0", "muport-did-resolver": "^0.3.0-alpha.2", "node-fetch": "^2.3.0", - "orbit-db": "git://github.com/orbitdb/orbit-db.git#dddb271", + "orbit-db": "git://github.com/3box/orbit-db.git#feat/legacy-create", + "orbit-db-access-controllers": "git://github.com/3box/orbit-db-access-controllers.git#feat/legacy-ac-support", "orbit-db-cache-postmsg-proxy": "^0.1.1", + "orbit-db-identity-provider": "^0.1.0", + "orbit-db-io": "git://github.com/3box/orbit-db-io.git#feat/backwards-compatibility", + "orbit-db-pubsub": "^0.5.5", "store": "^2.0.12", "tweetnacl": "^1.0.1", "tweetnacl-util": "^0.15.0" diff --git a/src/3box.js b/src/3box.js index c33e4e68..4c2440bd 100644 --- a/src/3box.js +++ b/src/3box.js @@ -78,18 +78,20 @@ class Box { this.pinningNode = opts.pinningNode || PINNING_NODE this._ipfs.swarm.connect(this.pinningNode, () => {}) + const keyring = this._3id.getKeyringBySpaceName(rootStoreName) + const identity = await keyring.getIdentity() + const key = keyring.getDBKey() // const cache = (opts.iframeStore && !!cacheProxy) ? cacheProxy : null - this._orbitdb = new OrbitDB(this._ipfs, opts.orbitPath) // , { cache }) + this._orbitdb = new OrbitDB(this._ipfs, identity, opts.orbitPath) // , { cache }) globalOrbitDB = this._orbitdb - - const dbKey = this._3id.getKeyringBySpaceName(rootStoreName).getDBKey() - const key = await this._orbitdb.keystore.importPrivateKey(dbKey) this._rootStore = await this._orbitdb.feed(rootStoreName, { - key, - write: [key.getPublic('hex')] + identity, + accessController: { + write: [key.getPublic('hex')], + legacy: true + } }) const rootStoreAddress = this._rootStore.address.toString() - this._pubsub = new Pubsub(this._ipfs, (await this._ipfs.id()).id) const onNewPeer = async (topic, peer) => { diff --git a/src/3id/index.js b/src/3id/index.js index 90b3b39f..19f3ab1d 100644 --- a/src/3id/index.js +++ b/src/3id/index.js @@ -57,7 +57,7 @@ class ThreeId { async _initMuport (muportIpfs) { let keys = this._mainKeyring.getPublicKeys() const doc = createMuportDocument(keys.signingKey, this.managementAddress, keys.asymEncryptionKey) - let docHash = (await this._ipfs.files.add(Buffer.from(JSON.stringify(doc))))[0].hash + let docHash = (await this._ipfs.add(Buffer.from(JSON.stringify(doc))))[0].hash this._muportDID = 'did:muport:' + docHash this.muportFingerprint = utils.sha256Multihash(this._muportDID) const publishToInfura = async () => { diff --git a/src/3id/keyring.js b/src/3id/keyring.js index 066c331c..f1cee8e7 100644 --- a/src/3id/keyring.js +++ b/src/3id/keyring.js @@ -3,6 +3,12 @@ const nacl = require('tweetnacl') nacl.util = require('tweetnacl-util') const SimpleSigner = require('did-jwt').SimpleSigner const { sha256 } = require('../utils/index') +const EC = require('elliptic').ec +const ec = new EC('secp256k1') +const IdentityProvider = require('./orbitProvider') +const Identities = require('orbit-db-identity-provider') + +Identities.addIdentityProvider(IdentityProvider) const BASE_PATH = "m/7696500'/0'/0'" const MM_PATH = "m/44'/60'/0'/0" @@ -58,7 +64,12 @@ class Keyring { } getDBKey () { - return this.signingKey.privateKey.slice(2) + return ec.keyFromPrivate(this.signingKey.privateKey.slice(2)) + } + + async getIdentity () { + const key = this.getDBKey() + return await Identities.createIdentity({ type: `3ID`, pubKey: key.getPublic('hex')}) } getDBSalt () { diff --git a/src/3id/orbitProvider.js b/src/3id/orbitProvider.js new file mode 100644 index 00000000..e9d681d9 --- /dev/null +++ b/src/3id/orbitProvider.js @@ -0,0 +1,42 @@ +// const Identities = require('orbit-db-identity-provider') +const IdentityProvider = require('orbit-db-identity-provider/src/identity-provider-interface.js') + + +class OrbitIdentityProvider { + constructor (options={}) { + // super(options) + console.log('create new identity') + console.log(options) + this.pubKey = options.pubKey + console.log('pubkey') + console.log(this.pubKey) + } + + static get type () { return '3ID' } // return type + // return identifier of external id (eg. a public key) + async getId () { + return this.pubKey + } + //return a signature of data (signature of the OrbtiDB public key) + async signIdentity (data) { + return 'signedstring' + } + + //return true if identity.sigantures are valid + static async verifyIdentity (identity) { + console.log(identity) + return true + } +} + + +module.exports = OrbitIdentityProvider + +// Identities.addIdentityProvider(MyIdentityProvider) + +// to create an identity of type `MyIdentityType` +// const identity = await Identities.createIdentity({ type: `MyIdentityType`}) + +// module.exports = (pubKey) => { +// return new OrbitProvider(pubkey ) +// } diff --git a/src/keyValueStore.js b/src/keyValueStore.js index 888dd7d0..e4371348 100644 --- a/src/keyValueStore.js +++ b/src/keyValueStore.js @@ -141,11 +141,15 @@ class KeyValueStore { } async _load (odbAddress) { - const dbKey = this._3id.getKeyringBySpaceName(this._name).getDBKey() - const key = await this._orbitdb.keystore.importPrivateKey(dbKey) + const keyring = this._3id.getKeyringBySpaceName(this._name) + const identity = await keyring.getIdentity() + const key = keyring.getDBKey() this._db = await this._orbitdb.keyvalue(odbAddress || this._name, { - key, - write: [key.getPublic('hex')] + identity, + accessController: { + write: [key.getPublic('hex')], + legacy: true + } }) await this._db.load() return this._db.address.toString() @@ -162,7 +166,7 @@ class KeyValueStore { async all () { this._requireLoad() - const entries = await this._db.all() + const entries = await this._db.all let allSimple = {} Object.keys(entries).map(key => { allSimple[key] = entries[key].value }) return allSimple diff --git a/src/thread.js b/src/thread.js index 682b3585..0dea2221 100644 --- a/src/thread.js +++ b/src/thread.js @@ -83,11 +83,13 @@ class Thread { async _load (odbAddress) { // TODO - threads should use the space keyring once pairwise DIDs are implemented - const dbKey = this._3id._mainKeyring.getDBKey() - const key = await this._orbitdb.keystore.importPrivateKey(dbKey) + const identity = await this._3id._mainKeyring.getIdentity() this._db = await this._orbitdb.log(odbAddress || this._name, { - key, - write: ['*'] + identity, + accessController: { + write: ['*'], + legacy: true + } }) await this._db.load() this._address = this._db.address.toString() From 01a19fcad6563f6bd892db6ddb048f1e3b5fb3f9 Mon Sep 17 00:00:00 2001 From: Zach Ferland Date: Thu, 16 May 2019 10:26:43 -0400 Subject: [PATCH 02/10] feat: poc thread access controllers --- example/index.html | 4 + example/index.js | 23 ++++- src/3box.js | 12 ++- src/access/moderator-access.js | 40 +++++++++ src/access/thread-open-mod-access.js | 126 +++++++++++++++++++++++++++ src/thread.js | 40 +++++++-- 6 files changed, 237 insertions(+), 8 deletions(-) create mode 100644 src/access/moderator-access.js create mode 100644 src/access/thread-open-mod-access.js diff --git a/example/index.html b/example/index.html index 38f0bf21..e5fb4a95 100644 --- a/example/index.html +++ b/example/index.html @@ -81,6 +81,10 @@

Threads:

+ + + +