Skip to content

Commit

Permalink
Auto encrypted publish when recps is set (#212)
Browse files Browse the repository at this point in the history
* Add test for auto encrypted publish

* Auto encrypt message in publish if recps is set

* Fix tests

* No need to substr recps

* Tidy
  • Loading branch information
arj03 authored Mar 12, 2021
1 parent 5a3358e commit 39d5d14
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -217,8 +218,11 @@ exports.init = function (sbot, config) {
stateFeedsReady,
(ready) => ready === true,
() => {
if (msg.recps) msg = ssbKeys.box(msg, msg.recps)

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)
Expand Down
19 changes: 1 addition & 18 deletions test/base-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,8 @@ 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)
})
})
})
})

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)
})
})
})
52 changes: 52 additions & 0 deletions test/private.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})

0 comments on commit 39d5d14

Please sign in to comment.