From 0931fea51409cd803b4a919734485553a1e64a79 Mon Sep 17 00:00:00 2001 From: haydenyoung Date: Sun, 12 May 2019 20:15:17 +0800 Subject: [PATCH 1/8] Expose db identity information. Update write permissions by adding another public key to the store. --- src/lib/orbitdb-api.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lib/orbitdb-api.js b/src/lib/orbitdb-api.js index 94f331b..08b92d4 100644 --- a/src/lib/orbitdb-api.js +++ b/src/lib/orbitdb-api.js @@ -201,6 +201,23 @@ class OrbitdbAPI extends Express { return contents } + this.get('/db/:dbname/identity', asyncMiddleware( async (req, res, next) => { + const db = await dbm.get(req.params.dbname); + const identity = db.identity; + + return res.json(identity); + })); + + var db_put_write_public_key = asyncMiddleware( async (req, res, next) => { + let db + db = await dbm.get(req.params.dbname) + await db.access.grant('write', req.params.pubkey) + + return res.json('') + }); + + this.put('/db/:dbname/access/write/:pubkey', db_put_write_public_key); + this.get('/db/:dbname/:item', asyncMiddleware( async (req, res, next) => { let result, contents contents = await getraw(req,res, next) From 2e1b1b94f2e9597e2f4acf9f8d45ab734a3e4294 Mon Sep 17 00:00:00 2001 From: haydenyoung Date: Sun, 12 May 2019 21:01:01 +0800 Subject: [PATCH 2/8] Access global identity. --- src/lib/db-manager.js | 4 ++++ src/lib/orbitdb-api.js | 7 +++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib/db-manager.js b/src/lib/db-manager.js index f970ee8..e06dc48 100644 --- a/src/lib/db-manager.js +++ b/src/lib/db-manager.js @@ -84,6 +84,10 @@ class DBManager { ) }; }; + + this.identity = () => { + return orbitdb.identity; + }; } } diff --git a/src/lib/orbitdb-api.js b/src/lib/orbitdb-api.js index 08b92d4..0f31e7d 100644 --- a/src/lib/orbitdb-api.js +++ b/src/lib/orbitdb-api.js @@ -201,11 +201,10 @@ class OrbitdbAPI extends Express { return contents } - this.get('/db/:dbname/identity', asyncMiddleware( async (req, res, next) => { - const db = await dbm.get(req.params.dbname); - const identity = db.identity; + this.get('/identity', asyncMiddleware( async (req, res, next) => { + const identity = dbm.identity() - return res.json(identity); + return res.json(identity) })); var db_put_write_public_key = asyncMiddleware( async (req, res, next) => { From abffeaec3113a09559e46b5364b9aee600f4c51a Mon Sep 17 00:00:00 2001 From: haydenyoung Date: Sun, 12 May 2019 21:28:23 +0800 Subject: [PATCH 3/8] POST write access. --- src/lib/orbitdb-api.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/orbitdb-api.js b/src/lib/orbitdb-api.js index 0f31e7d..b0116fb 100644 --- a/src/lib/orbitdb-api.js +++ b/src/lib/orbitdb-api.js @@ -215,6 +215,7 @@ class OrbitdbAPI extends Express { return res.json('') }); + this.post('/db/:dbname/access/write/:pubkey', db_put_write_public_key); this.put('/db/:dbname/access/write/:pubkey', db_put_write_public_key); this.get('/db/:dbname/:item', asyncMiddleware( async (req, res, next) => { From 5fb1a81a8577d6be87301671016d713dc6447abc Mon Sep 17 00:00:00 2001 From: haydenyoung Date: Mon, 13 May 2019 00:52:45 +0800 Subject: [PATCH 4/8] Document jSON-encoded param example. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 5fe4b1c..917bdac 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,13 @@ POST param (this would apply to type docstore only): curl http://localhost:3000/db/docstore -d "create=true" -d "type=docstore" -d "indexBy=name" ``` +Parameters can also be passed as JSON. This is useful if additional parameters +such as accessController need to be specified: + +```shell +curl -H "Content-Type: application/json" --data '{"create":"true","type":"feed","accessController":{"type": "orbitdb","write": ["1234"]}}' +``` + To open an existing database, specify the address of the database. If the database does not exist locally it will be fetched from the swarm. From ab093d1e28895437713429a9c704bda7e07f9df6 Mon Sep 17 00:00:00 2001 From: haydenyoung Date: Mon, 13 May 2019 10:23:02 +0800 Subject: [PATCH 5/8] Pass pubkey as a POST field. Docs updated. --- README.md | 125 ++++++++++++++++++++++++----------------- src/lib/orbitdb-api.js | 6 +- 2 files changed, 77 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 917bdac..7532d53 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,18 @@ - [Modulus Query](#modulus-query) - [Range Query](#range-query) - [GET /db/:dbname/:item](#get-dbdbnameitem) - - [POST /db/:dbname](#post-dbdbname) - [GET /db/:dbname/iterator](#get-dbdbnameiterator) - [GET /db/:dbname/index](#get-dbdbnameindex) + - [GET /identity](#get-identity) + - [POST /db/:dbname](#post-dbdbname) + - [POST|PUT /db/:dbname/add](#post-put-dbdbnameadd) + - [POST|PUT /db/:dbname/put](#post-put-dbdbnameput) + - [POST|PUT /db/:dbname/inc](#post-put-dbdbnameinc) + - [POST|PUT /db/:dbname/inc/:val](#post-put-dbdbnameincval) + - [DELETE /db/:dbname](#delete-dbdbname) + - [DELETE /db/:dbname/:item](#delete-dbdbnameitem) +- [Contribute](#contribute) +- [License](#license) ## Install @@ -185,56 +194,6 @@ curl -X GET http://localhost:3000/db/docstore/1 [{"_id":1, "value": "test"}] ``` -### POST /db/:dbname - -Creates a new database and returns information about the newly created database -or opens an existing database with the same name. - -Returns information about the database as a JSON object. - -The OrbitDB options ```create=true``` and ```type=eventlog|feed|docstore|keyvalue|counter``` -must be sent with the POST otherwise an error is thrown. - -```shell -curl http://localhost:3000/db/docstore -d "create=true" -d "type=docstore" -``` - -```json -{"address":{"root":"zdpuAmnfJZ6UTssG5Ns3o8ALXZJXVx5eTLTxf7gfFzHxurbJq","path":"docstore"},"dbname":"docstore","id":"/orbitdb/zdpuAmnfJZ6UTssG5Ns3o8ALXZJXVx5eTLTxf7gfFzHxurbJq/docstore","options":{"create":"true","indexBy":"_id","localOnly":false,"maxHistory":-1,"overwrite":true,"replicate":true},"type":"docstore"} -``` - -Additional OrbitDB-specific flags can also be passed. For example, if the index -field must be changed then the indexBy flag can be specified as an additional -POST param (this would apply to type docstore only): - -```shell -curl http://localhost:3000/db/docstore -d "create=true" -d "type=docstore" -d "indexBy=name" -``` - -Parameters can also be passed as JSON. This is useful if additional parameters -such as accessController need to be specified: - -```shell -curl -H "Content-Type: application/json" --data '{"create":"true","type":"feed","accessController":{"type": "orbitdb","write": ["1234"]}}' -``` - -To open an existing database, specify the address of the database. If the -database does not exist locally it will be fetched from the swarm. - -The address MUST be URL escaped. - -```shell -curl -X POST http://localhost:3000/db/zdpuAmnfJZ6UTssG5Ns3o8ALXZJXVx5eTLTxf7gfFzHxurbJq%2Fdocstore -``` - -By default, OrbitDB will open the database if one already exists with the same -name. To always overwrite the existing database with a new one, pass the -overwrite flag: - -```shell -curl http://localhost:3000/db/docstore -d "create=true" -d "type=docstore" -d "overwrite=true" -``` - ### GET /db/:dbname/iterator Gets items from an eventlog or feed database :dbname. @@ -299,6 +258,70 @@ The counter data store returns information about the current counter value: {"id":"04e6de9dd0e8d0069bcc6d8f3ef11cefe63bba6129c32f2cd422a0394814bc6723b26eb62731ee466020b0394d01dd08e4a5123eaad45e4d0840fd796652a22e42","counters":{"04e6de9dd0e8d0069bcc6d8f3ef11cefe63bba6129c32f2cd422a0394814bc6723b26eb62731ee466020b0394d01dd08e4a5123eaad45e4d0840fd796652a22e42":15}} ``` +### GET /identity + +Gets the identity information. + +Returns identity as a JSON object. + +```shell +curl -X GET http://localhost:3000/identity +``` + +```json +{"id":"03fc293ea95bdb5dea71d5d21cbbae2a57f2d2002c9966f0d5c7b0bda232d5934d","publicKey":"048161d9685991dc87f3e049aa04b1da461fdc5f8a280ed6234fa41c0f9bc98a1ce91f07494584a45b97160ac818e100a6b27777e0b1b09e6ba4795dcc797a6d8b","signatures":{"id":"3045022100e40ab2dcc83dde17c939d5515ce322e7f81bf47536ab342582db8c35f28d2a720220228e418cc3d2f3e0004d5f4292c0d2cf7975c93073e0cc831f0cb849e4ac920a","publicKey":"3045022100ad18ba66006e19e2952eabc9ffb532dd69d60593f90448a05d6f4903c2931e3502204009975030b839522c668cd693d357bf1f3d0423d604a6bc10645425a0a3dd1b"},"type":"orbitdb"} +``` + +### POST /db/:dbname + +Creates a new database and returns information about the newly created database +or opens an existing database with the same name. + +Returns information about the database as a JSON object. + +The OrbitDB options ```create=true``` and ```type=eventlog|feed|docstore|keyvalue|counter``` +must be sent with the POST otherwise an error is thrown. + +```shell +curl http://localhost:3000/db/docstore -d "create=true" -d "type=docstore" +``` + +```json +{"address":{"root":"zdpuAmnfJZ6UTssG5Ns3o8ALXZJXVx5eTLTxf7gfFzHxurbJq","path":"docstore"},"dbname":"docstore","id":"/orbitdb/zdpuAmnfJZ6UTssG5Ns3o8ALXZJXVx5eTLTxf7gfFzHxurbJq/docstore","options":{"create":"true","indexBy":"_id","localOnly":false,"maxHistory":-1,"overwrite":true,"replicate":true},"type":"docstore"} +``` + +Additional OrbitDB-specific flags can also be passed. For example, if the index +field must be changed then the indexBy flag can be specified as an additional +POST param (this would apply to type docstore only): + +```shell +curl http://localhost:3000/db/docstore -d "create=true" -d "type=docstore" -d "indexBy=name" +``` + +Parameters can also be passed as JSON. This is useful if additional parameters +such as accessController need to be specified: + +```shell +curl -H "Content-Type: application/json" --data '{"create":"true","type":"feed","accessController":{"type": "orbitdb","write": ["1234"]}}' +``` + +To open an existing database, specify the address of the database. If the +database does not exist locally it will be fetched from the swarm. + +The address MUST be URL escaped. + +```shell +curl -X POST http://localhost:3000/db/zdpuAmnfJZ6UTssG5Ns3o8ALXZJXVx5eTLTxf7gfFzHxurbJq%2Fdocstore +``` + +By default, OrbitDB will open the database if one already exists with the same +name. To always overwrite the existing database with a new one, pass the +overwrite flag: + +```shell +curl http://localhost:3000/db/docstore -d "create=true" -d "type=docstore" -d "overwrite=true" +``` + ### POST|PUT /db/:dbname/add Adds a new entry to the eventlog or feed database :dbname. diff --git a/src/lib/orbitdb-api.js b/src/lib/orbitdb-api.js index b0116fb..e548b34 100644 --- a/src/lib/orbitdb-api.js +++ b/src/lib/orbitdb-api.js @@ -210,13 +210,13 @@ class OrbitdbAPI extends Express { var db_put_write_public_key = asyncMiddleware( async (req, res, next) => { let db db = await dbm.get(req.params.dbname) - await db.access.grant('write', req.params.pubkey) + await db.access.grant('write', req.params.publicKey) return res.json('') }); - this.post('/db/:dbname/access/write/:pubkey', db_put_write_public_key); - this.put('/db/:dbname/access/write/:pubkey', db_put_write_public_key); + this.post('/db/:dbname/access/write', db_put_write_public_key); + this.put('/db/:dbname/access/write', db_put_write_public_key); this.get('/db/:dbname/:item', asyncMiddleware( async (req, res, next) => { let result, contents From a0e3d21a4a84632a410f35e8b9e09ebe92671c60 Mon Sep 17 00:00:00 2001 From: haydenyoung Date: Mon, 13 May 2019 11:18:43 +0800 Subject: [PATCH 6/8] Retrieve pubkey from post body. --- src/lib/orbitdb-api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/orbitdb-api.js b/src/lib/orbitdb-api.js index e548b34..8644d34 100644 --- a/src/lib/orbitdb-api.js +++ b/src/lib/orbitdb-api.js @@ -210,7 +210,7 @@ class OrbitdbAPI extends Express { var db_put_write_public_key = asyncMiddleware( async (req, res, next) => { let db db = await dbm.get(req.params.dbname) - await db.access.grant('write', req.params.publicKey) + await db.access.grant('write', req.body.publicKey) return res.json('') }); From df3173dceafe1829890897b88e899076ec0e849f Mon Sep 17 00:00:00 2001 From: phillmac Date: Wed, 15 May 2019 09:56:57 +1000 Subject: [PATCH 7/8] Remove unnecessary async --- src/lib/orbitdb-api.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/orbitdb-api.js b/src/lib/orbitdb-api.js index 8644d34..5b671ec 100644 --- a/src/lib/orbitdb-api.js +++ b/src/lib/orbitdb-api.js @@ -201,11 +201,11 @@ class OrbitdbAPI extends Express { return contents } - this.get('/identity', asyncMiddleware( async (req, res, next) => { + this.get('/identity', (req, res, next) => { const identity = dbm.identity() return res.json(identity) - })); + }); var db_put_write_public_key = asyncMiddleware( async (req, res, next) => { let db From 52e131642265303f1da4e4e86448bffc92155eb3 Mon Sep 17 00:00:00 2001 From: phillmac Date: Wed, 15 May 2019 15:46:05 +1000 Subject: [PATCH 8/8] Catch errors in GET /identity endpoint --- src/lib/orbitdb-api.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib/orbitdb-api.js b/src/lib/orbitdb-api.js index 5b671ec..e68d2b1 100644 --- a/src/lib/orbitdb-api.js +++ b/src/lib/orbitdb-api.js @@ -202,9 +202,12 @@ class OrbitdbAPI extends Express { } this.get('/identity', (req, res, next) => { - const identity = dbm.identity() - - return res.json(identity) + try { + const identity = dbm.identity() + return res.json(identity) + } catch(err) { + next(err) + } }); var db_put_write_public_key = asyncMiddleware( async (req, res, next) => {