2023-04-04 08:58:45 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-05-05 08:12:49 +00:00
|
|
|
std/math,
|
2023-04-04 08:58:45 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
2023-05-02 14:20:38 +00:00
|
|
|
metrics,
|
2023-04-04 08:58:45 +00:00
|
|
|
stew/byteutils,
|
2023-05-05 08:12:49 +00:00
|
|
|
stew/endians2,
|
2023-04-04 08:58:45 +00:00
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
|
|
|
libp2p/protocols/pubsub/errors,
|
|
|
|
nimcrypto/sha2,
|
|
|
|
secp256k1
|
|
|
|
|
2023-05-05 08:12:49 +00:00
|
|
|
const MessageWindowInSec = 5*60 # +- 5 minutes
|
|
|
|
|
2023-04-04 08:58:45 +00:00
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/waku_relay/protocol,
|
|
|
|
../../waku/waku_core
|
2023-05-02 14:20:38 +00:00
|
|
|
|
|
|
|
declarePublicCounter waku_msg_validator_signed_outcome, "number of messages for each validation outcome", ["result"]
|
2023-04-04 08:58:45 +00:00
|
|
|
|
2023-05-04 13:38:52 +00:00
|
|
|
# Application level message hash
|
2023-04-04 08:58:45 +00:00
|
|
|
proc msgHash*(pubSubTopic: string, msg: WakuMessage): array[32, byte] =
|
|
|
|
var ctx: sha256
|
|
|
|
ctx.init()
|
|
|
|
defer: ctx.clear()
|
|
|
|
|
|
|
|
ctx.update(pubsubTopic.toBytes())
|
|
|
|
ctx.update(msg.payload)
|
|
|
|
ctx.update(msg.contentTopic.toBytes())
|
2023-05-05 08:12:49 +00:00
|
|
|
ctx.update(msg.timestamp.uint64.toBytes(Endianness.littleEndian))
|
|
|
|
ctx.update(if msg.ephemeral: @[1.byte] else: @[0.byte])
|
2023-04-04 08:58:45 +00:00
|
|
|
|
|
|
|
return ctx.finish()
|
|
|
|
|
2023-05-05 08:12:49 +00:00
|
|
|
proc withinTimeWindow*(msg: WakuMessage): bool =
|
|
|
|
# Returns true if the message timestamp is:
|
|
|
|
# abs(now - msg.timestamp) < MessageWindowInSec
|
|
|
|
let ts = msg.timestamp
|
|
|
|
let now = getNowInNanosecondTime()
|
|
|
|
let window = getNanosecondTime(MessageWindowInSec)
|
|
|
|
|
|
|
|
if abs(now - ts) < window:
|
|
|
|
return true
|
|
|
|
return false
|
|
|
|
|
2023-04-04 08:58:45 +00:00
|
|
|
proc addSignedTopicValidator*(w: WakuRelay, topic: PubsubTopic, publicTopicKey: SkPublicKey) =
|
|
|
|
debug "adding validator to signed topic", topic=topic, publicTopicKey=publicTopicKey
|
|
|
|
|
2023-09-05 09:05:07 +00:00
|
|
|
proc validator(topic: string, msg: WakuMessage): Future[errors.ValidationResult] {.async.} =
|
2023-05-02 14:20:38 +00:00
|
|
|
var outcome = errors.ValidationResult.Reject
|
|
|
|
|
2023-09-05 09:05:07 +00:00
|
|
|
if msg.timestamp != 0:
|
|
|
|
if msg.withinTimeWindow():
|
|
|
|
let msgHash = SkMessage(topic.msgHash(msg))
|
|
|
|
let recoveredSignature = SkSignature.fromRaw(msg.meta)
|
|
|
|
if recoveredSignature.isOk():
|
|
|
|
if recoveredSignature.get.verify(msgHash, publicTopicKey):
|
|
|
|
outcome = errors.ValidationResult.Accept
|
2023-05-02 14:20:38 +00:00
|
|
|
|
|
|
|
waku_msg_validator_signed_outcome.inc(labelValues = [$outcome])
|
|
|
|
return outcome
|
2023-04-04 08:58:45 +00:00
|
|
|
|
|
|
|
w.addValidator(topic, validator)
|