Files: 8dc25143a3f37f1618f7a92b3c46dec512b43bfd / modules / crypto.js
1325 bytesRaw
1 | var ref = require('ssb-ref') |
2 | var keys = require('../keys') |
3 | var ssbKeys = require('ssb-keys') |
4 | |
5 | function unbox_value(msg) { |
6 | var plaintext = ssbKeys.unbox(msg.content, keys) |
7 | if(!plaintext) return null |
8 | return { |
9 | previous: msg.previous, |
10 | author: msg.author, |
11 | sequence: msg.sequence, |
12 | timestamp: msg.timestamp, |
13 | hash: msg.hash, |
14 | content: plaintext, |
15 | private: true |
16 | } |
17 | } |
18 | |
19 | |
20 | module.exports = { |
21 | |
22 | needs: {sbot_publish: 'first'}, |
23 | gives: { |
24 | message_unbox: true, message_box: true, publish: true |
25 | }, |
26 | create: function (api) { |
27 | |
28 | var exports = {} |
29 | exports.message_unbox = function (msg) { |
30 | if(msg.value) { |
31 | var value = unbox_value(msg.value) |
32 | if(value) |
33 | return { |
34 | key: msg.key, value: value, timestamp: msg.timestamp |
35 | } |
36 | } |
37 | else |
38 | return unbox_value(msg) |
39 | } |
40 | |
41 | exports.message_box = function (content) { |
42 | return ssbKeys.box(content, content.recps.map(function (e) { |
43 | return ref.isFeed(e) ? e : e.link |
44 | })) |
45 | } |
46 | |
47 | exports.publish = function (content, cb) { |
48 | if(content.recps) |
49 | content = exports.message_box(content) |
50 | api.sbot_publish(content, function (err, msg) { |
51 | if(err) throw err |
52 | console.log('PUBLISHED', msg) |
53 | if(cb) cb(err, msg) |
54 | }) |
55 | } |
56 | |
57 | return exports |
58 | } |
59 | } |
60 | |
61 |
Built with git-ssb-web