@@ -6,11 +6,7 @@ import type {
66 ShardId ,
77 ShardInfo
88} from "@waku/interfaces" ;
9- import {
10- decodeRelayShard ,
11- Logger ,
12- pubsubTopicToSingleShardInfo
13- } from "@waku/utils" ;
9+ import { decodeRelayShard , Logger } from "@waku/utils" ;
1410import { Libp2p } from "libp2p" ;
1511
1612const log = new Logger ( "shard-reader" ) ;
@@ -65,7 +61,7 @@ export class ShardReader implements IShardReader {
6561 pubsubTopic : PubsubTopic
6662 ) : Promise < boolean > {
6763 try {
68- const { clusterId, shard } = pubsubTopicToSingleShardInfo ( pubsubTopic ) ;
64+ const { clusterId, shard } = this . parsePubsubTopic ( pubsubTopic ) ;
6965 if ( clusterId !== this . clusterId ) return false ;
7066 return await this . isPeerOnShard ( id , shard ) ;
7167 } catch ( error ) {
@@ -93,6 +89,34 @@ export class ShardReader implements IShardReader {
9389 ) ;
9490 }
9591
92+ private parsePubsubTopic ( pubsubTopic : PubsubTopic ) : {
93+ clusterId : ClusterId ;
94+ shard : ShardId ;
95+ } {
96+ const parts = pubsubTopic . split ( "/" ) ;
97+
98+ if (
99+ parts . length !== 6 ||
100+ parts [ 1 ] !== "waku" ||
101+ parts [ 2 ] !== "2" ||
102+ parts [ 3 ] !== "rs"
103+ ) {
104+ throw new Error ( "Invalid pubsub topic" ) ;
105+ }
106+
107+ const clusterId = parseInt ( parts [ 4 ] , 10 ) ;
108+ const shard = parseInt ( parts [ 5 ] , 10 ) ;
109+
110+ if ( isNaN ( clusterId ) || isNaN ( shard ) ) {
111+ throw new Error ( "Invalid clusterId or shard" ) ;
112+ }
113+
114+ return {
115+ clusterId,
116+ shard
117+ } ;
118+ }
119+
96120 private async getRelayShards ( id : PeerId ) : Promise < ShardInfo | undefined > {
97121 try {
98122 const peer = await this . libp2p . peerStore . get ( id ) ;
0 commit comments