-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
86 lines (77 loc) · 2.75 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
module.exports = Bugoff
const crypto = require('crypto')
const { SEA } = require('gun')
const Bugout = require('bugout')
const events = require('events')
function Bugoff(identifier, opts) {
this.events = new events.EventEmitter()
this.ID = sha(identifier)
this.identifier = this.ID
this.opts = opts
this.bugout = new Bugout(identifier, opts)
this.address = this.bugout.address()
this.peers = {}
this.SEA = async (pair) => {
this.sea = pair || await SEA.pair()
return this.sea
}
// Bugout internals bindings
this.on = this.bugout.on.bind(this.bugout)
this.once = this.bugout.once.bind(this.bugout)
this.register = this.bugout.register.bind(this.bugout)
this.rpc = this.bugout.rpc.bind(this.bugout)
this.heartbeat = (interval) => {
// Hearbeat patch while waiting for Bugout update in NPM
return this.bugout.heartbeat(interval)
}
this.destroy = this.bugout.destroy.bind(this.bugout)
// Bugoff
this.events.on('encoded', encrypted => {
if(typeof encrypted === 'object') this.bugout.send(encrypted[0], encrypted[1])
else this.bugout.send(address, encrypted)
})
this.on('message', async (address, message) => {
let decrypted = await decrypt(address, message)
let addr = await decrypted.address
let pubkeys = await decrypted.pubkeys
let msg = await decrypted.message
if(decrypted) this.bugout.emit('decrypted', addr, pubkeys, msg)
})
let encrypt = async (address, message) => {
await new Promise(resolve => this.events.once('newPeer', resolve))
if(!message) {
msg = address
// this is a broadcast message, encrypt with this instance SEA pair
for(peer in this.peers){
let enc = [peer, await SEA.encrypt(msg, await SEA.secret(this.peers[peer].epub, this.sea))]
this.events.emit('encoded', enc)
}
} else
if(message){
// this is a direct message
let enc = await SEA.encrypt(message, await SEA.secret(this.peers[address].epub, this.sea))
this.events.emit('encoded', [address, enc])
}
}
this.send = encrypt
let decrypt = async (address, message) => {
let pubkeys
for(peer in this.peers){
if(peer === address){
pubkeys = {pub: this.peers[peer].pub, epub: this.peers[peer].epub}
}
}
let dec = await { address: address, pubkeys: pubkeys, message: SEA.decrypt(message, await SEA.secret(this.peers[address].epub, this.sea)) }
return dec
}
this.register('peer', (address, sea, cb) =>{
Object.assign(this.peers, {[address]:{pub: sea.pub, epub: sea.epub}})
this.events.emit('newPeer', this.peers)
})
this.on('seen', async address => {
this.rpc(address, 'peer', await this.sea)
})
function sha(input){
return crypto.createHash('sha256').update(JSON.stringify(input)).digest('hex')
}
}