git ssb

0+

Dominic / ssb-peer-invites



Tree: db9daf227bf427c246cd50b93653cdb076843279

Files: db9daf227bf427c246cd50b93653cdb076843279 / types.js

1984 bytesRaw
1var ref = require('ssb-ref')
2var caps = {invite: require('./cap')}
3var ssbKeys = require('ssb-keys')
4var createIsBase64 = require('is-canonical-base64')
5
6function isObject (o) {
7 return o && 'object' === typeof o
8}
9
10// signatures have a type (eg `.ed25519`) at the end,
11// but not gonna check it right here.
12var signature_rx = createIsBase64('', '\\.sig\\.\\w+')
13var box_rx = createIsBase64()
14
15function isSignature(b) {
16 return signature_rx.test(b)
17}
18
19function isMaybeBase64(b) {
20 return b === undefined || box_rx.test(b)
21}
22
23exports.isInvite = function (msg, caps) {
24 if(!isObject(caps)) throw new Error('caps must be provided')
25 //return true
26 return isObject(msg) && isObject(msg.content) && (
27 'user-invite' === msg.content.type &&
28 ref.isFeed(msg.content.host) &&
29 ref.isFeed(msg.content.invite) &&
30 isMaybeBase64(msg.content.reveal) &&
31 isMaybeBase64(msg.content.public) &&
32 // signature must be valid !!!
33 ssbKeys.verifyObj(msg.content.invite, caps.userInvite, msg.content)
34 )
35}
36
37exports.isAccept = function (msg) {
38 return isObject(msg) && isObject(msg.content) && (
39 'user-invite/accept' === msg.content.type &&
40 msg.content.id == msg.author &&
41 ref.isMsg(msg.content.receipt) &&
42 isMaybeBase64(msg.content.key) &&
43 // can't verify this without having the invite message.
44 // (that's intentional, forces implementers not to cut corners,
45 // but to check that the receipt is correct)
46 isSignature(msg.content.signature)
47 )
48}
49
50exports.isConfirm = function (msg) {
51 return isObject(msg) && isObject(msg.content) && (
52 'user-invite/confirm' === msg.content.type &&
53 exports.isAccept(msg.content.embed) &&
54 //second pointer back to receipt, so that links can find it
55 //(since it unfortunately does not handle links nested deeper
56 //inside objects. when we look up the message,
57 //confirm that content.embed.content.receipt is the same)
58 msg.content.embed.content.receipt === msg.content.receipt
59 )
60}
61
62
63
64

Built with git-ssb-web