Commit efa2e64385b5a9b4754d14271c1a1e14883f7e58
Merge pull request #5 from ssbc/no-no-crypto
Remove crypto from ssb-client repo, use existing libraries insteadPaul Frazee committed on 3/6/2015, 9:57:57 PM
Parent: a2def96f193dc31487faf92e6066dfd427c9d41f
Parent: 60721b17e91fae380c408e216a614944d0226a8e
Files changed
README.md | changed |
index.js | changed |
package.json | changed |
test/index.js | changed |
README.md | ||
---|---|---|
@@ -3,29 +3,40 @@ | ||
3 | 3 … | [scuttlebot](https://github.com/ssbc/scuttlebot) client |
4 | 4 … | |
5 | 5 … | ```js |
6 | 6 … | |
7 | -var Client = require('./') | |
8 | - | |
9 | -var config = require('ssb-config') | |
7 … | +var Client = require('ssb-client') | |
10 | 8 … | var ssbKeys = require('ssb-keys') |
11 | -var keys = ssbKeys.loadOrCreateSync(config) | |
12 | 9 … | |
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) | |
15 | 20 … | } |
16 | 21 … | |
17 | 22 … | var client = Client(keys, config) |
18 | - .connect(abortIf) //auth is automatic | |
23 … | + .connect(abortIf) | |
24 … | + .auth(ssbKeys.createAuth(keys), abortIf) | |
19 | 25 … | |
20 | -client.publish({ | |
26 … | +var feed = client.createFeed(keys) | |
27 … | +feed.add({ | |
21 | 28 … | type: 'post', text: 'hello, world!' |
22 | 29 … | }, function (err, msg) { |
23 | 30 … | abortIf(err) |
24 | 31 … | console.log(msg) |
25 | 32 … | client.close() |
26 | 33 … | }) |
27 | 34 … | |
35 … | +function abortIf (err) { | |
36 … | + if(err) throw err | |
37 … | +} | |
38 … | + | |
28 | 39 … | ``` |
29 | 40 … | |
30 | 41 … | ## License |
31 | 42 … |
index.js | |||
---|---|---|---|
@@ -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') | ||
9 | 10 … | ||
10 | 11 … | function isFunction (f) { | |
11 | 12 … | return 'function' === typeof f | |
12 | 13 … | } | |
@@ -36,39 +37,26 @@ | |||
36 | 37 … | if(isFunction(addr)) | |
37 | 38 … | cb = addr, addr = null | |
38 | 39 … | ||
39 | 40 … | addr = address(addr || config) | |
41 … | + client.addr = addr | ||
40 | 42 … | if (wsStream) { | |
41 | 43 … | wsStream.close() | |
42 | 44 … | client._emit('reconnecting') | |
43 | 45 … | } | |
44 | 46 … | ||
45 | 47 … | 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 | - | ||
61 | 48 … | wsStream = ws.connect(addr, { | |
62 | 49 … | onOpen: function() { | |
63 | 50 … | client._emit('connect') | |
64 | - //cb is called after auth, just above | ||
51 … | + if(called) return | ||
52 … | + called = true; cb && cb() | ||
65 | 53 … | }, | |
66 | 54 … | onClose: function() { | |
67 | 55 … | client._emit('close') | |
68 | 56 … | //rpcStream will detect close on it's own. | |
69 | 57 … | if(called) return | |
70 | - called = true; cb && cb(err, authed) | ||
58 … | + called = true; cb && cb() | ||
71 | 59 … | } | |
72 | 60 … | }) | |
73 | 61 … | ||
74 | 62 … | rpcStream = client.createStream() | |
@@ -93,40 +81,12 @@ | |||
93 | 81 … | }) | |
94 | 82 … | return client | |
95 | 83 … | } | |
96 | 84 … | ||
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) | ||
116 | 87 … | } | |
117 | 88 … | ||
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 | - | ||
129 | 89 … | return client | |
130 | 90 … | } | |
131 | 91 … | ||
132 | 92 … | function serialize (stream) { |
package.json | ||
---|---|---|
@@ -16,9 +16,9 @@ | ||
16 | 16 … | "pull-serializer": "~0.3.2", |
17 | 17 … | "pull-ws-server": "~1.3.0", |
18 | 18 … | "secure-scuttlebutt": "~9.0.1", |
19 | 19 … | "ssb-address": "~1.0.0", |
20 | - "ssb-keys": "~0.5.0", | |
20 … | + "ssb-keys": "~0.6.0", | |
21 | 21 … | "ssb-manifest": "~1.1.0", |
22 | 22 … | "ssb-config": "~1.0.0" |
23 | 23 … | }, |
24 | 24 … | "devDependencies": { |
test/index.js | ||
---|---|---|
@@ -10,18 +10,23 @@ | ||
10 | 10 … | var server = scuttlebot({ port: 45451, host: 'localhost' }, ssb, ssb.createFeed()).use(require('scuttlebot/plugins/logging')) |
11 | 11 … | |
12 | 12 … | var keys = ssbkeys.generate() |
13 | 13 … | 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) | |
20 | 20 … | t.equal(data.value.content.text, 'hello') |
21 | 21 … | console.log(data) |
22 | 22 … | client.close(function() { |
23 | 23 … | server.close() |
24 | 24 … | t.end() |
25 | 25 … | }) |
26 | 26 … | }) |
27 … | + | |
28 … | + function iferr (err) { | |
29 … | + if (err) | |
30 … | + throw err | |
31 … | + } | |
27 | 32 … | }) |
Built with git-ssb-web