feat(waku message): Prepare message based on `noise` option and use correct content topic

This commit is contained in:
Emil Ivanichkov 2024-05-10 12:20:45 +02:00 committed by Emil Ivanichkov
parent 935e278675
commit a47f562958
3 changed files with 35 additions and 6 deletions

View File

@ -0,0 +1,7 @@
import
std/times,
waku/waku_core
proc now*(): Timestamp =
getNanosecondTime(getTime().toUnixFloat())

View File

@ -12,7 +12,20 @@ import
waku/waku_core,
waku/utils/noise,
waku/waku_noise/noise_types,
waku/waku_noise/noise_handshake_processing
waku/waku_noise/noise_handshake_processing,
# Local modules
./utils
proc prepareMessage*(message: string, contentTopic: string): Result[WakuMessage, cstring] =
let
payload = toBytes(message)
msg = WakuMessage(payload: toBytes(message),
contentTopic: contentTopic,
ephemeral: true,
timestamp: now())
ok(msg)
proc prepareMessageWithHandshake*(message: string,
contentTopic: string,

View File

@ -11,7 +11,7 @@ import
metrics,
results,
serialization, json_serialization,
stew/io2,
stew/[io2, byteutils],
# Nimble packages - Waku
waku/node/waku_node,
@ -19,6 +19,7 @@ import
waku/waku_noise/noise_types,
# Local modules
../libs/waku_utils/utils,
../libs/waku_utils/waku_node,
../libs/waku_utils/waku_messages,
./config,
@ -96,9 +97,17 @@ proc loadHandshakeData*(handshakeDataFile: OutFile
proc wakuSendMessage*(wakuHost: ref WakuHost,
message: string,
contentTopic: string): Future[Result[void, string]] {.async.} =
let wakuMessage = prepareMessageWithHandshake(message, contentTopic,
wakuHost.wakuHandshake)
contentTopic: string,
noise: bool = false): Future[Result[void, string]] {.async.} =
# Use Content topic from wakuHost if sending noise message
let ct = if noise: wakuHost.contentTopic else: contentTopic
let wakuMessage =
if noise:
prepareMessageWithHandshake(message, ct,
wakuHost.wakuHandshake)
else:
prepareMessage(message, ct)
if wakuMessage.isErr:
error "Failed to prepare message", error = wakuMessage.error
return err("Failed to prepare message. Reason: " & $wakuMessage.error)
@ -108,7 +117,7 @@ proc wakuSendMessage*(wakuHost: ref WakuHost,
if res.isOk:
notice "Published message", message = message, pubSubTopic = wakuHost.pubSubTopic,
contentTopic = contentTopic
contentTopic = ct
return ok()
else:
error "Failed to publish message", error = res.error