2023-09-27 15:28:07 +05:30
|
|
|
import { DefaultPubSubTopic } from "@waku/core";
|
2022-12-05 17:07:03 +11:00
|
|
|
import type {
|
|
|
|
|
IDecodedMessage,
|
|
|
|
|
IDecoder,
|
2023-08-16 20:18:13 +05:30
|
|
|
IProtoMessage
|
2022-12-05 17:07:03 +11:00
|
|
|
} from "@waku/interfaces";
|
2022-12-21 00:48:16 +11:00
|
|
|
import { TopicOnlyMessage as ProtoTopicOnlyMessage } from "@waku/proto";
|
2022-09-19 16:20:33 +10:00
|
|
|
import debug from "debug";
|
|
|
|
|
|
|
|
|
|
const log = debug("waku:message:topic-only");
|
|
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
export class TopicOnlyMessage implements IDecodedMessage {
|
2023-02-24 23:22:04 +11:00
|
|
|
public payload: Uint8Array = new Uint8Array();
|
2022-09-30 12:50:43 +10:00
|
|
|
public rateLimitProof: undefined;
|
|
|
|
|
public timestamp: undefined;
|
2023-03-10 14:41:07 +11:00
|
|
|
public meta: undefined;
|
2022-11-16 11:00:43 +11:00
|
|
|
public ephemeral: undefined;
|
2022-09-30 12:50:43 +10:00
|
|
|
|
2023-03-10 16:43:21 +11:00
|
|
|
constructor(
|
|
|
|
|
public pubSubTopic: string,
|
2023-08-16 20:18:13 +05:30
|
|
|
private proto: ProtoTopicOnlyMessage
|
2023-03-10 16:43:21 +11:00
|
|
|
) {}
|
2022-09-19 16:20:33 +10:00
|
|
|
|
|
|
|
|
get contentTopic(): string {
|
2023-02-24 23:22:04 +11:00
|
|
|
return this.proto.contentTopic;
|
2022-09-19 16:20:33 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
export class TopicOnlyDecoder implements IDecoder<TopicOnlyMessage> {
|
2023-09-27 15:28:07 +05:30
|
|
|
pubSubTopic = DefaultPubSubTopic;
|
2022-09-19 16:20:33 +10:00
|
|
|
public contentTopic = "";
|
|
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined> {
|
2022-12-21 00:48:16 +11:00
|
|
|
const protoMessage = ProtoTopicOnlyMessage.decode(bytes);
|
2022-09-19 16:20:33 +10:00
|
|
|
log("Message decoded", protoMessage);
|
2022-09-30 12:50:43 +10:00
|
|
|
return Promise.resolve({
|
|
|
|
|
contentTopic: protoMessage.contentTopic,
|
2023-02-24 23:22:04 +11:00
|
|
|
payload: new Uint8Array(),
|
2022-09-30 12:50:43 +10:00
|
|
|
rateLimitProof: undefined,
|
|
|
|
|
timestamp: undefined,
|
2023-03-10 14:41:07 +11:00
|
|
|
meta: undefined,
|
2022-09-30 12:50:43 +10:00
|
|
|
version: undefined,
|
2023-08-16 20:18:13 +05:30
|
|
|
ephemeral: undefined
|
2022-09-30 12:50:43 +10:00
|
|
|
});
|
2022-09-19 16:20:33 +10:00
|
|
|
}
|
|
|
|
|
|
2022-09-30 12:55:25 +10:00
|
|
|
async fromProtoObj(
|
2023-03-10 16:43:21 +11:00
|
|
|
pubSubTopic: string,
|
2023-08-16 20:18:13 +05:30
|
|
|
proto: IProtoMessage
|
2022-09-30 12:55:25 +10:00
|
|
|
): Promise<TopicOnlyMessage | undefined> {
|
2023-03-10 16:43:21 +11:00
|
|
|
return new TopicOnlyMessage(pubSubTopic, proto);
|
2022-09-19 16:20:33 +10:00
|
|
|
}
|
|
|
|
|
}
|