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