-
Notifications
You must be signed in to change notification settings - Fork 0
/
nkn_safe_nodes.js
185 lines (141 loc) · 4.67 KB
/
nkn_safe_nodes.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import * as random from "./random.js";
const rpcSeed = "https://mainnet-rpc-node-0001.nkn.org/mainnet/api/wallet";
async function getNeighbours(rpcUrl) {
return await nkn.rpc.rpcCall(rpcUrl, "getneighbor", {})
}
class NKNNodes {
constructor() {
this.nodesSorted = new RBTree(function(a, b) {
const diff = BigInt("0x" + a) - BigInt("0x" + b);
if (diff > 0)
return 1;
if (diff < 0)
return -1;
return 0;
});
this.nodes = new Map();
this.bannedNodes = new Set();
}
add(node) {
if (this.nodes.has(node.id) || this.bannedNodes.has(node.id))
return false;
this.nodes.set(node.id, node);
this.nodesSorted.insert(node.id);
return true;
}
remove(id) {
if (!this.nodes.has(id))
return false;
this.nodes.delete(id);
this.nodesSorted.remove(id);
this.bannedNodes.add(id);
return true;
}
find(clientId) {
const closestAfter = this.nodesSorted.lowerBound(clientId);
const closestBefore = closestAfter.prev();
if(closestBefore === null)
return this.nodes.get(this.nodesSorted.max());
return this.nodes.get(closestBefore);
}
}
export async function seedNodes() {
const nodes = new NKNNodes();
const seedNeighbors = await getNeighbours(rpcSeed);
for (const node of seedNeighbors)
nodes.add(node);
return nodes;
}
function isNodeSafe(node) {
return node.tlsWebsocketDomain && node.tlsWebsocketDomain.includes("staticdns3");
}
function findIdentifierForSafeNode(nodes, pubkey) {
while (true) {
const identifier = random.seed256();
const addr = identifier + "." + pubkey;
const clientId = nkn.hash.sha256(addr);
const node = nodes.find(clientId);
if (node && isNodeSafe(node))
return identifier;
}
}
async function findAndVerifySafeNode(nodes, pubkey) {
const identifier = findIdentifierForSafeNode(nodes, pubkey);
const addr = identifier + "." + pubkey;
const clientId = nkn.hash.sha256(addr);
const node = nodes.find(clientId);
if (!node || !isNodeSafe(node))
throw new Error("Failed to find a safe node");
const nodeRpcAddr = `https://${node.tlsJsonRpcDomain}:${node.tlsJsonRpcPort}`;
var wssAddr = null;
try {
wssAddr = await nkn.rpc.getWssAddr(addr, {rpcServerAddr: nodeRpcAddr});
}
catch(e) {
nodes.remove(node.id);
throw new Error("Failed to verify node: " + e);
}
if (!wssAddr.addr.includes("staticdns3")) {
const neighbors = await getNeighbours(nodeRpcAddr);
for (const neighbor of neighbors)
nodes.add(neighbor);
throw new Error(`Node is not safe. Client ID: ${clientId}, found node ID: ${node.id}, NKN assigned node ID: ${wssAddr.id}`);
}
return identifier;
}
async function findSafeNodeRepeatedly(nodes, pubkey, maxTries=50) {
for (let i = 0; i < maxTries; i++) {
if(!haveSafeNode(nodes))
throw new Error("No safe nodes available");
try {
return await findAndVerifySafeNode(nodes, pubkey);
}
catch(e) {
// console.log(`Failed to find a safe node: ${e}`);
}
}
throw new Error("Failed to find a safe node");
}
export function waitForClientToConnect(client) {
return new Promise((resolve, reject) => {
client.onConnect(() => {
resolve(client);
});
client.onConnectFailed((error) => {
reject(error);
});
});
}
export function safeNodeClientGenerator(nodes) {
return async function() {
const key = new nkn.Key();
const pubkey = key.publicKey;
const identifier = await findSafeNodeRepeatedly(nodes, pubkey);
const client = new nkn.Client({
identifier: identifier,
seed: key.seed});
return await waitForClientToConnect(client);
};
}
function haveSafeNode(nodes) {
for (const node of nodes.nodes.values())
if (isNodeSafe(node))
return true;
return false;
}
export async function canConnectToUnsafeNodes(nodes) {
const unsafeNodes = Array.from(nodes.nodes.values()).filter(node => !isNodeSafe(node));
const sample = random.sampleArray(unsafeNodes, 5);
const promises = [];
for (const node of sample) {
const rpcAddr = `https://${node.tlsJsonRpcDomain}:${node.tlsJsonRpcPort}`;
promises.push(nkn.rpc.getNodeState({rpcServerAddr: rpcAddr}));
}
try {
await Promise.any(promises);
return true;
}
catch(e) {
return false;
}
}