@waku/core 0.0.34-c43cec2.0 → 0.0.34-caeafce.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-DxFKDXX2.js → base_protocol-Bp5a9PNG.js} +19 -20
- package/bundle/{index-yLOEQnIE.js → index-G1eRBjeI.js} +115 -84
- package/bundle/index.js +1828 -153
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-Che4t3mN.js → version_0-DJZG2fB2.js} +163 -42
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +2 -2
- package/dist/lib/base_protocol.js +2 -2
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/connection_manager/connection_manager.d.ts +56 -3
- package/dist/lib/connection_manager/connection_manager.js +96 -13
- package/dist/lib/connection_manager/connection_manager.js.map +1 -1
- package/dist/lib/filter/filter.d.ts +18 -0
- package/dist/lib/filter/filter.js +209 -0
- package/dist/lib/filter/filter.js.map +1 -0
- package/dist/lib/filter/index.d.ts +1 -18
- package/dist/lib/filter/index.js +1 -208
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/light_push/index.d.ts +1 -15
- package/dist/lib/light_push/index.js +1 -144
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/light_push/light_push.d.ts +15 -0
- package/dist/lib/light_push/light_push.js +144 -0
- package/dist/lib/light_push/light_push.js.map +1 -0
- package/dist/lib/light_push/utils.d.ts +0 -2
- package/dist/lib/light_push/utils.js +9 -17
- package/dist/lib/light_push/utils.js.map +1 -1
- package/dist/lib/metadata/index.d.ts +1 -3
- package/dist/lib/metadata/index.js +1 -118
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/metadata/metadata.d.ts +3 -0
- package/dist/lib/metadata/metadata.js +119 -0
- package/dist/lib/metadata/metadata.js.map +1 -0
- package/dist/lib/store/index.d.ts +1 -9
- package/dist/lib/store/index.js +1 -82
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/store/store.d.ts +9 -0
- package/dist/lib/store/store.js +83 -0
- package/dist/lib/store/store.js.map +1 -0
- package/dist/lib/stream_manager/stream_manager.d.ts +2 -2
- package/dist/lib/stream_manager/stream_manager.js +16 -17
- package/dist/lib/stream_manager/stream_manager.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +0 -2
- package/src/lib/base_protocol.ts +3 -3
- package/src/lib/connection_manager/connection_manager.ts +114 -20
- package/src/lib/filter/filter.ts +315 -0
- package/src/lib/filter/index.ts +1 -315
- package/src/lib/light_push/index.ts +1 -189
- package/src/lib/light_push/light_push.ts +188 -0
- package/src/lib/light_push/utils.ts +13 -21
- package/src/lib/metadata/index.ts +1 -182
- package/src/lib/metadata/metadata.ts +182 -0
- package/src/lib/store/index.ts +1 -136
- package/src/lib/store/store.ts +136 -0
- package/src/lib/stream_manager/stream_manager.ts +16 -18
- package/dist/lib/health_manager.d.ts +0 -14
- package/dist/lib/health_manager.js +0 -70
- package/dist/lib/health_manager.js.map +0 -1
- package/src/lib/health_manager.ts +0 -90
package/dist/lib/filter/index.js
CHANGED
@@ -1,209 +1,2 @@
|
|
1
|
-
|
2
|
-
import { Logger } from "@waku/utils";
|
3
|
-
import all from "it-all";
|
4
|
-
import * as lp from "it-length-prefixed";
|
5
|
-
import { pipe } from "it-pipe";
|
6
|
-
import { BaseProtocol } from "../base_protocol.js";
|
7
|
-
import { FilterPushRpc, FilterSubscribeResponse, FilterSubscribeRpc } from "./filter_rpc.js";
|
8
|
-
const log = new Logger("filter:v2");
|
9
|
-
export const FilterCodecs = {
|
10
|
-
SUBSCRIBE: "/vac/waku/filter-subscribe/2.0.0-beta1",
|
11
|
-
PUSH: "/vac/waku/filter-push/2.0.0-beta1"
|
12
|
-
};
|
13
|
-
export class FilterCore extends BaseProtocol {
|
14
|
-
handleIncomingMessage;
|
15
|
-
pubsubTopics;
|
16
|
-
constructor(handleIncomingMessage, pubsubTopics, libp2p) {
|
17
|
-
super(FilterCodecs.SUBSCRIBE, libp2p.components, pubsubTopics);
|
18
|
-
this.handleIncomingMessage = handleIncomingMessage;
|
19
|
-
this.pubsubTopics = pubsubTopics;
|
20
|
-
libp2p
|
21
|
-
.handle(FilterCodecs.PUSH, this.onRequest.bind(this), {
|
22
|
-
maxInboundStreams: 100
|
23
|
-
})
|
24
|
-
.catch((e) => {
|
25
|
-
log.error("Failed to register ", FilterCodecs.PUSH, e);
|
26
|
-
});
|
27
|
-
}
|
28
|
-
async subscribe(pubsubTopic, peer, contentTopics) {
|
29
|
-
const stream = await this.getStream(peer);
|
30
|
-
const request = FilterSubscribeRpc.createSubscribeRequest(pubsubTopic, contentTopics);
|
31
|
-
let res;
|
32
|
-
try {
|
33
|
-
res = await pipe([request.encode()], lp.encode, stream, lp.decode, async (source) => await all(source));
|
34
|
-
}
|
35
|
-
catch (error) {
|
36
|
-
log.error("Failed to send subscribe request", error);
|
37
|
-
return {
|
38
|
-
success: null,
|
39
|
-
failure: {
|
40
|
-
error: ProtocolError.GENERIC_FAIL,
|
41
|
-
peerId: peer.id
|
42
|
-
}
|
43
|
-
};
|
44
|
-
}
|
45
|
-
const { statusCode, requestId, statusDesc } = FilterSubscribeResponse.decode(res[0].slice());
|
46
|
-
if (statusCode < 200 || statusCode >= 300) {
|
47
|
-
log.error(`Filter subscribe request ${requestId} failed with status code ${statusCode}: ${statusDesc}`);
|
48
|
-
return {
|
49
|
-
failure: {
|
50
|
-
error: ProtocolError.REMOTE_PEER_REJECTED,
|
51
|
-
peerId: peer.id
|
52
|
-
},
|
53
|
-
success: null
|
54
|
-
};
|
55
|
-
}
|
56
|
-
return {
|
57
|
-
failure: null,
|
58
|
-
success: peer.id
|
59
|
-
};
|
60
|
-
}
|
61
|
-
async unsubscribe(pubsubTopic, peer, contentTopics) {
|
62
|
-
let stream;
|
63
|
-
try {
|
64
|
-
stream = await this.getStream(peer);
|
65
|
-
}
|
66
|
-
catch (error) {
|
67
|
-
log.error(`Failed to get a stream for remote peer${peer.id.toString()}`, error);
|
68
|
-
return {
|
69
|
-
success: null,
|
70
|
-
failure: {
|
71
|
-
error: ProtocolError.NO_STREAM_AVAILABLE,
|
72
|
-
peerId: peer.id
|
73
|
-
}
|
74
|
-
};
|
75
|
-
}
|
76
|
-
const unsubscribeRequest = FilterSubscribeRpc.createUnsubscribeRequest(pubsubTopic, contentTopics);
|
77
|
-
try {
|
78
|
-
await pipe([unsubscribeRequest.encode()], lp.encode, stream.sink);
|
79
|
-
}
|
80
|
-
catch (error) {
|
81
|
-
log.error("Failed to send unsubscribe request", error);
|
82
|
-
return {
|
83
|
-
success: null,
|
84
|
-
failure: {
|
85
|
-
error: ProtocolError.GENERIC_FAIL,
|
86
|
-
peerId: peer.id
|
87
|
-
}
|
88
|
-
};
|
89
|
-
}
|
90
|
-
return {
|
91
|
-
success: peer.id,
|
92
|
-
failure: null
|
93
|
-
};
|
94
|
-
}
|
95
|
-
async unsubscribeAll(pubsubTopic, peer) {
|
96
|
-
const stream = await this.getStream(peer);
|
97
|
-
const request = FilterSubscribeRpc.createUnsubscribeAllRequest(pubsubTopic);
|
98
|
-
const res = await pipe([request.encode()], lp.encode, stream, lp.decode, async (source) => await all(source));
|
99
|
-
if (!res || !res.length) {
|
100
|
-
return {
|
101
|
-
failure: {
|
102
|
-
error: ProtocolError.NO_RESPONSE,
|
103
|
-
peerId: peer.id
|
104
|
-
},
|
105
|
-
success: null
|
106
|
-
};
|
107
|
-
}
|
108
|
-
const { statusCode, requestId, statusDesc } = FilterSubscribeResponse.decode(res[0].slice());
|
109
|
-
if (statusCode < 200 || statusCode >= 300) {
|
110
|
-
log.error(`Filter unsubscribe all request ${requestId} failed with status code ${statusCode}: ${statusDesc}`);
|
111
|
-
return {
|
112
|
-
failure: {
|
113
|
-
error: ProtocolError.REMOTE_PEER_REJECTED,
|
114
|
-
peerId: peer.id
|
115
|
-
},
|
116
|
-
success: null
|
117
|
-
};
|
118
|
-
}
|
119
|
-
return {
|
120
|
-
failure: null,
|
121
|
-
success: peer.id
|
122
|
-
};
|
123
|
-
}
|
124
|
-
async ping(peer) {
|
125
|
-
let stream;
|
126
|
-
try {
|
127
|
-
stream = await this.getStream(peer);
|
128
|
-
}
|
129
|
-
catch (error) {
|
130
|
-
log.error(`Failed to get a stream for remote peer${peer.id.toString()}`, error);
|
131
|
-
return {
|
132
|
-
success: null,
|
133
|
-
failure: {
|
134
|
-
error: ProtocolError.NO_STREAM_AVAILABLE,
|
135
|
-
peerId: peer.id
|
136
|
-
}
|
137
|
-
};
|
138
|
-
}
|
139
|
-
const request = FilterSubscribeRpc.createSubscriberPingRequest();
|
140
|
-
let res;
|
141
|
-
try {
|
142
|
-
res = await pipe([request.encode()], lp.encode, stream, lp.decode, async (source) => await all(source));
|
143
|
-
}
|
144
|
-
catch (error) {
|
145
|
-
log.error("Failed to send ping request", error);
|
146
|
-
return {
|
147
|
-
success: null,
|
148
|
-
failure: {
|
149
|
-
error: ProtocolError.GENERIC_FAIL,
|
150
|
-
peerId: peer.id
|
151
|
-
}
|
152
|
-
};
|
153
|
-
}
|
154
|
-
if (!res || !res.length) {
|
155
|
-
return {
|
156
|
-
success: null,
|
157
|
-
failure: {
|
158
|
-
error: ProtocolError.NO_RESPONSE,
|
159
|
-
peerId: peer.id
|
160
|
-
}
|
161
|
-
};
|
162
|
-
}
|
163
|
-
const { statusCode, requestId, statusDesc } = FilterSubscribeResponse.decode(res[0].slice());
|
164
|
-
if (statusCode < 200 || statusCode >= 300) {
|
165
|
-
log.error(`Filter ping request ${requestId} failed with status code ${statusCode}: ${statusDesc}`);
|
166
|
-
return {
|
167
|
-
success: null,
|
168
|
-
failure: {
|
169
|
-
error: ProtocolError.REMOTE_PEER_REJECTED,
|
170
|
-
peerId: peer.id
|
171
|
-
}
|
172
|
-
};
|
173
|
-
}
|
174
|
-
return {
|
175
|
-
success: peer.id,
|
176
|
-
failure: null
|
177
|
-
};
|
178
|
-
}
|
179
|
-
onRequest(streamData) {
|
180
|
-
const { connection, stream } = streamData;
|
181
|
-
const { remotePeer } = connection;
|
182
|
-
log.info(`Received message from ${remotePeer.toString()}`);
|
183
|
-
try {
|
184
|
-
pipe(stream, lp.decode, async (source) => {
|
185
|
-
for await (const bytes of source) {
|
186
|
-
const response = FilterPushRpc.decode(bytes.slice());
|
187
|
-
const { pubsubTopic, wakuMessage } = response;
|
188
|
-
if (!wakuMessage) {
|
189
|
-
log.error("Received empty message");
|
190
|
-
return;
|
191
|
-
}
|
192
|
-
if (!pubsubTopic) {
|
193
|
-
log.error("Pubsub topic missing from push message");
|
194
|
-
return;
|
195
|
-
}
|
196
|
-
await this.handleIncomingMessage(pubsubTopic, wakuMessage, connection.remotePeer.toString());
|
197
|
-
}
|
198
|
-
}).then(() => {
|
199
|
-
log.info("Receiving pipe closed.");
|
200
|
-
}, async (e) => {
|
201
|
-
log.error(`Error with receiving pipe on peer:${connection.remotePeer.toString()} -- stream:${stream.id} -- protocol:${stream.protocol}: `, e);
|
202
|
-
});
|
203
|
-
}
|
204
|
-
catch (e) {
|
205
|
-
log.error("Error decoding message", e);
|
206
|
-
}
|
207
|
-
}
|
208
|
-
}
|
1
|
+
export { FilterCodecs, FilterCore } from "./filter.js";
|
209
2
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/filter/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/filter/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC"}
|
@@ -1,15 +1 @@
|
|
1
|
-
|
2
|
-
import { type CoreProtocolResult, type IBaseProtocolCore, type IEncoder, type IMessage, type Libp2p, PubsubTopic } from "@waku/interfaces";
|
3
|
-
import { PushResponse } from "@waku/proto";
|
4
|
-
import { BaseProtocol } from "../base_protocol.js";
|
5
|
-
export declare const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
6
|
-
export { PushResponse };
|
7
|
-
/**
|
8
|
-
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
9
|
-
*/
|
10
|
-
export declare class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
11
|
-
readonly pubsubTopics: PubsubTopic[];
|
12
|
-
constructor(pubsubTopics: PubsubTopic[], libp2p: Libp2p);
|
13
|
-
private preparePushMessage;
|
14
|
-
send(encoder: IEncoder, message: IMessage, peer: Peer): Promise<CoreProtocolResult>;
|
15
|
-
}
|
1
|
+
export { LightPushCore, LightPushCodec, PushResponse } from "./light_push.js";
|
@@ -1,145 +1,2 @@
|
|
1
|
-
|
2
|
-
import { PushResponse } from "@waku/proto";
|
3
|
-
import { isMessageSizeUnderCap } from "@waku/utils";
|
4
|
-
import { Logger } from "@waku/utils";
|
5
|
-
import all from "it-all";
|
6
|
-
import * as lp from "it-length-prefixed";
|
7
|
-
import { pipe } from "it-pipe";
|
8
|
-
import { Uint8ArrayList } from "uint8arraylist";
|
9
|
-
import { BaseProtocol } from "../base_protocol.js";
|
10
|
-
import { PushRpc } from "./push_rpc.js";
|
11
|
-
import { isRLNResponseError, matchRLNErrorMessage } from "./utils.js";
|
12
|
-
const log = new Logger("light-push");
|
13
|
-
export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
14
|
-
export { PushResponse };
|
15
|
-
/**
|
16
|
-
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
17
|
-
*/
|
18
|
-
export class LightPushCore extends BaseProtocol {
|
19
|
-
pubsubTopics;
|
20
|
-
constructor(pubsubTopics, libp2p) {
|
21
|
-
super(LightPushCodec, libp2p.components, pubsubTopics);
|
22
|
-
this.pubsubTopics = pubsubTopics;
|
23
|
-
}
|
24
|
-
async preparePushMessage(encoder, message) {
|
25
|
-
try {
|
26
|
-
if (!message.payload || message.payload.length === 0) {
|
27
|
-
log.error("Failed to send waku light push: payload is empty");
|
28
|
-
return { query: null, error: ProtocolError.EMPTY_PAYLOAD };
|
29
|
-
}
|
30
|
-
if (!(await isMessageSizeUnderCap(encoder, message))) {
|
31
|
-
log.error("Failed to send waku light push: message is bigger than 1MB");
|
32
|
-
return { query: null, error: ProtocolError.SIZE_TOO_BIG };
|
33
|
-
}
|
34
|
-
const protoMessage = await encoder.toProtoObj(message);
|
35
|
-
if (!protoMessage) {
|
36
|
-
log.error("Failed to encode to protoMessage, aborting push");
|
37
|
-
return {
|
38
|
-
query: null,
|
39
|
-
error: ProtocolError.ENCODE_FAILED
|
40
|
-
};
|
41
|
-
}
|
42
|
-
const query = PushRpc.createRequest(protoMessage, encoder.pubsubTopic);
|
43
|
-
return { query, error: null };
|
44
|
-
}
|
45
|
-
catch (error) {
|
46
|
-
log.error("Failed to prepare push message", error);
|
47
|
-
return {
|
48
|
-
query: null,
|
49
|
-
error: ProtocolError.GENERIC_FAIL
|
50
|
-
};
|
51
|
-
}
|
52
|
-
}
|
53
|
-
// TODO(weboko): use peer.id as parameter instead
|
54
|
-
async send(encoder, message, peer) {
|
55
|
-
const { query, error: preparationError } = await this.preparePushMessage(encoder, message);
|
56
|
-
if (preparationError || !query) {
|
57
|
-
return {
|
58
|
-
success: null,
|
59
|
-
failure: {
|
60
|
-
error: preparationError,
|
61
|
-
peerId: peer.id
|
62
|
-
}
|
63
|
-
};
|
64
|
-
}
|
65
|
-
let stream;
|
66
|
-
try {
|
67
|
-
stream = await this.getStream(peer);
|
68
|
-
}
|
69
|
-
catch (error) {
|
70
|
-
log.error("Failed to get stream", error);
|
71
|
-
return {
|
72
|
-
success: null,
|
73
|
-
failure: {
|
74
|
-
error: ProtocolError.NO_STREAM_AVAILABLE,
|
75
|
-
peerId: peer.id
|
76
|
-
}
|
77
|
-
};
|
78
|
-
}
|
79
|
-
let res;
|
80
|
-
try {
|
81
|
-
res = await pipe([query.encode()], lp.encode, stream, lp.decode, async (source) => await all(source));
|
82
|
-
}
|
83
|
-
catch (err) {
|
84
|
-
log.error("Failed to send waku light push request", err);
|
85
|
-
return {
|
86
|
-
success: null,
|
87
|
-
failure: {
|
88
|
-
error: ProtocolError.GENERIC_FAIL,
|
89
|
-
peerId: peer.id
|
90
|
-
}
|
91
|
-
};
|
92
|
-
}
|
93
|
-
const bytes = new Uint8ArrayList();
|
94
|
-
res.forEach((chunk) => {
|
95
|
-
bytes.append(chunk);
|
96
|
-
});
|
97
|
-
let response;
|
98
|
-
try {
|
99
|
-
response = PushRpc.decode(bytes).response;
|
100
|
-
}
|
101
|
-
catch (err) {
|
102
|
-
log.error("Failed to decode push reply", err);
|
103
|
-
return {
|
104
|
-
success: null,
|
105
|
-
failure: {
|
106
|
-
error: ProtocolError.DECODE_FAILED,
|
107
|
-
peerId: peer.id
|
108
|
-
}
|
109
|
-
};
|
110
|
-
}
|
111
|
-
if (!response) {
|
112
|
-
log.error("Remote peer fault: No response in PushRPC");
|
113
|
-
return {
|
114
|
-
success: null,
|
115
|
-
failure: {
|
116
|
-
error: ProtocolError.NO_RESPONSE,
|
117
|
-
peerId: peer.id
|
118
|
-
}
|
119
|
-
};
|
120
|
-
}
|
121
|
-
if (isRLNResponseError(response.info)) {
|
122
|
-
const rlnErrorCase = matchRLNErrorMessage(response.info);
|
123
|
-
log.error("Remote peer rejected the message: ", rlnErrorCase);
|
124
|
-
return {
|
125
|
-
success: null,
|
126
|
-
failure: {
|
127
|
-
error: rlnErrorCase,
|
128
|
-
peerId: peer.id
|
129
|
-
}
|
130
|
-
};
|
131
|
-
}
|
132
|
-
if (!response.isSuccess) {
|
133
|
-
log.error("Remote peer rejected the message: ", response.info);
|
134
|
-
return {
|
135
|
-
success: null,
|
136
|
-
failure: {
|
137
|
-
error: ProtocolError.REMOTE_PEER_REJECTED,
|
138
|
-
peerId: peer.id
|
139
|
-
}
|
140
|
-
};
|
141
|
-
}
|
142
|
-
return { success: peer.id, failure: null };
|
143
|
-
}
|
144
|
-
}
|
1
|
+
export { LightPushCore, LightPushCodec, PushResponse } from "./light_push.js";
|
145
2
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/light_push/index.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/light_push/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC"}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import type { PeerId } from "@libp2p/interface";
|
2
|
+
import { type CoreProtocolResult, type IBaseProtocolCore, type IEncoder, type IMessage, type Libp2p, PubsubTopic } from "@waku/interfaces";
|
3
|
+
import { PushResponse } from "@waku/proto";
|
4
|
+
import { BaseProtocol } from "../base_protocol.js";
|
5
|
+
export declare const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
6
|
+
export { PushResponse };
|
7
|
+
/**
|
8
|
+
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
9
|
+
*/
|
10
|
+
export declare class LightPushCore extends BaseProtocol implements IBaseProtocolCore {
|
11
|
+
readonly pubsubTopics: PubsubTopic[];
|
12
|
+
constructor(pubsubTopics: PubsubTopic[], libp2p: Libp2p);
|
13
|
+
private preparePushMessage;
|
14
|
+
send(encoder: IEncoder, message: IMessage, peerId: PeerId): Promise<CoreProtocolResult>;
|
15
|
+
}
|
@@ -0,0 +1,144 @@
|
|
1
|
+
import { ProtocolError } from "@waku/interfaces";
|
2
|
+
import { PushResponse } from "@waku/proto";
|
3
|
+
import { isMessageSizeUnderCap } from "@waku/utils";
|
4
|
+
import { Logger } from "@waku/utils";
|
5
|
+
import all from "it-all";
|
6
|
+
import * as lp from "it-length-prefixed";
|
7
|
+
import { pipe } from "it-pipe";
|
8
|
+
import { Uint8ArrayList } from "uint8arraylist";
|
9
|
+
import { BaseProtocol } from "../base_protocol.js";
|
10
|
+
import { PushRpc } from "./push_rpc.js";
|
11
|
+
import { isRLNResponseError } from "./utils.js";
|
12
|
+
const log = new Logger("light-push");
|
13
|
+
export const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
|
14
|
+
export { PushResponse };
|
15
|
+
/**
|
16
|
+
* Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
17
|
+
*/
|
18
|
+
export class LightPushCore extends BaseProtocol {
|
19
|
+
pubsubTopics;
|
20
|
+
constructor(pubsubTopics, libp2p) {
|
21
|
+
super(LightPushCodec, libp2p.components, pubsubTopics);
|
22
|
+
this.pubsubTopics = pubsubTopics;
|
23
|
+
}
|
24
|
+
async preparePushMessage(encoder, message) {
|
25
|
+
try {
|
26
|
+
if (!message.payload || message.payload.length === 0) {
|
27
|
+
log.error("Failed to send waku light push: payload is empty");
|
28
|
+
return { query: null, error: ProtocolError.EMPTY_PAYLOAD };
|
29
|
+
}
|
30
|
+
if (!(await isMessageSizeUnderCap(encoder, message))) {
|
31
|
+
log.error("Failed to send waku light push: message is bigger than 1MB");
|
32
|
+
return { query: null, error: ProtocolError.SIZE_TOO_BIG };
|
33
|
+
}
|
34
|
+
const protoMessage = await encoder.toProtoObj(message);
|
35
|
+
if (!protoMessage) {
|
36
|
+
log.error("Failed to encode to protoMessage, aborting push");
|
37
|
+
return {
|
38
|
+
query: null,
|
39
|
+
error: ProtocolError.ENCODE_FAILED
|
40
|
+
};
|
41
|
+
}
|
42
|
+
const query = PushRpc.createRequest(protoMessage, encoder.pubsubTopic);
|
43
|
+
return { query, error: null };
|
44
|
+
}
|
45
|
+
catch (error) {
|
46
|
+
log.error("Failed to prepare push message", error);
|
47
|
+
return {
|
48
|
+
query: null,
|
49
|
+
error: ProtocolError.GENERIC_FAIL
|
50
|
+
};
|
51
|
+
}
|
52
|
+
}
|
53
|
+
async send(encoder, message, peerId) {
|
54
|
+
const { query, error: preparationError } = await this.preparePushMessage(encoder, message);
|
55
|
+
if (preparationError || !query) {
|
56
|
+
return {
|
57
|
+
success: null,
|
58
|
+
failure: {
|
59
|
+
error: preparationError,
|
60
|
+
peerId
|
61
|
+
}
|
62
|
+
};
|
63
|
+
}
|
64
|
+
let stream;
|
65
|
+
try {
|
66
|
+
stream = await this.getStream(peerId);
|
67
|
+
}
|
68
|
+
catch (error) {
|
69
|
+
log.error("Failed to get stream", error);
|
70
|
+
return {
|
71
|
+
success: null,
|
72
|
+
failure: {
|
73
|
+
error: ProtocolError.NO_STREAM_AVAILABLE,
|
74
|
+
peerId: peerId
|
75
|
+
}
|
76
|
+
};
|
77
|
+
}
|
78
|
+
let res;
|
79
|
+
try {
|
80
|
+
res = await pipe([query.encode()], lp.encode, stream, lp.decode, async (source) => await all(source));
|
81
|
+
}
|
82
|
+
catch (err) {
|
83
|
+
// can fail only because of `stream` abortion
|
84
|
+
log.error("Failed to send waku light push request", err);
|
85
|
+
return {
|
86
|
+
success: null,
|
87
|
+
failure: {
|
88
|
+
error: ProtocolError.STREAM_ABORTED,
|
89
|
+
peerId: peerId
|
90
|
+
}
|
91
|
+
};
|
92
|
+
}
|
93
|
+
const bytes = new Uint8ArrayList();
|
94
|
+
res.forEach((chunk) => {
|
95
|
+
bytes.append(chunk);
|
96
|
+
});
|
97
|
+
let response;
|
98
|
+
try {
|
99
|
+
response = PushRpc.decode(bytes).response;
|
100
|
+
}
|
101
|
+
catch (err) {
|
102
|
+
log.error("Failed to decode push reply", err);
|
103
|
+
return {
|
104
|
+
success: null,
|
105
|
+
failure: {
|
106
|
+
error: ProtocolError.DECODE_FAILED,
|
107
|
+
peerId: peerId
|
108
|
+
}
|
109
|
+
};
|
110
|
+
}
|
111
|
+
if (!response) {
|
112
|
+
log.error("Remote peer fault: No response in PushRPC");
|
113
|
+
return {
|
114
|
+
success: null,
|
115
|
+
failure: {
|
116
|
+
error: ProtocolError.NO_RESPONSE,
|
117
|
+
peerId: peerId
|
118
|
+
}
|
119
|
+
};
|
120
|
+
}
|
121
|
+
if (isRLNResponseError(response.info)) {
|
122
|
+
log.error("Remote peer fault: RLN generation");
|
123
|
+
return {
|
124
|
+
success: null,
|
125
|
+
failure: {
|
126
|
+
error: ProtocolError.RLN_PROOF_GENERATION,
|
127
|
+
peerId: peerId
|
128
|
+
}
|
129
|
+
};
|
130
|
+
}
|
131
|
+
if (!response.isSuccess) {
|
132
|
+
log.error("Remote peer rejected the message: ", response.info);
|
133
|
+
return {
|
134
|
+
success: null,
|
135
|
+
failure: {
|
136
|
+
error: ProtocolError.REMOTE_PEER_REJECTED,
|
137
|
+
peerId: peerId
|
138
|
+
}
|
139
|
+
};
|
140
|
+
}
|
141
|
+
return { success: peerId, failure: null };
|
142
|
+
}
|
143
|
+
}
|
144
|
+
//# sourceMappingURL=light_push.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"light_push.js","sourceRoot":"","sources":["../../../src/lib/light_push/light_push.ts"],"names":[],"mappings":"AACA,OAAO,EAML,aAAa,EAGd,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,GAAG,MAAM,QAAQ,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,YAAY,CAAC,CAAC;AAErC,MAAM,CAAC,MAAM,cAAc,GAAG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C;QAED,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC;YACxB,GAAG,CAAC,KAAK,CAAC,oCAAoC,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC;YAC/D,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,OAAO,EAAE;oBACP,KAAK,EAAE,aAAa,CAAC,oBAAoB;oBACzC,MAAM,EAAE,MAAM;iBACf;aACF,CAAC;QACJ,CAAC;QAED,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC5C,CAAC;CACF"}
|
@@ -1,26 +1,18 @@
|
|
1
|
-
import { ProtocolError } from "@waku/interfaces";
|
2
1
|
// should match nwaku
|
3
2
|
// https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/rln_relay.nim#L309
|
4
3
|
// https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/tests/waku_rln_relay/rln/waku_rln_relay_utils.nim#L20
|
5
|
-
const RLN_GENERATION_PREFIX_ERROR = "could not generate rln
|
4
|
+
const RLN_GENERATION_PREFIX_ERROR = "could not generate rln proof";
|
5
|
+
const RLN_MESSAGE_ID_PREFIX_ERROR = "could not get new message id to generate an rln proof";
|
6
|
+
// rare case on nwaku side
|
7
|
+
// https://github.com/waku-org/nwaku/blob/a4e92a3d02448fd708857b7b6cac2a7faa7eb4f9/waku/waku_lightpush/callbacks.nim#L49
|
8
|
+
// https://github.com/waku-org/nwaku/blob/a4e92a3d02448fd708857b7b6cac2a7faa7eb4f9/waku/node/waku_node.nim#L1117
|
9
|
+
const RLN_REMOTE_VALIDATION = "RLN validation failed";
|
6
10
|
export const isRLNResponseError = (info) => {
|
7
11
|
if (!info) {
|
8
12
|
return false;
|
9
13
|
}
|
10
|
-
return info.includes(RLN_GENERATION_PREFIX_ERROR)
|
11
|
-
|
12
|
-
|
13
|
-
const rlnErrorMap = {
|
14
|
-
[ProtocolError.RLN_IDENTITY_MISSING]: ProtocolError.RLN_IDENTITY_MISSING,
|
15
|
-
[ProtocolError.RLN_MEMBERSHIP_INDEX]: ProtocolError.RLN_MEMBERSHIP_INDEX,
|
16
|
-
[ProtocolError.RLN_LIMIT_MISSING]: ProtocolError.RLN_LIMIT_MISSING
|
17
|
-
};
|
18
|
-
const infoLowerCase = info.toLowerCase();
|
19
|
-
for (const errorKey in rlnErrorMap) {
|
20
|
-
if (infoLowerCase.includes(errorKey.toLowerCase())) {
|
21
|
-
return rlnErrorMap[errorKey];
|
22
|
-
}
|
23
|
-
}
|
24
|
-
return ProtocolError.RLN_PROOF_GENERATION;
|
14
|
+
return (info.includes(RLN_GENERATION_PREFIX_ERROR) ||
|
15
|
+
info.includes(RLN_MESSAGE_ID_PREFIX_ERROR) ||
|
16
|
+
info.includes(RLN_REMOTE_VALIDATION));
|
25
17
|
};
|
26
18
|
//# sourceMappingURL=utils.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/light_push/utils.ts"],"names":[],"mappings":"AAAA,
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/light_push/utils.ts"],"names":[],"mappings":"AAAA,qBAAqB;AACrB,yHAAyH;AACzH,wIAAwI;AACxI,MAAM,2BAA2B,GAAG,8BAA8B,CAAC;AACnE,MAAM,2BAA2B,GAC/B,uDAAuD,CAAC;AAE1D,0BAA0B;AAC1B,wHAAwH;AACxH,gHAAgH;AAChH,MAAM,qBAAqB,GAAG,uBAAuB,CAAC;AAEtD,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,IAAa,EAAW,EAAE;IAC3D,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,CACL,IAAI,CAAC,QAAQ,CAAC,2BAA2B,CAAC;QAC1C,IAAI,CAAC,QAAQ,CAAC,2BAA2B,CAAC;QAC1C,IAAI,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CACrC,CAAC;AACJ,CAAC,CAAC"}
|
@@ -1,3 +1 @@
|
|
1
|
-
|
2
|
-
export declare const MetadataCodec = "/vac/waku/metadata/1.0.0";
|
3
|
-
export declare function wakuMetadata(pubsubTopics: PubsubTopic[]): (components: Libp2pComponents) => IMetadata;
|
1
|
+
export { wakuMetadata, MetadataCodec } from "./metadata.js";
|