Skip to content

Commit

Permalink
feat: add pubsub topic parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
weboko committed Apr 18, 2024
1 parent 69159ec commit 7100f31
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
18 changes: 8 additions & 10 deletions src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array | undefined> {
Expand Down Expand Up @@ -77,12 +77,11 @@ export class NoiseHandshakeEncoder implements IEncoder {
*/
export class NoiseHandshakeDecoder implements IDecoder<NoiseHandshakeMessage> {
/**
* @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<IProtoMessage | undefined> {
Expand Down Expand Up @@ -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<Uint8Array | undefined> {
Expand Down Expand Up @@ -199,12 +198,11 @@ export class NoiseSecureTransferEncoder implements IEncoder {
export class NoiseSecureTransferDecoder implements IDecoder<NoiseSecureMessage> {
/**
* @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<IProtoMessage | undefined> {
Expand Down
20 changes: 12 additions & 8 deletions src/pairing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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(),
});
Expand Down Expand Up @@ -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(),
});
Expand All @@ -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
Expand All @@ -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(),
});
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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];
}
Expand Down

0 comments on commit 7100f31

Please sign in to comment.