git ssb

2+

Dominic / ssb-client



Tree: e36c17319f691d90c34fade06cc2dda223f17485

Files: e36c17319f691d90c34fade06cc2dda223f17485 / index.js

1322 bytesRaw
1var muxrpc = require('muxrpc')
2var Serializer = require('pull-serializer')
3var chan = require('ssb-channel')
4var ssbKeys = require('ssb-keys')
5var manifest = require('ssb-manifest')
6var createMsg = require('secure-scuttlebutt/message')(require('secure-scuttlebutt/defaults'))
7
8module.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
43function serialize (stream) {
44 return Serializer(stream, JSON, {split: '\n\n'})
45}
46
47function 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