Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto encrypted publish when recps is set #212

Merged
merged 5 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
})