Skip to content

Commit

Permalink
Merge pull request #37 from ssbc/get-own-dm
Browse files Browse the repository at this point in the history
Add getOwnDMKey function
  • Loading branch information
Powersource authored Nov 7, 2023
2 parents 3f08690 + 39e3531 commit 3ff3f1c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ sbot.db.create(
Adding this module as a secret-stack plugin means that you can use these methods
on the `sbot.box2` namespace:

- `setOwnDMKey(key)`: Adds a `key` (a buffer) to the list of keys that can be
- `setOwnDMKey(key)`: Sets a `key` (a buffer) as the key that will be
used to encrypt messages to yourself. By specifying the direct message (DM)
for yourself, you are free to supply that from any source. The key you provide
_will_ be persisted locally. For direct messaging other feeds, a key is
automatically derived.
- `getOwnDMKey(cb)`: Gets the key that would be used for DM'ing yourself. On the format `{ key, scheme }`.
- `addGroupInfo(groupId, addInfo, cb)`: `groupId` must be a cloaked message Id or a uri encoded group and `addInfo` must be an object. Can be called multiple times to add multiple read keys. The first key that is added will automatically also be set as the write key. To change the write key, use `pickGroupWriteKey`. If you add a key to an excluded group, the group will be un-excluded. Returns a promise if cb isn't provided. `addInfo` can have these keys:
- `key` must be a buffer. The key can then be used for decrypting messages from the group, and if picked with `pickGroupWriteKey`, as a "recp" to encrypt messages to the group. Note that the keys are not persisted in this module.
- `scheme` _String_ - scheme of that encryption key (optional, there is only one option at the moment which we default to)
Expand Down
7 changes: 7 additions & 0 deletions format.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ function makeEncryptionFormat() {
})
}

function getOwnDMKey(cb) {
keyringReady.onReady(() => {
cb(null, keyring.self.get())
})
}

function addDMPairSync(myKeys, theirId) {
if (!keyringReady.ready) throw new Error('keyring not ready')
const myId = myKeys.id
Expand Down Expand Up @@ -405,6 +411,7 @@ function makeEncryptionFormat() {
decrypt,
// ssb-box2 specific APIs:
setOwnDMKey,
getOwnDMKey,
addGroupInfo,
pickGroupWriteKey,
excludeGroupInfo,
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ exports.init = function (ssb, config) {

return {
setOwnDMKey: encryptionFormat.setOwnDMKey,
getOwnDMKey: encryptionFormat.getOwnDMKey,
canDM: encryptionFormat.canDM,
addGroupInfo: encryptionFormat.addGroupInfo,
pickGroupWriteKey: encryptionFormat.pickGroupWriteKey,
Expand Down
74 changes: 49 additions & 25 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: Unlicense

const { promisify: p } = require('util')
const test = require('tape')
const { check } = require('ssb-encryption-format')
const ssbKeys = require('ssb-keys')
Expand Down Expand Up @@ -213,6 +214,25 @@ test('cannot decrypt own DM after we changed our own DM keys', (t) => {
})
})

test('can get own self dm key', async (t) => {
const box2 = Box2()
const keys = ssbKeys.generate(null, 'alice', 'buttwoo-v1')

await p(box2.setup)({ keys })

const ownKey = Buffer.from(
'30720d8f9cbf37f6d7062826f6decac93e308060a8aaaa77e6a4747f40ee1a76',
'hex'
)

box2.setOwnDMKey(ownKey)

const gottenKey = await p(box2.getOwnDMKey)()

t.equal(gottenKey.key, ownKey, 'got correct key')
t.equal(gottenKey.scheme, keySchemes.feed_id_self, 'got correct scheme')
})

test('cannot encrypt to zero valid recipients', (t) => {
const box2 = Box2()
const keys = ssbKeys.generate(null, 'alice', 'buttwoo-v1')
Expand Down Expand Up @@ -367,30 +387,34 @@ test('decrypt as pobox recipient', (t) => {
const testkey = poBoxDH.toBuffer().secret

box2.setup({ keys }, () => {
box2.addPoBox(poBoxId, {
key: testkey,
}, (err) => {
t.error(err, "added pobox key")

const opts = {
keys,
content: { type: 'post', text: 'super secret' },
previous: null,
timestamp: 12345678900,
tag: buttwoo.tags.SSB_FEED,
hmacKey: null,
recps: [poBoxId, ssbKeys.generate(null, '2').id],
box2.addPoBox(
poBoxId,
{
key: testkey,
},
(err) => {
t.error(err, 'added pobox key')

const opts = {
keys,
content: { type: 'post', text: 'super secret' },
previous: null,
timestamp: 12345678900,
tag: buttwoo.tags.SSB_FEED,
hmacKey: null,
recps: [poBoxId, ssbKeys.generate(null, '2').id],
}

const plaintext = buttwoo.toPlaintextBuffer(opts)
t.true(Buffer.isBuffer(plaintext), 'plaintext is a buffer')

const ciphertext = box2.encrypt(plaintext, opts)

const decrypted = box2.decrypt(ciphertext, { ...opts, author: keys.id })
t.deepEqual(decrypted, plaintext, 'decrypted plaintext is the same')

t.end()
}

const plaintext = buttwoo.toPlaintextBuffer(opts)
t.true(Buffer.isBuffer(plaintext), 'plaintext is a buffer')

const ciphertext = box2.encrypt(plaintext, opts)

const decrypted = box2.decrypt(ciphertext, { ...opts, author: keys.id })
t.deepEqual(decrypted, plaintext, 'decrypted plaintext is the same')

t.end()
})
)
})
})
})

0 comments on commit 3ff3f1c

Please sign in to comment.