Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export { createEncoder, createDecoder } from "./lib/message/version_0.js";
export { createCodec } from "./lib/message/index.js";
export type {
Encoder,
Decoder,
DecodedMessage
} from "./lib/message/version_0.js";
export type { Codec } from "./lib/message/index.js";
export * as message from "./lib/message/index.js";

export * as waku_filter from "./lib/filter/index.js";
Expand Down
76 changes: 76 additions & 0 deletions packages/core/src/lib/message/codec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type {
ICodec,
IDecodedMessage,
IDecoder,
IEncoder,
IMessage,
IMetaSetter,
IProtoMessage,
IRoutingInfo,
PubsubTopic
} from "@waku/interfaces";

import { Decoder, Encoder } from "./version_0.js";

export class Codec implements ICodec<IDecodedMessage> {
private encoder: IEncoder;
private decoder: IDecoder<IDecodedMessage>;

public constructor(
public contentTopic: string,
public ephemeral: boolean = false,
public routingInfo: IRoutingInfo,
public metaSetter?: IMetaSetter
) {
this.encoder = new Encoder(
contentTopic,
ephemeral,
routingInfo,
metaSetter
);
this.decoder = new Decoder(contentTopic, routingInfo);
}

public get pubsubTopic(): PubsubTopic {
return this.routingInfo.pubsubTopic;
}

public async toWire(message: IMessage): Promise<Uint8Array | undefined> {
return this.encoder.toWire(message);
}

public async toProtoObj(
message: IMessage
): Promise<IProtoMessage | undefined> {
return this.encoder.toProtoObj(message);
}

public fromWireToProtoObj(
bytes: Uint8Array
): Promise<IProtoMessage | undefined> {
return this.decoder.fromWireToProtoObj(bytes);
}

public async fromProtoObj(
pubsubTopic: string,
proto: IProtoMessage
): Promise<IDecodedMessage | undefined> {
return this.decoder.fromProtoObj(pubsubTopic, proto);
}
}

type CodecParams = {
contentTopic: string;
ephemeral: boolean;
routingInfo: IRoutingInfo;
metaSetter?: IMetaSetter;
};

export function createCodec(params: CodecParams): Codec {
return new Codec(
params.contentTopic,
params.ephemeral,
params.routingInfo,
params.metaSetter
);
}
2 changes: 2 additions & 0 deletions packages/core/src/lib/message/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const OneMillion = BigInt(1_000_000);
export const Version = 0;
2 changes: 2 additions & 0 deletions packages/core/src/lib/message/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * as version_0 from "./version_0.js";
export { Codec, createCodec } from "./codec.js";
export { OneMillion, Version } from "./constants.js";
4 changes: 2 additions & 2 deletions packages/core/src/lib/message/version_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import type {
import { proto_message as proto } from "@waku/proto";
import { Logger } from "@waku/utils";

import { OneMillion, Version } from "./constants.js";

const log = new Logger("message:version-0");
const OneMillion = BigInt(1_000_000);

export const Version = 0;
export { proto };

export class DecodedMessage implements IDecodedMessage {
Expand Down
2 changes: 2 additions & 0 deletions packages/interfaces/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,5 @@ export interface IDecoder<T extends IDecodedMessage> {
proto: IProtoMessage
) => Promise<T | undefined>;
}

export type ICodec<T extends IDecodedMessage> = IEncoder & IDecoder<T>;
38 changes: 37 additions & 1 deletion packages/interfaces/src/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { IFilter } from "./filter.js";
import type { HealthStatus } from "./health_status.js";
import type { Libp2p } from "./libp2p.js";
import type { ILightPush } from "./light_push.js";
import { IDecodedMessage, IDecoder, IEncoder } from "./message.js";
import { ICodec, IDecodedMessage, IDecoder, IEncoder } from "./message.js";
import type { Protocols } from "./protocols.js";
import type { IRelay } from "./relay.js";
import type { ShardId } from "./sharding.js";
Expand All @@ -25,6 +25,8 @@ export type CreateEncoderParams = CreateDecoderParams & {
ephemeral?: boolean;
};

export type CreateCodecParams = CreateDecoderParams & CreateEncoderParams;

export interface IWakuEvents {
/**
* Emitted when a connection is established or lost.
Expand Down Expand Up @@ -188,6 +190,8 @@ export interface IWaku {
waitForPeers(protocols?: Protocols[], timeoutMs?: number): Promise<void>;

/**
* @deprecated Use {@link createCodec} instead
*
* Creates a decoder for Waku messages on a specific content topic.
*
* A decoder is used to decode messages from the Waku network format.
Expand Down Expand Up @@ -217,6 +221,8 @@ export interface IWaku {
createDecoder(params: CreateDecoderParams): IDecoder<IDecodedMessage>;

/**
* @deprecated Use {@link createCodec} instead
*
* Creates an encoder for Waku messages on a specific content topic.
*
* An encoder is used to encode messages into the Waku network format.
Expand Down Expand Up @@ -246,6 +252,36 @@ export interface IWaku {
*/
createEncoder(params: CreateEncoderParams): IEncoder;

/**
* Creates a codec for Waku messages on a specific content topic.
*
* A codec is used to encode and decode messages from the Waku network format.
* The codec automatically handles shard configuration based on the Waku node's network settings.
*
* @param {CreateCodecParams} params - Configuration for the codec including content topic and optionally shard information and ephemeral flag
* @returns {ICodec<IDecodedMessage>} A codec instance configured for the specified content topic
* @throws {Error} If the shard configuration is incompatible with the node's network settings
*
* @example
* ```typescript
* // Create a codec with default network shard settings
* const codec = waku.createCodec({
* contentTopic: "/my-app/1/chat/proto"
* });
*
* // Create a codec with custom shard settings
* const customCodec = waku.createCodec({
* contentTopic: "/my-app/1/chat/proto",
* ephemeral: true,
* shardInfo: {
* clusterId: 1,
* shard: 5
* }
* });
* ```
*/
createCodec(params: CreateCodecParams): ICodec<IDecodedMessage>;

/**
* @returns {boolean} `true` if the node was started and `false` otherwise
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/rln/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {

export class RlnMessage<T extends IDecodedMessage> implements IRlnMessage {
public pubsubTopic = "";
public version = message.version_0.Version;
public version = message.Version;

public constructor(
private rlnInstance: RLNInstance,
Expand Down
23 changes: 22 additions & 1 deletion packages/sdk/src/waku/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import {
TypedEventEmitter
} from "@libp2p/interface";
import type { MultiaddrInput } from "@multiformats/multiaddr";
import { ConnectionManager, createDecoder, createEncoder } from "@waku/core";
import {
ConnectionManager,
createCodec,
createDecoder,
createEncoder
} from "@waku/core";
import type {
CreateCodecParams,
CreateDecoderParams,
CreateEncoderParams,
CreateNodeOptions,
ICodec,
IDecodedMessage,
IDecoder,
IEncoder,
Expand Down Expand Up @@ -264,6 +271,7 @@ export class WakuNode implements IWaku {
params.contentTopic,
params.shardId
);

return createDecoder(params.contentTopic, routingInfo);
}

Expand All @@ -280,6 +288,19 @@ export class WakuNode implements IWaku {
});
}

public createCodec(params: CreateCodecParams): ICodec<IDecodedMessage> {
const routingInfo = this.createRoutingInfo(
params.contentTopic,
params.shardId
);

return createCodec({
contentTopic: params.contentTopic,
ephemeral: params.ephemeral ?? false,
routingInfo: routingInfo
});
}

private createRoutingInfo(
contentTopic?: string,
shardId?: number
Expand Down
Loading