gennet.js 0.2.1 → 0.3.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 CHANGED
@@ -43,6 +43,17 @@ await gennet.net.send('0xRecipientAddress', 'Hello!');
43
43
  gennet.disconnect();
44
44
  ```
45
45
 
46
+ ## Authentication
47
+
48
+ For Gennet-Nodes with JWT authentication enabled, pass the token via options:
49
+
50
+ ```typescript
51
+ const gennet = new GenNet('ws://localhost:18789', { token: 'eyJhbGciOi...' });
52
+ await gennet.connect();
53
+ ```
54
+
55
+ Works with both WebSocket and HTTP providers.
56
+
46
57
  ## Providers
47
58
 
48
59
  gennet.js auto-detects the provider from the URL:
package/dist/index.cjs CHANGED
@@ -14,8 +14,10 @@ class RpcError extends Error {
14
14
  let requestId = 0;
15
15
  class HttpProvider {
16
16
  url;
17
- constructor(url) {
17
+ token;
18
+ constructor(url, token) {
18
19
  this.url = url;
20
+ this.token = token;
19
21
  }
20
22
  get connected() {
21
23
  return true;
@@ -23,9 +25,13 @@ class HttpProvider {
23
25
  async request(method, params) {
24
26
  const id = ++requestId;
25
27
  const body = JSON.stringify({ jsonrpc: "2.0", id, method, params });
28
+ const headers = { "Content-Type": "application/json" };
29
+ if (this.token) {
30
+ headers["Authorization"] = `Bearer ${this.token}`;
31
+ }
26
32
  const res = await fetch(this.url, {
27
33
  method: "POST",
28
- headers: { "Content-Type": "application/json" },
34
+ headers,
29
35
  body
30
36
  });
31
37
  if (!res.ok) {
@@ -312,9 +318,9 @@ class GenNet {
312
318
  agent;
313
319
  mempool;
314
320
  provider;
315
- constructor(providerOrUrl) {
321
+ constructor(providerOrUrl, options) {
316
322
  if (typeof providerOrUrl === "string") {
317
- this.provider = GenNet.createProvider(providerOrUrl);
323
+ this.provider = GenNet.createProvider(providerOrUrl, options);
318
324
  } else {
319
325
  this.provider = providerOrUrl;
320
326
  }
@@ -371,12 +377,13 @@ class GenNet {
371
377
  return this.provider.request(method, params);
372
378
  }
373
379
  // ── Private ────────────────────────────────────────────────
374
- static createProvider(url) {
380
+ static createProvider(url, options) {
375
381
  if (url.startsWith("ws://") || url.startsWith("wss://")) {
376
- return new WebSocketProvider(url);
382
+ const wsUrl = options?.token ? `${url}${url.includes("?") ? "&" : "?"}token=${encodeURIComponent(options.token)}` : url;
383
+ return new WebSocketProvider(wsUrl);
377
384
  }
378
385
  if (url.startsWith("http://") || url.startsWith("https://")) {
379
- return new HttpProvider(url);
386
+ return new HttpProvider(url, options?.token);
380
387
  }
381
388
  throw new Error(`Unbekanntes URL-Schema: ${url} (erwartet: ws://, wss://, http://, https://)`);
382
389
  }
package/dist/index.d.cts CHANGED
@@ -39,6 +39,14 @@ interface ModuleInfo {
39
39
  state: string;
40
40
  status?: string;
41
41
  }
42
+ interface TransportInfo {
43
+ enabled: boolean;
44
+ host?: string;
45
+ port?: number;
46
+ api: string[];
47
+ origins?: string[];
48
+ auth?: boolean;
49
+ }
42
50
  interface NodeInfo {
43
51
  state: GatewayState;
44
52
  peerId: string | null;
@@ -53,6 +61,9 @@ interface NodeInfo {
53
61
  listenPort: number;
54
62
  datadir: string;
55
63
  modules: ModuleInfo[];
64
+ ws: TransportInfo;
65
+ http: TransportInfo;
66
+ ipc: boolean;
56
67
  }
57
68
  interface PeerInfo {
58
69
  peerId: string;
@@ -209,6 +220,11 @@ declare class Mempool {
209
220
  * gennet.disconnect();
210
221
  * ```
211
222
  */
223
+ /** Optionen für den GenNet Client. */
224
+ interface GenNetOptions {
225
+ /** JWT-Token für authentifizierte Verbindungen. */
226
+ token?: string;
227
+ }
212
228
  declare class GenNet {
213
229
  readonly admin: Admin;
214
230
  readonly net: Net;
@@ -216,7 +232,7 @@ declare class GenNet {
216
232
  readonly agent: Agent;
217
233
  readonly mempool: Mempool;
218
234
  private readonly provider;
219
- constructor(providerOrUrl: string | Provider);
235
+ constructor(providerOrUrl: string | Provider, options?: GenNetOptions);
220
236
  /** Verbindung herstellen (nur bei WebSocket nötig). */
221
237
  connect(): Promise<void>;
222
238
  /** Verbindung schließen. */
@@ -278,7 +294,8 @@ declare class WebSocketProvider implements Provider {
278
294
  */
279
295
  declare class HttpProvider implements Provider {
280
296
  private readonly url;
281
- constructor(url: string);
297
+ private readonly token;
298
+ constructor(url: string, token?: string);
282
299
  get connected(): boolean;
283
300
  request(method: string, params?: Record<string, unknown> | unknown[]): Promise<unknown>;
284
301
  on<E extends ProviderEvent>(_event: E, _listener: ProviderEventListener<E>): void;
@@ -287,4 +304,4 @@ declare class HttpProvider implements Provider {
287
304
  }
288
305
 
289
306
  export { Admin, Agent, GenNet, HttpProvider, Mempool, Net, Personal, RpcError, WebSocketProvider };
290
- export type { AgentResult, GatewayState, IdentityInfo, JsonRpcErrorResponse, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccessResponse, ModuleInfo, NodeInfo, PeerInfo, Provider, ProviderEvent, ProviderEventListener, ReconnectOptions, Subscription, SubscriptionTopic };
307
+ export type { AgentResult, GatewayState, GenNetOptions, IdentityInfo, JsonRpcErrorResponse, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccessResponse, ModuleInfo, NodeInfo, PeerInfo, Provider, ProviderEvent, ProviderEventListener, ReconnectOptions, Subscription, SubscriptionTopic, TransportInfo };
package/dist/index.d.mts CHANGED
@@ -39,6 +39,14 @@ interface ModuleInfo {
39
39
  state: string;
40
40
  status?: string;
41
41
  }
42
+ interface TransportInfo {
43
+ enabled: boolean;
44
+ host?: string;
45
+ port?: number;
46
+ api: string[];
47
+ origins?: string[];
48
+ auth?: boolean;
49
+ }
42
50
  interface NodeInfo {
43
51
  state: GatewayState;
44
52
  peerId: string | null;
@@ -53,6 +61,9 @@ interface NodeInfo {
53
61
  listenPort: number;
54
62
  datadir: string;
55
63
  modules: ModuleInfo[];
64
+ ws: TransportInfo;
65
+ http: TransportInfo;
66
+ ipc: boolean;
56
67
  }
57
68
  interface PeerInfo {
58
69
  peerId: string;
@@ -209,6 +220,11 @@ declare class Mempool {
209
220
  * gennet.disconnect();
210
221
  * ```
211
222
  */
223
+ /** Optionen für den GenNet Client. */
224
+ interface GenNetOptions {
225
+ /** JWT-Token für authentifizierte Verbindungen. */
226
+ token?: string;
227
+ }
212
228
  declare class GenNet {
213
229
  readonly admin: Admin;
214
230
  readonly net: Net;
@@ -216,7 +232,7 @@ declare class GenNet {
216
232
  readonly agent: Agent;
217
233
  readonly mempool: Mempool;
218
234
  private readonly provider;
219
- constructor(providerOrUrl: string | Provider);
235
+ constructor(providerOrUrl: string | Provider, options?: GenNetOptions);
220
236
  /** Verbindung herstellen (nur bei WebSocket nötig). */
221
237
  connect(): Promise<void>;
222
238
  /** Verbindung schließen. */
@@ -278,7 +294,8 @@ declare class WebSocketProvider implements Provider {
278
294
  */
279
295
  declare class HttpProvider implements Provider {
280
296
  private readonly url;
281
- constructor(url: string);
297
+ private readonly token;
298
+ constructor(url: string, token?: string);
282
299
  get connected(): boolean;
283
300
  request(method: string, params?: Record<string, unknown> | unknown[]): Promise<unknown>;
284
301
  on<E extends ProviderEvent>(_event: E, _listener: ProviderEventListener<E>): void;
@@ -287,4 +304,4 @@ declare class HttpProvider implements Provider {
287
304
  }
288
305
 
289
306
  export { Admin, Agent, GenNet, HttpProvider, Mempool, Net, Personal, RpcError, WebSocketProvider };
290
- export type { AgentResult, GatewayState, IdentityInfo, JsonRpcErrorResponse, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccessResponse, ModuleInfo, NodeInfo, PeerInfo, Provider, ProviderEvent, ProviderEventListener, ReconnectOptions, Subscription, SubscriptionTopic };
307
+ export type { AgentResult, GatewayState, GenNetOptions, IdentityInfo, JsonRpcErrorResponse, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccessResponse, ModuleInfo, NodeInfo, PeerInfo, Provider, ProviderEvent, ProviderEventListener, ReconnectOptions, Subscription, SubscriptionTopic, TransportInfo };
package/dist/index.d.ts CHANGED
@@ -39,6 +39,14 @@ interface ModuleInfo {
39
39
  state: string;
40
40
  status?: string;
41
41
  }
42
+ interface TransportInfo {
43
+ enabled: boolean;
44
+ host?: string;
45
+ port?: number;
46
+ api: string[];
47
+ origins?: string[];
48
+ auth?: boolean;
49
+ }
42
50
  interface NodeInfo {
43
51
  state: GatewayState;
44
52
  peerId: string | null;
@@ -53,6 +61,9 @@ interface NodeInfo {
53
61
  listenPort: number;
54
62
  datadir: string;
55
63
  modules: ModuleInfo[];
64
+ ws: TransportInfo;
65
+ http: TransportInfo;
66
+ ipc: boolean;
56
67
  }
57
68
  interface PeerInfo {
58
69
  peerId: string;
@@ -209,6 +220,11 @@ declare class Mempool {
209
220
  * gennet.disconnect();
210
221
  * ```
211
222
  */
223
+ /** Optionen für den GenNet Client. */
224
+ interface GenNetOptions {
225
+ /** JWT-Token für authentifizierte Verbindungen. */
226
+ token?: string;
227
+ }
212
228
  declare class GenNet {
213
229
  readonly admin: Admin;
214
230
  readonly net: Net;
@@ -216,7 +232,7 @@ declare class GenNet {
216
232
  readonly agent: Agent;
217
233
  readonly mempool: Mempool;
218
234
  private readonly provider;
219
- constructor(providerOrUrl: string | Provider);
235
+ constructor(providerOrUrl: string | Provider, options?: GenNetOptions);
220
236
  /** Verbindung herstellen (nur bei WebSocket nötig). */
221
237
  connect(): Promise<void>;
222
238
  /** Verbindung schließen. */
@@ -278,7 +294,8 @@ declare class WebSocketProvider implements Provider {
278
294
  */
279
295
  declare class HttpProvider implements Provider {
280
296
  private readonly url;
281
- constructor(url: string);
297
+ private readonly token;
298
+ constructor(url: string, token?: string);
282
299
  get connected(): boolean;
283
300
  request(method: string, params?: Record<string, unknown> | unknown[]): Promise<unknown>;
284
301
  on<E extends ProviderEvent>(_event: E, _listener: ProviderEventListener<E>): void;
@@ -287,4 +304,4 @@ declare class HttpProvider implements Provider {
287
304
  }
288
305
 
289
306
  export { Admin, Agent, GenNet, HttpProvider, Mempool, Net, Personal, RpcError, WebSocketProvider };
290
- export type { AgentResult, GatewayState, IdentityInfo, JsonRpcErrorResponse, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccessResponse, ModuleInfo, NodeInfo, PeerInfo, Provider, ProviderEvent, ProviderEventListener, ReconnectOptions, Subscription, SubscriptionTopic };
307
+ export type { AgentResult, GatewayState, GenNetOptions, IdentityInfo, JsonRpcErrorResponse, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccessResponse, ModuleInfo, NodeInfo, PeerInfo, Provider, ProviderEvent, ProviderEventListener, ReconnectOptions, Subscription, SubscriptionTopic, TransportInfo };
package/dist/index.mjs CHANGED
@@ -12,8 +12,10 @@ class RpcError extends Error {
12
12
  let requestId = 0;
13
13
  class HttpProvider {
14
14
  url;
15
- constructor(url) {
15
+ token;
16
+ constructor(url, token) {
16
17
  this.url = url;
18
+ this.token = token;
17
19
  }
18
20
  get connected() {
19
21
  return true;
@@ -21,9 +23,13 @@ class HttpProvider {
21
23
  async request(method, params) {
22
24
  const id = ++requestId;
23
25
  const body = JSON.stringify({ jsonrpc: "2.0", id, method, params });
26
+ const headers = { "Content-Type": "application/json" };
27
+ if (this.token) {
28
+ headers["Authorization"] = `Bearer ${this.token}`;
29
+ }
24
30
  const res = await fetch(this.url, {
25
31
  method: "POST",
26
- headers: { "Content-Type": "application/json" },
32
+ headers,
27
33
  body
28
34
  });
29
35
  if (!res.ok) {
@@ -310,9 +316,9 @@ class GenNet {
310
316
  agent;
311
317
  mempool;
312
318
  provider;
313
- constructor(providerOrUrl) {
319
+ constructor(providerOrUrl, options) {
314
320
  if (typeof providerOrUrl === "string") {
315
- this.provider = GenNet.createProvider(providerOrUrl);
321
+ this.provider = GenNet.createProvider(providerOrUrl, options);
316
322
  } else {
317
323
  this.provider = providerOrUrl;
318
324
  }
@@ -369,12 +375,13 @@ class GenNet {
369
375
  return this.provider.request(method, params);
370
376
  }
371
377
  // ── Private ────────────────────────────────────────────────
372
- static createProvider(url) {
378
+ static createProvider(url, options) {
373
379
  if (url.startsWith("ws://") || url.startsWith("wss://")) {
374
- return new WebSocketProvider(url);
380
+ const wsUrl = options?.token ? `${url}${url.includes("?") ? "&" : "?"}token=${encodeURIComponent(options.token)}` : url;
381
+ return new WebSocketProvider(wsUrl);
375
382
  }
376
383
  if (url.startsWith("http://") || url.startsWith("https://")) {
377
- return new HttpProvider(url);
384
+ return new HttpProvider(url, options?.token);
378
385
  }
379
386
  throw new Error(`Unbekanntes URL-Schema: ${url} (erwartet: ws://, wss://, http://, https://)`);
380
387
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gennet.js",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Client Library for GenNet — interact with GenNet nodes via JSON-RPC",
5
5
  "type": "module",
6
6
  "sideEffects": false,