feat(waku utils): Add functions for sending messages
This commit is contained in:
parent
a334f2d7f5
commit
5c3b44449d
|
@ -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
|
|
@ -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.} =
|
||||
|
|
Loading…
Reference in New Issue