git ssb

2+

Dominic / ssb-client



Commit efa2e64385b5a9b4754d14271c1a1e14883f7e58

Merge pull request #5 from ssbc/no-no-crypto

Remove crypto from ssb-client repo, use existing libraries instead
Paul Frazee committed on 3/6/2015, 9:57:57 PM
Parent: a2def96f193dc31487faf92e6066dfd427c9d41f
Parent: 60721b17e91fae380c408e216a614944d0226a8e

Files changed

README.mdchanged
index.jschanged
package.jsonchanged
test/index.jschanged
README.mdView
@@ -3,29 +3,40 @@
33 [scuttlebot](https://github.com/ssbc/scuttlebot) client
44
55 ```js
66
7-var Client = require('./')
8-
9-var config = require('ssb-config')
7 +var Client = require('ssb-client')
108 var ssbKeys = require('ssb-keys')
11-var keys = ssbKeys.loadOrCreateSync(config)
129
13-function abortIf (err) {
14- if(err) throw err
10 +// desktop app:
11 +var keys = ssbKeys.loadOrCreateSync('./app-private.key')
12 +
13 +// web app:
14 +var keys
15 +try {
16 + keys = JSON.parse(localStorage.keys)
17 +} catch (e) {
18 + keys = ssbKeys.generate()
19 + localStorage.keys = JSON.stringify(keys)
1520 }
1621
1722 var client = Client(keys, config)
18- .connect(abortIf) //auth is automatic
23 + .connect(abortIf)
24 + .auth(ssbKeys.createAuth(keys), abortIf)
1925
20-client.publish({
26 +var feed = client.createFeed(keys)
27 +feed.add({
2128 type: 'post', text: 'hello, world!'
2229 }, function (err, msg) {
2330 abortIf(err)
2431 console.log(msg)
2532 client.close()
2633 })
2734
35 +function abortIf (err) {
36 + if(err) throw err
37 +}
38 +
2839 ```
2940
3041 ## License
3142
index.jsView
@@ -1,12 +1,13 @@
1-var pull = require('pull-stream')
2-var muxrpc = require('muxrpc')
3-var address = require('ssb-address')
4-var ws = require('pull-ws-server')
5-var Serializer = require('pull-serializer')
6-var ssbKeys = require('ssb-keys')
7-var loadManf = require('ssb-manifest/load')
8-var createMsg = require('secure-scuttlebutt/message')(require('secure-scuttlebutt/defaults'))
1 +var pull = require('pull-stream')
2 +var muxrpc = require('muxrpc')
3 +var address = require('ssb-address')
4 +var ws = require('pull-ws-server')
5 +var Serializer = require('pull-serializer')
6 +var ssbKeys = require('ssb-keys')
7 +var loadManf = require('ssb-manifest/load')
8 +var ssbFeed = require('secure-scuttlebutt/feed')
9 +var ssbDefaults = require('secure-scuttlebutt/defaults')
910
1011 function isFunction (f) {
1112 return 'function' === typeof f
1213 }
@@ -36,39 +37,26 @@
3637 if(isFunction(addr))
3738 cb = addr, addr = null
3839
3940 addr = address(addr || config)
41 + client.addr = addr
4042 if (wsStream) {
4143 wsStream.close()
4244 client._emit('reconnecting')
4345 }
4446
4547 var called = false
46-
47- client.addr = addr
48-
49- //if auth is not the first method called,
50- //then the other methods will get auth errors.
51- //since rpc calls are queued, we can just do it here.
52- client.auth(function (err, authed) {
53- if (err)
54- client._emit('error', err)
55- else
56- client._emit('authed', authed)
57- if(called) return
58- called = true; cb && cb(err, authed)
59- })
60-
6148 wsStream = ws.connect(addr, {
6249 onOpen: function() {
6350 client._emit('connect')
64- //cb is called after auth, just above
51 + if(called) return
52 + called = true; cb && cb()
6553 },
6654 onClose: function() {
6755 client._emit('close')
6856 //rpcStream will detect close on it's own.
6957 if(called) return
70- called = true; cb && cb(err, authed)
58 + called = true; cb && cb()
7159 }
7260 })
7361
7462 rpcStream = client.createStream()
@@ -93,40 +81,12 @@
9381 })
9482 return client
9583 }
9684
97- client.publish = function (content, cb) {
98- client.getLatest(client.keys.id, function (err, prev) {
99- if (!prev) {
100- var init = createMsg(client.keys, null, { type: 'init', public: client.keys.public }, null)
101- client.add(init, function (err, res) {
102- if (err)
103- return cb(err)
104- prev = res.value
105- next()
106- })
107- } else
108- next()
109-
110- function next () {
111- var msg = createMsg(client.keys, null, content, prev||null)
112- client.add(msg, cb)
113- }
114- })
115- return client
85 + client.createFeed = function (keys) {
86 + return ssbFeed(this, keys, ssbDefaults)
11687 }
11788
118- var auth_ = client.auth
119- client.auth = function (cb) {
120- var authReq = ssbKeys.signObj(client.keys, {
121- role: 'client',
122- ts: Date.now(),
123- public: client.keys.public
124- })
125- auth_.call(client, authReq, cb)
126- return client
127- }
128-
12989 return client
13090 }
13191
13292 function serialize (stream) {
package.jsonView
@@ -16,9 +16,9 @@
1616 "pull-serializer": "~0.3.2",
1717 "pull-ws-server": "~1.3.0",
1818 "secure-scuttlebutt": "~9.0.1",
1919 "ssb-address": "~1.0.0",
20- "ssb-keys": "~0.5.0",
20 + "ssb-keys": "~0.6.0",
2121 "ssb-manifest": "~1.1.0",
2222 "ssb-config": "~1.0.0"
2323 },
2424 "devDependencies": {
test/index.jsView
@@ -10,18 +10,23 @@
1010 var server = scuttlebot({ port: 45451, host: 'localhost' }, ssb, ssb.createFeed()).use(require('scuttlebot/plugins/logging'))
1111
1212 var keys = ssbkeys.generate()
1313 var client = ssbclient(keys)
14- client.connect({ port: 45451, host: 'localhost' }, function (err) {
15- if (err)
16- throw err
17- })
18- client.publish({type: 'post', text: 'hello'}, function (err, data) {
19- if(err) throw err
14 + client.connect({ port: 45451, host: 'localhost' }, iferr)
15 + client.auth(ssbkeys.createAuth(keys), iferr)
16 +
17 + var feed = client.createFeed(keys)
18 + feed.add({type: 'post', text: 'hello'}, function (err, data) {
19 + iferr(err)
2020 t.equal(data.value.content.text, 'hello')
2121 console.log(data)
2222 client.close(function() {
2323 server.close()
2424 t.end()
2525 })
2626 })
27 +
28 + function iferr (err) {
29 + if (err)
30 + throw err
31 + }
2732 })

Built with git-ssb-web