@waku/core 0.0.36-acc9100.0 → 0.0.36-b0a2e39.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/bundle/{base_protocol-DvQrudwy.js → base_protocol-DEDdl6tx.js} +1 -1
- package/bundle/{index-CTo1my9M.js → index-DckUzRoN.js} +80 -1
- package/bundle/index.js +330 -68
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-CyeTW0Vr.js → version_0-DyRL7WVV.js} +370 -32
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/connection_manager/connection_manager.d.ts +1 -1
- package/dist/lib/connection_manager/connection_manager.js +1 -1
- package/dist/lib/light_push/index.d.ts +2 -1
- package/dist/lib/light_push/index.js +2 -1
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/light_push/light_push.d.ts +2 -7
- package/dist/lib/light_push/light_push.js +19 -35
- package/dist/lib/light_push/light_push.js.map +1 -1
- package/dist/lib/light_push/light_push_v3.d.ts +14 -0
- package/dist/lib/light_push/light_push_v3.js +187 -0
- package/dist/lib/light_push/light_push_v3.js.map +1 -0
- package/dist/lib/light_push/{push_rpc.d.ts → push_rpc_v2.d.ts} +3 -3
- package/dist/lib/light_push/{push_rpc.js → push_rpc_v2.js} +4 -4
- package/dist/lib/light_push/push_rpc_v2.js.map +1 -0
- package/dist/lib/light_push/push_rpc_v3.d.ts +11 -0
- package/dist/lib/light_push/push_rpc_v3.js +32 -0
- package/dist/lib/light_push/push_rpc_v3.js.map +1 -0
- package/dist/lib/light_push/utils.d.ts +24 -0
- package/dist/lib/light_push/utils.js +70 -0
- package/dist/lib/light_push/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +5 -1
- package/src/lib/connection_manager/connection_manager.ts +1 -1
- package/src/lib/light_push/index.ts +2 -1
- package/src/lib/light_push/light_push.ts +26 -43
- package/src/lib/light_push/light_push_v3.ts +319 -0
- package/src/lib/light_push/{push_rpc.ts → push_rpc_v2.ts} +5 -5
- package/src/lib/light_push/push_rpc_v3.ts +45 -0
- package/src/lib/light_push/utils.ts +95 -0
- package/dist/lib/light_push/push_rpc.js.map +0 -1
@@ -0,0 +1,14 @@
|
|
1
|
+
import type { PeerId } from "@libp2p/interface";
|
2
|
+
import { type IBaseProtocolCore, type IEncoder, type IMessage, type Libp2p, type LightPushResult, PubsubTopic } from "@waku/interfaces";
|
3
|
+
import { BaseProtocol } from "../base_protocol.js";
|
4
|
+
export declare class LightPushCoreV3 extends BaseProtocol implements IBaseProtocolCore {
|
5
|
+
readonly pubsubTopics: PubsubTopic[];
|
6
|
+
constructor(pubsubTopics: PubsubTopic[], libp2p: Libp2p);
|
7
|
+
send(encoder: IEncoder, message: IMessage, peerId: PeerId): Promise<LightPushResult>;
|
8
|
+
private validateAndPrepareMessage;
|
9
|
+
private executeRequest;
|
10
|
+
private processResponse;
|
11
|
+
private acquireStream;
|
12
|
+
private sendAndReceive;
|
13
|
+
private decodeResponse;
|
14
|
+
}
|
@@ -0,0 +1,187 @@
|
|
1
|
+
import { getLightPushStatusDescriptionV3, isSuccessStatusCodeV3, LightPushCodecV3, lightPushStatusCodeToProtocolErrorV3, LightPushStatusCodeV3, ProtocolError } from "@waku/interfaces";
|
2
|
+
import { proto_lightpush_v3 } from "@waku/proto";
|
3
|
+
import { Logger } from "@waku/utils";
|
4
|
+
import all from "it-all";
|
5
|
+
import * as lp from "it-length-prefixed";
|
6
|
+
import { pipe } from "it-pipe";
|
7
|
+
import { Uint8ArrayList } from "uint8arraylist";
|
8
|
+
import { BaseProtocol } from "../base_protocol.js";
|
9
|
+
import { PushRpcV3 } from "./push_rpc_v3.js";
|
10
|
+
import { isRLNResponseError, isValidRequestId, validateMessage } from "./utils.js";
|
11
|
+
const log = new Logger("light-push-v3");
|
12
|
+
const STREAM_TIMEOUT_MS = 30000;
|
13
|
+
class LightPushError extends Error {
|
14
|
+
protocol;
|
15
|
+
peerId;
|
16
|
+
response;
|
17
|
+
constructor(protocol, peerId, response, message) {
|
18
|
+
super(message || protocol);
|
19
|
+
this.protocol = protocol;
|
20
|
+
this.peerId = peerId;
|
21
|
+
this.response = response;
|
22
|
+
this.name = "LightPushError";
|
23
|
+
}
|
24
|
+
}
|
25
|
+
function withTimeout(promise, timeoutMs) {
|
26
|
+
return Promise.race([
|
27
|
+
promise,
|
28
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error("Stream operation timed out")), timeoutMs))
|
29
|
+
]);
|
30
|
+
}
|
31
|
+
function createSuccessResult(peerId, response) {
|
32
|
+
return {
|
33
|
+
success: peerId,
|
34
|
+
failure: null,
|
35
|
+
requestId: response.requestId,
|
36
|
+
statusCode: response.statusCode,
|
37
|
+
statusDesc: response.statusDesc,
|
38
|
+
relayPeerCount: response.relayPeerCount
|
39
|
+
};
|
40
|
+
}
|
41
|
+
function createFailureResult(error) {
|
42
|
+
const result = {
|
43
|
+
success: null,
|
44
|
+
failure: {
|
45
|
+
error: error.protocol,
|
46
|
+
peerId: error.peerId
|
47
|
+
}
|
48
|
+
};
|
49
|
+
if (error.response) {
|
50
|
+
result.requestId = error.response.requestId;
|
51
|
+
result.statusCode = error.response.statusCode;
|
52
|
+
result.statusDesc = error.response.statusDesc;
|
53
|
+
result.relayPeerCount = error.response.relayPeerCount;
|
54
|
+
}
|
55
|
+
return result;
|
56
|
+
}
|
57
|
+
function validateResponseSemantics(response, query, peerId) {
|
58
|
+
if (!isValidRequestId(response.requestId)) {
|
59
|
+
log.error("Invalid request ID format", { received: response.requestId });
|
60
|
+
throw new LightPushError(ProtocolError.DECODE_FAILED, peerId, response, "Invalid request ID format");
|
61
|
+
}
|
62
|
+
if (response.requestId !== query.query?.requestId &&
|
63
|
+
response.statusCode !== LightPushStatusCodeV3.TOO_MANY_REQUESTS) {
|
64
|
+
log.error("Request ID mismatch", {
|
65
|
+
sent: query.query?.requestId,
|
66
|
+
received: response.requestId
|
67
|
+
});
|
68
|
+
throw new LightPushError(ProtocolError.GENERIC_FAIL, peerId, response, "Request ID mismatch");
|
69
|
+
}
|
70
|
+
if (response.statusDesc && isRLNResponseError(response.statusDesc)) {
|
71
|
+
log.error("Remote peer fault: RLN generation");
|
72
|
+
throw new LightPushError(ProtocolError.RLN_PROOF_GENERATION, peerId, response, "RLN proof generation failed");
|
73
|
+
}
|
74
|
+
}
|
75
|
+
function ensureSuccessStatus(response, peerId) {
|
76
|
+
if (!isSuccessStatusCodeV3(response.statusCode)) {
|
77
|
+
const errorMessage = getLightPushStatusDescriptionV3(response.statusCode, response.statusDesc);
|
78
|
+
log.error("Remote peer rejected the message:", errorMessage);
|
79
|
+
const protocolError = lightPushStatusCodeToProtocolErrorV3(response.statusCode);
|
80
|
+
throw new LightPushError(protocolError, peerId, response, errorMessage);
|
81
|
+
}
|
82
|
+
}
|
83
|
+
function logResponseInfo(response) {
|
84
|
+
if (response.statusCode === LightPushStatusCodeV3.TOO_MANY_REQUESTS) {
|
85
|
+
if (response.requestId === "N/A") {
|
86
|
+
log.warn("Rate limited by nwaku node", {
|
87
|
+
statusDesc: response.statusDesc || "Request rejected due to too many requests"
|
88
|
+
});
|
89
|
+
}
|
90
|
+
}
|
91
|
+
if (response.relayPeerCount !== undefined) {
|
92
|
+
log.info(`Message relayed to ${response.relayPeerCount} peers`);
|
93
|
+
}
|
94
|
+
}
|
95
|
+
export class LightPushCoreV3 extends BaseProtocol {
|
96
|
+
pubsubTopics;
|
97
|
+
constructor(pubsubTopics, libp2p) {
|
98
|
+
super(LightPushCodecV3, libp2p.components, pubsubTopics);
|
99
|
+
this.pubsubTopics = pubsubTopics;
|
100
|
+
}
|
101
|
+
async send(encoder, message, peerId) {
|
102
|
+
try {
|
103
|
+
const validated = await this.validateAndPrepareMessage(encoder, message, peerId);
|
104
|
+
const streamResponse = await this.executeRequest(validated, peerId);
|
105
|
+
const finalResponse = this.processResponse(streamResponse, peerId);
|
106
|
+
logResponseInfo(finalResponse);
|
107
|
+
return createSuccessResult(peerId, finalResponse);
|
108
|
+
}
|
109
|
+
catch (error) {
|
110
|
+
if (error instanceof LightPushError) {
|
111
|
+
return createFailureResult(error);
|
112
|
+
}
|
113
|
+
log.error("Unexpected error in send", error);
|
114
|
+
return createFailureResult(new LightPushError(ProtocolError.GENERIC_FAIL, peerId));
|
115
|
+
}
|
116
|
+
}
|
117
|
+
async validateAndPrepareMessage(encoder, message, peerId) {
|
118
|
+
const validationError = await validateMessage(message, encoder);
|
119
|
+
if (validationError) {
|
120
|
+
throw new LightPushError(validationError, peerId);
|
121
|
+
}
|
122
|
+
const protoMessage = await encoder.toProtoObj(message);
|
123
|
+
if (!protoMessage) {
|
124
|
+
log.error("Failed to encode to protoMessage, aborting push");
|
125
|
+
throw new LightPushError(ProtocolError.ENCODE_FAILED, peerId);
|
126
|
+
}
|
127
|
+
const query = PushRpcV3.createRequest(protoMessage, encoder.pubsubTopic);
|
128
|
+
return { protoMessage: protoMessage, query };
|
129
|
+
}
|
130
|
+
async executeRequest(validated, peerId) {
|
131
|
+
const stream = await this.acquireStream(peerId);
|
132
|
+
try {
|
133
|
+
const rawResponse = await this.sendAndReceive(validated.query, stream);
|
134
|
+
const response = this.decodeResponse(rawResponse, peerId);
|
135
|
+
return { response, query: validated.query };
|
136
|
+
}
|
137
|
+
finally {
|
138
|
+
void stream.close();
|
139
|
+
}
|
140
|
+
}
|
141
|
+
processResponse(streamResponse, peerId) {
|
142
|
+
const { response, query } = streamResponse;
|
143
|
+
validateResponseSemantics(response, query, peerId);
|
144
|
+
ensureSuccessStatus(response, peerId);
|
145
|
+
return response;
|
146
|
+
}
|
147
|
+
async acquireStream(peerId) {
|
148
|
+
try {
|
149
|
+
return await this.getStream(peerId);
|
150
|
+
}
|
151
|
+
catch (error) {
|
152
|
+
log.error("Failed to get stream", error);
|
153
|
+
throw new LightPushError(ProtocolError.NO_STREAM_AVAILABLE, peerId);
|
154
|
+
}
|
155
|
+
}
|
156
|
+
async sendAndReceive(query, stream) {
|
157
|
+
try {
|
158
|
+
return await withTimeout(pipe([query.encode()], lp.encode, stream, lp.decode, async (source) => await all(source)), STREAM_TIMEOUT_MS);
|
159
|
+
}
|
160
|
+
catch (err) {
|
161
|
+
log.error("Failed to send waku light push request", err);
|
162
|
+
throw new Error("Stream operation failed");
|
163
|
+
}
|
164
|
+
}
|
165
|
+
decodeResponse(rawResponse, peerId) {
|
166
|
+
const bytes = rawResponse.reduce((acc, chunk) => {
|
167
|
+
acc.append(chunk);
|
168
|
+
return acc;
|
169
|
+
}, new Uint8ArrayList());
|
170
|
+
try {
|
171
|
+
const response = proto_lightpush_v3.LightpushResponse.decode(bytes);
|
172
|
+
if (!response) {
|
173
|
+
log.error("Remote peer fault: No response received");
|
174
|
+
throw new LightPushError(ProtocolError.NO_RESPONSE, peerId);
|
175
|
+
}
|
176
|
+
return response;
|
177
|
+
}
|
178
|
+
catch (err) {
|
179
|
+
if (err instanceof LightPushError) {
|
180
|
+
throw err;
|
181
|
+
}
|
182
|
+
log.error("Failed to decode push response", err);
|
183
|
+
throw new LightPushError(ProtocolError.DECODE_FAILED, peerId);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
}
|
187
|
+
//# sourceMappingURL=light_push_v3.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"light_push_v3.js","sourceRoot":"","sources":["../../../src/lib/light_push/light_push_v3.ts"],"names":[],"mappings":"AACA,OAAO,EACL,+BAA+B,EAI/B,qBAAqB,EAErB,gBAAgB,EAEhB,oCAAoC,EACpC,qBAAqB,EACrB,aAAa,EAEd,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,kBAAkB,EAAe,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,GAAG,MAAM,QAAQ,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACzC,OAAO,EAA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C,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,GAAG,YAAY,cAAc,EAAE,CAAC;gBAClC,MAAM,GAAG,CAAC;YACZ,CAAC;YACD,GAAG,CAAC,KAAK,CAAC,gCAAgC,EAAE,GAAG,CAAC,CAAC;YACjD,MAAM,IAAI,cAAc,CAAC,aAAa,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;CACF"}
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import { proto_lightpush as proto } from "@waku/proto";
|
2
2
|
import type { Uint8ArrayList } from "uint8arraylist";
|
3
|
-
export declare class
|
3
|
+
export declare class PushRpcV2 {
|
4
4
|
proto: proto.PushRpc;
|
5
5
|
constructor(proto: proto.PushRpc);
|
6
|
-
static createRequest(message: proto.WakuMessage, pubsubTopic: string):
|
7
|
-
static decode(bytes: Uint8ArrayList):
|
6
|
+
static createRequest(message: proto.WakuMessage, pubsubTopic: string): PushRpcV2;
|
7
|
+
static decode(bytes: Uint8ArrayList): PushRpcV2;
|
8
8
|
encode(): Uint8Array;
|
9
9
|
get query(): proto.PushRequest | undefined;
|
10
10
|
get response(): proto.PushResponse | undefined;
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import { proto_lightpush as proto } from "@waku/proto";
|
2
2
|
import { v4 as uuid } from "uuid";
|
3
|
-
export class
|
3
|
+
export class PushRpcV2 {
|
4
4
|
proto;
|
5
5
|
constructor(proto) {
|
6
6
|
this.proto = proto;
|
7
7
|
}
|
8
8
|
static createRequest(message, pubsubTopic) {
|
9
|
-
return new
|
9
|
+
return new PushRpcV2({
|
10
10
|
requestId: uuid(),
|
11
11
|
request: {
|
12
12
|
message: message,
|
@@ -17,7 +17,7 @@ export class PushRpc {
|
|
17
17
|
}
|
18
18
|
static decode(bytes) {
|
19
19
|
const res = proto.PushRpc.decode(bytes);
|
20
|
-
return new
|
20
|
+
return new PushRpcV2(res);
|
21
21
|
}
|
22
22
|
encode() {
|
23
23
|
return proto.PushRpc.encode(this.proto);
|
@@ -29,4 +29,4 @@ export class PushRpc {
|
|
29
29
|
return this.proto.response;
|
30
30
|
}
|
31
31
|
}
|
32
|
-
//# sourceMappingURL=
|
32
|
+
//# sourceMappingURL=push_rpc_v2.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"push_rpc_v2.js","sourceRoot":"","sources":["../../../src/lib/light_push/push_rpc_v2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,IAAI,KAAK,EAAE,MAAM,aAAa,CAAC;AAEvD,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAElC,MAAM,OAAO,SAAS;IACM;IAA1B,YAA0B,KAAoB;QAApB,UAAK,GAAL,KAAK,CAAe;IAAG,CAAC;IAE3C,MAAM,CAAC,aAAa,CACzB,OAA0B,EAC1B,WAAmB;QAEnB,OAAO,IAAI,SAAS,CAAC;YACnB,SAAS,EAAE,IAAI,EAAE;YACjB,OAAO,EAAE;gBACP,OAAO,EAAE,OAAO;gBAChB,WAAW,EAAE,WAAW;aACzB;YACD,QAAQ,EAAE,SAAS;SACpB,CAAC,CAAC;IACL,CAAC;IAEM,MAAM,CAAC,MAAM,CAAC,KAAqB;QACxC,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACxC,OAAO,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;IAEM,MAAM;QACX,OAAO,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;IAC5B,CAAC;IAED,IAAW,QAAQ;QACjB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;IAC7B,CAAC;CACF"}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { proto_lightpush_v3, WakuMessage } from "@waku/proto";
|
2
|
+
import type { Uint8ArrayList } from "uint8arraylist";
|
3
|
+
export declare class PushRpcV3 {
|
4
|
+
request?: proto_lightpush_v3.LightpushRequest;
|
5
|
+
response?: proto_lightpush_v3.LightpushResponse;
|
6
|
+
private constructor();
|
7
|
+
static createRequest(message: WakuMessage, pubsubTopic?: string): PushRpcV3;
|
8
|
+
static decode(bytes: Uint8ArrayList | Uint8Array): PushRpcV3;
|
9
|
+
encode(): Uint8Array;
|
10
|
+
get query(): proto_lightpush_v3.LightpushRequest | undefined;
|
11
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import { proto_lightpush_v3 } from "@waku/proto";
|
2
|
+
import { v4 as uuid } from "uuid";
|
3
|
+
export class PushRpcV3 {
|
4
|
+
request;
|
5
|
+
response;
|
6
|
+
constructor(request, response) {
|
7
|
+
this.request = request;
|
8
|
+
this.response = response;
|
9
|
+
}
|
10
|
+
static createRequest(message, pubsubTopic) {
|
11
|
+
const request = {
|
12
|
+
requestId: uuid(),
|
13
|
+
message: message,
|
14
|
+
...(pubsubTopic && { pubsubTopic })
|
15
|
+
};
|
16
|
+
return new PushRpcV3(request, undefined);
|
17
|
+
}
|
18
|
+
static decode(bytes) {
|
19
|
+
const response = proto_lightpush_v3.LightpushResponse.decode(bytes);
|
20
|
+
return new PushRpcV3(undefined, response);
|
21
|
+
}
|
22
|
+
encode() {
|
23
|
+
if (!this.request) {
|
24
|
+
throw new Error("Cannot encode without a request");
|
25
|
+
}
|
26
|
+
return proto_lightpush_v3.LightpushRequest.encode(this.request);
|
27
|
+
}
|
28
|
+
get query() {
|
29
|
+
return this.request;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
//# sourceMappingURL=push_rpc_v3.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"push_rpc_v3.js","sourceRoot":"","sources":["../../../src/lib/light_push/push_rpc_v3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAe,MAAM,aAAa,CAAC;AAE9D,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAElC,MAAM,OAAO,SAAS;IACb,OAAO,CAAuC;IAC9C,QAAQ,CAAwC;IAEvD,YACE,OAA6C,EAC7C,QAA+C;QAE/C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAEM,MAAM,CAAC,aAAa,CACzB,OAAoB,EACpB,WAAoB;QAEpB,MAAM,OAAO,GAAwC;YACnD,SAAS,EAAE,IAAI,EAAE;YACjB,OAAO,EAAE,OAAO;YAChB,GAAG,CAAC,WAAW,IAAI,EAAE,WAAW,EAAE,CAAC;SACpC,CAAC;QAEF,OAAO,IAAI,SAAS,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAC3C,CAAC;IAEM,MAAM,CAAC,MAAM,CAAC,KAAkC;QACrD,MAAM,QAAQ,GAAG,kBAAkB,CAAC,iBAAiB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACpE,OAAO,IAAI,SAAS,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAC5C,CAAC;IAEM,MAAM;QACX,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACrD,CAAC;QACD,OAAO,kBAAkB,CAAC,gBAAgB,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAClE,CAAC;IAED,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF"}
|
@@ -1 +1,25 @@
|
|
1
|
+
import type { IEncoder, IMessage } from "@waku/interfaces";
|
2
|
+
import { ProtocolError } from "@waku/interfaces";
|
3
|
+
/**
|
4
|
+
* Validates a Light Push v3 request ID format.
|
5
|
+
* Request IDs should be valid UUIDs, with special handling for "N/A" when rate limited.
|
6
|
+
*
|
7
|
+
* @param requestId - The request ID to validate
|
8
|
+
* @returns true if the request ID is valid, false otherwise
|
9
|
+
*/
|
10
|
+
export declare function isValidRequestId(requestId: string | undefined): boolean;
|
1
11
|
export declare const isRLNResponseError: (info?: string) => boolean;
|
12
|
+
export declare function mapInfoToProtocolError(info?: string): ProtocolError;
|
13
|
+
/**
|
14
|
+
* Validates that a message conforms to Light Push protocol requirements.
|
15
|
+
*
|
16
|
+
* @param message - The message to validate
|
17
|
+
* @param encoder - The encoder to use for message size validation
|
18
|
+
* @returns A ProtocolError if validation fails, null if the message is valid
|
19
|
+
*
|
20
|
+
* @remarks
|
21
|
+
* This function performs the following validations:
|
22
|
+
* - Checks if the message payload exists and is not empty
|
23
|
+
* - Verifies the encoded message size is under the network cap (1MB)
|
24
|
+
*/
|
25
|
+
export declare function validateMessage(message: IMessage, encoder: IEncoder): Promise<ProtocolError | null>;
|
@@ -1,3 +1,21 @@
|
|
1
|
+
import { ProtocolError } from "@waku/interfaces";
|
2
|
+
import { isMessageSizeUnderCap, Logger } from "@waku/utils";
|
3
|
+
const log = new Logger("waku:light-push:utils");
|
4
|
+
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
5
|
+
/**
|
6
|
+
* Validates a Light Push v3 request ID format.
|
7
|
+
* Request IDs should be valid UUIDs, with special handling for "N/A" when rate limited.
|
8
|
+
*
|
9
|
+
* @param requestId - The request ID to validate
|
10
|
+
* @returns true if the request ID is valid, false otherwise
|
11
|
+
*/
|
12
|
+
export function isValidRequestId(requestId) {
|
13
|
+
if (!requestId)
|
14
|
+
return false;
|
15
|
+
if (requestId === "N/A")
|
16
|
+
return true; // Special case for rate-limited responses
|
17
|
+
return UUID_REGEX.test(requestId);
|
18
|
+
}
|
1
19
|
// should match nwaku
|
2
20
|
// https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/rln_relay.nim#L309
|
3
21
|
// https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/tests/waku_rln_relay/rln/waku_rln_relay_utils.nim#L20
|
@@ -15,4 +33,56 @@ export const isRLNResponseError = (info) => {
|
|
15
33
|
info.includes(RLN_MESSAGE_ID_PREFIX_ERROR) ||
|
16
34
|
info.includes(RLN_REMOTE_VALIDATION));
|
17
35
|
};
|
36
|
+
export function mapInfoToProtocolError(info) {
|
37
|
+
if (!info) {
|
38
|
+
return ProtocolError.REMOTE_PEER_REJECTED;
|
39
|
+
}
|
40
|
+
const lowerInfo = info.toLowerCase();
|
41
|
+
if (isRLNResponseError(info)) {
|
42
|
+
return ProtocolError.RLN_PROOF_GENERATION;
|
43
|
+
}
|
44
|
+
if (lowerInfo.includes("rate limit") ||
|
45
|
+
lowerInfo.includes("too many requests")) {
|
46
|
+
return ProtocolError.REMOTE_PEER_REJECTED;
|
47
|
+
}
|
48
|
+
if (lowerInfo.includes("topic") &&
|
49
|
+
(lowerInfo.includes("not found") || lowerInfo.includes("not configured"))) {
|
50
|
+
return ProtocolError.TOPIC_NOT_CONFIGURED;
|
51
|
+
}
|
52
|
+
if (lowerInfo.includes("too large") || lowerInfo.includes("size")) {
|
53
|
+
return ProtocolError.SIZE_TOO_BIG;
|
54
|
+
}
|
55
|
+
if (lowerInfo.includes("decode") ||
|
56
|
+
lowerInfo.includes("invalid") ||
|
57
|
+
lowerInfo.includes("malformed")) {
|
58
|
+
return ProtocolError.DECODE_FAILED;
|
59
|
+
}
|
60
|
+
if (lowerInfo.includes("empty") && lowerInfo.includes("payload")) {
|
61
|
+
return ProtocolError.EMPTY_PAYLOAD;
|
62
|
+
}
|
63
|
+
return ProtocolError.REMOTE_PEER_REJECTED;
|
64
|
+
}
|
65
|
+
/**
|
66
|
+
* Validates that a message conforms to Light Push protocol requirements.
|
67
|
+
*
|
68
|
+
* @param message - The message to validate
|
69
|
+
* @param encoder - The encoder to use for message size validation
|
70
|
+
* @returns A ProtocolError if validation fails, null if the message is valid
|
71
|
+
*
|
72
|
+
* @remarks
|
73
|
+
* This function performs the following validations:
|
74
|
+
* - Checks if the message payload exists and is not empty
|
75
|
+
* - Verifies the encoded message size is under the network cap (1MB)
|
76
|
+
*/
|
77
|
+
export async function validateMessage(message, encoder) {
|
78
|
+
if (!message.payload?.length) {
|
79
|
+
log.error("Failed to send waku light push: payload is empty");
|
80
|
+
return ProtocolError.EMPTY_PAYLOAD;
|
81
|
+
}
|
82
|
+
if (!(await isMessageSizeUnderCap(encoder, message))) {
|
83
|
+
log.error("Failed to send waku light push: message is bigger than 1MB");
|
84
|
+
return ProtocolError.SIZE_TOO_BIG;
|
85
|
+
}
|
86
|
+
return null;
|
87
|
+
}
|
18
88
|
//# sourceMappingURL=utils.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/light_push/utils.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/light_push/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE5D,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,uBAAuB,CAAC,CAAC;AAEhD,MAAM,UAAU,GACd,iEAAiE,CAAC;AAEpE;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAA6B;IAC5D,IAAI,CAAC,SAAS;QAAE,OAAO,KAAK,CAAC;IAC7B,IAAI,SAAS,KAAK,KAAK;QAAE,OAAO,IAAI,CAAC,CAAC,0CAA0C;IAChF,OAAO,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;AACpC,CAAC;AAED,qBAAqB;AACrB,yHAAyH;AACzH,wIAAwI;AACxI,MAAM,2BAA2B,GAAG,8BAA8B,CAAC;AACnE,MAAM,2BAA2B,GAC/B,uD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C,QAAQ,CAAC,QAAQ,CAAC;QAC5B,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC;QAC7B,SAAS,CAAC,QAAQ,CAAC,WAAW,CAAC,EAC/B,CAAC;QACD,OAAO,aAAa,CAAC,aAAa,CAAC;IACrC,CAAC;IAED,IAAI,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QACjE,OAAO,aAAa,CAAC,aAAa,CAAC;IACrC,CAAC;IAED,OAAO,aAAa,CAAC,oBAAoB,CAAC;AAC5C,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,OAAiB,EACjB,OAAiB;IAEjB,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;QAC7B,GAAG,CAAC,KAAK,CAAC,kDAAkD,CAAC,CAAC;QAC9D,OAAO,aAAa,CAAC,aAAa,CAAC;IACrC,CAAC;IAED,IAAI,CAAC,CAAC,MAAM,qBAAqB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC;QACrD,GAAG,CAAC,KAAK,CAAC,4DAA4D,CAAC,CAAC;QACxE,OAAO,aAAa,CAAC,YAAY,CAAC;IACpC,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/package.json
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"name":"@waku/core","version":"0.0.36-
|
1
|
+
{"name":"@waku/core","version":"0.0.36-b0a2e39.0","description":"TypeScript implementation of the Waku v2 protocol","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"},"./lib/message/version_0":{"types":"./dist/lib/message/version_0.d.ts","import":"./dist/lib/message/version_0.js"},"./lib/base_protocol":{"types":"./dist/lib/base_protocol.d.ts","import":"./dist/lib/base_protocol.js"}},"typesVersions":{"*":{"lib/*":["dist/lib/*"],"constants/*":["dist/constants/*"]}},"type":"module","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/core#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralised","communication","web3","ethereum","dapps"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:tsc":"tsc -p tsconfig.dev.json","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","test":"NODE_ENV=test run-s test:*","test:node":"NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha","test:browser":"NODE_ENV=test karma start karma.conf.cjs","watch:build":"tsc -p tsconfig.json -w","watch:test":"mocha --watch","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=20"},"dependencies":{"@waku/enr":"0.0.30-b0a2e39.0","@waku/interfaces":"0.0.31-b0a2e39.0","@libp2p/ping":"2.0.1","@waku/proto":"0.0.11-b0a2e39.0","@waku/utils":"0.0.24-b0a2e39.0","debug":"^4.3.4","@noble/hashes":"^1.3.2","it-all":"^3.0.4","it-length-prefixed":"^9.0.4","it-pipe":"^3.0.1","uint8arraylist":"^2.4.3","uuid":"^9.0.0"},"devDependencies":{"@libp2p/peer-id":"^5.0.1","@libp2p/interface":"^2.1.3","@multiformats/multiaddr":"^12.0.0","@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^4.3.11","@types/debug":"^4.1.12","@types/mocha":"^10.0.6","@types/uuid":"^9.0.8","@waku/build-utils":"*","chai":"^4.3.10","sinon":"^18.0.0","cspell":"^8.6.1","fast-check":"^3.19.0","ignore-loader":"^0.1.2","isomorphic-fetch":"^3.0.0","mocha":"^10.3.0","npm-run-all":"^4.1.5","process":"^0.11.10","rollup":"^4.12.0"},"peerDependencies":{"@multiformats/multiaddr":"^12.0.0","libp2p":"2.1.8"},"peerDependenciesMeta":{"@multiformats/multiaddr":{"optional":true},"libp2p":{"optional":true}},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
|
package/src/index.ts
CHANGED
@@ -10,7 +10,11 @@ export * as waku_filter from "./lib/filter/index.js";
|
|
10
10
|
export { FilterCore, FilterCodecs } from "./lib/filter/index.js";
|
11
11
|
|
12
12
|
export * as waku_light_push from "./lib/light_push/index.js";
|
13
|
-
export {
|
13
|
+
export {
|
14
|
+
LightPushCodec,
|
15
|
+
LightPushCore,
|
16
|
+
LightPushCoreV3
|
17
|
+
} from "./lib/light_push/index.js";
|
14
18
|
|
15
19
|
export * as waku_store from "./lib/store/index.js";
|
16
20
|
export { StoreCore, StoreCodec } from "./lib/store/index.js";
|
@@ -267,7 +267,7 @@ export class ConnectionManager
|
|
267
267
|
* // Dial using multiaddr with specific protocols
|
268
268
|
* await connectionManager.dialPeer(multiaddr, [
|
269
269
|
* "/vac/waku/relay/2.0.0",
|
270
|
-
* "/vac/waku/lightpush/
|
270
|
+
* "/vac/waku/lightpush/3.0.0"
|
271
271
|
* ]);
|
272
272
|
* ```
|
273
273
|
*
|
@@ -1 +1,2 @@
|
|
1
|
-
export { LightPushCore, LightPushCodec
|
1
|
+
export { LightPushCore, LightPushCodec } from "./light_push.js";
|
2
|
+
export { LightPushCoreV3 } from "./light_push_v3.js";
|
@@ -1,16 +1,15 @@
|
|
1
1
|
import type { PeerId, Stream } from "@libp2p/interface";
|
2
2
|
import {
|
3
|
-
type CoreProtocolResult,
|
4
3
|
type IBaseProtocolCore,
|
5
4
|
type IEncoder,
|
6
5
|
type IMessage,
|
7
6
|
type Libp2p,
|
7
|
+
type LightPushResult,
|
8
8
|
ProtocolError,
|
9
9
|
PubsubTopic,
|
10
10
|
type ThisOrThat
|
11
11
|
} from "@waku/interfaces";
|
12
|
-
import {
|
13
|
-
import { isMessageSizeUnderCap } from "@waku/utils";
|
12
|
+
import { proto_lightpush as proto_lightpush_v2 } from "@waku/proto";
|
14
13
|
import { Logger } from "@waku/utils";
|
15
14
|
import all from "it-all";
|
16
15
|
import * as lp from "it-length-prefixed";
|
@@ -19,19 +18,13 @@ import { Uint8ArrayList } from "uint8arraylist";
|
|
19
18
|
|
20
19
|
import { BaseProtocol } from "../base_protocol.js";
|
21
20
|
|
22
|
-
import {
|
23
|
-
import {
|
21
|
+
import { PushRpcV2 } from "./push_rpc_v2.js";
|
22
|
+
import { mapInfoToProtocolError, validateMessage } from "./utils.js";
|
24
23
|
|
25
24
|
const log = new Logger("light-push");
|
26
25
|
|
27
26
|
export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
28
|
-
export { PushResponse };
|
29
27
|
|
30
|
-
type PreparePushMessageResult = ThisOrThat<"query", PushRpc>;
|
31
|
-
|
32
|
-
/**
|
33
|
-
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
34
|
-
*/
|
35
28
|
export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
36
29
|
public constructor(
|
37
30
|
public readonly pubsubTopics: PubsubTopic[],
|
@@ -43,16 +36,11 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
43
36
|
private async preparePushMessage(
|
44
37
|
encoder: IEncoder,
|
45
38
|
message: IMessage
|
46
|
-
): Promise<
|
39
|
+
): Promise<ThisOrThat<"query", PushRpcV2>> {
|
47
40
|
try {
|
48
|
-
|
49
|
-
|
50
|
-
return { query: null, error:
|
51
|
-
}
|
52
|
-
|
53
|
-
if (!(await isMessageSizeUnderCap(encoder, message))) {
|
54
|
-
log.error("Failed to send waku light push: message is bigger than 1MB");
|
55
|
-
return { query: null, error: ProtocolError.SIZE_TOO_BIG };
|
41
|
+
const validationError = await validateMessage(message, encoder);
|
42
|
+
if (validationError) {
|
43
|
+
return { query: null, error: validationError };
|
56
44
|
}
|
57
45
|
|
58
46
|
const protoMessage = await encoder.toProtoObj(message);
|
@@ -64,7 +52,7 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
64
52
|
};
|
65
53
|
}
|
66
54
|
|
67
|
-
const query =
|
55
|
+
const query = PushRpcV2.createRequest(protoMessage, encoder.pubsubTopic);
|
68
56
|
return { query, error: null };
|
69
57
|
} catch (error) {
|
70
58
|
log.error("Failed to prepare push message", error);
|
@@ -80,7 +68,7 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
80
68
|
encoder: IEncoder,
|
81
69
|
message: IMessage,
|
82
70
|
peerId: PeerId
|
83
|
-
): Promise<
|
71
|
+
): Promise<LightPushResult> {
|
84
72
|
const { query, error: preparationError } = await this.preparePushMessage(
|
85
73
|
encoder,
|
86
74
|
message
|
@@ -120,7 +108,6 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
120
108
|
async (source) => await all(source)
|
121
109
|
);
|
122
110
|
} catch (err) {
|
123
|
-
// can fail only because of `stream` abortion
|
124
111
|
log.error("Failed to send waku light push request", err);
|
125
112
|
return {
|
126
113
|
success: null,
|
@@ -131,14 +118,14 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
131
118
|
};
|
132
119
|
}
|
133
120
|
|
134
|
-
const bytes =
|
135
|
-
|
136
|
-
|
137
|
-
});
|
121
|
+
const bytes = res.reduce((acc, chunk) => {
|
122
|
+
acc.append(chunk);
|
123
|
+
return acc;
|
124
|
+
}, new Uint8ArrayList());
|
138
125
|
|
139
|
-
let response: PushResponse | undefined;
|
126
|
+
let response: proto_lightpush_v2.PushResponse | undefined;
|
140
127
|
try {
|
141
|
-
response =
|
128
|
+
response = PushRpcV2.decode(bytes).response;
|
142
129
|
} catch (err) {
|
143
130
|
log.error("Failed to decode push reply", err);
|
144
131
|
return {
|
@@ -161,28 +148,24 @@ export class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
|
161
148
|
};
|
162
149
|
}
|
163
150
|
|
164
|
-
if (isRLNResponseError(response.info)) {
|
165
|
-
log.error("Remote peer fault: RLN generation");
|
166
|
-
return {
|
167
|
-
success: null,
|
168
|
-
failure: {
|
169
|
-
error: ProtocolError.RLN_PROOF_GENERATION,
|
170
|
-
peerId: peerId
|
171
|
-
}
|
172
|
-
};
|
173
|
-
}
|
174
|
-
|
175
151
|
if (!response.isSuccess) {
|
176
|
-
|
152
|
+
const errorMessage = response.info || "Message rejected";
|
153
|
+
log.error("Remote peer rejected the message: ", errorMessage);
|
154
|
+
|
155
|
+
const error = mapInfoToProtocolError(response.info);
|
156
|
+
|
177
157
|
return {
|
178
158
|
success: null,
|
179
159
|
failure: {
|
180
|
-
error:
|
160
|
+
error: error,
|
181
161
|
peerId: peerId
|
182
162
|
}
|
183
163
|
};
|
184
164
|
}
|
185
165
|
|
186
|
-
return {
|
166
|
+
return {
|
167
|
+
success: peerId,
|
168
|
+
failure: null
|
169
|
+
};
|
187
170
|
}
|
188
171
|
}
|