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

Add decrypted api for for box2 decrypted messages #410

Merged
merged 6 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,40 @@ operators:
See [jitdb operators] and [operators/index.js] for a complete list of supported
operators.

### decrypted

A pull-stream source with newly decrypted values returned as full
arj03 marked this conversation as resolved.
Show resolved Hide resolved
messages. JITDB doesn't have a concept of exactly what values are
decrypted so a separate api is needed. This api can be combined with
`where` to receive old values, live values or decrypted values.

Example:

``` js
const pull = require('pull-stream')
const cat = require('pull-cat')

pull(
cat([
sbot2.db.query(
where(type('post')),
toPullStream()
),
pull(
sbot2.db.decrypted,
pull.filter((msg) => {
return msg.value.content.type === 'post'
})
)
]),
pull.drain(
(result) => {
console.log("got a new post", result.value)
}
)
)
```

### add(nativeMsg, cb)

Validate and add a message to the database. The callback will the (possible)
Expand Down
19 changes: 19 additions & 0 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,22 @@ exports.init = function (sbot, config) {

const reindexingLock = mutexify()

function LazyPull() {
let onValue

return {
add(val) {
if (onValue) onValue(null, val)
},
source(abort, cb) {
if (abort) cb(abort)
else if (!onValue) onValue = cb
},
}
}

const decryptedValues = LazyPull()
arj03 marked this conversation as resolved.
Show resolved Hide resolved

function reindexEncrypted(cb) {
indexingActive.set(indexingActive.value + 1)
reindexingLock((unlock) => {
Expand Down Expand Up @@ -1008,6 +1024,8 @@ exports.init = function (sbot, config) {

const pValue = bipf.seekKey2(buf, 0, BIPF_VALUE, 0)

decryptedValues.add(bipf.decode(buf, 0))

onDrain('keys', () => {
indexes['keys'].getSeq(key, (err, seq) => {
// prettier-ignore
Expand Down Expand Up @@ -1120,6 +1138,7 @@ exports.init = function (sbot, config) {
getMsg,
query,
prepare,
decrypted: decryptedValues.source,
del,
deleteFeed,
add,
Expand Down
109 changes: 109 additions & 0 deletions test/query-decrypted-box2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-FileCopyrightText: 2022 Anders Rune Jensen
//
// SPDX-License-Identifier: Unlicense

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 ssbUri = require('ssb-uri2')
const pull = require('pull-stream')
const cat = require('pull-cat')
const fs = require('fs')
const {
where,
type,
toPullStream,
toCallback
} = require('../operators')

const dir = '/tmp/ssb-db2-query-decrypted-handle-box2'
rimraf.sync(dir)
mkdirp.sync(dir)

const dir2 = '/tmp/ssb-db2-query-decrypted-handle-box2-2'
rimraf.sync(dir2)
mkdirp.sync(dir2)

test('decrypted api contains newly decrypted box2 messages', (t) => {
const keys = ssbKeys.loadOrCreateSync(path.join(dir, 'secret'))
const sbot = SecretStack({ appKey: caps.shs })
.use(require('../'))
.call(null, { keys, path: dir })

const testkey = Buffer.from(
'30720d8f9cbf37f6d7062826f6decac93e308060a8aaaa77e6a4747f40ee1a76',
'hex'
)
const groupId = ssbUri.compose({
type: 'identity',
format: 'group',
data: '-oaWWDs8g73EZFUMfW37R_ULtFEjwKN_DczvdYihjbU=',
})
sbot.box2.addGroupInfo(groupId, { key: testkey })

const post = {
feedFormat: 'classic',
content: { type: 'post', text: 'Testing!' },
recps: [groupId],
encryptionFormat: 'box2',
}

sbot.db.create(post, (err, msgBoxed) => {
t.error(err, 'no err')
t.equal(typeof msgBoxed.value.content, 'string')
t.true(msgBoxed.value.content.endsWith('.box2'), '.box2')

const keys2 = ssbKeys.loadOrCreateSync(path.join(dir2, 'secret'))
const sbot2 = SecretStack({ appKey: caps.shs })
.use(require('../'))
.call(null, { keys: keys2, path: dir2 })

// setup decrypted handler
pull(
cat([
arj03 marked this conversation as resolved.
Show resolved Hide resolved
sbot2.db.query(
where(type('post')),
toPullStream()
),
pull(
sbot2.db.decrypted,
pull.filter((msg) => {
return msg.value.content.type === 'post'
})
)
]),
pull.drain(
(result) => {
t.equal(result.value.content.text, 'Testing!')
sbot.close(() => {
sbot2.close(t.end)
})
}
)
)

sbot2.db.add(msgBoxed.value, (err) => {
t.error(err, 'no err')

// make sure we have queries indexed before adding the key
pull(
sbot2.db.query(
where(type('post')),
toCallback((err, results) => {
t.error(err, 'no err')
t.equal(results.length, 0, 'no results')

sbot2.box2.addGroupInfo(groupId, { key: testkey })
sbot2.db.reindexEncrypted((err) => {
t.error(err, 'no err')
})
})
)
)
})
})
})
arj03 marked this conversation as resolved.
Show resolved Hide resolved