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
|
|
|
|
|
|
|
|
|
|
|
|
import
|
|
|
|
libp2p/protobuf/minprotobuf
|
|
|
|
|
|
|
|
type
|
|
|
|
ContentTopic* = uint32
|
|
|
|
|
|
|
|
WakuMessage* = object
|
|
|
|
payload*: seq[byte]
|
|
|
|
contentTopic*: ContentTopic
|
|
|
|
version*: uint32
|
2021-04-07 18:08:46 +00:00
|
|
|
# sender generated timestamp
|
|
|
|
timestamp*: float64
|
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
|
2021-03-16 18:18:40 +00:00
|
|
|
proof*: seq[byte]
|
2021-04-07 18:08:46 +00:00
|
|
|
|
2021-01-06 09:35:05 +00:00
|
|
|
|
|
|
|
# Encoding and decoding -------------------------------------------------------
|
|
|
|
proc init*(T: type WakuMessage, buffer: seq[byte]): ProtoResult[T] =
|
|
|
|
var msg = WakuMessage()
|
|
|
|
let pb = initProtoBuffer(buffer)
|
|
|
|
|
|
|
|
discard ? pb.getField(1, msg.payload)
|
|
|
|
discard ? pb.getField(2, msg.contentTopic)
|
|
|
|
discard ? pb.getField(3, msg.version)
|
2021-04-07 18:08:46 +00:00
|
|
|
discard ? pb.getField(4, msg.timestamp)
|
|
|
|
# XXX Experimental, this is part of https://rfc.vac.dev/spec/17/ spec and not yet part of WakuMessage spec
|
|
|
|
discard ? pb.getField(21, msg.proof)
|
2021-01-06 09:35:05 +00:00
|
|
|
|
|
|
|
ok(msg)
|
|
|
|
|
|
|
|
proc encode*(message: WakuMessage): ProtoBuffer =
|
|
|
|
result = initProtoBuffer()
|
|
|
|
|
|
|
|
result.write(1, message.payload)
|
|
|
|
result.write(2, message.contentTopic)
|
|
|
|
result.write(3, message.version)
|
2021-04-07 18:08:46 +00:00
|
|
|
result.write(4, message.timestamp)
|
|
|
|
result.write(21, message.proof)
|