dignity.js 0.5.4 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -3
- package/dist/dignity.cjs.js +513 -17
- package/dist/dignity.cjs.js.map +4 -4
- package/dist/dignity.esm.js +513 -17
- package/dist/dignity.esm.js.map +3 -3
- package/dist/dignity.min.js +27 -27
- package/docs/assets/dignity.esm.js +477 -51
- package/docs/index.html +346 -6
- package/docs/openapi-like.json +9 -1
- package/examples/decentralized-chess-lite.js +2 -2
- package/package.json +2 -1
- package/src/core/dignity-p2p.js +473 -16
- package/src/gossip/peer-group.js +64 -0
- package/src/index.js +13 -1
- package/src/network/in-memory-network.js +42 -0
- package/src/network/peerjs-network.js +28 -0
- package/src/security/message-security-service.js +10 -2
package/src/index.js
CHANGED
|
@@ -30,6 +30,13 @@ const {
|
|
|
30
30
|
MessageSecurityService,
|
|
31
31
|
DEFAULT_SECURITY_OPTIONS
|
|
32
32
|
} = require('./security/message-security-service');
|
|
33
|
+
const {
|
|
34
|
+
PEER_GROUP_SCOPE_PREFIX,
|
|
35
|
+
DEFAULT_PEER_GROUP_OPTIONS,
|
|
36
|
+
peerGroupScope,
|
|
37
|
+
parsePeerGroupScope,
|
|
38
|
+
selectFanoutPeers
|
|
39
|
+
} = require('./gossip/peer-group');
|
|
33
40
|
|
|
34
41
|
module.exports = {
|
|
35
42
|
DignityP2P,
|
|
@@ -47,5 +54,10 @@ module.exports = {
|
|
|
47
54
|
VDF,
|
|
48
55
|
SlothPermutation,
|
|
49
56
|
MessageSecurityService,
|
|
50
|
-
DEFAULT_SECURITY_OPTIONS
|
|
57
|
+
DEFAULT_SECURITY_OPTIONS,
|
|
58
|
+
PEER_GROUP_SCOPE_PREFIX,
|
|
59
|
+
DEFAULT_PEER_GROUP_OPTIONS,
|
|
60
|
+
peerGroupScope,
|
|
61
|
+
parsePeerGroupScope,
|
|
62
|
+
selectFanoutPeers
|
|
51
63
|
};
|
|
@@ -20,6 +20,19 @@ class InMemoryNetworkHub {
|
|
|
20
20
|
}
|
|
21
21
|
await Promise.all(deliveries);
|
|
22
22
|
}
|
|
23
|
+
|
|
24
|
+
async sendToPeers(senderId, message, peerIds = []) {
|
|
25
|
+
const targets = new Set((peerIds || []).filter((peerId) => peerId && peerId !== senderId));
|
|
26
|
+
const deliveries = [];
|
|
27
|
+
|
|
28
|
+
for (const [nodeId, adapter] of this.adapters.entries()) {
|
|
29
|
+
if (nodeId !== senderId && targets.has(nodeId)) {
|
|
30
|
+
deliveries.push(adapter.receive(message));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
await Promise.all(deliveries);
|
|
35
|
+
}
|
|
23
36
|
}
|
|
24
37
|
|
|
25
38
|
class InMemoryNetworkAdapter {
|
|
@@ -31,6 +44,7 @@ class InMemoryNetworkAdapter {
|
|
|
31
44
|
this.hub = hub;
|
|
32
45
|
this.nodeId = null;
|
|
33
46
|
this.messageHandlers = new Set();
|
|
47
|
+
this.connectedPeers = new Set();
|
|
34
48
|
}
|
|
35
49
|
|
|
36
50
|
async start(nodeId) {
|
|
@@ -44,6 +58,14 @@ class InMemoryNetworkAdapter {
|
|
|
44
58
|
}
|
|
45
59
|
|
|
46
60
|
this.nodeId = null;
|
|
61
|
+
this.connectedPeers.clear();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async connectToPeer(remotePeerId) {
|
|
65
|
+
if (!remotePeerId || remotePeerId === this.nodeId) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.connectedPeers.add(remotePeerId);
|
|
47
69
|
}
|
|
48
70
|
|
|
49
71
|
async broadcast(message) {
|
|
@@ -54,6 +76,26 @@ class InMemoryNetworkAdapter {
|
|
|
54
76
|
await this.hub.broadcast(this.nodeId, message);
|
|
55
77
|
}
|
|
56
78
|
|
|
79
|
+
async sendToPeers(message, peerIds = []) {
|
|
80
|
+
if (!this.nodeId) {
|
|
81
|
+
throw new Error('Network adapter has not been started');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
await this.hub.sendToPeers(this.nodeId, message, peerIds);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
listOpenPeerIds() {
|
|
88
|
+
return [...this.connectedPeers];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
getOpenConnectionCount() {
|
|
92
|
+
return this.connectedPeers.size;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
isConnectedTo(remotePeerId) {
|
|
96
|
+
return this.connectedPeers.has(remotePeerId);
|
|
97
|
+
}
|
|
98
|
+
|
|
57
99
|
onMessage(handler) {
|
|
58
100
|
this.messageHandlers.add(handler);
|
|
59
101
|
}
|
|
@@ -185,6 +185,34 @@ class PeerJSNetworkAdapter {
|
|
|
185
185
|
await Promise.all(deliveries);
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
+
async sendToPeers(message, peerIds = []) {
|
|
189
|
+
if (!this.peer) {
|
|
190
|
+
throw new Error('PeerJS network adapter has not been started');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const targets = new Set((peerIds || []).filter(Boolean));
|
|
194
|
+
if (targets.size === 0) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const deliveries = [];
|
|
199
|
+
for (const [peerId, connection] of this.connections.entries()) {
|
|
200
|
+
if (targets.has(peerId) && connection.open) {
|
|
201
|
+
deliveries.push(connection.send(message));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
await Promise.all(deliveries);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async disconnectPeer(remotePeerId) {
|
|
209
|
+
const connection = this.connections.get(remotePeerId);
|
|
210
|
+
if (connection && typeof connection.close === 'function') {
|
|
211
|
+
connection.close();
|
|
212
|
+
}
|
|
213
|
+
this.connections.delete(remotePeerId);
|
|
214
|
+
}
|
|
215
|
+
|
|
188
216
|
getOpenConnectionCount() {
|
|
189
217
|
return this.listOpenPeerIds().length;
|
|
190
218
|
}
|
|
@@ -387,8 +387,16 @@ class MessageSecurityService {
|
|
|
387
387
|
|
|
388
388
|
let key;
|
|
389
389
|
if (encryption.kdf === 'pbkdf2') {
|
|
390
|
-
const
|
|
391
|
-
|
|
390
|
+
const configuredIterations = this.options.kdfIterations || DEFAULT_SECURITY_OPTIONS.kdfIterations;
|
|
391
|
+
const requestedIterations = encryption.kdfIterations || configuredIterations;
|
|
392
|
+
const minIterations = Math.max(1000, Math.floor(configuredIterations * 0.1));
|
|
393
|
+
const maxIterations = configuredIterations * 2;
|
|
394
|
+
|
|
395
|
+
if (requestedIterations < minIterations || requestedIterations > maxIterations) {
|
|
396
|
+
throw new Error(`Invalid kdfIterations: ${requestedIterations}`);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
key = await deriveBroadcastKey(password, salt, requestedIterations);
|
|
392
400
|
} else {
|
|
393
401
|
key = legacyBroadcastKey(password, salt);
|
|
394
402
|
}
|