From 01230c9cdc226a71fbf7fe443870f3316eb03eda Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Wed, 1 Nov 2023 18:31:17 +0100 Subject: [PATCH 1/2] Add getOwnDMKey function --- format.js | 7 +++++ index.js | 1 + test/index.js | 74 ++++++++++++++++++++++++++++++++++----------------- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/format.js b/format.js index 13f835f..278948b 100644 --- a/format.js +++ b/format.js @@ -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 @@ -405,6 +411,7 @@ function makeEncryptionFormat() { decrypt, // ssb-box2 specific APIs: setOwnDMKey, + getOwnDMKey, addGroupInfo, pickGroupWriteKey, excludeGroupInfo, diff --git a/index.js b/index.js index bc9d965..0219dcd 100644 --- a/index.js +++ b/index.js @@ -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, diff --git a/test/index.js b/test/index.js index c8189f0..2312dd1 100644 --- a/test/index.js +++ b/test/index.js @@ -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') @@ -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') @@ -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() - }) + ) }) -}) \ No newline at end of file +}) From 39e35310591ae24a4ee1b5d8c9c9244170b7979e Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Thu, 2 Nov 2023 19:27:14 +0100 Subject: [PATCH 2/2] Document getOwnDMKey and update it for setOwnDMKey --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6154b1f..ae8feee 100644 --- a/README.md +++ b/README.md @@ -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)