git ssb

0+

Dominic / ssb-peer-invites



Tree: 391163e63d7f2d8f4b5c8568fb7bfe8c355a54d0

Files: 391163e63d7f2d8f4b5c8568fb7bfe8c355a54d0 / test / accept.js

2610 bytesRaw
1var crypto = require('crypto')
2var createClient = require('ssb-client')
3var ssbKeys = require('ssb-keys')
4var tape = require('tape')
5var pull = require('pull-stream')
6
7var I = require('../valid')
8
9var createSbot = require('ssb-server')
10 .use(require('ssb-links'))
11 .use({
12 name: 'replicate', version: '1.0.0',
13 manifest: { request: 'sync' },
14 init: function () {
15 return { request: function () {} }
16 }
17 })
18 .use(require('ssb-friends'))
19 .use(require('../'))
20
21var caps = require('./randcaps')()
22
23var alice = createSbot({
24 temp: true,
25 timeout: 1000,
26 port: 12342,
27 keys:ssbKeys.generate(),
28 caps: caps
29})
30var bob = createSbot({
31 temp: true,
32 timeout: 1000,
33 port: 12343,
34 keys:ssbKeys.generate(),
35 caps: caps
36})
37
38tape('create an invite', function (t) {
39
40 var seed = crypto.randomBytes(32)
41
42 var content = I.createInvite(seed, alice.id, {name: 'bob'}, {text: 'welcome to ssb!'}, caps)
43 alice.publish(content, function (err, msg) {
44 I.verifyInvitePublic(msg.value, caps)
45
46 createClient(
47 ssbKeys.generate(null, seed),
48 {
49 remote: alice.getAddress('device') || alice.getAddress('device'),
50 caps: caps,
51 manifest: {
52 peerInvites: {
53 getInvite: 'async',
54 confirm: 'async'
55 }
56 }
57 },
58 function (err, _bob) {
59 if(err) throw err
60 _bob.peerInvites.getInvite(msg.key, function (err, invite) {
61 if(err) throw err
62 t.ok(invite)
63 t.deepEqual(invite, msg.value)
64 //check this invite is valid. would throw if it wasn't.
65 I.verifyInvitePrivate(invite, seed, caps)
66
67 //bob chooses to accept this invite.
68 var accept_content = I.createAccept(invite, seed, bob.id, caps)
69
70 bob.publish(accept_content, function (err, accept) {
71 if(err) throw err
72 _bob.peerInvites.confirm(accept.value, function (err, msg) {
73 if(err) throw err
74 t.ok(msg)
75 var confirm_id = '%'+ssbKeys.hash(JSON.stringify(msg, null, 2))
76 alice.get(confirm_id, function (err, _msg) {
77 if(err) throw err
78 t.deepEqual(msg, _msg)
79
80
81 //calling accept again should return the previous accept message.
82 _bob.peerInvites.confirm(accept.value, function (err, msg2) {
83 if(err) throw err
84 t.deepEqual(msg2, msg)
85 alice.close()
86 bob.close()
87 t.end()
88 })
89 })
90 })
91 })
92 })
93 }
94 )
95 })
96})
97
98

Built with git-ssb-web