-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (80 loc) · 2.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var debug = require('debug')('meta-swarm')
var events = require('events')
var inherits = require('inherits')
var Meta = require('meta-swarm')
var nacl = require('tweetnacl')
var swarm = require('secure-webrtc-swarm')
inherits(Connect, events.EventEmitter)
module.exports = Connect
function Connect (keyPair, whitelist, hubs, opts) {
if (!(this instanceof Connect)) return new Connect(opts)
if (!opts) opts = {}
this.hubs = hubs || []
if (!keyPair) {
keyPair = nacl.box.keyPair()
keyPair = {
secretKey: nacl.util.encodeBase64(keyPair.secretKey),
publicKey: nacl.util.encodeBase64(keyPair.publicKey)
}
}
this.keyPair = keyPair
this.issuedInvites = opts.issuedInvites || []
this.receivedInvites = opts.receivedInvites || {}
this.opts = opts.opts || {}
this.opts.keyPair = this.keyPair
this.opts.issuedInvites = opts.issuedInvites || []
this.opts.receivedInvites = opts.receivedInvites || {}
this.opts.namespace = opts.namespace || 'peermusic'
this.opts.whitelist = whitelist || []
this._init()
}
Connect.prototype._init = function () {
var events = ['connect', 'disconnect']
Object.assign(this.opts, {events})
this.metaSwarm = new Meta(swarm, this.hubs, this.opts)
if (!this.metaSwarm.opts.issuedInvites) {
this.metaSwarm.opts.issuedInvites = []
}
if (!this.metaSwarm.opts.receivedInvites) {
this.metaSwarm.opts.receivedInvites = {}
}
}
Connect.prototype.issueInvite = function (hub) {
if (!hub) throw new Error('hub needed')
var signKeyPair = nacl.sign.keyPair()
var signPrivKey = nacl.util.encodeBase64(signKeyPair.secretKey)
var signPubKey = nacl.util.encodeBase64(signKeyPair.publicKey)
// var myPubKey = nacl.util.encodeBase64(o2a(keyPair.publicKey))
var myPubKey = this.keyPair.publicKey
if (this.hubs.indexOf(hub) === -1) {
this.metaSwarm.addHub(hub)
this.hubs.push(hub)
}
this.issuedInvites.push(signPubKey)
this.metaSwarm.swarms[hub].issuedInvites.push(signPubKey)
return [signPubKey,
'web+peermusic://INVITE#' + hub + '#' + myPubKey + '#' + signPrivKey]
}
Connect.prototype.receiveInvite = function (uri) {
function parseInvite (uri) {
// format: web+peermusic://INVITE#host:port#boxPubkey#signPrivKey
var parts = uri.split('#')
var hub = parts[1]
var pubKey = parts[2]
var signPrivKey = parts[3]
return [hub, pubKey, {[pubKey]: signPrivKey}]
}
var invite = parseInvite(uri)
if (this.hubs.indexOf(invite[0]) === -1) {
debug('skipping already known hub')
this.metaSwarm.addHub(invite[0])
this.hubs.push(invite[0])
}
if (this.receivedInvites[invite[1]]) {
debug('we already received an invite from that peer but continue')
}
debug('adding new invite', invite[2])
this.receivedInvites = Object.assign(this.receivedInvites, invite[2])
this.metaSwarm.swarms[invite[0]].receivedInvites = this.receivedInvites
return invite
}