git ssb

2+

Dominic / ssb-client



Tree: 7d728d80bb3a7d1bbd873622563897459e6055fb

Files: 7d728d80bb3a7d1bbd873622563897459e6055fb / index.js

2396 bytesRaw
1'use strict'
2var path = require('path')
3var ssbKeys = require('ssb-keys')
4var explain = require('explain-error')
5var path = require('path')
6var fs = require('fs')
7
8var MultiServer = require('multiserver')
9var WS = require('multiserver/plugins/ws')
10var Net = require('multiserver/plugins/net')
11var Shs = require('multiserver/plugins/shs')
12
13var muxrpc = require('muxrpc')
14var pull = require('pull-stream')
15
16function toSodiumKeys(keys) {
17 if(!keys || !keys.public) return null
18 return {
19 publicKey:
20 new Buffer(keys.public.replace('.ed25519',''), 'base64'),
21 secretKey:
22 new Buffer(keys.private.replace('.ed25519',''), 'base64'),
23 }
24}
25
26var cap =
27 new Buffer('1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=', 'base64')
28
29var createConfig = require('ssb-config/inject')
30
31module.exports = function (keys, opts, cb) {
32 var config
33 if (typeof keys == 'function') {
34 cb = keys
35 keys = null
36 opts = null
37 }
38 else if (typeof opts == 'function') {
39 cb = opts
40 opts = keys
41 keys = null
42 }
43 if(typeof opts === 'string' || opts == null || !keys)
44 config = createConfig(typeof opts === 'string' ? opts : null)
45 else
46 config = {}
47 keys = keys || ssbKeys.loadOrCreateSync(path.join(config.path, 'secret'))
48 opts = opts || {}
49
50 var remote
51 if(opts.remote)
52 remote = opts.remote
53 else {
54 var host = opts.host || 'localhost'
55 var port = opts.port || config.port || 8008
56 var key = opts.key || keys.id
57
58 remote = 'net:'+host+':'+port+'~shs:'+key.substring(1).replace('.ed25519', '')
59 }
60
61 var manifest = opts.manifest || (function () {
62 try {
63 return JSON.parse(fs.readFileSync(
64 path.join(config.path, 'manifest.json')
65 ))
66 } catch (err) {
67 throw explain(err, 'could not load manifest file')
68 }
69 })()
70
71 var shs = Shs({
72 keys: toSodiumKeys(keys),
73 appKey: opts.appKey || cap,
74
75 //no client auth. we can't receive connections anyway.
76 auth: function (cb) { cb(null, false) },
77 timeout: config.timers && config.timers.handshake || 3000
78 })
79
80 var ms = MultiServer([
81 [Net({}), shs],
82 [WS({}), shs]
83 ])
84
85 ms.client(remote, function (err, stream) {
86 if(err) return cb(explain(err, 'could not connect to sbot'))
87 var sbot = muxrpc(manifest, false)()
88 pull(stream, sbot.createStream(), stream)
89 cb(null, sbot)
90 })
91}
92
93

Built with git-ssb-web