git ssb

0+

Dominic / ssb-peer-invites



Tree: 391163e63d7f2d8f4b5c8568fb7bfe8c355a54d0

Files: 391163e63d7f2d8f4b5c8568fb7bfe8c355a54d0 / types.js

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

Built with git-ssb-web