feat!: determine pubsubTopic based on contentTopic

feat!: determine pubsubTopic based on contentTopic
This commit is contained in:
Sasha 2024-04-16 02:09:52 +02:00 committed by GitHub
commit 814512cbcc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 24 additions and 18 deletions

1
package-lock.json generated
View File

@ -17,6 +17,7 @@
"@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",

View File

@ -123,6 +123,7 @@
"@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",
@ -130,4 +131,4 @@
"uint8arraylist": "^2.3.2",
"uint8arrays": "^4.0.2"
}
}
}

View File

@ -1,6 +1,5 @@
import { DecodedMessage } from "@waku/core/lib/message/version_0";
import {
DefaultPubsubTopic,
type IDecodedMessage,
type IDecoder,
type IEncoder,
@ -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";
@ -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);
@ -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);
@ -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);
@ -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);
@ -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);

View File

@ -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,

View File

@ -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";
}
/**

View File

@ -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}