Files: e36c17319f691d90c34fade06cc2dda223f17485 / index.js
1322 bytesRaw
1 | var muxrpc = require('muxrpc') |
2 | var Serializer = require('pull-serializer') |
3 | var chan = require('ssb-channel') |
4 | var ssbKeys = require('ssb-keys') |
5 | var manifest = require('ssb-manifest') |
6 | var createMsg = require('secure-scuttlebutt/message')(require('secure-scuttlebutt/defaults')) |
7 | |
8 | module.exports = function (keys, address) { |
9 | var rpc = muxrpc(manifest, false, serialize)() |
10 | var client = chan.connect(ssb, address) |
11 | client.keys = keys |
12 | |
13 | copyApi(manifest, rpc, client) |
14 | |
15 | client.add = function (content, cb) { |
16 | // :TODO: get prev |
17 | var prev = null |
18 | var msg = createMsg(keys, null, content, prev) |
19 | rpc.add(msg, cb) |
20 | } |
21 | |
22 | client.auth = function (cb) { |
23 | var authReq = ssbKeys.signObj(keys, { |
24 | role: 'client', |
25 | ts: Date.now(), |
26 | public: keys.public |
27 | }) |
28 | rpc.auth(authReq, cb) |
29 | } |
30 | |
31 | client.on('connect', function () { |
32 | client.auth(function (err) { |
33 | if (err) |
34 | client.emit('error', err) |
35 | else |
36 | client.emit('authed') |
37 | }) |
38 | }) |
39 | |
40 | return client |
41 | } |
42 | |
43 | function serialize (stream) { |
44 | return Serializer(stream, JSON, {split: '\n\n'}) |
45 | } |
46 | |
47 | function copyApi (manifest, src, dst) { |
48 | for (var k in manifest) { |
49 | if (typeof manifest[k] == 'object') { |
50 | dst[k] = {} |
51 | copyApi(manifest[k], src[k], dst[k]) |
52 | } else { |
53 | dst[k] = src[k] |
54 | } |
55 | } |
56 | } |
Built with git-ssb-web