@waku/core 0.0.36-16328a3.0 → 0.0.36-3730abc.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.
Files changed (61) hide show
  1. package/bundle/index.js +476 -54
  2. package/bundle/lib/message/version_0.js +1 -1
  3. package/bundle/{version_0-CiYGrPc2.js → version_0-y7BmRolm.js} +919 -184
  4. package/dist/.tsbuildinfo +1 -1
  5. package/dist/index.d.ts +2 -0
  6. package/dist/index.js +2 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/lib/connection_manager/connection_manager.d.ts +1 -1
  9. package/dist/lib/connection_manager/connection_manager.js +1 -1
  10. package/dist/lib/light_push/index.d.ts +5 -1
  11. package/dist/lib/light_push/index.js +5 -1
  12. package/dist/lib/light_push/index.js.map +1 -1
  13. package/dist/lib/light_push/light_push.d.ts +3 -4
  14. package/dist/lib/light_push/light_push.js +18 -19
  15. package/dist/lib/light_push/light_push.js.map +1 -1
  16. package/dist/lib/light_push/light_push_v3.d.ts +10 -0
  17. package/dist/lib/light_push/light_push_v3.js +172 -0
  18. package/dist/lib/light_push/light_push_v3.js.map +1 -0
  19. package/dist/lib/light_push/push_rpc.d.ts +1 -1
  20. package/dist/lib/light_push/push_rpc.js.map +1 -1
  21. package/dist/lib/light_push/push_rpc_v2.d.ts +11 -0
  22. package/dist/lib/light_push/push_rpc_v2.js +32 -0
  23. package/dist/lib/light_push/push_rpc_v2.js.map +1 -0
  24. package/dist/lib/light_push/push_rpc_v3.d.ts +11 -0
  25. package/dist/lib/light_push/push_rpc_v3.js +33 -0
  26. package/dist/lib/light_push/push_rpc_v3.js.map +1 -0
  27. package/dist/lib/light_push/status_codes.d.ts +14 -0
  28. package/dist/lib/light_push/status_codes.js +49 -0
  29. package/dist/lib/light_push/status_codes.js.map +1 -0
  30. package/dist/lib/light_push/status_codes_v3.d.ts +17 -0
  31. package/dist/lib/light_push/status_codes_v3.js +69 -0
  32. package/dist/lib/light_push/status_codes_v3.js.map +1 -0
  33. package/dist/lib/message/version_0.d.ts +2 -3
  34. package/dist/lib/message/version_0.js +1 -4
  35. package/dist/lib/message/version_0.js.map +1 -1
  36. package/dist/lib/message_hash/index.d.ts +1 -0
  37. package/dist/lib/message_hash/index.js +2 -0
  38. package/dist/lib/message_hash/index.js.map +1 -0
  39. package/dist/lib/message_hash/message_hash.d.ts +52 -0
  40. package/dist/lib/message_hash/message_hash.js +84 -0
  41. package/dist/lib/message_hash/message_hash.js.map +1 -0
  42. package/dist/lib/store/rpc.js +16 -10
  43. package/dist/lib/store/rpc.js.map +1 -1
  44. package/dist/lib/store/store.js +12 -2
  45. package/dist/lib/store/store.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/index.ts +11 -0
  48. package/src/lib/connection_manager/connection_manager.ts +1 -1
  49. package/src/lib/light_push/index.ts +24 -1
  50. package/src/lib/light_push/light_push.ts +25 -23
  51. package/src/lib/light_push/light_push_v3.ts +233 -0
  52. package/src/lib/light_push/push_rpc.ts +1 -1
  53. package/src/lib/light_push/push_rpc_v2.ts +38 -0
  54. package/src/lib/light_push/push_rpc_v3.ts +46 -0
  55. package/src/lib/light_push/status_codes.ts +71 -0
  56. package/src/lib/light_push/status_codes_v3.ts +97 -0
  57. package/src/lib/message/version_0.ts +3 -7
  58. package/src/lib/message_hash/index.ts +1 -0
  59. package/src/lib/message_hash/message_hash.ts +106 -0
  60. package/src/lib/store/rpc.ts +23 -19
  61. package/src/lib/store/store.ts +13 -1
@@ -0,0 +1,46 @@
1
+ import { proto_lightpush_v3, WakuMessage } from "@waku/proto";
2
+ import type { Uint8ArrayList } from "uint8arraylist";
3
+ import { v4 as uuid } from "uuid";
4
+
5
+ export class PushRpcV3 {
6
+ public request?: proto_lightpush_v3.LightpushRequest;
7
+ public response?: proto_lightpush_v3.LightpushResponse;
8
+
9
+ private constructor(
10
+ request?: proto_lightpush_v3.LightpushRequest,
11
+ response?: proto_lightpush_v3.LightpushResponse
12
+ ) {
13
+ this.request = request;
14
+ this.response = response;
15
+ }
16
+
17
+ public static createRequest(
18
+ message: WakuMessage,
19
+ pubsubTopic?: string
20
+ ): PushRpcV3 {
21
+ const request: proto_lightpush_v3.LightpushRequest = {
22
+ requestId: uuid(),
23
+ message: message,
24
+ // Only include pubsubTopic if explicitly provided (for nwaku autosharding compatibility)
25
+ ...(pubsubTopic && { pubsubTopic })
26
+ };
27
+
28
+ return new PushRpcV3(request, undefined);
29
+ }
30
+
31
+ public static decode(bytes: Uint8ArrayList | Uint8Array): PushRpcV3 {
32
+ const response = proto_lightpush_v3.LightpushResponse.decode(bytes);
33
+ return new PushRpcV3(undefined, response);
34
+ }
35
+
36
+ public encode(): Uint8Array {
37
+ if (!this.request) {
38
+ throw new Error("Cannot encode without a request");
39
+ }
40
+ return proto_lightpush_v3.LightpushRequest.encode(this.request);
41
+ }
42
+
43
+ public get query(): proto_lightpush_v3.LightpushRequest | undefined {
44
+ return this.request;
45
+ }
46
+ }
@@ -0,0 +1,71 @@
1
+ import { ProtocolError } from "@waku/interfaces";
2
+
3
+ export enum LightPushStatusCode {
4
+ SUCCESS = 200,
5
+ BAD_REQUEST = 400,
6
+ UNSUPPORTED_PUBSUB_TOPIC = 404,
7
+ REQUEST_TOO_LARGE = 413,
8
+ TOO_MANY_REQUESTS = 429,
9
+ INTERNAL_SERVER_ERROR = 500,
10
+ NO_PEERS_TO_RELAY = 503
11
+ }
12
+
13
+ export function lightPushStatusCodeToProtocolError(
14
+ statusCode: number
15
+ ): ProtocolError | null {
16
+ switch (statusCode) {
17
+ case LightPushStatusCode.SUCCESS:
18
+ return null;
19
+
20
+ case LightPushStatusCode.BAD_REQUEST:
21
+ return ProtocolError.GENERIC_FAIL;
22
+
23
+ case LightPushStatusCode.UNSUPPORTED_PUBSUB_TOPIC:
24
+ return ProtocolError.TOPIC_NOT_CONFIGURED;
25
+
26
+ case LightPushStatusCode.REQUEST_TOO_LARGE:
27
+ return ProtocolError.SIZE_TOO_BIG;
28
+
29
+ case LightPushStatusCode.TOO_MANY_REQUESTS:
30
+ return ProtocolError.GENERIC_FAIL;
31
+
32
+ case LightPushStatusCode.INTERNAL_SERVER_ERROR:
33
+ return ProtocolError.REMOTE_PEER_REJECTED;
34
+
35
+ case LightPushStatusCode.NO_PEERS_TO_RELAY:
36
+ return ProtocolError.NO_PEER_AVAILABLE;
37
+
38
+ default:
39
+ return ProtocolError.REMOTE_PEER_REJECTED;
40
+ }
41
+ }
42
+
43
+ export const lightPushStatusDescriptions: Record<number, string> = {
44
+ [LightPushStatusCode.SUCCESS]: "Message pushed successfully",
45
+ [LightPushStatusCode.BAD_REQUEST]:
46
+ "Invalid request format or missing required fields",
47
+ [LightPushStatusCode.UNSUPPORTED_PUBSUB_TOPIC]:
48
+ "The specified pubsub topic is not supported",
49
+ [LightPushStatusCode.REQUEST_TOO_LARGE]:
50
+ "Message size exceeds maximum allowed size",
51
+ [LightPushStatusCode.TOO_MANY_REQUESTS]:
52
+ "Rate limit exceeded, too many requests",
53
+ [LightPushStatusCode.INTERNAL_SERVER_ERROR]: "Internal server error occurred",
54
+ [LightPushStatusCode.NO_PEERS_TO_RELAY]:
55
+ "No relay peers available to forward the message"
56
+ };
57
+
58
+ export function isSuccessStatusCode(statusCode: number): boolean {
59
+ return statusCode === LightPushStatusCode.SUCCESS;
60
+ }
61
+
62
+ export function getLightPushStatusDescription(
63
+ statusCode: number,
64
+ statusDesc?: string
65
+ ): string {
66
+ return (
67
+ statusDesc ||
68
+ lightPushStatusDescriptions[statusCode] ||
69
+ `Unknown status code: ${statusCode}`
70
+ );
71
+ }
@@ -0,0 +1,97 @@
1
+ import { ProtocolError } from "@waku/interfaces";
2
+
3
+ export enum LightPushStatusCodeV3 {
4
+ SUCCESS = 200,
5
+ BAD_REQUEST = 400,
6
+ PAYLOAD_TOO_LARGE = 413,
7
+ INVALID_MESSAGE_ERROR = 420,
8
+ UNSUPPORTED_PUBSUB_TOPIC = 421,
9
+ TOO_MANY_REQUESTS = 429,
10
+ INTERNAL_SERVER_ERROR = 500,
11
+ SERVICE_NOT_AVAILABLE = 503,
12
+ OUT_OF_RLN_PROOF = 504,
13
+ NO_PEERS_TO_RELAY = 505
14
+ }
15
+
16
+ export const lightPushStatusDescriptionsV3: Record<
17
+ LightPushStatusCodeV3,
18
+ string
19
+ > = {
20
+ [LightPushStatusCodeV3.SUCCESS]: "Message sent successfully",
21
+ [LightPushStatusCodeV3.BAD_REQUEST]: "Bad request format",
22
+ [LightPushStatusCodeV3.PAYLOAD_TOO_LARGE]:
23
+ "Message payload exceeds maximum size",
24
+ [LightPushStatusCodeV3.INVALID_MESSAGE_ERROR]: "Message validation failed",
25
+ [LightPushStatusCodeV3.UNSUPPORTED_PUBSUB_TOPIC]: "Unsupported pubsub topic",
26
+ [LightPushStatusCodeV3.TOO_MANY_REQUESTS]: "Rate limit exceeded",
27
+ [LightPushStatusCodeV3.INTERNAL_SERVER_ERROR]: "Internal server error",
28
+ [LightPushStatusCodeV3.SERVICE_NOT_AVAILABLE]:
29
+ "Service temporarily unavailable",
30
+ [LightPushStatusCodeV3.OUT_OF_RLN_PROOF]: "RLN proof generation failed",
31
+ [LightPushStatusCodeV3.NO_PEERS_TO_RELAY]: "No relay peers available"
32
+ };
33
+
34
+ export function lightPushStatusCodeToProtocolErrorV3(
35
+ statusCode: LightPushStatusCodeV3 | number | undefined
36
+ ): ProtocolError {
37
+ if (!statusCode) {
38
+ return ProtocolError.GENERIC_FAIL;
39
+ }
40
+
41
+ switch (statusCode) {
42
+ case LightPushStatusCodeV3.SUCCESS:
43
+ return ProtocolError.GENERIC_FAIL;
44
+
45
+ case LightPushStatusCodeV3.BAD_REQUEST:
46
+ return ProtocolError.DECODE_FAILED;
47
+
48
+ case LightPushStatusCodeV3.PAYLOAD_TOO_LARGE:
49
+ return ProtocolError.SIZE_TOO_BIG;
50
+
51
+ case LightPushStatusCodeV3.INVALID_MESSAGE_ERROR:
52
+ return ProtocolError.EMPTY_PAYLOAD;
53
+
54
+ case LightPushStatusCodeV3.UNSUPPORTED_PUBSUB_TOPIC:
55
+ return ProtocolError.TOPIC_NOT_CONFIGURED;
56
+
57
+ case LightPushStatusCodeV3.TOO_MANY_REQUESTS:
58
+ return ProtocolError.REMOTE_PEER_REJECTED;
59
+
60
+ case LightPushStatusCodeV3.INTERNAL_SERVER_ERROR:
61
+ return ProtocolError.GENERIC_FAIL;
62
+
63
+ case LightPushStatusCodeV3.SERVICE_NOT_AVAILABLE:
64
+ return ProtocolError.NO_PEER_AVAILABLE;
65
+
66
+ case LightPushStatusCodeV3.OUT_OF_RLN_PROOF:
67
+ return ProtocolError.RLN_PROOF_GENERATION;
68
+
69
+ case LightPushStatusCodeV3.NO_PEERS_TO_RELAY:
70
+ return ProtocolError.NO_PEER_AVAILABLE;
71
+
72
+ default:
73
+ return ProtocolError.REMOTE_PEER_REJECTED;
74
+ }
75
+ }
76
+
77
+ export function isSuccessStatusCodeV3(statusCode: number | undefined): boolean {
78
+ return statusCode === LightPushStatusCodeV3.SUCCESS;
79
+ }
80
+
81
+ export function getLightPushStatusDescriptionV3(
82
+ statusCode: number | undefined,
83
+ customDesc?: string
84
+ ): string {
85
+ if (customDesc) {
86
+ return customDesc;
87
+ }
88
+
89
+ if (!statusCode) {
90
+ return "Unknown error";
91
+ }
92
+
93
+ return (
94
+ lightPushStatusDescriptionsV3[statusCode as LightPushStatusCodeV3] ||
95
+ `Unknown status code: ${statusCode}`
96
+ );
97
+ }
@@ -22,7 +22,7 @@ export { proto };
22
22
  export class DecodedMessage implements IDecodedMessage {
23
23
  public constructor(
24
24
  public pubsubTopic: string,
25
- protected proto: proto.WakuMessage
25
+ private proto: proto.WakuMessage
26
26
  ) {}
27
27
 
28
28
  public get ephemeral(): boolean {
@@ -37,10 +37,6 @@ export class DecodedMessage implements IDecodedMessage {
37
37
  return this.proto.contentTopic;
38
38
  }
39
39
 
40
- public get _rawTimestamp(): bigint | undefined {
41
- return this.proto.timestamp;
42
- }
43
-
44
40
  public get timestamp(): Date | undefined {
45
41
  // In the case we receive a value that is bigger than JS's max number,
46
42
  // we catch the error and return undefined.
@@ -63,7 +59,7 @@ export class DecodedMessage implements IDecodedMessage {
63
59
  public get version(): number {
64
60
  // https://rfc.vac.dev/spec/14/
65
61
  // > If omitted, the value SHOULD be interpreted as version 0.
66
- return this.proto.version ?? 0;
62
+ return this.proto.version ?? Version;
67
63
  }
68
64
 
69
65
  public get rateLimitProof(): IRateLimitProof | undefined {
@@ -160,7 +156,7 @@ export class Decoder implements IDecoder<IDecodedMessage> {
160
156
  public async fromProtoObj(
161
157
  pubsubTopic: string,
162
158
  proto: IProtoMessage
163
- ): Promise<DecodedMessage | undefined> {
159
+ ): Promise<IDecodedMessage | undefined> {
164
160
  // https://rfc.vac.dev/spec/14/
165
161
  // > If omitted, the value SHOULD be interpreted as version 0.
166
162
  if (proto.version ?? 0 !== Version) {
@@ -0,0 +1 @@
1
+ export { messageHash, messageHashStr } from "./message_hash.js";
@@ -0,0 +1,106 @@
1
+ import { sha256 } from "@noble/hashes/sha256";
2
+ import type { IDecodedMessage, IProtoMessage } from "@waku/interfaces";
3
+ import { isDefined } from "@waku/utils";
4
+ import {
5
+ bytesToHex,
6
+ concat,
7
+ numberToBytes,
8
+ utf8ToBytes
9
+ } from "@waku/utils/bytes";
10
+
11
+ /**
12
+ * Deterministic Message Hashing as defined in
13
+ * [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/#deterministic-message-hashing)
14
+ *
15
+ * Computes a SHA-256 hash of the concatenation of pubsub topic, payload, content topic, meta, and timestamp.
16
+ *
17
+ * @param pubsubTopic - The pubsub topic string
18
+ * @param message - The message to be hashed
19
+ * @returns A Uint8Array containing the SHA-256 hash
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * import { messageHash } from "@waku/core";
24
+ *
25
+ * const pubsubTopic = "/waku/2/default-waku/proto";
26
+ * const message = {
27
+ * payload: new Uint8Array([1, 2, 3, 4]),
28
+ * contentTopic: "/waku/2/default-content/proto",
29
+ * meta: new Uint8Array([5, 6, 7, 8]),
30
+ * timestamp: new Date()
31
+ * };
32
+ *
33
+ * const hash = messageHash(pubsubTopic, message);
34
+ * ```
35
+ */
36
+ export function messageHash(
37
+ pubsubTopic: string,
38
+ message: IProtoMessage | IDecodedMessage
39
+ ): Uint8Array {
40
+ const pubsubTopicBytes = utf8ToBytes(pubsubTopic);
41
+ const contentTopicBytes = utf8ToBytes(message.contentTopic);
42
+ const timestampBytes = tryConvertTimestampToBytes(message.timestamp);
43
+
44
+ const bytes = concat(
45
+ [
46
+ pubsubTopicBytes,
47
+ message.payload,
48
+ contentTopicBytes,
49
+ message.meta,
50
+ timestampBytes
51
+ ].filter(isDefined)
52
+ );
53
+
54
+ return sha256(bytes);
55
+ }
56
+
57
+ function tryConvertTimestampToBytes(
58
+ timestamp: Date | number | bigint | undefined
59
+ ): undefined | Uint8Array {
60
+ if (!timestamp) {
61
+ return;
62
+ }
63
+
64
+ let bigIntTimestamp: bigint;
65
+
66
+ if (typeof timestamp === "bigint") {
67
+ bigIntTimestamp = timestamp;
68
+ } else {
69
+ bigIntTimestamp = BigInt(timestamp.valueOf()) * 1000000n;
70
+ }
71
+
72
+ return numberToBytes(bigIntTimestamp);
73
+ }
74
+
75
+ /**
76
+ * Computes a deterministic message hash and returns it as a hexadecimal string.
77
+ * This is a convenience wrapper around messageHash that converts the result to a hex string.
78
+ *
79
+ * @param pubsubTopic - The pubsub topic string
80
+ * @param message - The message to be hashed
81
+ * @returns A string containing the hex representation of the SHA-256 hash
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * import { messageHashStr } from "@waku/core";
86
+ *
87
+ * const pubsubTopic = "/waku/2/default-waku/proto";
88
+ * const message = {
89
+ * payload: new Uint8Array([1, 2, 3, 4]),
90
+ * contentTopic: "/waku/2/default-content/proto",
91
+ * meta: new Uint8Array([5, 6, 7, 8]),
92
+ * timestamp: new Date()
93
+ * };
94
+ *
95
+ * const hashString = messageHashStr(pubsubTopic, message);
96
+ * console.log(hashString); // e.g. "a1b2c3d4..."
97
+ * ```
98
+ */
99
+ export function messageHashStr(
100
+ pubsubTopic: string,
101
+ message: IProtoMessage | IDecodedMessage
102
+ ): string {
103
+ const hash = messageHash(pubsubTopic, message);
104
+ const hashStr = bytesToHex(hash);
105
+ return hashStr;
106
+ }
@@ -14,6 +14,7 @@ export class StoreQueryRequest {
14
14
  public static create(params: QueryRequestParams): StoreQueryRequest {
15
15
  const request = new StoreQueryRequest({
16
16
  ...params,
17
+ contentTopics: params.contentTopics || [],
17
18
  requestId: uuid(),
18
19
  timeStart: params.timeStart
19
20
  ? BigInt(params.timeStart.getTime() * ONE_MILLION)
@@ -27,26 +28,29 @@ export class StoreQueryRequest {
27
28
  : undefined
28
29
  });
29
30
 
30
- // Validate request parameters based on RFC
31
- if (
32
- (params.pubsubTopic && !params.contentTopics) ||
33
- (!params.pubsubTopic && params.contentTopics)
34
- ) {
35
- throw new Error(
36
- "Both pubsubTopic and contentTopics must be set or unset"
37
- );
38
- }
31
+ const isHashQuery = params.messageHashes && params.messageHashes.length > 0;
32
+ const hasContentTopics =
33
+ params.contentTopics && params.contentTopics.length > 0;
34
+ const hasTimeFilter = params.timeStart || params.timeEnd;
39
35
 
40
- if (
41
- params.messageHashes &&
42
- (params.pubsubTopic ||
43
- params.contentTopics ||
44
- params.timeStart ||
45
- params.timeEnd)
46
- ) {
47
- throw new Error(
48
- "Message hash lookup queries cannot include content filter criteria"
49
- );
36
+ if (isHashQuery) {
37
+ if (hasContentTopics || hasTimeFilter) {
38
+ throw new Error(
39
+ "Message hash lookup queries cannot include content filter criteria (contentTopics, timeStart, or timeEnd)"
40
+ );
41
+ }
42
+ } else {
43
+ if (
44
+ (params.pubsubTopic &&
45
+ (!params.contentTopics || params.contentTopics.length === 0)) ||
46
+ (!params.pubsubTopic &&
47
+ params.contentTopics &&
48
+ params.contentTopics.length > 0)
49
+ ) {
50
+ throw new Error(
51
+ "Both pubsubTopic and contentTopics must be set together for content-filtered queries"
52
+ );
53
+ }
50
54
  }
51
55
 
52
56
  return request;
@@ -40,9 +40,14 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
40
40
  decoders: Map<string, IDecoder<T>>,
41
41
  peerId: PeerId
42
42
  ): AsyncGenerator<Promise<T | undefined>[]> {
43
+ // Only validate decoder content topics for content-filtered queries
44
+ const isHashQuery =
45
+ queryOpts.messageHashes && queryOpts.messageHashes.length > 0;
43
46
  if (
47
+ !isHashQuery &&
48
+ queryOpts.contentTopics &&
44
49
  queryOpts.contentTopics.toString() !==
45
- Array.from(decoders.keys()).toString()
50
+ Array.from(decoders.keys()).toString()
46
51
  ) {
47
52
  throw new Error(
48
53
  "Internal error, the decoders should match the query's content topics"
@@ -56,6 +61,13 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
56
61
  paginationCursor: currentCursor
57
62
  });
58
63
 
64
+ log.info("Sending store query request:", {
65
+ hasMessageHashes: !!queryOpts.messageHashes?.length,
66
+ messageHashCount: queryOpts.messageHashes?.length,
67
+ pubsubTopic: queryOpts.pubsubTopic,
68
+ contentTopics: queryOpts.contentTopics
69
+ });
70
+
59
71
  let stream;
60
72
  try {
61
73
  stream = await this.getStream(peerId);