git ssb

0+

Dominic / ssb-peer-invites



Tree: 5f50cce8f8b985aae2b4894160b4ba9751ca8b0f

Files: 5f50cce8f8b985aae2b4894160b4ba9751ca8b0f / util.js

1253 bytesRaw
1var chloride = require('chloride')
2
3exports.box = function box (data, key) {
4 if(!data) return
5 var b = Buffer.from(JSON.stringify(data))
6 return chloride.crypto_secretbox_easy(b, key.slice(0, 24), key).toString('base64')
7}
8
9exports.unbox = function unbox (ctxt, key) {
10 var b = Buffer.from(ctxt, 'base64')
11 var ptxt = chloride.crypto_secretbox_open_easy(b, key.slice(0, 24), key)
12 if(!ptxt) return
13 try {
14 return JSON.parse(ptxt)
15 } catch(err) {
16 console.error(err)
17 }
18}
19
20exports.hash = function hash (s) {
21 return chloride.crypto_hash_sha256(
22 'string' === typeof s ? Buffer.from(s, 'utf8') : s
23 )
24}
25
26exports.parse = function parse (str) {
27 if(!/^inv\:/.test(str)) throw new Error('invites must start with "inv:", got '+JSON.stringify(str))
28 var ary = str.substring(4).split(',')
29 return {
30 seed: ary[0],
31 invite: ary[1],
32 cap: ary[2],
33 pubs: ary.slice(3)
34 }
35}
36exports.stringify = function stringify (invite) {
37 return 'inv:'+[
38 invite.seed,
39 invite.invite,
40 invite.cap || ''
41 ]
42 .concat(invite.pubs)
43 .join(',')
44}
45
46exports.sort = function sort (found) {
47 return found.sort(function (a, b) {
48 return (!!b.willReplicate) - (!!a.willReplicate) || (b.availability - a.availability)
49 })
50}
51

Built with git-ssb-web