Files: 3c079c7c75a9d3c1510d61209f3944aacdc90f9e / index.js
2210 bytesRaw
1 | var ssbKeys = require('ssb-keys') |
2 | var FlumeQueryLinks = require('./lib/flumeview-links-raw') |
3 | var explain = require('explain-error') |
4 | var pull = require('pull-stream') |
5 | |
6 | var toUrlFriendly = require('base64-url').escape |
7 | |
8 | var indexes = [ |
9 | { key: 'TSP', value: ['timestamp'] }, |
10 | { key: 'ATY', value: [['value', 'author'], ['value', 'content', 'type'], 'timestamp'] } |
11 | ] |
12 | |
13 | var indexVersion = 0 |
14 | |
15 | exports.name = 'private' |
16 | exports.version = require('./package.json').version |
17 | exports.manifest = { |
18 | publish: 'async', |
19 | unbox: 'sync', |
20 | read: 'source' |
21 | } |
22 | |
23 | exports.init = function (ssb, config) { |
24 | var index = ssb._flumeUse( |
25 | `private-${toUrlFriendly(ssb.id.slice(1, 10))}`, |
26 | FlumeQueryLinks(indexes, (msg, emit) => { |
27 | var value = unbox(msg) |
28 | if (value) { |
29 | emit(value) |
30 | } |
31 | }, indexVersion) |
32 | ) |
33 | |
34 | return { |
35 | read: function (opts) { |
36 | return pull( |
37 | index.read(opts), |
38 | pull.map(unbox) |
39 | ) |
40 | }, |
41 | |
42 | unbox: function (msgOrData) { |
43 | if (typeof msgOrData === 'string') { |
44 | try { |
45 | var data = ssbKeys.unbox(msgOrData, ssb.keys.private) |
46 | } catch (e) { |
47 | throw explain(e, 'failed to decrypt') |
48 | } |
49 | return data |
50 | } else if (msgOrData && msgOrData.value && msgOrData.value.content === 'string') { |
51 | return unbox(msgOrData) |
52 | } |
53 | }, |
54 | |
55 | publish: function (content, recps, cb) { |
56 | try { |
57 | var ciphertext = ssbKeys.box(content, recps) |
58 | } catch (e) { |
59 | return cb(explain(e, 'failed to encrypt')) |
60 | } |
61 | ssb.publish(ciphertext, cb) |
62 | } |
63 | } |
64 | |
65 | function unbox (msg) { |
66 | if (typeof msg.value.content === 'string') { |
67 | var value = unboxValue(msg.value) |
68 | if (value) { |
69 | return { |
70 | key: msg.key, value: value, timestamp: msg.timestamp |
71 | } |
72 | } |
73 | } |
74 | } |
75 | |
76 | function unboxValue (value) { |
77 | var plaintext = null |
78 | try { |
79 | plaintext = ssbKeys.unbox(value.content, ssb.keys.private) |
80 | } catch (ex) {} |
81 | if (!plaintext) return null |
82 | return { |
83 | previous: value.previous, |
84 | author: value.author, |
85 | sequence: value.sequence, |
86 | timestamp: value.timestamp, |
87 | hash: value.hash, |
88 | content: plaintext, |
89 | private: true |
90 | } |
91 | } |
92 | } |
93 |
Built with git-ssb-web