From 238dbabd265d7cb3487ec7ed0d5dec8ec5be81f1 Mon Sep 17 00:00:00 2001 From: Anders Rune Jensen Date: Thu, 11 Mar 2021 20:55:41 +0100 Subject: [PATCH 1/5] Add test for auto encrypted publish --- test/base-index.js | 17 --------------- test/private.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 test/private.js diff --git a/test/base-index.js b/test/base-index.js index 089ea308..cf099b12 100644 --- a/test/base-index.js +++ b/test/base-index.js @@ -94,20 +94,3 @@ test('get all latest', (t) => { }) }) }) - -test('encrypted', (t) => { - let content = { type: 'post', text: 'super secret', recps: [keys.id] } - content = ssbKeys.box( - content, - content.recps.map((x) => x.substr(1)) - ) - - db.publish(content, (err, privateMsg) => { - t.error(err, 'no err') - - db.get(privateMsg.key, (err, msg) => { - t.equal(msg.content.text, 'super secret') - sbot.close(t.end) - }) - }) -}) diff --git a/test/private.js b/test/private.js new file mode 100644 index 00000000..2732344a --- /dev/null +++ b/test/private.js @@ -0,0 +1,52 @@ +const test = require('tape') +const ssbKeys = require('ssb-keys') +const path = require('path') +const rimraf = require('rimraf') +const mkdirp = require('mkdirp') +const SecretStack = require('secret-stack') +const caps = require('ssb-caps') + +const dir = '/tmp/ssb-db2-private' + +rimraf.sync(dir) +mkdirp.sync(dir) + +const keys = ssbKeys.loadOrCreateSync(path.join(dir, 'secret')) + +const sbot = SecretStack({ appKey: caps.shs }).use(require('../')).call(null, { + keys, + path: dir, +}) +const db = sbot.db + +test('publish encrypted message', (t) => { + let content = { type: 'post', text: 'super secret', recps: [keys.id] } + content = ssbKeys.box( + content, + content.recps.map((x) => x.substr(1)) + ) + + db.publish(content, (err, privateMsg) => { + t.error(err, 'no err') + + t.equal(typeof privateMsg.value.content, 'string') + db.get(privateMsg.key, (err, msg) => { + t.equal(msg.content.text, 'super secret') + t.end() + }) + }) +}) + +test('publish: auto encrypt message with recps', (t) => { + let content = { type: 'post', text: 'super secret', recps: [keys.id] } + + db.publish(content, (err, privateMsg) => { + t.error(err, 'no err') + + t.equal(typeof privateMsg.value.content, 'string') + db.get(privateMsg.key, (err, msg) => { + t.equal(msg.content.text, 'super secret') + sbot.close(t.end) + }) + }) +}) From 9f0dccb8d85a78c97ea4ab85c615c7167055f1f3 Mon Sep 17 00:00:00 2001 From: Anders Rune Jensen Date: Thu, 11 Mar 2021 21:07:08 +0100 Subject: [PATCH 2/5] Auto encrypt message in publish if recps is set --- README.md | 3 ++- db.js | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9acf6ee5..7e976a93 100644 --- a/README.md +++ b/README.md @@ -345,7 +345,8 @@ method is safe to use. ### publish(msg, cb) Convenience method for validating and adding a message to the database -written by the feed running the secret-stack. +written by the feed running the secret-stack. If message contains +recps, the message will automatically be encrypted. ### add(msg, cb) diff --git a/db.js b/db.js index 4d54fdcc..71a5ccee 100644 --- a/db.js +++ b/db.js @@ -1,4 +1,5 @@ const push = require('push-stream') +const ssbKeys = require('ssb-keys') const hash = require('ssb-keys/util').hash const validate = require('ssb-validate') const Obv = require('obz') @@ -217,8 +218,16 @@ exports.init = function (sbot, config) { stateFeedsReady, (ready) => ready === true, () => { + if (msg.recps) { + msg = ssbKeys.box( + msg, + msg.recps.map((x) => x.substr(1)) + ) + } + state.queue = [] state = validate.appendNew(state, null, config.keys, msg, Date.now()) + rawAdd(state.queue[0].value, true, (err, data) => { post.set(data) cb(err, data) From d54ae853fb1094a18feebb435c82767ee3738c9e Mon Sep 17 00:00:00 2001 From: Anders Rune Jensen Date: Thu, 11 Mar 2021 21:37:00 +0100 Subject: [PATCH 3/5] Fix tests --- test/base-index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/base-index.js b/test/base-index.js index cf099b12..2ff9ec00 100644 --- a/test/base-index.js +++ b/test/base-index.js @@ -89,7 +89,7 @@ test('get all latest', (t) => { t.equal(postMsg.value.sequence, status.sequence) t.equal(postMsg.value.timestamp, status.timestamp) - t.end() + sbot.close(t.end) }) }) }) From 82ee11c38a9b7e5e45762ac8ece989aadbda8eb6 Mon Sep 17 00:00:00 2001 From: Anders Rune Jensen Date: Fri, 12 Mar 2021 11:29:53 +0100 Subject: [PATCH 4/5] No need to substr recps --- db.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/db.js b/db.js index 71a5ccee..9a73a006 100644 --- a/db.js +++ b/db.js @@ -219,10 +219,7 @@ exports.init = function (sbot, config) { (ready) => ready === true, () => { if (msg.recps) { - msg = ssbKeys.box( - msg, - msg.recps.map((x) => x.substr(1)) - ) + msg = ssbKeys.box(msg, msg.recps) } state.queue = [] From a59d3139387b83fcffa4f3870e2b978a2a5b197a Mon Sep 17 00:00:00 2001 From: Anders Rune Jensen Date: Fri, 12 Mar 2021 11:30:59 +0100 Subject: [PATCH 5/5] Tidy --- db.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/db.js b/db.js index 9a73a006..031eab12 100644 --- a/db.js +++ b/db.js @@ -218,9 +218,7 @@ exports.init = function (sbot, config) { stateFeedsReady, (ready) => ready === true, () => { - if (msg.recps) { - msg = ssbKeys.box(msg, msg.recps) - } + if (msg.recps) msg = ssbKeys.box(msg, msg.recps) state.queue = [] state = validate.appendNew(state, null, config.keys, msg, Date.now())