git ssb

2+

Dominic / ssb-client



Commit e36c17319f691d90c34fade06cc2dda223f17485

initial commit

Paul Frazee committed on 3/4/2015, 3:00:01 AM

Files changed

README.mdadded
index.jsadded
package.jsonadded
README.mdView
@@ -1,0 +1,42 @@
1 +# ssb-client v1
2 +
3 +[scuttlebot](https://github.com/ssbc/scuttlebot) client
4 +
5 +```js
6 +var pull = require('pull-stream')
7 +var ssbkeys = require('ssb-keys')
8 +var ssbclient = require('ssb-client')
9 +
10 +// generate a new keypair
11 +var keys = ssbkeys.generate()
12 +
13 +// connect to the local scuttlebot
14 +var client = ssbclient(keys, 'localhost')
15 +
16 +// listen to connection events
17 +client.on('connect', function () {
18 + console.log('connection established, authenticating...')
19 +})
20 +client.on('authed', function () {
21 + console.log('ready to go')
22 + doWork()
23 +})
24 +client.on('error', function (err) {
25 + // connection lost
26 + console.log('connection or authentication error', err)
27 + client.reconnect({ wait: 5000 })
28 +})
29 +client.on('reconnecting', function () {
30 + console.log('attempting reconnect...')
31 +})
32 +
33 +// make calls to the scuttlebot api
34 +function doWork() {
35 + pull(client.createFeedStream(), pull.drain(console.log))
36 + client.add({ type: 'post', text: 'hello, world!' }, console.log)
37 +}
38 +```
39 +
40 +## License
41 +
42 +MIT, Copyright 2015 Paul Frazee and Dominic Tarr
index.jsView
@@ -1,0 +1,56 @@
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 +}
package.jsonView
@@ -1,0 +1,27 @@
1 +{
2 + "name": "ssb-client",
3 + "version": "1.0.0",
4 + "description": "scuttlebot client",
5 + "main": "index.js",
6 + "scripts": {
7 + "test": "echo \"Error: no test specified\" && exit 1"
8 + },
9 + "repository": {
10 + "type": "git",
11 + "url": "https://github.com/ssbc/ssb-client.git"
12 + },
13 + "dependencies": {
14 + "muxrpc": "~3.3.1",
15 + "pull-serializer": "~0.3.2",
16 + "secure-scuttlebutt": "~9.0.1",
17 + "ssb-channel": "~1.0.2",
18 + "ssb-keys": "~0.4.1",
19 + "ssb-manifest": "~1.0.0"
20 + },
21 + "author": "Paul Frazee <pfrazee@gmail.com>",
22 + "license": "MIT",
23 + "bugs": {
24 + "url": "https://github.com/ssbc/ssb-client/issues"
25 + },
26 + "homepage": "https://github.com/ssbc/ssb-client"
27 +}

Built with git-ssb-web