feat(waku utils): Add functions for sending messages

This commit is contained in:
Emil Ivanichkov 2024-04-19 14:45:13 +03:00 committed by Emil Ivanichkov
parent a334f2d7f5
commit 5c3b44449d
2 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,25 @@
import
# Nimble packages
chronos,
eth/[p2p/discoveryv5/enr],
httputils,
libp2p/crypto/crypto,
metrics,
results,
stew/io2, stew/byteutils,
# Nimble packages - Waku
waku/waku_core,
waku/utils/noise,
waku/waku_noise/noise_types,
waku/waku_noise/noise_handshake_processing
proc prepareMessageWithHandshake*(message: string,
contentTopic: string,
hs: var HandshakeResult): Result[WakuMessage, cstring] =
let
byteMessage = toBytes(message)
payload = writeMessage(hs, byteMessage, hs.nametagsOutbound)
wakuMessage = encodePayloadV2(payload, contentTopic)
wakuMessage

View File

@ -20,8 +20,9 @@ import
# Local modules
../libs/waku_utils/waku_node,
./rest/rest_serialization,
../libs/waku_utils/waku_messages,
./config,
./rest/rest_serialization,
./filepaths
from confutils import OutFile, `$`
@ -93,6 +94,26 @@ proc loadHandshakeData*(handshakeDataFile: OutFile
ok(handshakeResult.get)
proc wakuSendMessage*(wakuHost: ref WakuHost,
message: string,
contentTopic: string): Future[Result[void, string]] {.async.} =
let wakuMessage = prepareMessageWithHandshake(message, contentTopic,
wakuHost.wakuHandshake)
if wakuMessage.isErr:
error "Failed to prepare message", error = wakuMessage.error
return err("Failed to prepare message. Reason: " & $wakuMessage.error)
let res = await wakuHost.wakuNode.publish(some(wakuHost.pubsubTopic),
wakuMessage.get)
if res.isOk:
notice "Published message", message = message, pubSubTopic = wakuHost.pubSubTopic,
contentTopic = contentTopic
return ok()
else:
error "Failed to publish message", error = res.error
return err("Failed to publish message. Reason: " & res.error)
proc init*(T: type WakuHost,
rng: ref HmacDrbgContext,
config: StatusNodeManagerConfig): Future[WakuHost] {.async.} =