Skip to content

Commit

Permalink
chore!: update message.proto: payload and content topic are always de…
Browse files Browse the repository at this point in the history
  • Loading branch information
fryorcraken committed Feb 24, 2023
1 parent 853b4ca commit 2710889
Show file tree
Hide file tree
Showing 21 changed files with 87 additions and 135 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/lib/message/topic_only_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ 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;

constructor(private proto: ProtoTopicOnlyMessage) {}

get contentTopic(): string {
return this.proto.contentTopic ?? "";
return this.proto.contentTopic;
}
}

Expand All @@ -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,
Expand Down
35 changes: 11 additions & 24 deletions packages/core/src/lib/message/version_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/lib/to_proto_message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/to_proto_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions packages/interfaces/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions packages/interfaces/src/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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[];
Expand Down
8 changes: 0 additions & 8 deletions packages/message-encryption/src/ecies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions packages/message-encryption/src/symmetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 8 additions & 14 deletions packages/proto/src/lib/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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;
Expand Down
22 changes: 8 additions & 14 deletions packages/proto/src/lib/light_push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions packages/proto/src/lib/message.proto
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// 14/WAKU2-MESSAGE rfc: https://rfc.vac.dev/spec/14/

syntax = "proto3";

message RateLimitProof {
Expand All @@ -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;
}

Loading

0 comments on commit 2710889

Please sign in to comment.