Skip to content

Commit 5cf8ed2

Browse files
committed
chore!: update message.proto: payload and content topic are always defined
Ref: https://github.com/vacp2p/waku
1 parent 18d3138 commit 5cf8ed2

21 files changed

+87
-135
lines changed

packages/core/src/lib/message/topic_only_message.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import debug from "debug";
99
const log = debug("waku:message:topic-only");
1010

1111
export class TopicOnlyMessage implements IDecodedMessage {
12-
public payload: undefined;
12+
public payload: Uint8Array = new Uint8Array();
1313
public rateLimitProof: undefined;
1414
public timestamp: undefined;
1515
public ephemeral: undefined;
1616

1717
constructor(private proto: ProtoTopicOnlyMessage) {}
1818

1919
get contentTopic(): string {
20-
return this.proto.contentTopic ?? "";
20+
return this.proto.contentTopic;
2121
}
2222
}
2323

@@ -29,7 +29,7 @@ export class TopicOnlyDecoder implements IDecoder<TopicOnlyMessage> {
2929
log("Message decoded", protoMessage);
3030
return Promise.resolve({
3131
contentTopic: protoMessage.contentTopic,
32-
payload: undefined,
32+
payload: new Uint8Array(),
3333
rateLimitProof: undefined,
3434
timestamp: undefined,
3535
version: undefined,

packages/core/src/lib/message/version_0.ts

+11-24
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,15 @@ export { proto };
1919
export class DecodedMessage implements IDecodedMessage {
2020
constructor(protected proto: proto.WakuMessage) {}
2121

22-
get _rawPayload(): Uint8Array | undefined {
23-
if (this.proto.payload) {
24-
return new Uint8Array(this.proto.payload);
25-
}
26-
return;
27-
}
28-
2922
get ephemeral(): boolean {
3023
return Boolean(this.proto.ephemeral);
3124
}
3225

33-
get payload(): Uint8Array | undefined {
34-
return this._rawPayload;
26+
get payload(): Uint8Array {
27+
return this.proto.payload;
3528
}
3629

37-
get contentTopic(): string | undefined {
30+
get contentTopic(): string {
3831
return this.proto.contentTopic;
3932
}
4033

@@ -51,18 +44,15 @@ export class DecodedMessage implements IDecodedMessage {
5144
const timestamp = this.proto.timestamp / OneMillion;
5245
return new Date(Number(timestamp));
5346
}
54-
55-
if (this.proto.timestampDeprecated) {
56-
return new Date(this.proto.timestampDeprecated * 1000);
57-
}
47+
return;
5848
} catch (e) {
5949
return;
6050
}
61-
return;
6251
}
6352

6453
get version(): number {
65-
// https://github.com/status-im/js-waku/issues/921
54+
// https://rfc.vac.dev/spec/14/
55+
// > If omitted, the value SHOULD be interpreted as version 0.
6656
return this.proto.version ?? 0;
6757
}
6858

@@ -115,8 +105,8 @@ export class Decoder implements IDecoder<DecodedMessage> {
115105
const protoMessage = proto.WakuMessage.decode(bytes);
116106
log("Message decoded", protoMessage);
117107
return Promise.resolve({
118-
payload: protoMessage.payload ?? undefined,
119-
contentTopic: protoMessage.contentTopic ?? undefined,
108+
payload: protoMessage.payload,
109+
contentTopic: protoMessage.contentTopic,
120110
version: protoMessage.version ?? undefined,
121111
timestamp: protoMessage.timestamp ?? undefined,
122112
rateLimitProof: protoMessage.rateLimitProof ?? undefined,
@@ -127,12 +117,9 @@ export class Decoder implements IDecoder<DecodedMessage> {
127117
async fromProtoObj(
128118
proto: IProtoMessage
129119
): Promise<DecodedMessage | undefined> {
130-
// https://github.com/status-im/js-waku/issues/921
131-
if (proto.version === undefined) {
132-
proto.version = 0;
133-
}
134-
135-
if (proto.version !== Version) {
120+
// https://rfc.vac.dev/spec/14/
121+
// > If omitted, the value SHOULD be interpreted as version 0.
122+
if (proto.version ?? 0 !== Version) {
136123
log(
137124
"Failed to decode due to incorrect version, expected:",
138125
Version,

packages/core/src/lib/to_proto_message.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { toProtoMessage } from "./to_proto_message.js";
66
describe("to proto message", () => {
77
it("Fields are not dropped", () => {
88
const wire: WakuMessageProto = {
9+
payload: new Uint8Array(),
910
contentTopic: "foo",
1011
};
1112

packages/core/src/lib/to_proto_message.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { IProtoMessage } from "@waku/interfaces";
22
import { WakuMessage as WakuMessageProto } from "@waku/proto";
33

44
const EmptyMessage: IProtoMessage = {
5-
payload: undefined,
6-
contentTopic: undefined,
5+
payload: new Uint8Array(),
6+
contentTopic: "",
77
version: undefined,
88
timestamp: undefined,
99
rateLimitProof: undefined,

packages/interfaces/src/message.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export interface IRateLimitProof {
1313
* Field types matches the protobuf type over the wire
1414
*/
1515
export interface IProtoMessage {
16-
payload: Uint8Array | undefined;
17-
contentTopic: string | undefined;
16+
payload: Uint8Array;
17+
contentTopic: string;
1818
version: number | undefined;
1919
timestamp: bigint | undefined;
2020
rateLimitProof: IRateLimitProof | undefined;
@@ -25,7 +25,7 @@ export interface IProtoMessage {
2525
* Interface for messages to encode and send.
2626
*/
2727
export interface IMessage {
28-
payload?: Uint8Array;
28+
payload: Uint8Array;
2929
timestamp?: Date;
3030
rateLimitProof?: IRateLimitProof;
3131
}
@@ -48,8 +48,8 @@ export interface IEncoder {
4848
}
4949

5050
export interface IDecodedMessage {
51-
payload: Uint8Array | undefined;
52-
contentTopic: string | undefined;
51+
payload: Uint8Array;
52+
contentTopic: string;
5353
timestamp: Date | undefined;
5454
rateLimitProof: IRateLimitProof | undefined;
5555
ephemeral: boolean | undefined;

packages/interfaces/src/protocols.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { PeerId } from "@libp2p/interface-peer-id";
22
import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
33
import type { Libp2pOptions } from "libp2p";
44

5-
import type { IMessage } from "./message.js";
5+
import type { IDecodedMessage } from "./message.js";
66

77
export enum Protocols {
88
Relay = "relay",
@@ -58,7 +58,9 @@ export type ProtocolOptions = {
5858
peerId?: PeerId;
5959
};
6060

61-
export type Callback<T extends IMessage> = (msg: T) => void | Promise<void>;
61+
export type Callback<T extends IDecodedMessage> = (
62+
msg: T
63+
) => void | Promise<void>;
6264

6365
export interface SendResult {
6466
recipients: PeerId[];

packages/message-encryption/src/ecies.ts

-8
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ export class Encoder implements IEncoder {
4545

4646
async toProtoObj(message: IMessage): Promise<IProtoMessage | undefined> {
4747
const timestamp = message.timestamp ?? new Date();
48-
if (!message.payload) {
49-
log("No payload to encrypt, skipping: ", message);
50-
return;
51-
}
5248
const preparedPayload = await preCipher(message.payload, this.sigPrivKey);
5349

5450
const payload = await encryptAsymmetric(preparedPayload, this.publicKey);
@@ -113,10 +109,6 @@ export class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
113109
}
114110

115111
let payload;
116-
if (!cipherPayload) {
117-
log(`No payload to decrypt for contentTopic ${this.contentTopic}`);
118-
return;
119-
}
120112

121113
try {
122114
payload = await decryptAsymmetric(cipherPayload, this.privateKey);

packages/message-encryption/src/symmetric.ts

-8
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ export class Encoder implements IEncoder {
4444

4545
async toProtoObj(message: IMessage): Promise<IProtoMessage | undefined> {
4646
const timestamp = message.timestamp ?? new Date();
47-
if (!message.payload) {
48-
log("No payload to encrypt, skipping: ", message);
49-
return;
50-
}
5147
const preparedPayload = await preCipher(message.payload, this.sigPrivKey);
5248

5349
const payload = await encryptSymmetric(preparedPayload, this.symKey);
@@ -112,10 +108,6 @@ export class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
112108
}
113109

114110
let payload;
115-
if (!cipherPayload) {
116-
log(`No payload to decrypt for contentTopic ${this.contentTopic}`);
117-
return;
118-
}
119111

120112
try {
121113
payload = await decryptSymmetric(cipherPayload, this.symKey);

packages/proto/src/lib/filter.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,9 @@ export namespace RateLimitProof {
426426
}
427427

428428
export interface WakuMessage {
429-
payload?: Uint8Array;
430-
contentTopic?: string;
429+
payload: Uint8Array;
430+
contentTopic: string;
431431
version?: number;
432-
timestampDeprecated?: number;
433432
timestamp?: bigint;
434433
rateLimitProof?: RateLimitProof;
435434
ephemeral?: boolean;
@@ -446,12 +445,12 @@ export namespace WakuMessage {
446445
w.fork();
447446
}
448447

449-
if (obj.payload != null) {
448+
if (obj.payload != null && obj.payload.byteLength > 0) {
450449
w.uint32(10);
451450
w.bytes(obj.payload);
452451
}
453452

454-
if (obj.contentTopic != null) {
453+
if (obj.contentTopic != null && obj.contentTopic !== "") {
455454
w.uint32(18);
456455
w.string(obj.contentTopic);
457456
}
@@ -461,11 +460,6 @@ export namespace WakuMessage {
461460
w.uint32(obj.version);
462461
}
463462

464-
if (obj.timestampDeprecated != null) {
465-
w.uint32(33);
466-
w.double(obj.timestampDeprecated);
467-
}
468-
469463
if (obj.timestamp != null) {
470464
w.uint32(80);
471465
w.sint64(obj.timestamp);
@@ -486,7 +480,10 @@ export namespace WakuMessage {
486480
}
487481
},
488482
(reader, length) => {
489-
const obj: any = {};
483+
const obj: any = {
484+
payload: new Uint8Array(0),
485+
contentTopic: "",
486+
};
490487

491488
const end = length == null ? reader.len : reader.pos + length;
492489

@@ -503,9 +500,6 @@ export namespace WakuMessage {
503500
case 3:
504501
obj.version = reader.uint32();
505502
break;
506-
case 4:
507-
obj.timestampDeprecated = reader.double();
508-
break;
509503
case 10:
510504
obj.timestamp = reader.sint64();
511505
break;

packages/proto/src/lib/light_push.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,9 @@ export namespace RateLimitProof {
358358
}
359359

360360
export interface WakuMessage {
361-
payload?: Uint8Array;
362-
contentTopic?: string;
361+
payload: Uint8Array;
362+
contentTopic: string;
363363
version?: number;
364-
timestampDeprecated?: number;
365364
timestamp?: bigint;
366365
rateLimitProof?: RateLimitProof;
367366
ephemeral?: boolean;
@@ -378,12 +377,12 @@ export namespace WakuMessage {
378377
w.fork();
379378
}
380379

381-
if (obj.payload != null) {
380+
if (obj.payload != null && obj.payload.byteLength > 0) {
382381
w.uint32(10);
383382
w.bytes(obj.payload);
384383
}
385384

386-
if (obj.contentTopic != null) {
385+
if (obj.contentTopic != null && obj.contentTopic !== "") {
387386
w.uint32(18);
388387
w.string(obj.contentTopic);
389388
}
@@ -393,11 +392,6 @@ export namespace WakuMessage {
393392
w.uint32(obj.version);
394393
}
395394

396-
if (obj.timestampDeprecated != null) {
397-
w.uint32(33);
398-
w.double(obj.timestampDeprecated);
399-
}
400-
401395
if (obj.timestamp != null) {
402396
w.uint32(80);
403397
w.sint64(obj.timestamp);
@@ -418,7 +412,10 @@ export namespace WakuMessage {
418412
}
419413
},
420414
(reader, length) => {
421-
const obj: any = {};
415+
const obj: any = {
416+
payload: new Uint8Array(0),
417+
contentTopic: "",
418+
};
422419

423420
const end = length == null ? reader.len : reader.pos + length;
424421

@@ -435,9 +432,6 @@ export namespace WakuMessage {
435432
case 3:
436433
obj.version = reader.uint32();
437434
break;
438-
case 4:
439-
obj.timestampDeprecated = reader.double();
440-
break;
441435
case 10:
442436
obj.timestamp = reader.sint64();
443437
break;

packages/proto/src/lib/message.proto

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// 14/WAKU2-MESSAGE rfc: https://rfc.vac.dev/spec/14/
2+
13
syntax = "proto3";
24

35
message RateLimitProof {
@@ -7,16 +9,14 @@ message RateLimitProof {
79
bytes share_x = 4;
810
bytes share_y = 5;
911
bytes nullifier = 6;
10-
bytes rlnIdentifier = 7;
12+
bytes rln_identifier = 7;
1113
}
1214

1315
message WakuMessage {
14-
optional bytes payload = 1;
15-
optional string content_topic = 2;
16+
bytes payload = 1;
17+
string content_topic = 2;
1618
optional uint32 version = 3;
17-
optional double timestamp_deprecated = 4;
18-
optional sint64 timestamp = 10;
19+
optional sint64 timestamp = 10;
1920
optional RateLimitProof rate_limit_proof = 21;
2021
optional bool ephemeral = 31;
2122
}
22-

0 commit comments

Comments
 (0)