mirror of https://github.com/waku-org/nwaku.git
90 lines
2.4 KiB
Nim
90 lines
2.4 KiB
Nim
## Waku Message module.
|
|
##
|
|
## See https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-message.md
|
|
## for spec.
|
|
##
|
|
## For payload content and encryption, see waku/v2/node/waku_payload.nim
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
{.push raises: [Defect].}
|
|
else:
|
|
{.push raises: [].}
|
|
|
|
|
|
import
|
|
../../common/protobuf,
|
|
../utils/time
|
|
|
|
when defined(rln):
|
|
import
|
|
./waku_rln_relay/waku_rln_relay_types
|
|
|
|
|
|
const MaxWakuMessageSize* = 1024 * 1024 # In bytes. Corresponds to PubSub default
|
|
|
|
type
|
|
PubsubTopic* = string
|
|
ContentTopic* = string
|
|
|
|
const
|
|
DefaultPubsubTopic*: PubsubTopic = PubsubTopic("/waku/2/default-waku/proto")
|
|
DefaultContentTopic*: ContentTopic = ContentTopic("/waku/2/default-content/proto")
|
|
|
|
|
|
type WakuMessage* = object
|
|
payload*: seq[byte]
|
|
contentTopic*: ContentTopic
|
|
version*: uint32
|
|
# sender generated timestamp
|
|
timestamp*: Timestamp
|
|
# the proof field indicates that the message is not a spam
|
|
# this field will be used in the rln-relay protocol
|
|
# XXX Experimental, this is part of https://rfc.vac.dev/spec/17/ spec and not yet part of WakuMessage spec
|
|
when defined(rln):
|
|
proof*: RateLimitProof
|
|
# The ephemeral field indicates if the message should
|
|
# be stored. bools and uints are
|
|
# equivalent in serialization of the protobuf
|
|
ephemeral*: bool
|
|
|
|
|
|
## Encoding and decoding
|
|
|
|
proc encode*(message: WakuMessage): ProtoBuffer =
|
|
var buf = initProtoBuffer()
|
|
|
|
buf.write3(1, message.payload)
|
|
buf.write3(2, message.contentTopic)
|
|
buf.write3(3, message.version)
|
|
buf.write3(10, zint64(message.timestamp))
|
|
when defined(rln):
|
|
buf.write3(21, message.proof.encode())
|
|
buf.write3(31, uint64(message.ephemeral))
|
|
buf.finish3()
|
|
|
|
buf
|
|
|
|
proc decode*(T: type WakuMessage, buffer: seq[byte]): ProtoResult[T] =
|
|
var msg = WakuMessage(ephemeral: false)
|
|
let pb = initProtoBuffer(buffer)
|
|
|
|
discard ?pb.getField(1, msg.payload)
|
|
discard ?pb.getField(2, msg.contentTopic)
|
|
discard ?pb.getField(3, msg.version)
|
|
|
|
var timestamp: zint64
|
|
discard ?pb.getField(10, timestamp)
|
|
msg.timestamp = Timestamp(timestamp)
|
|
|
|
# XXX Experimental, this is part of https://rfc.vac.dev/spec/17/ spec
|
|
when defined(rln):
|
|
var proofBytes: seq[byte]
|
|
discard ?pb.getField(21, proofBytes)
|
|
msg.proof = ?RateLimitProof.init(proofBytes)
|
|
|
|
var ephemeral: uint
|
|
if ?pb.getField(31, ephemeral):
|
|
msg.ephemeral = bool(ephemeral)
|
|
|
|
ok(msg)
|