2021-01-06 09:35:05 +00:00
|
|
|
## 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
|
|
|
|
|
2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-07-15 18:25:52 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
|
2021-01-06 09:35:05 +00:00
|
|
|
import
|
2022-02-17 15:00:15 +00:00
|
|
|
libp2p/protobuf/minprotobuf,
|
2022-11-07 15:24:16 +00:00
|
|
|
libp2p/varint
|
|
|
|
import
|
2022-06-16 14:04:47 +00:00
|
|
|
../utils/protobuf,
|
2022-03-16 20:51:18 +00:00
|
|
|
../utils/time,
|
2022-11-07 15:24:16 +00:00
|
|
|
./waku_rln_relay/waku_rln_relay_types
|
|
|
|
|
2021-01-06 09:35:05 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
const MaxWakuMessageSize* = 1024 * 1024 # In bytes. Corresponds to PubSub default
|
2022-01-06 12:42:37 +00:00
|
|
|
|
2021-01-06 09:35:05 +00:00
|
|
|
type
|
2022-11-07 15:24:16 +00:00
|
|
|
PubsubTopic* = string
|
2021-04-08 09:55:19 +00:00
|
|
|
ContentTopic* = string
|
2021-01-06 09:35:05 +00:00
|
|
|
|
2022-11-09 08:55:47 +00:00
|
|
|
const
|
|
|
|
DefaultPubsubTopic*: PubsubTopic = "/waku/2/default-waku/proto"
|
|
|
|
DefaultContentTopic*: ContentTopic = "/waku/2/default-content/proto"
|
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
|
|
|
|
type WakuMessage* = object
|
2021-01-06 09:35:05 +00:00
|
|
|
payload*: seq[byte]
|
|
|
|
contentTopic*: ContentTopic
|
|
|
|
version*: uint32
|
2021-04-07 18:08:46 +00:00
|
|
|
# sender generated timestamp
|
2022-02-17 15:00:15 +00:00
|
|
|
timestamp*: Timestamp
|
2021-03-16 18:18:40 +00:00
|
|
|
# the proof field indicates that the message is not a spam
|
|
|
|
# this field will be used in the rln-relay protocol
|
2021-04-07 18:08:46 +00:00
|
|
|
# XXX Experimental, this is part of https://rfc.vac.dev/spec/17/ spec and not yet part of WakuMessage spec
|
2022-03-16 20:51:18 +00:00
|
|
|
proof*: RateLimitProof
|
2022-09-13 10:37:06 +00:00
|
|
|
# The ephemeral field indicates if the message should
|
|
|
|
# be stored. bools and uints are
|
|
|
|
# equivalent in serialization of the protobuf
|
|
|
|
ephemeral*: bool
|
2021-01-06 09:35:05 +00:00
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
|
|
|
|
## 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))
|
|
|
|
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] =
|
2022-09-13 10:37:06 +00:00
|
|
|
var msg = WakuMessage(ephemeral: false)
|
2021-01-06 09:35:05 +00:00
|
|
|
let pb = initProtoBuffer(buffer)
|
|
|
|
|
2022-11-07 15:24:16 +00:00
|
|
|
discard ?pb.getField(1, msg.payload)
|
|
|
|
discard ?pb.getField(2, msg.contentTopic)
|
|
|
|
discard ?pb.getField(3, msg.version)
|
2021-10-20 00:37:29 +00:00
|
|
|
|
2022-02-17 15:00:15 +00:00
|
|
|
var timestamp: zint64
|
2022-11-07 15:24:16 +00:00
|
|
|
discard ?pb.getField(10, timestamp)
|
2022-02-17 15:00:15 +00:00
|
|
|
msg.timestamp = Timestamp(timestamp)
|
|
|
|
|
2022-03-16 20:51:18 +00:00
|
|
|
# XXX Experimental, this is part of https://rfc.vac.dev/spec/17/ spec
|
|
|
|
var proofBytes: seq[byte]
|
2022-11-07 15:24:16 +00:00
|
|
|
discard ?pb.getField(21, proofBytes)
|
|
|
|
msg.proof = ?RateLimitProof.init(proofBytes)
|
2022-09-13 10:37:06 +00:00
|
|
|
|
|
|
|
var ephemeral: uint
|
2022-11-07 15:24:16 +00:00
|
|
|
if ?pb.getField(31, ephemeral):
|
2022-09-13 10:37:06 +00:00
|
|
|
msg.ephemeral = bool(ephemeral)
|
|
|
|
|
2021-01-06 09:35:05 +00:00
|
|
|
ok(msg)
|