-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
82 lines (80 loc) · 2.67 KB
/
data.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
const fs = require('fs')
class Data {
constructor(passed){
this.dht = passed.dht
this.ed = passed.ed
this.keypair = passed.redo ? this.ed.keygen(Buffer.from(passed.key)) : this.ed.keygen()
this.seq = passed.redo ? passed.seq : 0
this.info = Buffer.from(passed.info, 'utf-8')
this.hash = passed.redo ? passed.hash : null
this.check = null
this.putData()
this.start()
}
updateData(val){
let self = this
this.seq = this.seq + 1
this.info = val
this.dht.put({k: this.keypair.pk, seq: this.seq, v: this.info, sign: (buf) => {return this.ed.sign(buf, this.keypair.sk)}}, function (error, hashData) {
if(error){
console.log(error)
} else if(hashData){
self.hash = hashData.toString('hex').toUpperCase()
self.saveData()
console.log('hash:' + self.hash)
} else {
console.log('error: nothing happened')
}
})
}
// getData(){
// this.dht.get(this.hash, (error, hashData) => {
// if(error){
// console.log(error)
// } else if(hashData){
// console.log('data\n' + hashData)
// } else {
// console.log('error: nothing happened')
// }
// })
// }
putData(){
let self = this
this.dht.put({k: this.keypair.pk, seq: this.seq, v: this.info, sign: (buf) => {return this.ed.sign(buf, this.keypair.sk)}}, function (error, hashData){
if(error){
console.log(error)
} else if(hashData){
self.hash = hashData.toString('hex').toUpperCase()
self.saveData()
console.log('hash:' + self.hash)
} else {
console.log('error: nothing happened')
}
})
}
// getSomething(val){
// this.dht.get(val, (error, hashData) => {
// if(error){
// console.log(error)
// } else if(hashData){
// console.log('data\n' + hashData)
// } else {
// console.log('error: nothing happened')
// }
// })
// }
saveData(){
fs.writeFileSync('./data/' + this.hash, JSON.stringify({key: this.keypair.sk, seq: this.seq, info: this.info, hash: this.hash}))
console.log('\nsaved')
}
start(){
this.check = setInterval(() => {
this.putData()
}, 7200000)
}
finish(){
// this.check = clearInterval(this.check)
clearInterval(this.check)
}
}
module.exports = Data