-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosmos.js
149 lines (140 loc) · 3.93 KB
/
cosmos.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var ipfsClient = require('ipfs-http-client')
var ipfs = ipfsClient();
var fs = require('fs');
const blue = require('./blue');
const Poller = require('./poller');
const ip = require('public-ip');
/**
* Description:
* This class handles all the ipfs methods required to store data
* in any format(coming soon)
*
* This feature is not enabled or utlized atm, will be once
* replication of data is stable
* func initialize: initialize the node and connect it with all the other nodes
* func pinit: pin a specific hash or all hashes from all the pins
* func showPins: show all the hashes currently pinned
* func removePin: remove a specific hash from the pinset
* func addFile: add a file from the filesystem into ipfs
* func addHash:
* func setIpns:
* func addHash:
* func getIpns:
* func rmPins:
* func swarmConnect:
*/
var cosmos = {
initialize: async () => {
let poller = new Poller(1000);
id = await ipfs.id()
ipv4 = await ip.v4()
console.log(ipv4)
for (var i = id.addresses.length; i-- > 0; ) {
if(id.addresses[i].includes(`/ip4/${ipv4}`)){
await blue.write(ipv4, id.addresses[i])
}
}
for(let key of keys = await blue.getKeys()){
if(key !== ipv4){
value = await blue.read(key)
await cosmos.swarmConnect(value, key)
}
}
let k = (await blue.getKeys()).length
/**
* Delegate one node randomly to restart
*/
poller.onPoll(async () => {
let keys = await blue.getKeys()
// check all nodes connect if not delete the one that isnt connecting
if (keys.length > k || keys.length < k) {
k = keys.length;
console.log('New node joined!')
for (let key of keys = await blue.getKeys()) {
if (key !== ipv4) {
value = await blue.read(key)
await cosmos.swarmConnect(value, key)
}
}
}
poller.poll();
});
poller.poll();
},
pinit: async (hash) => {
if(hash){
await ipfs.pin.add(hash)
return
}
pinset = await cosmos.showPins()
for(let hash of pinset){
await ipfs.pin.add(hash)
}
},
showPins: async () => {
pinset = await ipfs.pin.ls()
var hashes = []
for (let pin of pinset) {
if(pin.type === 'recursive' ){
await cosmos.pinit(pin.hash)
hashes.push(pin.hash)
}
}
return hashes
},
removePin: async (pin) => {
await ipfs.pin.rm(pin)
},
addFile: async (path) => {
hash = await ipfs.addFromFs(path, { recursive: true })
console.log('Orig hash is: ' + hash[0].hash)
return await cosmos.addHash(hash[0].hash)
},
addHash: async (hash) => {
await cosmos.pinit(hash)
// ipns might return empty
var res = await cosmos.getIpns()
res = await cosmos.getHash(res)
console.log(res)
if(res || true){
res += `\n/ipfs/${ipfsClient.Buffer.from(hash)}`
body = await ipfs.add(ipfsClient.Buffer.from(res))
console.log(body)
await cosmos.setIpns(body[0].hash)
console.log('added')
}
console.log('done')
},
getHash: async (hash) => {
path = await ipfs.get(hash)
return path[0].content.toString('UTF8')
},
setIpns: async (hash) => {
res = await ipfs.name.publish(hash)
console.log(res)
},
getIpns: async () => {
const addr = '/ipns/QmP98mu6uzBN3k5PN5LmVy7G6z71s14NCELxRprZUsW1ZQ'
return await ipfs.name.resolve(addr)
},
rmPins: async () => {
body = await ipfs.add(ipfsClient.Buffer.from(''))
await cosmos.setIpns(body[0].hash)
pinset = await cosmos.showPins()
for(let hash of pinset){
await ipfs.pin.rm(hash)
}
},
swarmConnect: async(addr, key) => {
try {
await ipfs.swarm.connect(addr).then(obj => console.log(key, obj.Strings))
} catch (e) {
console.log(e.message)
if (e.statusCode === 500 && (await blue.getKeys()).length > 2) {
blue.deleteField(key)
console.log(`${key} deleted`)
}
}
}
}
module.exports = cosmos;