gennet.js 0.2.2 → 0.3.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/README.md +11 -0
- package/dist/index.cjs +14 -7
- package/dist/index.d.cts +12 -3
- package/dist/index.d.mts +12 -3
- package/dist/index.d.ts +12 -3
- package/dist/index.mjs +14 -7
- package/package.json +1 -1
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
|
-
|
|
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
|
|
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
|
-
|
|
382
|
+
const wsUrl = options?.token ? `${url}${url.includes("?") ? "&" : "?"}token=${encodeURIComponent(options.token)}` : url;
|
|
383
|
+
return new WebSocketProvider(wsUrl, { reconnect: options?.reconnect });
|
|
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
|
@@ -45,6 +45,7 @@ interface TransportInfo {
|
|
|
45
45
|
port?: number;
|
|
46
46
|
api: string[];
|
|
47
47
|
origins?: string[];
|
|
48
|
+
auth?: boolean;
|
|
48
49
|
}
|
|
49
50
|
interface NodeInfo {
|
|
50
51
|
state: GatewayState;
|
|
@@ -219,6 +220,13 @@ declare class Mempool {
|
|
|
219
220
|
* gennet.disconnect();
|
|
220
221
|
* ```
|
|
221
222
|
*/
|
|
223
|
+
/** Optionen für den GenNet Client. */
|
|
224
|
+
interface GenNetOptions {
|
|
225
|
+
/** JWT-Token für authentifizierte Verbindungen. */
|
|
226
|
+
token?: string;
|
|
227
|
+
/** WebSocket Reconnect-Optionen. */
|
|
228
|
+
reconnect?: ReconnectOptions;
|
|
229
|
+
}
|
|
222
230
|
declare class GenNet {
|
|
223
231
|
readonly admin: Admin;
|
|
224
232
|
readonly net: Net;
|
|
@@ -226,7 +234,7 @@ declare class GenNet {
|
|
|
226
234
|
readonly agent: Agent;
|
|
227
235
|
readonly mempool: Mempool;
|
|
228
236
|
private readonly provider;
|
|
229
|
-
constructor(providerOrUrl: string | Provider);
|
|
237
|
+
constructor(providerOrUrl: string | Provider, options?: GenNetOptions);
|
|
230
238
|
/** Verbindung herstellen (nur bei WebSocket nötig). */
|
|
231
239
|
connect(): Promise<void>;
|
|
232
240
|
/** Verbindung schließen. */
|
|
@@ -288,7 +296,8 @@ declare class WebSocketProvider implements Provider {
|
|
|
288
296
|
*/
|
|
289
297
|
declare class HttpProvider implements Provider {
|
|
290
298
|
private readonly url;
|
|
291
|
-
|
|
299
|
+
private readonly token;
|
|
300
|
+
constructor(url: string, token?: string);
|
|
292
301
|
get connected(): boolean;
|
|
293
302
|
request(method: string, params?: Record<string, unknown> | unknown[]): Promise<unknown>;
|
|
294
303
|
on<E extends ProviderEvent>(_event: E, _listener: ProviderEventListener<E>): void;
|
|
@@ -297,4 +306,4 @@ declare class HttpProvider implements Provider {
|
|
|
297
306
|
}
|
|
298
307
|
|
|
299
308
|
export { Admin, Agent, GenNet, HttpProvider, Mempool, Net, Personal, RpcError, WebSocketProvider };
|
|
300
|
-
export type { AgentResult, GatewayState, IdentityInfo, JsonRpcErrorResponse, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccessResponse, ModuleInfo, NodeInfo, PeerInfo, Provider, ProviderEvent, ProviderEventListener, ReconnectOptions, Subscription, SubscriptionTopic, TransportInfo };
|
|
309
|
+
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
|
@@ -45,6 +45,7 @@ interface TransportInfo {
|
|
|
45
45
|
port?: number;
|
|
46
46
|
api: string[];
|
|
47
47
|
origins?: string[];
|
|
48
|
+
auth?: boolean;
|
|
48
49
|
}
|
|
49
50
|
interface NodeInfo {
|
|
50
51
|
state: GatewayState;
|
|
@@ -219,6 +220,13 @@ declare class Mempool {
|
|
|
219
220
|
* gennet.disconnect();
|
|
220
221
|
* ```
|
|
221
222
|
*/
|
|
223
|
+
/** Optionen für den GenNet Client. */
|
|
224
|
+
interface GenNetOptions {
|
|
225
|
+
/** JWT-Token für authentifizierte Verbindungen. */
|
|
226
|
+
token?: string;
|
|
227
|
+
/** WebSocket Reconnect-Optionen. */
|
|
228
|
+
reconnect?: ReconnectOptions;
|
|
229
|
+
}
|
|
222
230
|
declare class GenNet {
|
|
223
231
|
readonly admin: Admin;
|
|
224
232
|
readonly net: Net;
|
|
@@ -226,7 +234,7 @@ declare class GenNet {
|
|
|
226
234
|
readonly agent: Agent;
|
|
227
235
|
readonly mempool: Mempool;
|
|
228
236
|
private readonly provider;
|
|
229
|
-
constructor(providerOrUrl: string | Provider);
|
|
237
|
+
constructor(providerOrUrl: string | Provider, options?: GenNetOptions);
|
|
230
238
|
/** Verbindung herstellen (nur bei WebSocket nötig). */
|
|
231
239
|
connect(): Promise<void>;
|
|
232
240
|
/** Verbindung schließen. */
|
|
@@ -288,7 +296,8 @@ declare class WebSocketProvider implements Provider {
|
|
|
288
296
|
*/
|
|
289
297
|
declare class HttpProvider implements Provider {
|
|
290
298
|
private readonly url;
|
|
291
|
-
|
|
299
|
+
private readonly token;
|
|
300
|
+
constructor(url: string, token?: string);
|
|
292
301
|
get connected(): boolean;
|
|
293
302
|
request(method: string, params?: Record<string, unknown> | unknown[]): Promise<unknown>;
|
|
294
303
|
on<E extends ProviderEvent>(_event: E, _listener: ProviderEventListener<E>): void;
|
|
@@ -297,4 +306,4 @@ declare class HttpProvider implements Provider {
|
|
|
297
306
|
}
|
|
298
307
|
|
|
299
308
|
export { Admin, Agent, GenNet, HttpProvider, Mempool, Net, Personal, RpcError, WebSocketProvider };
|
|
300
|
-
export type { AgentResult, GatewayState, IdentityInfo, JsonRpcErrorResponse, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccessResponse, ModuleInfo, NodeInfo, PeerInfo, Provider, ProviderEvent, ProviderEventListener, ReconnectOptions, Subscription, SubscriptionTopic, TransportInfo };
|
|
309
|
+
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
|
@@ -45,6 +45,7 @@ interface TransportInfo {
|
|
|
45
45
|
port?: number;
|
|
46
46
|
api: string[];
|
|
47
47
|
origins?: string[];
|
|
48
|
+
auth?: boolean;
|
|
48
49
|
}
|
|
49
50
|
interface NodeInfo {
|
|
50
51
|
state: GatewayState;
|
|
@@ -219,6 +220,13 @@ declare class Mempool {
|
|
|
219
220
|
* gennet.disconnect();
|
|
220
221
|
* ```
|
|
221
222
|
*/
|
|
223
|
+
/** Optionen für den GenNet Client. */
|
|
224
|
+
interface GenNetOptions {
|
|
225
|
+
/** JWT-Token für authentifizierte Verbindungen. */
|
|
226
|
+
token?: string;
|
|
227
|
+
/** WebSocket Reconnect-Optionen. */
|
|
228
|
+
reconnect?: ReconnectOptions;
|
|
229
|
+
}
|
|
222
230
|
declare class GenNet {
|
|
223
231
|
readonly admin: Admin;
|
|
224
232
|
readonly net: Net;
|
|
@@ -226,7 +234,7 @@ declare class GenNet {
|
|
|
226
234
|
readonly agent: Agent;
|
|
227
235
|
readonly mempool: Mempool;
|
|
228
236
|
private readonly provider;
|
|
229
|
-
constructor(providerOrUrl: string | Provider);
|
|
237
|
+
constructor(providerOrUrl: string | Provider, options?: GenNetOptions);
|
|
230
238
|
/** Verbindung herstellen (nur bei WebSocket nötig). */
|
|
231
239
|
connect(): Promise<void>;
|
|
232
240
|
/** Verbindung schließen. */
|
|
@@ -288,7 +296,8 @@ declare class WebSocketProvider implements Provider {
|
|
|
288
296
|
*/
|
|
289
297
|
declare class HttpProvider implements Provider {
|
|
290
298
|
private readonly url;
|
|
291
|
-
|
|
299
|
+
private readonly token;
|
|
300
|
+
constructor(url: string, token?: string);
|
|
292
301
|
get connected(): boolean;
|
|
293
302
|
request(method: string, params?: Record<string, unknown> | unknown[]): Promise<unknown>;
|
|
294
303
|
on<E extends ProviderEvent>(_event: E, _listener: ProviderEventListener<E>): void;
|
|
@@ -297,4 +306,4 @@ declare class HttpProvider implements Provider {
|
|
|
297
306
|
}
|
|
298
307
|
|
|
299
308
|
export { Admin, Agent, GenNet, HttpProvider, Mempool, Net, Personal, RpcError, WebSocketProvider };
|
|
300
|
-
export type { AgentResult, GatewayState, IdentityInfo, JsonRpcErrorResponse, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, JsonRpcSuccessResponse, ModuleInfo, NodeInfo, PeerInfo, Provider, ProviderEvent, ProviderEventListener, ReconnectOptions, Subscription, SubscriptionTopic, TransportInfo };
|
|
309
|
+
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
|
-
|
|
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
|
|
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
|
-
|
|
380
|
+
const wsUrl = options?.token ? `${url}${url.includes("?") ? "&" : "?"}token=${encodeURIComponent(options.token)}` : url;
|
|
381
|
+
return new WebSocketProvider(wsUrl, { reconnect: options?.reconnect });
|
|
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
|
}
|