-
Notifications
You must be signed in to change notification settings - Fork 18
/
ipfsSaveState.js
41 lines (39 loc) · 1.25 KB
/
ipfsSaveState.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
const { ipfs } = require("./index");
exports.ipfsSaveState = (blocknum, buffer, ipfsc, tries) => {
return new Promise((resolve, reject) => {
if(tries)console.log('Retry IPFS Save:', tries)
ipfs.add(buffer, (err, ipfs_return) => {
if (!err) {
var hash = '';
try {
hash = ipfs_return[0].hash;
} catch (e) { console.log(e); }
console.log(blocknum + `:Saved: ${hash}`);
resolve({
hashLastIBlock: hash,
hashBlock: blocknum
})
} else {
reject(err)
/*
cycleipfs(cycle++)
if (cycle >= 25) {
cycle = 0;
return;
}
*/
}
})
})
}
exports.ipfsPeerConnect = (peerid) => {
return new Promise((resolve, reject) => {
//ipfs.swarm.addrs().then((addrs) => {console.log(addrs)})
ipfs.swarm.connect(`/p2p/${peerid}`, (err, res) => {
if(res)resolve(res.Strings[0])
if(err){
resolve(`Failed to connect to${peerid}`)
}
})
})
}