2023-02-10 08:54:47 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-03-03 09:07:25 +00:00
|
|
|
stew/[byteutils, results],
|
|
|
|
libp2p/crypto/crypto
|
2023-02-10 08:54:47 +00:00
|
|
|
import
|
|
|
|
../../waku/v1/protocol/waku_protocol,
|
2023-04-19 11:29:23 +00:00
|
|
|
../../waku/v2/waku_core
|
2023-02-10 08:54:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
const
|
|
|
|
ContentTopicApplication = "waku"
|
|
|
|
ContentTopicAppVersion = "1"
|
|
|
|
|
|
|
|
|
2023-03-03 09:07:25 +00:00
|
|
|
proc toV1Topic*(contentTopic: ContentTopic): waku_protocol.Topic {.raises: [ValueError]} =
|
2023-02-10 08:54:47 +00:00
|
|
|
## Extracts the 4-byte array v1 topic from a content topic
|
|
|
|
## with format `/waku/1/<v1-topic-bytes-as-hex>/rfc26`
|
|
|
|
|
2023-03-07 10:10:36 +00:00
|
|
|
let ns = NsContentTopic.parse(contentTopic)
|
2023-03-03 09:07:25 +00:00
|
|
|
if ns.isErr():
|
|
|
|
let err = ns.tryError()
|
|
|
|
raise newException(ValueError, $err)
|
|
|
|
|
|
|
|
let name = ns.value.name
|
|
|
|
hexToByteArray(hexStr=name, N=4) # Byte array length
|
2023-02-10 08:54:47 +00:00
|
|
|
|
|
|
|
proc toV2ContentTopic*(v1Topic: waku_protocol.Topic): ContentTopic =
|
|
|
|
## Convert a 4-byte array v1 topic to a namespaced content topic
|
|
|
|
## with format `/waku/1/<v1-topic-bytes-as-hex>/rfc26`
|
|
|
|
##
|
|
|
|
## <v1-topic-bytes-as-hex> should be prefixed with `0x`
|
2023-03-07 10:10:36 +00:00
|
|
|
var namespacedTopic = NsContentTopic()
|
2023-02-10 08:54:47 +00:00
|
|
|
|
|
|
|
namespacedTopic.application = ContentTopicApplication
|
|
|
|
namespacedTopic.version = ContentTopicAppVersion
|
2023-03-07 10:10:36 +00:00
|
|
|
namespacedTopic.name = v1Topic.to0xHex()
|
2023-02-10 08:54:47 +00:00
|
|
|
namespacedTopic.encoding = "rfc26"
|
|
|
|
|
|
|
|
return ContentTopic($namespacedTopic)
|
|
|
|
|
|
|
|
|
|
|
|
proc isBridgeable*(msg: WakuMessage): bool =
|
|
|
|
## Determines if a Waku v2 msg is on a bridgeable content topic
|
2023-03-07 10:10:36 +00:00
|
|
|
let ns = NsContentTopic.parse(msg.contentTopic)
|
2023-03-03 09:07:25 +00:00
|
|
|
if ns.isErr():
|
|
|
|
return false
|
2023-02-10 08:54:47 +00:00
|
|
|
|
2023-03-03 09:07:25 +00:00
|
|
|
return ns.value.application == ContentTopicApplication and ns.value.version == ContentTopicAppVersion
|