Skip to content

Commit

Permalink
feat!: determine pubsubTopic based on contentTopic
Browse files Browse the repository at this point in the history
feat!: determine pubsubTopic based on contentTopic
  • Loading branch information
weboko authored Apr 16, 2024
2 parents da1f593 + 69159ec commit 814512c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,12 @@
"@stablelib/x25519": "^1.0.1",
"@waku/core": "0.0.28",
"@waku/proto": "0.0.6",
"@waku/utils": "^0.0.16",
"bn.js": "^5.2.1",
"eventemitter3": "^5.0.0",
"p-event": "^5.0.1",
"pkcs7-padding": "^0.1.1",
"uint8arraylist": "^2.3.2",
"uint8arrays": "^4.0.2"
}
}
}
28 changes: 17 additions & 11 deletions src/codec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DecodedMessage } from "@waku/core/lib/message/version_0";
import {
DefaultPubsubTopic,
type IDecodedMessage,
type IDecoder,
type IEncoder,
Expand All @@ -9,6 +8,7 @@ import {
type IProtoMessage,
} from "@waku/interfaces";
import { WakuMessage } from "@waku/proto";
import { contentTopicToPubsubTopic } from "@waku/utils";
import debug from "debug";

import { HandshakeResult, HandshakeStepResult } from "./handshake.js";
Expand All @@ -25,8 +25,6 @@ const version = 2;
* Used internally in the pairing object to represent a handshake message
*/
export class NoiseHandshakeMessage extends DecodedMessage implements IDecodedMessage {
pubSubTopic = DefaultPubsubTopic;

get payloadV2(): PayloadV2 {
if (!this.payload) throw new Error("no payload available");
return PayloadV2.deserialize(this.payload);
Expand All @@ -43,13 +41,15 @@ export class NoiseHandshakeEncoder implements IEncoder {
* @param hsStepResult the result of a step executed while performing the handshake process
* @param ephemeral makes messages ephemeral in the Waku network
*/
pubsubTopic = DefaultPubsubTopic;
pubsubTopic: string;

constructor(
public contentTopic: string,
private hsStepResult: HandshakeStepResult,
public ephemeral: boolean = true
) {}
) {
this.pubsubTopic = contentTopicToPubsubTopic(contentTopic);
}

async toWire(message: IMessage): Promise<Uint8Array | undefined> {
const protoMessage = await this.toProtoObj(message);
Expand Down Expand Up @@ -79,9 +79,11 @@ export class NoiseHandshakeDecoder implements IDecoder<NoiseHandshakeMessage> {
/**
* @param contentTopic content topic on which the encoded WakuMessages were sent
*/
pubsubTopic = DefaultPubsubTopic;
pubsubTopic: string;

constructor(public contentTopic: string) {}
constructor(public contentTopic: string) {
this.pubsubTopic = contentTopicToPubsubTopic(contentTopic);
}

fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined> {
const protoMessage = WakuMessage.decode(bytes);
Expand Down Expand Up @@ -141,14 +143,16 @@ export class NoiseSecureTransferEncoder implements IEncoder {
* @param ephemeral whether messages should be tagged as ephemeral defaults to true.
* @param metaSetter callback function that set the `meta` field.
*/
pubsubTopic = DefaultPubsubTopic;
pubsubTopic: string;

constructor(
public contentTopic: string,
private hsResult: HandshakeResult,
public ephemeral: boolean = true,
public metaSetter?: IMetaSetter
) {}
) {
this.pubsubTopic = contentTopicToPubsubTopic(contentTopic);
}

async toWire(message: IMessage): Promise<Uint8Array | undefined> {
const protoMessage = await this.toProtoObj(message);
Expand Down Expand Up @@ -197,9 +201,11 @@ export class NoiseSecureTransferDecoder implements IDecoder<NoiseSecureMessage>
* @param contentTopic content topic on which the encoded WakuMessages were sent
* @param hsResult handshake result obtained after the handshake is successful
*/
pubsubTopic = DefaultPubsubTopic;
pubsubTopic: string;

constructor(public contentTopic: string, private hsResult: HandshakeResult) {}
constructor(public contentTopic: string, private hsResult: HandshakeResult) {
this.pubsubTopic = contentTopicToPubsubTopic(contentTopic);
}

fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined> {
const protoMessage = WakuMessage.decode(bytes);
Expand Down
2 changes: 1 addition & 1 deletion src/pairing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const PUBSUB_TOPIC = "default";

const EMPTY_PROTOMESSAGE = {
timestamp: undefined,
contentTopic: "",
contentTopic: "/js-noise/1/message/proto",
ephemeral: undefined,
meta: undefined,
rateLimitProof: undefined,
Expand Down
5 changes: 2 additions & 3 deletions src/pairing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,12 @@ export class WakuPairing {

/**
* Convert a QR into a content topic
* Must follow - https://github.com/vacp2p/rfc-index/blob/main/waku/informational/23/topics.md
* @param qr
* @returns content topic string
*/
private static toContentTopic(qr: QR): string {
return (
"/" + qr.applicationName + "/" + qr.applicationVersion + "/wakunoise/1/sessions_shard-" + qr.shardId + "/proto"
);
return "/" + qr.applicationName + "/" + qr.applicationVersion + "/" + qr.shardId + "/proto";
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/waku-noise-pairing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ describe("Waku Noise Sessions", () => {
expect(uint8ArrayEquals(bobCommittedStaticKey, readQR.committedStaticKey)).to.be.true;

// We set the contentTopic from the content topic parameters exchanged in the QR
const contentTopic =
"/" + applicationName + "/" + applicationVersion + "/wakunoise/1/sessions_shard-" + shardId + "/proto";
const contentTopic = "/" + applicationName + "/" + applicationVersion + "/" + shardId + "/proto";

// Pre-handshake message
// <- eB {H(sB||r), contentTopicParams, messageNametag}
Expand Down

0 comments on commit 814512c

Please sign in to comment.