From 7100f31ed8dab84399fa735487b24c85b696d200 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 18 Apr 2024 23:26:47 +0200 Subject: [PATCH 1/5] feat: add pubsub topic parameter --- src/codec.ts | 18 ++++++++---------- src/pairing.ts | 20 ++++++++++++-------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/codec.ts b/src/codec.ts index 206cc6e..7ac68f1 100644 --- a/src/codec.ts +++ b/src/codec.ts @@ -37,18 +37,18 @@ export class NoiseHandshakeMessage extends DecodedMessage implements IDecodedMes */ export class NoiseHandshakeEncoder implements IEncoder { /** + * @param pubsubTopic pubsub topic on which handshake happens * @param contentTopic content topic on which the encoded WakuMessages will be sent * @param hsStepResult the result of a step executed while performing the handshake process * @param ephemeral makes messages ephemeral in the Waku network */ - pubsubTopic: string; constructor( public contentTopic: string, + public pubsubTopic: string, private hsStepResult: HandshakeStepResult, public ephemeral: boolean = true ) { - this.pubsubTopic = contentTopicToPubsubTopic(contentTopic); } async toWire(message: IMessage): Promise { @@ -77,12 +77,11 @@ export class NoiseHandshakeEncoder implements IEncoder { */ export class NoiseHandshakeDecoder implements IDecoder { /** + * @param pubsubTopic pubsub topic on which handshake happens * @param contentTopic content topic on which the encoded WakuMessages were sent */ - pubsubTopic: string; - constructor(public contentTopic: string) { - this.pubsubTopic = contentTopicToPubsubTopic(contentTopic); + constructor(public contentTopic: string, public pubsubTopic: string) { } fromWireToProtoObj(bytes: Uint8Array): Promise { @@ -139,19 +138,19 @@ export class NoiseSecureMessage extends DecodedMessage implements IDecodedMessag export class NoiseSecureTransferEncoder implements IEncoder { /** * @param contentTopic content topic on which the encoded WakuMessages were sent. + * @param pubsubTopic pubsub topic on which handshake happens * @param hsResult handshake result obtained after the handshake is successful. * @param ephemeral whether messages should be tagged as ephemeral defaults to true. * @param metaSetter callback function that set the `meta` field. */ - pubsubTopic: string; constructor( public contentTopic: string, + public pubsubTopic: string, private hsResult: HandshakeResult, public ephemeral: boolean = true, public metaSetter?: IMetaSetter ) { - this.pubsubTopic = contentTopicToPubsubTopic(contentTopic); } async toWire(message: IMessage): Promise { @@ -199,12 +198,11 @@ export class NoiseSecureTransferEncoder implements IEncoder { export class NoiseSecureTransferDecoder implements IDecoder { /** * @param contentTopic content topic on which the encoded WakuMessages were sent + * @param pubsubTopic pubsub topic on which handshake happens * @param hsResult handshake result obtained after the handshake is successful */ - pubsubTopic: string; - constructor(public contentTopic: string, private hsResult: HandshakeResult) { - this.pubsubTopic = contentTopicToPubsubTopic(contentTopic); + constructor(public contentTopic: string, public pubsubTopic: string, private hsResult: HandshakeResult) { } fromWireToProtoObj(bytes: Uint8Array): Promise { diff --git a/src/pairing.ts b/src/pairing.ts index 36e5b92..cd3a2f6 100644 --- a/src/pairing.ts +++ b/src/pairing.ts @@ -82,6 +82,7 @@ export class WakuPairing { } /** + * @param pubsubTopic pubsubTopic to be used for handshake * @param sender object that implements Sender interface to publish waku messages * @param responder object that implements Responder interface to subscribe and receive waku messages * @param myStaticKey x25519 keypair @@ -90,6 +91,7 @@ export class WakuPairing { * @param encoderParameters optional parameters for the resulting encoders */ constructor( + private pubsubTopic: string, private sender: ISender, private responder: IReceiver, private myStaticKey: KeyPair, @@ -223,7 +225,7 @@ export class WakuPairing { private async initiatorHandshake(): Promise<[NoiseSecureTransferEncoder, NoiseSecureTransferDecoder]> { // Subscribe to the contact content topic - const decoder = new NoiseHandshakeDecoder(this.contentTopic); + const decoder = new NoiseHandshakeDecoder(this.contentTopic, this.pubsubTopic); const subscriptionIterator = await this.responder.toSubscriptionIterator(decoder); // The handshake initiator writes a Waku2 payload v2 containing the handshake message @@ -236,7 +238,7 @@ export class WakuPairing { // We prepare a message from initiator's payload2 // At this point wakuMsg is sent over the Waku network to responder content topic - let encoder = new NoiseHandshakeEncoder(this.contentTopic, hsStep); + let encoder = new NoiseHandshakeEncoder(this.contentTopic, this.pubsubTopic, hsStep); await this.sender.send(encoder, { payload: new Uint8Array(), }); @@ -281,7 +283,7 @@ export class WakuPairing { messageNametag: this.handshake.hs.toMessageNametag(), }); - encoder = new NoiseHandshakeEncoder(this.contentTopic, hsStep); + encoder = new NoiseHandshakeEncoder(this.contentTopic, this.pubsubTopic, hsStep); await this.sender.send(encoder, { payload: new Uint8Array(), }); @@ -291,12 +293,12 @@ export class WakuPairing { this.eventEmitter.emit("pairingComplete"); - return WakuPairing.getSecureCodec(this.contentTopic, this.handshakeResult, this.encoderParameters); + return WakuPairing.getSecureCodec(this.contentTopic, this.pubsubTopic, this.handshakeResult, this.encoderParameters); } private async responderHandshake(): Promise<[NoiseSecureTransferEncoder, NoiseSecureTransferDecoder]> { // Subscribe to the contact content topic - const decoder = new NoiseHandshakeDecoder(this.contentTopic); + const decoder = new NoiseHandshakeDecoder(this.contentTopic, this.pubsubTopic); const subscriptionIterator = await this.responder.toSubscriptionIterator(decoder); // the received reads the initiator's payloads, and returns the (decrypted) transport message the initiator sent @@ -322,7 +324,7 @@ export class WakuPairing { }); // We prepare a Waku message from responder's payload2 - const encoder = new NoiseHandshakeEncoder(this.contentTopic, hsStep); + const encoder = new NoiseHandshakeEncoder(this.contentTopic, this.pubsubTopic, hsStep); await this.sender.send(encoder, { payload: new Uint8Array(), }); @@ -355,7 +357,7 @@ export class WakuPairing { this.eventEmitter.emit("pairingComplete"); - return WakuPairing.getSecureCodec(this.contentTopic, this.handshakeResult, this.encoderParameters); + return WakuPairing.getSecureCodec(this.contentTopic, this.pubsubTopic, this.handshakeResult, this.encoderParameters); } /** @@ -368,16 +370,18 @@ export class WakuPairing { */ static getSecureCodec( contentTopic: string, + pubsubTopic: string, hsResult: HandshakeResult, encoderParameters: EncoderParameters ): [NoiseSecureTransferEncoder, NoiseSecureTransferDecoder] { const secureEncoder = new NoiseSecureTransferEncoder( contentTopic, + pubsubTopic, hsResult, encoderParameters.ephemeral, encoderParameters.metaSetter ); - const secureDecoder = new NoiseSecureTransferDecoder(contentTopic, hsResult); + const secureDecoder = new NoiseSecureTransferDecoder(contentTopic, pubsubTopic, hsResult); return [secureEncoder, secureDecoder]; } From fa5cc104a1be2c7ef407159f19384a54ff67b806 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 18 Apr 2024 23:33:18 +0200 Subject: [PATCH 2/5] remove unused --- src/codec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/codec.ts b/src/codec.ts index 7ac68f1..7a0b948 100644 --- a/src/codec.ts +++ b/src/codec.ts @@ -8,7 +8,6 @@ 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"; From 64e59328fac5162a6f5b1ba3ee62a0197d6a217d Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 18 Apr 2024 23:38:27 +0200 Subject: [PATCH 3/5] fix lint --- src/codec.ts | 12 ++++-------- src/pairing.ts | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/codec.ts b/src/codec.ts index 7a0b948..8690993 100644 --- a/src/codec.ts +++ b/src/codec.ts @@ -47,8 +47,7 @@ export class NoiseHandshakeEncoder implements IEncoder { public pubsubTopic: string, private hsStepResult: HandshakeStepResult, public ephemeral: boolean = true - ) { - } + ) {} async toWire(message: IMessage): Promise { const protoMessage = await this.toProtoObj(message); @@ -80,8 +79,7 @@ export class NoiseHandshakeDecoder implements IDecoder { * @param contentTopic content topic on which the encoded WakuMessages were sent */ - constructor(public contentTopic: string, public pubsubTopic: string) { - } + constructor(public contentTopic: string, public pubsubTopic: string) {} fromWireToProtoObj(bytes: Uint8Array): Promise { const protoMessage = WakuMessage.decode(bytes); @@ -149,8 +147,7 @@ export class NoiseSecureTransferEncoder implements IEncoder { private hsResult: HandshakeResult, public ephemeral: boolean = true, public metaSetter?: IMetaSetter - ) { - } + ) {} async toWire(message: IMessage): Promise { const protoMessage = await this.toProtoObj(message); @@ -201,8 +198,7 @@ export class NoiseSecureTransferDecoder implements IDecoder * @param hsResult handshake result obtained after the handshake is successful */ - constructor(public contentTopic: string, public pubsubTopic: string, private hsResult: HandshakeResult) { - } + constructor(public contentTopic: string, public pubsubTopic: string, private hsResult: HandshakeResult) {} fromWireToProtoObj(bytes: Uint8Array): Promise { const protoMessage = WakuMessage.decode(bytes); diff --git a/src/pairing.ts b/src/pairing.ts index cd3a2f6..da43025 100644 --- a/src/pairing.ts +++ b/src/pairing.ts @@ -293,7 +293,12 @@ export class WakuPairing { this.eventEmitter.emit("pairingComplete"); - return WakuPairing.getSecureCodec(this.contentTopic, this.pubsubTopic, this.handshakeResult, this.encoderParameters); + return WakuPairing.getSecureCodec( + this.contentTopic, + this.pubsubTopic, + this.handshakeResult, + this.encoderParameters + ); } private async responderHandshake(): Promise<[NoiseSecureTransferEncoder, NoiseSecureTransferDecoder]> { @@ -357,7 +362,12 @@ export class WakuPairing { this.eventEmitter.emit("pairingComplete"); - return WakuPairing.getSecureCodec(this.contentTopic, this.pubsubTopic, this.handshakeResult, this.encoderParameters); + return WakuPairing.getSecureCodec( + this.contentTopic, + this.pubsubTopic, + this.handshakeResult, + this.encoderParameters + ); } /** From 10ce3a6deedb109a1c89f40789a05eab521351bc Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 18 Apr 2024 23:49:15 +0200 Subject: [PATCH 4/5] up tests --- src/pairing.spec.ts | 14 +++++++------- src/waku-noise-pairing.spec.ts | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/pairing.spec.ts b/src/pairing.spec.ts index 8e031fe..d737359 100644 --- a/src/pairing.spec.ts +++ b/src/pairing.spec.ts @@ -11,7 +11,7 @@ import { DH25519 } from "./dh25519"; import { MessageNametagBufferSize } from "./messagenametag"; import { ResponderParameters, WakuPairing } from "./pairing"; -const PUBSUB_TOPIC = "default"; +const PUBSUB_TOPIC = "/waku/2/default-waku/proto"; const EMPTY_PROTOMESSAGE = { timestamp: undefined, @@ -72,14 +72,14 @@ describe("js-noise: pairing object", () => { const aliceStaticKey = dhKey.generateKeyPair(); const recvParameters = new ResponderParameters(); - const bobPairingObj = new WakuPairing(sender, responder, bobStaticKey, recvParameters); + const bobPairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, bobStaticKey, recvParameters); const bobExecP1 = bobPairingObj.execute(); // Confirmation is done by manually confirmAuthCodeFlow(bobPairingObj, true); const initParameters = bobPairingObj.getPairingInfo(); - const alicePairingObj = new WakuPairing(sender, responder, aliceStaticKey, initParameters); + const alicePairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, aliceStaticKey, initParameters); const aliceExecP1 = alicePairingObj.execute(); // Confirmation is done manually @@ -115,8 +115,8 @@ describe("js-noise: pairing object", () => { it("should timeout", async function () { const dhKey = new DH25519(); - const bobPairingObj = new WakuPairing(sender, responder, dhKey.generateKeyPair(), new ResponderParameters()); - const alicePairingObj = new WakuPairing(sender, responder, dhKey.generateKeyPair(), bobPairingObj.getPairingInfo()); + const bobPairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, dhKey.generateKeyPair(), new ResponderParameters()); + const alicePairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, dhKey.generateKeyPair(), bobPairingObj.getPairingInfo()); const bobExecP1 = bobPairingObj.execute(1000); const aliceExecP1 = alicePairingObj.execute(1000); @@ -147,14 +147,14 @@ describe("js-noise: pairing object", () => { }; const recvParameters = new ResponderParameters(); - const bobPairingObj = new WakuPairing(sender, responder, bobStaticKey, recvParameters, undefined, { metaSetter }); + const bobPairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, bobStaticKey, recvParameters, undefined, { metaSetter }); const bobExecP1 = bobPairingObj.execute(); // Confirmation is done by manually confirmAuthCodeFlow(bobPairingObj, true); const initParameters = bobPairingObj.getPairingInfo(); - const alicePairingObj = new WakuPairing(sender, responder, aliceStaticKey, initParameters, undefined, { + const alicePairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, aliceStaticKey, initParameters, undefined, { metaSetter, }); const aliceExecP1 = alicePairingObj.execute(); diff --git a/src/waku-noise-pairing.spec.ts b/src/waku-noise-pairing.spec.ts index 50b44ec..a23f874 100644 --- a/src/waku-noise-pairing.spec.ts +++ b/src/waku-noise-pairing.spec.ts @@ -18,7 +18,7 @@ import { NoiseHandshakePatterns } from "./patterns.js"; import { NoisePublicKey } from "./publickey.js"; import { QR } from "./qr.js"; -const PUBSUB_TOPIC = "default"; +const PUBSUB_TOPIC = "/waku/2/default-waku/proto"; describe("Waku Noise Sessions", () => { const rng = new HMACDRBG(); @@ -114,7 +114,7 @@ describe("Waku Noise Sessions", () => { messageNametag: qrMessageNameTag, }); - let encoder = new NoiseHandshakeEncoder(contentTopic, aliceStep); + let encoder = new NoiseHandshakeEncoder(contentTopic, PUBSUB_TOPIC, aliceStep); // We prepare a Waku message from Alice's payload2 // At this point wakuMsg is sent over the Waku network and is received @@ -122,7 +122,7 @@ describe("Waku Noise Sessions", () => { let wakuMsgBytes = await encoder.toWire({ payload: new Uint8Array() }); // We decode the WakuMessage from the ProtoBuffer - let decoder = new NoiseHandshakeDecoder(contentTopic); + let decoder = new NoiseHandshakeDecoder(contentTopic, PUBSUB_TOPIC); let wakuMsgProto = await decoder.fromWireToProtoObj(wakuMsgBytes!); let v2Msg = await decoder.fromProtoObj(PUBSUB_TOPIC, wakuMsgProto!); @@ -157,14 +157,14 @@ describe("Waku Noise Sessions", () => { bobStep = bobHS.stepHandshake({ transportMessage: sentTransportMessage, messageNametag: bobMessageNametag }); // We prepare a Waku message from Bob's payload2 - encoder = new NoiseHandshakeEncoder(contentTopic, bobStep); + encoder = new NoiseHandshakeEncoder(contentTopic, PUBSUB_TOPIC, bobStep); // At this point wakuMsg is sent over the Waku network and is received // We simulate this by creating the ProtoBuffer from wakuMsg wakuMsgBytes = await encoder.toWire({ payload: new Uint8Array() }); // We decode the WakuMessage from the ProtoBuffer - decoder = new NoiseHandshakeDecoder(contentTopic); + decoder = new NoiseHandshakeDecoder(contentTopic, PUBSUB_TOPIC); wakuMsgProto = await decoder.fromWireToProtoObj(wakuMsgBytes!); v2Msg = await decoder.fromProtoObj(PUBSUB_TOPIC, wakuMsgProto!); @@ -194,14 +194,14 @@ describe("Waku Noise Sessions", () => { aliceStep = aliceHS.stepHandshake({ transportMessage: sentTransportMessage, messageNametag: aliceMessageNametag }); // We prepare a Waku message from Alice's payload2 - encoder = new NoiseHandshakeEncoder(contentTopic, aliceStep); + encoder = new NoiseHandshakeEncoder(contentTopic, PUBSUB_TOPIC, aliceStep); // At this point wakuMsg is sent over the Waku network and is received // We simulate this by creating the ProtoBuffer from wakuMsg wakuMsgBytes = await encoder.toWire({ payload: new Uint8Array() }); // We decode the WakuMessage from the ProtoBuffer - decoder = new NoiseHandshakeDecoder(contentTopic); + decoder = new NoiseHandshakeDecoder(contentTopic, PUBSUB_TOPIC); wakuMsgProto = await decoder.fromWireToProtoObj(wakuMsgBytes!); v2Msg = await decoder.fromProtoObj(PUBSUB_TOPIC, wakuMsgProto!); @@ -224,11 +224,11 @@ describe("Waku Noise Sessions", () => { const aliceHSResult = aliceHS.finalizeHandshake(); const bobHSResult = bobHS.finalizeHandshake(); - const aliceEncoder = new NoiseSecureTransferEncoder(contentTopic, aliceHSResult); - const bobEncoder = new NoiseSecureTransferEncoder(contentTopic, bobHSResult); + const aliceEncoder = new NoiseSecureTransferEncoder(contentTopic, PUBSUB_TOPIC, aliceHSResult); + const bobEncoder = new NoiseSecureTransferEncoder(contentTopic, PUBSUB_TOPIC, bobHSResult); - const aliceDecoder = new NoiseSecureTransferDecoder(contentTopic, aliceHSResult); - const bobDecoder = new NoiseSecureTransferDecoder(contentTopic, bobHSResult); + const aliceDecoder = new NoiseSecureTransferDecoder(contentTopic, PUBSUB_TOPIC, aliceHSResult); + const bobDecoder = new NoiseSecureTransferDecoder(contentTopic, PUBSUB_TOPIC, bobHSResult); // We test read/write of random messages exchanged between Alice and Bob // Note that we exchange more than the number of messages contained in the nametag buffer to test if they are filled correctly as the communication proceeds From 64485c8c141dde44db44ed56c11b1c75efdbb8a6 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 18 Apr 2024 23:51:05 +0200 Subject: [PATCH 5/5] lint --- src/pairing.spec.ts | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/pairing.spec.ts b/src/pairing.spec.ts index d737359..69b2d47 100644 --- a/src/pairing.spec.ts +++ b/src/pairing.spec.ts @@ -115,8 +115,20 @@ describe("js-noise: pairing object", () => { it("should timeout", async function () { const dhKey = new DH25519(); - const bobPairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, dhKey.generateKeyPair(), new ResponderParameters()); - const alicePairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, dhKey.generateKeyPair(), bobPairingObj.getPairingInfo()); + const bobPairingObj = new WakuPairing( + PUBSUB_TOPIC, + sender, + responder, + dhKey.generateKeyPair(), + new ResponderParameters() + ); + const alicePairingObj = new WakuPairing( + PUBSUB_TOPIC, + sender, + responder, + dhKey.generateKeyPair(), + bobPairingObj.getPairingInfo() + ); const bobExecP1 = bobPairingObj.execute(1000); const aliceExecP1 = alicePairingObj.execute(1000); @@ -147,16 +159,26 @@ describe("js-noise: pairing object", () => { }; const recvParameters = new ResponderParameters(); - const bobPairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, bobStaticKey, recvParameters, undefined, { metaSetter }); + const bobPairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, bobStaticKey, recvParameters, undefined, { + metaSetter, + }); const bobExecP1 = bobPairingObj.execute(); // Confirmation is done by manually confirmAuthCodeFlow(bobPairingObj, true); const initParameters = bobPairingObj.getPairingInfo(); - const alicePairingObj = new WakuPairing(PUBSUB_TOPIC, sender, responder, aliceStaticKey, initParameters, undefined, { - metaSetter, - }); + const alicePairingObj = new WakuPairing( + PUBSUB_TOPIC, + sender, + responder, + aliceStaticKey, + initParameters, + undefined, + { + metaSetter, + } + ); const aliceExecP1 = alicePairingObj.execute(); // Confirmation is done manually