git ssb

0+

Dominic / ssb-peer-invites



Commit 12ee744340c9fc9639316ac7656ef5324003cb16

return and accept stringifies invites

Dominic Tarr committed on 12/23/2018, 2:08:09 AM
Parent: fe3ae12adbf9dfc9bd4146fa5708fee8f9297e46

Files changed

index.jschanged
util.jschanged
index.jsView
@@ -6,10 +6,10 @@
66 var paramap = require('pull-paramap')
77 var ssbClient = require('ssb-client')
88 var crypto = require('crypto')
99 var ssbKeys = require('ssb-keys')
10 +var u = require('./util')
1011
11-
1212 function code(err, c) {
1313 err.code = 'user-invites:'+c
1414 return err
1515 }
@@ -25,8 +25,12 @@
2525 function isObject (o) {
2626 return o && typeof o == 'object'
2727 }
2828
29 +function isString (s) {
30 + return typeof s == 'string'
31 +}
32 +
2933 function toBuffer(b) {
3034 return Buffer.isBuffer(b) ? b : Buffer.from(b, 'base64')
3135 }
3236
@@ -323,13 +327,15 @@
323327 id: host_id,
324328 content: I.createInvite(seed, host_id, opts.reveal, opts.private, caps)
325329 }, function (err, data) {
326330 if(err) return cb(err)
327- cb(null, {
331 + var invite = {
328332 seed: seed,
329333 invite: data.key,
334 + cap: opts.cap,
330335 pubs: near.map(function (e) { return e.address }),
331- })
336 + }
337 + cb(null, u.stringify(invite))
332338 })
333339 })
334340 }
335341
@@ -364,8 +370,9 @@
364370
365371 //TODO: check if invite is already held locally
366372 // if so, just get it. when got, update local db.
367373 invites.openInvite = function (invite, cb) {
374 + if(isString(invite)) invite = u.parseInvite(invite)
368375 invites.getInvite(invite.invite, function (err, msg) {
369376 if(msg)
370377 next(msg)
371378 else
@@ -395,8 +402,9 @@
395402 })
396403 }
397404
398405 invites.acceptInvite = function (opts, cb) {
406 + if(isString(opts)) opts = u.parseInvite(opts)
399407 var invite = isObject(opts.invite) ? opts.invite : opts
400408 var invite_id = invite.invite
401409 var id = opts.id || sbot.id
402410 var pubs = invite.pubs
util.jsView
@@ -26,4 +26,24 @@
2626
2727 exports.hash = hash
2828 exports.box = box
2929 exports.unbox = unbox
30 +exports.parse = function (str) {
31 + if(!/^inv\:/.test(str)) throw new Error('invites must start with "inv:", got '+JSON.stringify(str))
32 + var ary = str.substring(4).split(',')
33 + return {
34 + seed: ary[0],
35 + invite: ary[1],
36 + cap: ary[2],
37 + pubs: ary.slice(3)
38 + }
39 +}
40 +exports.stringify = function (invite) {
41 + return 'inv:'+[
42 + invite.seed,
43 + invite.invite,
44 + invite.cap || ''
45 + ]
46 + .concat(invite.pubs)
47 + .join(',')
48 +}
49 +

Built with git-ssb-web