chore!: update message.proto: payload and content topic are always defined

Ref: https://github.com/vacp2p/waku
This commit is contained in:
fryorcraken.eth 2023-02-24 23:22:04 +11:00
parent 18d31381d2
commit 5cf8ed2030
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
21 changed files with 87 additions and 135 deletions

View File

@ -9,7 +9,7 @@ import debug from "debug";
const log = debug("waku:message:topic-only");
export class TopicOnlyMessage implements IDecodedMessage {
public payload: undefined;
public payload: Uint8Array = new Uint8Array();
public rateLimitProof: undefined;
public timestamp: undefined;
public ephemeral: undefined;
@ -17,7 +17,7 @@ export class TopicOnlyMessage implements IDecodedMessage {
constructor(private proto: ProtoTopicOnlyMessage) {}
get contentTopic(): string {
return this.proto.contentTopic ?? "";
return this.proto.contentTopic;
}
}
@ -29,7 +29,7 @@ export class TopicOnlyDecoder implements IDecoder<TopicOnlyMessage> {
log("Message decoded", protoMessage);
return Promise.resolve({
contentTopic: protoMessage.contentTopic,
payload: undefined,
payload: new Uint8Array(),
rateLimitProof: undefined,
timestamp: undefined,
version: undefined,

View File

@ -19,22 +19,15 @@ export { proto };
export class DecodedMessage implements IDecodedMessage {
constructor(protected proto: proto.WakuMessage) {}
get _rawPayload(): Uint8Array | undefined {
if (this.proto.payload) {
return new Uint8Array(this.proto.payload);
}
return;
}
get ephemeral(): boolean {
return Boolean(this.proto.ephemeral);
}
get payload(): Uint8Array | undefined {
return this._rawPayload;
get payload(): Uint8Array {
return this.proto.payload;
}
get contentTopic(): string | undefined {
get contentTopic(): string {
return this.proto.contentTopic;
}
@ -51,18 +44,15 @@ export class DecodedMessage implements IDecodedMessage {
const timestamp = this.proto.timestamp / OneMillion;
return new Date(Number(timestamp));
}
if (this.proto.timestampDeprecated) {
return new Date(this.proto.timestampDeprecated * 1000);
}
return;
} catch (e) {
return;
}
return;
}
get version(): number {
// https://github.com/status-im/js-waku/issues/921
// https://rfc.vac.dev/spec/14/
// > If omitted, the value SHOULD be interpreted as version 0.
return this.proto.version ?? 0;
}
@ -115,8 +105,8 @@ export class Decoder implements IDecoder<DecodedMessage> {
const protoMessage = proto.WakuMessage.decode(bytes);
log("Message decoded", protoMessage);
return Promise.resolve({
payload: protoMessage.payload ?? undefined,
contentTopic: protoMessage.contentTopic ?? undefined,
payload: protoMessage.payload,
contentTopic: protoMessage.contentTopic,
version: protoMessage.version ?? undefined,
timestamp: protoMessage.timestamp ?? undefined,
rateLimitProof: protoMessage.rateLimitProof ?? undefined,
@ -127,12 +117,9 @@ export class Decoder implements IDecoder<DecodedMessage> {
async fromProtoObj(
proto: IProtoMessage
): Promise<DecodedMessage | undefined> {
// https://github.com/status-im/js-waku/issues/921
if (proto.version === undefined) {
proto.version = 0;
}
if (proto.version !== Version) {
// https://rfc.vac.dev/spec/14/
// > If omitted, the value SHOULD be interpreted as version 0.
if (proto.version ?? 0 !== Version) {
log(
"Failed to decode due to incorrect version, expected:",
Version,

View File

@ -6,6 +6,7 @@ import { toProtoMessage } from "./to_proto_message.js";
describe("to proto message", () => {
it("Fields are not dropped", () => {
const wire: WakuMessageProto = {
payload: new Uint8Array(),
contentTopic: "foo",
};

View File

@ -2,8 +2,8 @@ import { IProtoMessage } from "@waku/interfaces";
import { WakuMessage as WakuMessageProto } from "@waku/proto";
const EmptyMessage: IProtoMessage = {
payload: undefined,
contentTopic: undefined,
payload: new Uint8Array(),
contentTopic: "",
version: undefined,
timestamp: undefined,
rateLimitProof: undefined,

View File

@ -13,8 +13,8 @@ export interface IRateLimitProof {
* Field types matches the protobuf type over the wire
*/
export interface IProtoMessage {
payload: Uint8Array | undefined;
contentTopic: string | undefined;
payload: Uint8Array;
contentTopic: string;
version: number | undefined;
timestamp: bigint | undefined;
rateLimitProof: IRateLimitProof | undefined;
@ -25,7 +25,7 @@ export interface IProtoMessage {
* Interface for messages to encode and send.
*/
export interface IMessage {
payload?: Uint8Array;
payload: Uint8Array;
timestamp?: Date;
rateLimitProof?: IRateLimitProof;
}
@ -48,8 +48,8 @@ export interface IEncoder {
}
export interface IDecodedMessage {
payload: Uint8Array | undefined;
contentTopic: string | undefined;
payload: Uint8Array;
contentTopic: string;
timestamp: Date | undefined;
rateLimitProof: IRateLimitProof | undefined;
ephemeral: boolean | undefined;

View File

@ -2,7 +2,7 @@ import type { PeerId } from "@libp2p/interface-peer-id";
import type { Peer, PeerStore } from "@libp2p/interface-peer-store";
import type { Libp2pOptions } from "libp2p";
import type { IMessage } from "./message.js";
import type { IDecodedMessage } from "./message.js";
export enum Protocols {
Relay = "relay",
@ -58,7 +58,9 @@ export type ProtocolOptions = {
peerId?: PeerId;
};
export type Callback<T extends IMessage> = (msg: T) => void | Promise<void>;
export type Callback<T extends IDecodedMessage> = (
msg: T
) => void | Promise<void>;
export interface SendResult {
recipients: PeerId[];

View File

@ -45,10 +45,6 @@ export class Encoder implements IEncoder {
async toProtoObj(message: IMessage): Promise<IProtoMessage | undefined> {
const timestamp = message.timestamp ?? new Date();
if (!message.payload) {
log("No payload to encrypt, skipping: ", message);
return;
}
const preparedPayload = await preCipher(message.payload, this.sigPrivKey);
const payload = await encryptAsymmetric(preparedPayload, this.publicKey);
@ -113,10 +109,6 @@ export class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
}
let payload;
if (!cipherPayload) {
log(`No payload to decrypt for contentTopic ${this.contentTopic}`);
return;
}
try {
payload = await decryptAsymmetric(cipherPayload, this.privateKey);

View File

@ -44,10 +44,6 @@ export class Encoder implements IEncoder {
async toProtoObj(message: IMessage): Promise<IProtoMessage | undefined> {
const timestamp = message.timestamp ?? new Date();
if (!message.payload) {
log("No payload to encrypt, skipping: ", message);
return;
}
const preparedPayload = await preCipher(message.payload, this.sigPrivKey);
const payload = await encryptSymmetric(preparedPayload, this.symKey);
@ -112,10 +108,6 @@ export class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
}
let payload;
if (!cipherPayload) {
log(`No payload to decrypt for contentTopic ${this.contentTopic}`);
return;
}
try {
payload = await decryptSymmetric(cipherPayload, this.symKey);

View File

@ -426,10 +426,9 @@ export namespace RateLimitProof {
}
export interface WakuMessage {
payload?: Uint8Array;
contentTopic?: string;
payload: Uint8Array;
contentTopic: string;
version?: number;
timestampDeprecated?: number;
timestamp?: bigint;
rateLimitProof?: RateLimitProof;
ephemeral?: boolean;
@ -446,12 +445,12 @@ export namespace WakuMessage {
w.fork();
}
if (obj.payload != null) {
if (obj.payload != null && obj.payload.byteLength > 0) {
w.uint32(10);
w.bytes(obj.payload);
}
if (obj.contentTopic != null) {
if (obj.contentTopic != null && obj.contentTopic !== "") {
w.uint32(18);
w.string(obj.contentTopic);
}
@ -461,11 +460,6 @@ export namespace WakuMessage {
w.uint32(obj.version);
}
if (obj.timestampDeprecated != null) {
w.uint32(33);
w.double(obj.timestampDeprecated);
}
if (obj.timestamp != null) {
w.uint32(80);
w.sint64(obj.timestamp);
@ -486,7 +480,10 @@ export namespace WakuMessage {
}
},
(reader, length) => {
const obj: any = {};
const obj: any = {
payload: new Uint8Array(0),
contentTopic: "",
};
const end = length == null ? reader.len : reader.pos + length;
@ -503,9 +500,6 @@ export namespace WakuMessage {
case 3:
obj.version = reader.uint32();
break;
case 4:
obj.timestampDeprecated = reader.double();
break;
case 10:
obj.timestamp = reader.sint64();
break;

View File

@ -358,10 +358,9 @@ export namespace RateLimitProof {
}
export interface WakuMessage {
payload?: Uint8Array;
contentTopic?: string;
payload: Uint8Array;
contentTopic: string;
version?: number;
timestampDeprecated?: number;
timestamp?: bigint;
rateLimitProof?: RateLimitProof;
ephemeral?: boolean;
@ -378,12 +377,12 @@ export namespace WakuMessage {
w.fork();
}
if (obj.payload != null) {
if (obj.payload != null && obj.payload.byteLength > 0) {
w.uint32(10);
w.bytes(obj.payload);
}
if (obj.contentTopic != null) {
if (obj.contentTopic != null && obj.contentTopic !== "") {
w.uint32(18);
w.string(obj.contentTopic);
}
@ -393,11 +392,6 @@ export namespace WakuMessage {
w.uint32(obj.version);
}
if (obj.timestampDeprecated != null) {
w.uint32(33);
w.double(obj.timestampDeprecated);
}
if (obj.timestamp != null) {
w.uint32(80);
w.sint64(obj.timestamp);
@ -418,7 +412,10 @@ export namespace WakuMessage {
}
},
(reader, length) => {
const obj: any = {};
const obj: any = {
payload: new Uint8Array(0),
contentTopic: "",
};
const end = length == null ? reader.len : reader.pos + length;
@ -435,9 +432,6 @@ export namespace WakuMessage {
case 3:
obj.version = reader.uint32();
break;
case 4:
obj.timestampDeprecated = reader.double();
break;
case 10:
obj.timestamp = reader.sint64();
break;

View File

@ -1,3 +1,5 @@
// 14/WAKU2-MESSAGE rfc: https://rfc.vac.dev/spec/14/
syntax = "proto3";
message RateLimitProof {
@ -7,16 +9,14 @@ message RateLimitProof {
bytes share_x = 4;
bytes share_y = 5;
bytes nullifier = 6;
bytes rlnIdentifier = 7;
bytes rln_identifier = 7;
}
message WakuMessage {
optional bytes payload = 1;
optional string content_topic = 2;
bytes payload = 1;
string content_topic = 2;
optional uint32 version = 3;
optional double timestamp_deprecated = 4;
optional sint64 timestamp = 10;
optional sint64 timestamp = 10;
optional RateLimitProof rate_limit_proof = 21;
optional bool ephemeral = 31;
}

View File

@ -130,10 +130,9 @@ export namespace RateLimitProof {
}
export interface WakuMessage {
payload?: Uint8Array;
contentTopic?: string;
payload: Uint8Array;
contentTopic: string;
version?: number;
timestampDeprecated?: number;
timestamp?: bigint;
rateLimitProof?: RateLimitProof;
ephemeral?: boolean;
@ -150,12 +149,12 @@ export namespace WakuMessage {
w.fork();
}
if (obj.payload != null) {
if (obj.payload != null && obj.payload.byteLength > 0) {
w.uint32(10);
w.bytes(obj.payload);
}
if (obj.contentTopic != null) {
if (obj.contentTopic != null && obj.contentTopic !== "") {
w.uint32(18);
w.string(obj.contentTopic);
}
@ -165,11 +164,6 @@ export namespace WakuMessage {
w.uint32(obj.version);
}
if (obj.timestampDeprecated != null) {
w.uint32(33);
w.double(obj.timestampDeprecated);
}
if (obj.timestamp != null) {
w.uint32(80);
w.sint64(obj.timestamp);
@ -190,7 +184,10 @@ export namespace WakuMessage {
}
},
(reader, length) => {
const obj: any = {};
const obj: any = {
payload: new Uint8Array(0),
contentTopic: "",
};
const end = length == null ? reader.len : reader.pos + length;
@ -207,9 +204,6 @@ export namespace WakuMessage {
case 3:
obj.version = reader.uint32();
break;
case 4:
obj.timestampDeprecated = reader.double();
break;
case 10:
obj.timestamp = reader.sint64();
break;

View File

@ -662,10 +662,9 @@ export namespace RateLimitProof {
}
export interface WakuMessage {
payload?: Uint8Array;
contentTopic?: string;
payload: Uint8Array;
contentTopic: string;
version?: number;
timestampDeprecated?: number;
timestamp?: bigint;
rateLimitProof?: RateLimitProof;
ephemeral?: boolean;
@ -682,12 +681,12 @@ export namespace WakuMessage {
w.fork();
}
if (obj.payload != null) {
if (obj.payload != null && obj.payload.byteLength > 0) {
w.uint32(10);
w.bytes(obj.payload);
}
if (obj.contentTopic != null) {
if (obj.contentTopic != null && obj.contentTopic !== "") {
w.uint32(18);
w.string(obj.contentTopic);
}
@ -697,11 +696,6 @@ export namespace WakuMessage {
w.uint32(obj.version);
}
if (obj.timestampDeprecated != null) {
w.uint32(33);
w.double(obj.timestampDeprecated);
}
if (obj.timestamp != null) {
w.uint32(80);
w.sint64(obj.timestamp);
@ -722,7 +716,10 @@ export namespace WakuMessage {
}
},
(reader, length) => {
const obj: any = {};
const obj: any = {
payload: new Uint8Array(0),
contentTopic: "",
};
const end = length == null ? reader.len : reader.pos + length;
@ -739,9 +736,6 @@ export namespace WakuMessage {
case 3:
obj.version = reader.uint32();
break;
case 4:
obj.timestampDeprecated = reader.double();
break;
case 10:
obj.timestamp = reader.sint64();
break;

View File

@ -1,5 +1,5 @@
syntax = "proto3";
message TopicOnlyMessage {
optional string content_topic = 2;
string content_topic = 2;
}

View File

@ -9,7 +9,7 @@ import type { Codec } from "protons-runtime";
import type { Uint8ArrayList } from "uint8arraylist";
export interface TopicOnlyMessage {
contentTopic?: string;
contentTopic: string;
}
export namespace TopicOnlyMessage {
@ -23,7 +23,7 @@ export namespace TopicOnlyMessage {
w.fork();
}
if (obj.contentTopic != null) {
if (obj.contentTopic != null && obj.contentTopic !== "") {
w.uint32(18);
w.string(obj.contentTopic);
}
@ -33,7 +33,9 @@ export namespace TopicOnlyMessage {
}
},
(reader, length) => {
const obj: any = {};
const obj: any = {
contentTopic: "",
};
const end = length == null ? reader.len : reader.pos + length;

View File

@ -192,10 +192,10 @@ describe("Waku Message Ephemeral field", () => {
}
const normalMsg = messages.find(
(msg) => bytesToUtf8(msg.payload!) === normalTxt
(msg) => bytesToUtf8(msg.payload) === normalTxt
);
const ephemeralMsg = messages.find(
(msg) => bytesToUtf8(msg.payload!) === ephemeralTxt
(msg) => bytesToUtf8(msg.payload) === ephemeralTxt
);
expect(normalMsg).to.not.be.undefined;
@ -241,10 +241,10 @@ describe("Waku Message Ephemeral field", () => {
}
const normalMsg = messages.find(
(msg) => bytesToUtf8(msg.payload!) === normalTxt
(msg) => bytesToUtf8(msg.payload) === normalTxt
);
const ephemeralMsg = messages.find(
(msg) => bytesToUtf8(msg.payload!) === ephemeralTxt
(msg) => bytesToUtf8(msg.payload) === ephemeralTxt
);
expect(normalMsg).to.not.be.undefined;
@ -291,10 +291,10 @@ describe("Waku Message Ephemeral field", () => {
}
const normalMsg = messages.find(
(msg) => bytesToUtf8(msg.payload!) === normalTxt
(msg) => bytesToUtf8(msg.payload) === normalTxt
);
const ephemeralMsg = messages.find(
(msg) => bytesToUtf8(msg.payload!) === ephemeralTxt
(msg) => bytesToUtf8(msg.payload) === ephemeralTxt
);
expect(normalMsg).to.not.be.undefined;

View File

@ -52,7 +52,7 @@ describe("Waku Filter", () => {
log("Got a message");
messageCount++;
expect(msg.contentTopic).to.eq(TestContentTopic);
expect(bytesToUtf8(msg.payload!)).to.eq(messageText);
expect(bytesToUtf8(msg.payload)).to.eq(messageText);
};
await waku.filter.subscribe([TestDecoder], callback);

View File

@ -106,6 +106,6 @@ describe("Waku Light Push [node only]", () => {
}
expect(msgs[0].contentTopic).to.equal(TestContentTopic);
expect(base64ToUtf8(msgs[0].payload!)).to.equal(messageText);
expect(base64ToUtf8(msgs[0].payload)).to.equal(messageText);
});
});

View File

@ -130,7 +130,7 @@ describe("Waku Relay [node only]", () => {
const receivedMsg = await receivedMsgPromise;
expect(receivedMsg.contentTopic).to.eq(TestContentTopic);
expect(bytesToUtf8(receivedMsg.payload!)).to.eq(messageText);
expect(bytesToUtf8(receivedMsg.payload)).to.eq(messageText);
expect(receivedMsg.timestamp?.valueOf()).to.eq(
messageTimestamp.valueOf()
);
@ -173,10 +173,10 @@ describe("Waku Relay [node only]", () => {
}
expect(fooMessages[0].contentTopic).to.eq(fooContentTopic);
expect(bytesToUtf8(fooMessages[0].payload!)).to.eq(fooMessageText);
expect(bytesToUtf8(fooMessages[0].payload)).to.eq(fooMessageText);
expect(barMessages[0].contentTopic).to.eq(barContentTopic);
expect(bytesToUtf8(barMessages[0].payload!)).to.eq(barMessageText);
expect(bytesToUtf8(barMessages[0].payload)).to.eq(barMessageText);
expect(fooMessages.length).to.eq(1);
expect(barMessages.length).to.eq(1);

View File

@ -90,7 +90,7 @@ describe("Waku Store", () => {
expect(messages?.length).eq(totalMsgs);
const result = messages?.findIndex((msg) => {
return msg.payload![0]! === 0;
return msg.payload[0]! === 0;
});
expect(result).to.not.eq(-1);
});
@ -173,8 +173,8 @@ describe("Waku Store", () => {
expect(messages.length).be.eq(totalMsgs);
expect(bytesToUtf8(testMessage.payload!)).to.be.eq(
bytesToUtf8(messages[cursorIndex + 1].payload!)
expect(bytesToUtf8(testMessage.payload)).to.be.eq(
bytesToUtf8(messages[cursorIndex + 1].payload)
);
});
@ -214,7 +214,7 @@ describe("Waku Store", () => {
expect(messages?.length).eq(totalMsgs);
const result = messages?.findIndex((msg) => {
return msg.payload![0]! === 0;
return msg.payload[0]! === 0;
});
expect(result).to.not.eq(-1);
});
@ -294,7 +294,7 @@ describe("Waku Store", () => {
);
expect(messages?.length).eq(totalMsgs);
const payloads = messages.map((msg) => msg.payload![0]!);
const payloads = messages.map((msg) => msg.payload[0]!);
expect(payloads).to.deep.eq(Array.from(Array(totalMsgs).keys()));
});

View File

@ -187,7 +187,7 @@ describe("Decryption Keys", () => {
const receivedMsg = await receivedMsgPromise;
expect(receivedMsg.contentTopic).to.eq(TestContentTopic);
expect(bytesToUtf8(receivedMsg.payload!)).to.eq(messageText);
expect(bytesToUtf8(receivedMsg.payload)).to.eq(messageText);
expect(receivedMsg.timestamp?.valueOf()).to.eq(messageTimestamp.valueOf());
});
});