-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.js
51 lines (45 loc) · 1.57 KB
/
format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-FileCopyrightText: 2022 Andre 'Staltz' Medeiros
//
// SPDX-License-Identifier: LGPL-3.0-only
const sodium = require('chloride');
const privateBox = require('private-box');
const Ref = require('ssb-ref');
const {
isButtwooV1FeedSSBURI,
isBendyButtV1FeedSSBURI,
decompose,
isClassicFeedSSBURI,
} = require('ssb-uri2');
const encryptionFormat = {
name: 'box',
encrypt(plaintextBuf, opts) {
const encryptionKeys = opts.recps
.map(function convertToBase64DataStr(recp) {
if (Ref.isFeed(recp)) return recp.slice(1, -8);
else if (
isClassicFeedSSBURI(recp) ||
isBendyButtV1FeedSSBURI(recp) ||
isButtwooV1FeedSSBURI(recp)
)
return decompose(recp).data;
else if (recp && typeof recp === 'string') {
// prettier-ignore
throw new Error('encryption format "box" does not support recipient "' + recp + '"');
} else return null;
})
.filter((maybeBase64DataStr) => !!maybeBase64DataStr)
.map((base64DataStr) => Buffer.from(base64DataStr, 'base64'))
.map(sodium.crypto_sign_ed25519_pk_to_curve25519);
return privateBox.multibox(plaintextBuf, encryptionKeys);
},
decrypt(ciphertextBuf, opts) {
const secretKey =
opts.keys._exchangeKey || // use the cache
sodium.crypto_sign_ed25519_sk_to_curve25519(
Buffer.from(opts.keys.private, 'base64'),
);
if (opts.keys.private) opts.keys._exchangeKey = secretKey; // set the cache
return privateBox.multibox_open(ciphertextBuf, secretKey);
},
};
module.exports = encryptionFormat;