@kynesyslabs/demosdk 2.1.15 → 2.2.1
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/build/encryption/PQC/enigma.d.ts +83 -0
- package/build/encryption/PQC/enigma.js +243 -0
- package/build/encryption/PQC/enigma.js.map +1 -0
- package/build/encryption/PQC/falconts/falcon.d.ts +1 -1
- package/build/encryption/PQC/falconts/index.d.ts +1 -0
- package/build/encryption/PQC/{ml-dsa/utils.d.ts → utils.d.ts} +2 -2
- package/build/encryption/PQC/{ml-dsa/utils.js → utils.js} +2 -1
- package/build/encryption/PQC/utils.js.map +1 -0
- package/build/encryption/index.d.ts +2 -1
- package/build/encryption/index.js +4 -2
- package/build/encryption/index.js.map +1 -1
- package/build/encryption/unifiedCrypto.d.ts +125 -0
- package/build/encryption/unifiedCrypto.js +408 -0
- package/build/encryption/unifiedCrypto.js.map +1 -0
- package/build/instant_messaging/index.d.ts +300 -0
- package/build/instant_messaging/index.js +630 -0
- package/build/instant_messaging/index.js.map +1 -0
- package/build/multichain/core/btc.js +5 -1
- package/build/multichain/core/btc.js.map +1 -1
- package/build/utils/uint8Serialize.d.ts +2 -0
- package/build/utils/uint8Serialize.js +22 -0
- package/build/utils/uint8Serialize.js.map +1 -0
- package/package.json +4 -2
- package/build/encryption/PQC/index.d.ts +0 -216
- package/build/encryption/PQC/index.js +0 -520
- package/build/encryption/PQC/index.js.map +0 -1
- package/build/encryption/PQC/ml-dsa/utils.js.map +0 -1
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MessagingPeer class for peer-to-peer communication through a signaling server
|
|
3
|
+
*
|
|
4
|
+
* This class handles:
|
|
5
|
+
* - Connection to signaling server
|
|
6
|
+
* - Peer registration with public key
|
|
7
|
+
* - Peer discovery
|
|
8
|
+
* - Message exchange with other peers
|
|
9
|
+
* - Public key requests
|
|
10
|
+
* - Automatic reconnection with exponential backoff
|
|
11
|
+
*
|
|
12
|
+
* Message Handling:
|
|
13
|
+
* - Messages are automatically encrypted when sent and decrypted when received
|
|
14
|
+
* - To handle incoming messages with type "message", register a handler using onMessage:
|
|
15
|
+
* ```typescript
|
|
16
|
+
* peer.onMessage((message, fromId) => {
|
|
17
|
+
* // Handle the decrypted message here
|
|
18
|
+
* console.log(`Message from ${fromId}:`, message);
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
* - You can register multiple handlers for different purposes (each handler will receive the decrypted message and sender's ID)
|
|
22
|
+
* - Handlers are executed in the order they are registered
|
|
23
|
+
* - If no handlers are registered, messages will be silently ignored
|
|
24
|
+
* - For request-response patterns (like getting a peer's public key), use the Promise-based methods
|
|
25
|
+
* that handle the response matching automatically
|
|
26
|
+
*
|
|
27
|
+
* Request-Response Pattern:
|
|
28
|
+
* - The class provides a robust request-response pattern through the sendToServerAndWait method
|
|
29
|
+
* - This method sends a message to the server and waits for a specific response type
|
|
30
|
+
* - It supports custom error handling, retry logic, and filtering by additional criteria
|
|
31
|
+
* - Example usage:
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // Basic usage
|
|
34
|
+
* const response = await peer.sendToServerAndWait(
|
|
35
|
+
* {
|
|
36
|
+
* type: "custom_action",
|
|
37
|
+
* payload: { someData: "value" }
|
|
38
|
+
* },
|
|
39
|
+
* "custom_action_success"
|
|
40
|
+
* );
|
|
41
|
+
*
|
|
42
|
+
* // With retry logic
|
|
43
|
+
* const response = await peer.sendToServerAndWait(
|
|
44
|
+
* {
|
|
45
|
+
* type: "custom_action",
|
|
46
|
+
* payload: { someData: "value" }
|
|
47
|
+
* },
|
|
48
|
+
* "custom_action_success",
|
|
49
|
+
* {
|
|
50
|
+
* retryCount: 3,
|
|
51
|
+
* timeout: 5000
|
|
52
|
+
* }
|
|
53
|
+
* );
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* Message Types:
|
|
57
|
+
* - "message": Encrypted peer-to-peer messages
|
|
58
|
+
* ```typescript
|
|
59
|
+
* // Example of a received message payload
|
|
60
|
+
* {
|
|
61
|
+
* type: "message",
|
|
62
|
+
* payload: {
|
|
63
|
+
* message: {
|
|
64
|
+
* algorithm: "ml-kem-aes",
|
|
65
|
+
* encryptedData: Uint8Array,
|
|
66
|
+
* cipherText: Uint8Array
|
|
67
|
+
* },
|
|
68
|
+
* fromId: "sender-peer-id"
|
|
69
|
+
* }
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* // After decryption, handlers receive:
|
|
73
|
+
* // message = "Hello, this is the decrypted message"
|
|
74
|
+
* // fromId = "sender-peer-id"
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* Complete Example: Building a barebone messenger app and handling incoming messages
|
|
78
|
+
* ```typescript
|
|
79
|
+
* // In your application file (e.g., myMessenger.ts)
|
|
80
|
+
* import { MessagingPeer } from './path/to/instant_messaging';
|
|
81
|
+
*
|
|
82
|
+
* // Create a peer instance
|
|
83
|
+
* const peer = new MessagingPeer({
|
|
84
|
+
* serverUrl: 'ws://your-signaling-server:3000',
|
|
85
|
+
* clientId: 'your-unique-id',
|
|
86
|
+
* publicKey: yourPublicKey // Your ml-kem public key
|
|
87
|
+
* });
|
|
88
|
+
*
|
|
89
|
+
* // Connect to the server
|
|
90
|
+
* await peer.connect();
|
|
91
|
+
*
|
|
92
|
+
* // Register a handler for incoming messages
|
|
93
|
+
* peer.onMessage((message, fromId) => {
|
|
94
|
+
* // Print the message to the console
|
|
95
|
+
* console.log(`Message from ${fromId}:`, message);
|
|
96
|
+
*
|
|
97
|
+
* // If you're building a UI, you might update the DOM:
|
|
98
|
+
* const messageElement = document.createElement('div');
|
|
99
|
+
* messageElement.textContent = `${fromId}: ${message}`;
|
|
100
|
+
* document.getElementById('messages-container').appendChild(messageElement);
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* // Discover other peers
|
|
104
|
+
* const peers = await peer.discoverPeers();
|
|
105
|
+
* console.log('Available peers:', peers);
|
|
106
|
+
*
|
|
107
|
+
* // Send a message to a specific peer
|
|
108
|
+
* await peer.sendMessage('target-peer-id', 'Hello from me!');
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* Usage:
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const peer = new MessagingPeer({
|
|
114
|
+
* serverUrl: 'ws://localhost:3000',
|
|
115
|
+
* clientId: 'unique-id',
|
|
116
|
+
* publicKey: a ml-kem public key
|
|
117
|
+
* });
|
|
118
|
+
*
|
|
119
|
+
* // Connect and register
|
|
120
|
+
* await peer.connect();
|
|
121
|
+
*
|
|
122
|
+
* // Discover other peers
|
|
123
|
+
* const peers = await peer.discoverPeers();
|
|
124
|
+
*
|
|
125
|
+
* // Send a message
|
|
126
|
+
* await peer.sendMessage('target-peer-id', 'Hello!');
|
|
127
|
+
*
|
|
128
|
+
* // Listen for messages
|
|
129
|
+
* peer.onMessage((message, fromId) => {
|
|
130
|
+
* console.log(`Message from ${fromId}:`, message);
|
|
131
|
+
* });
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export interface MessagingPeerConfig {
|
|
135
|
+
serverUrl: string;
|
|
136
|
+
clientId: string;
|
|
137
|
+
publicKey: Uint8Array;
|
|
138
|
+
}
|
|
139
|
+
export interface Message {
|
|
140
|
+
type: "register" | "discover" | "message" | "peer_disconnected" | "request_public_key" | "public_key_response" | "error";
|
|
141
|
+
payload: any;
|
|
142
|
+
}
|
|
143
|
+
type MessageHandler = (message: any, fromId: string) => void;
|
|
144
|
+
type ErrorHandler = (error: {
|
|
145
|
+
type: string;
|
|
146
|
+
details: string;
|
|
147
|
+
}) => void;
|
|
148
|
+
type PeerDisconnectedHandler = (peerId: string) => void;
|
|
149
|
+
type ConnectionStateHandler = (state: "connected" | "disconnected" | "reconnecting") => void;
|
|
150
|
+
export declare class MessagingPeer {
|
|
151
|
+
ws: WebSocket | null;
|
|
152
|
+
private config;
|
|
153
|
+
private messageHandlers;
|
|
154
|
+
private errorHandlers;
|
|
155
|
+
private peerDisconnectedHandlers;
|
|
156
|
+
private connectionStateHandlers;
|
|
157
|
+
private connectedPeers;
|
|
158
|
+
private messageQueue;
|
|
159
|
+
isConnected: boolean;
|
|
160
|
+
private reconnectAttempts;
|
|
161
|
+
private maxReconnectAttempts;
|
|
162
|
+
private baseReconnectDelay;
|
|
163
|
+
private reconnectTimeout;
|
|
164
|
+
private isReconnecting;
|
|
165
|
+
constructor(config: MessagingPeerConfig);
|
|
166
|
+
/**
|
|
167
|
+
* Connects to the signaling server and registers the peer
|
|
168
|
+
* @returns Promise that resolves when connected and registered
|
|
169
|
+
*/
|
|
170
|
+
connect(): Promise<void>;
|
|
171
|
+
/**
|
|
172
|
+
* Establishes WebSocket connection and sets up event handlers
|
|
173
|
+
*/
|
|
174
|
+
private connectWebSocket;
|
|
175
|
+
/**
|
|
176
|
+
* Attempts to reconnect to the server with exponential backoff
|
|
177
|
+
*/
|
|
178
|
+
private attemptReconnect;
|
|
179
|
+
/**
|
|
180
|
+
* Awaits a response for a specific message type
|
|
181
|
+
* @param messageType - The type of message to wait for
|
|
182
|
+
* @param filterFn - Optional function to filter messages by additional criteria
|
|
183
|
+
* @param timeout - Optional timeout in milliseconds (default: 10000)
|
|
184
|
+
* @returns Promise that resolves with the message payload or rejects with an error
|
|
185
|
+
*/
|
|
186
|
+
awaitResponse<T = any>(messageType: Message["type"], filterFn?: (message: Message) => boolean, timeout?: number): Promise<T>;
|
|
187
|
+
/**
|
|
188
|
+
* Registers the peer with the signaling server
|
|
189
|
+
*/
|
|
190
|
+
register(): void;
|
|
191
|
+
/**
|
|
192
|
+
* Registers the peer with the signaling server and waits for confirmation
|
|
193
|
+
* @returns Promise that resolves when registration is confirmed
|
|
194
|
+
*/
|
|
195
|
+
registerAndWait(): Promise<void>;
|
|
196
|
+
/**
|
|
197
|
+
* Discovers all connected peers
|
|
198
|
+
* @returns Promise that resolves with an array of peer IDs
|
|
199
|
+
*/
|
|
200
|
+
discoverPeers(): Promise<string[]>;
|
|
201
|
+
/**
|
|
202
|
+
* Sends a message to a specific peer
|
|
203
|
+
* @param targetId - The ID of the target peer
|
|
204
|
+
* @param message - The message to send
|
|
205
|
+
*/
|
|
206
|
+
sendMessage(targetId: string, message: string): Promise<void>;
|
|
207
|
+
/**
|
|
208
|
+
* Requests a peer's public key
|
|
209
|
+
* @param peerId - The ID of the peer whose public key to request
|
|
210
|
+
* @returns Promise that resolves with the peer's public key
|
|
211
|
+
*/
|
|
212
|
+
requestPublicKey(peerId: string): Promise<Uint8Array>;
|
|
213
|
+
/**
|
|
214
|
+
* Adds a handler for incoming messages
|
|
215
|
+
* @param handler - Function to call when a message is received
|
|
216
|
+
*/
|
|
217
|
+
onMessage(handler: MessageHandler): void;
|
|
218
|
+
/**
|
|
219
|
+
* Adds a handler for errors
|
|
220
|
+
* @param handler - Function to call when an error occurs
|
|
221
|
+
*/
|
|
222
|
+
onError(handler: ErrorHandler): void;
|
|
223
|
+
/**
|
|
224
|
+
* Adds a handler for peer disconnection events
|
|
225
|
+
* @param handler - Function to call when a peer disconnects
|
|
226
|
+
*/
|
|
227
|
+
onPeerDisconnected(handler: PeerDisconnectedHandler): void;
|
|
228
|
+
/**
|
|
229
|
+
* Adds a handler for connection state changes
|
|
230
|
+
* @param handler - Function to call when connection state changes
|
|
231
|
+
*/
|
|
232
|
+
onConnectionStateChange(handler: ConnectionStateHandler): void;
|
|
233
|
+
/**
|
|
234
|
+
* Removes a message handler
|
|
235
|
+
* @param handler - The handler to remove
|
|
236
|
+
*/
|
|
237
|
+
removeMessageHandler(handler: MessageHandler): void;
|
|
238
|
+
/**
|
|
239
|
+
* Removes an error handler
|
|
240
|
+
* @param handler - The handler to remove
|
|
241
|
+
*/
|
|
242
|
+
removeErrorHandler(handler: ErrorHandler): void;
|
|
243
|
+
/**
|
|
244
|
+
* Removes a peer disconnected handler
|
|
245
|
+
* @param handler - The handler to remove
|
|
246
|
+
*/
|
|
247
|
+
removePeerDisconnectedHandler(handler: PeerDisconnectedHandler): void;
|
|
248
|
+
/**
|
|
249
|
+
* Removes a connection state change handler
|
|
250
|
+
* @param handler - The handler to remove
|
|
251
|
+
*/
|
|
252
|
+
removeConnectionStateHandler(handler: ConnectionStateHandler): void;
|
|
253
|
+
/**
|
|
254
|
+
* Closes the connection to the signaling server
|
|
255
|
+
*/
|
|
256
|
+
disconnect(): void;
|
|
257
|
+
/**
|
|
258
|
+
* Sends a message to the signaling server
|
|
259
|
+
* @param message - The message to send
|
|
260
|
+
*/
|
|
261
|
+
private sendToServer;
|
|
262
|
+
/**
|
|
263
|
+
* Sends a message to the server and waits for a specific response type
|
|
264
|
+
* @param message - The message to send
|
|
265
|
+
* @param expectedResponseType - The type of response to wait for
|
|
266
|
+
* @param options - Additional options for handling the response
|
|
267
|
+
* @returns Promise that resolves with the response payload or rejects with an error
|
|
268
|
+
*/
|
|
269
|
+
sendToServerAndWait<T = any>(message: Message, expectedResponseType: Message["type"], options?: {
|
|
270
|
+
timeout?: number;
|
|
271
|
+
errorHandler?: (error: any) => void;
|
|
272
|
+
retryCount?: number;
|
|
273
|
+
filterFn?: (message: Message) => boolean;
|
|
274
|
+
}): Promise<T>;
|
|
275
|
+
/**
|
|
276
|
+
* Handles incoming messages from the signaling server
|
|
277
|
+
* @param message - The received message
|
|
278
|
+
*/
|
|
279
|
+
private handleMessage;
|
|
280
|
+
/**
|
|
281
|
+
* Handles errors
|
|
282
|
+
* @param error - The error to handle
|
|
283
|
+
*/
|
|
284
|
+
private handleError;
|
|
285
|
+
/**
|
|
286
|
+
* Notifies connection state change handlers
|
|
287
|
+
* @param state - The new connection state
|
|
288
|
+
*/
|
|
289
|
+
private notifyConnectionState;
|
|
290
|
+
/**
|
|
291
|
+
* Adds a temporary message handler that will be removed after handling one message
|
|
292
|
+
* @param handler - The temporary handler to add
|
|
293
|
+
*/
|
|
294
|
+
private addTemporaryMessageHandler;
|
|
295
|
+
/**
|
|
296
|
+
* Process queued messages
|
|
297
|
+
*/
|
|
298
|
+
private processMessageQueue;
|
|
299
|
+
}
|
|
300
|
+
export {};
|