Skip to content

Commit

Permalink
refactor: move isValidTopic to utils/topic
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Nov 29, 2023
1 parent 9c2552c commit 9e6bbff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
16 changes: 1 addition & 15 deletions src/conversations/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
buildUserIntroTopic,
buildUserInviteTopic,
dateToNs,
isValidTopic,
nsToDate,
} from '../utils'
import { PublicKeyBundle } from '../crypto'
Expand All @@ -25,21 +26,6 @@ const messageHasHeaders = (msg: MessageV1): boolean => {
return Boolean(msg.recipientAddress && msg.senderAddress)
}

// validate that a topic only contains ASCII characters
const isValidTopic = (topic: string): boolean => {
// eslint-disable-next-line no-control-regex
const regex = /^[\x00-\x7F]+$/
const index = topic.indexOf('0/')
if (index !== -1) {
const unwrappedTopic = topic.substring(
index + 2,
topic.lastIndexOf('/proto')
)
return regex.test(unwrappedTopic)
}
return false
}

/**
* Conversations allows you to view ongoing 1:1 messaging sessions with another wallet
*/
Expand Down
15 changes: 15 additions & 0 deletions src/utils/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,18 @@ export const buildUserPrivateStoreTopic = (addrPrefixedKey: string): string => {
// e.g. "0x1111111111222222222233333333334444444444/key_bundle"
return buildContentTopic(`privatestore-${addrPrefixedKey}`)
}

// validate that a topic only contains ASCII characters
export const isValidTopic = (topic: string): boolean => {
// eslint-disable-next-line no-control-regex
const regex = /^[\x00-\x7F]+$/
const index = topic.indexOf('0/')
if (index !== -1) {
const unwrappedTopic = topic.substring(
index + 2,
topic.lastIndexOf('/proto')
)
return regex.test(unwrappedTopic)
}
return false
}

0 comments on commit 9e6bbff

Please sign in to comment.