-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPFSNKNHelper.js
134 lines (115 loc) · 4.69 KB
/
IPFSNKNHelper.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
'use strict';
const ApiHandler = require('./APIHandler');
/**
* This class does the bookkeeping to have all nodes send the publish messages to all other nodes.
* To do that, this class registers itself on IPFS pubsub. There each node will broadcast it's own
* existance by public key. Found keys will then get added to the list of NKN addresses to send publish messages too.
*
*/
class IPFSNKNHelper
{
constructor(apiHandler)
{
this.ipfsClient = apiHandler.ipfsClient;
this.apiHandler = apiHandler;
this.nknClient = null;
this.opsnNknChannel = "__openpubsubnetwork.nkn"
this.opsnAddChannel = "__openpubsubnetwork.addChannel"
this.opsnPin = "__openpubsubnetwork.pin"
// Subscribe to the channel
this.ipfsClient.pubsub.subscribe(this.opsnNknChannel, (msg) => {
this.subscribe(msg)
});
this.ipfsClient.pubsub.subscribe(this.opsnAddChannel, (msg) => {
this.subscribeNewChannel(msg)
});
this.ipfsClient.pubsub.subscribe(this.opsnPin, (msg) => {
this.subscribePinRequest(msg)
});
}
setNknClient(nknClient)
{
this.nknClient = nknClient
}
async askAllNknsToReport()
{
this.ipfsClient.pubsub.publish(this.opsnNknChannel, JSON.stringify({ task: "IDENTIFY", nknPublicKey: this.nknClient.getPublicKey() }));
}
async broadcastSubscribeRequest(channel)
{
this.ipfsClient.pubsub.publish(this.opsnAddChannel, channel);
}
async addSelfToAddrs()
{
if (!this.nknClient.destaddrs.includes(this.nknClient.getPublicKey()))
{
this.nknClient.destaddrs.push(this.nknClient.getPublicKey())
console.log(`Added self (${this.nknClient.getPublicKey()}) to the list of addresses to send publish messages too.`)
}
}
async subscribe(msg)
{
let enc = new TextDecoder("utf-8");
let decodedStr = enc.decode(msg.data);
let nknPublicKey = null
try {
let jsonData = JSON.parse(decodedStr);
if (jsonData?.task)
{
// We have been asked to identify ourselves to jsonData.nknPublicKey
if (jsonData.task == "IDENTIFY" && jsonData?.nknPublicKey) {
// Tell the network my public key
this.ipfsClient.pubsub.publish(this.opsnNknChannel, JSON.stringify({ task: "IDENTIFY_RESPONSE", nknPublicKey: this.nknClient.getPublicKey() }));
// But.. We could've received this message too. In that case a new node just identified itself on the network which WE should add to our local addrs.
// That's only true when our own public key is different to the one we received.
if (this.nknClient.getPublicKey() != jsonData.nknPublicKey) {
nknPublicKey = jsonData.nknPublicKey
} else {
return;
}
} else if (jsonData.task == "IDENTIFY_RESPONSE" && jsonData?.nknPublicKey) {
// Is this response for us?
if (this.nknClient.getPublicKey() != jsonData.nknPublicKey) {
// Not us, we want to add this one to our list of addrs to send messages too
nknPublicKey = jsonData.nknPublicKey
} else {
return;
}
} else {
return;
}
} else {
console.log("There's no task in this data. Ignoring.")
}
} catch(e) {
console.log("Some bozo is apparently spamming us with fake data i assume... Ignoring.");
return;
}
if(nknPublicKey == null)
{
console.log("We did receive data but not valid NKN address.")
console.log(decodedStr)
return;
}
if (!this.nknClient.destaddrs.includes(nknPublicKey))
{
this.nknClient.destaddrs.push(nknPublicKey)
console.log(`Added ${nknPublicKey} to the list of addresses to send publish messages too.`)
}
}
async subscribeNewChannel(msg)
{
let enc = new TextDecoder("utf-8");
let decodedStr = enc.decode(msg.data);
this.apiHandler.registerSubscribe(decodedStr)
}
async subscribePinRequest(msg)
{
let enc = new TextDecoder("utf-8");
let decodedStr = enc.decode(msg.data);
let decodedData = JSON.parse(decodedStr);
console.log(`IPFS Pinning ${decodedData?.cid} (just blindly trusting this...)`)
this.ipfsClient.pin.add(decodedData?.cid)
}
}
module.exports = IPFSNKNHelper;