-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
109 lines (105 loc) · 3.22 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
100
101
102
103
104
105
106
107
108
109
import crypto from 'crypto'
import Gun from 'gun'
import SEA from 'gun/sea.js'
import Pair from './pair.js'
Gun.chain.gunsafe = function(opts) {
const gun = this
let pair
gun.gunsafe = {
name: async (key, name) => {
let hash = crypto.createHash('SHA256').update(name).digest('hex')
pair = await Pair(key, hash)
gun.user().auth(pair)
},
put: async (name, data) => {
if(typeof data === 'object') data = JSON.stringify(data)
if(typeof data === 'string') data = await SEA.encrypt(data, pair)
gun.user().get('gunsafe').get('items').get(name).put(data)
gun.user().get('gunsafe').get('list').set(name)
},
get: async (name, run, global, cb) => {
gun.user().get('gunsafe').get('items').get(name).once(async data => {
if(!data) return cb('Record not found')
data = await SEA.decrypt(data, pair)
try { data = data.join(' '); if(!run && !global) cb(data) }
catch (err){if(err){}}
try { data = JSON.parse(data) }
catch (err){if(err){}}
if(typeof data === 'object'){
let index = Object.keys(data)
let str
for(let i in index){
if(data[index[i]]){
str = str + data[index[i]] + '\r\n'
}
}
str = str.substring(9)
data = str
}
if(run){
try{
if(global === false) {
let fn = new Function(data);
fn()
}
else eval(data)
} catch { cb(data) }
} else {
cb(data)
}
gun.user().get('gunsafe').get('items').get(name).off()
})
},
list: async (del, cb) => {
let last = []
gun.user().get('gunsafe').get('list').map().once(data => {
if(last.includes(data)) return
gun.user().get('gunsafe').get('items').get(data).once(d => {
if(d === null && del) { cb('[ deleted ] ' + data) }
else if(d !== null && !del){ cb(data) }
})
last.push(data)
})
},
delete: async (name) => {
if(!name) {
gun.user().get('gunsafe').put(null)
gun.user().get('gunsafe').get('list').map().once(data => {
gun.user().get('gunsafe').get('items').get(data).put(null)
gun.user().get('gunsafe').get('list').put(null)
})
} else {
gun.user().get('gunsafe').get('items').get(name).put(null)
}
},
peers: (peers) => {
if(peers && typeof peers === 'object') gun.opt({ peers: peers })
if(peers === false){
gun.back('opt.peers')
gun._.opt.peers = {}
}
if(!peers) return gun._.opt.peers
},
key: () => {
return pair
},
pair: async (epriv) => {
if(!epriv){
let keys = await SEA.pair()
let encryptedKeys = await SEA.encrypt(pair, keys.epriv)
gun.get('gunsafe').get('pair').put(encryptedKeys)
return keys.epriv
} else {
gun.get('gunsafe').get('pair').once(async data => {
gun.user().leave()
data = await SEA.decrypt(data, epriv)
gun.user().auth(data, ack => {})
gun.on('auth', ack => {
pair = ack.sea
})
})
}
}
}
return gun
}