2023-04-04 08:58:45 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
chronicles,
|
|
|
|
chronos,
|
2023-05-02 14:20:38 +00:00
|
|
|
metrics,
|
2023-04-04 08:58:45 +00:00
|
|
|
stew/byteutils,
|
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
|
|
|
libp2p/protocols/pubsub/errors,
|
|
|
|
nimcrypto/sha2,
|
|
|
|
secp256k1
|
|
|
|
|
|
|
|
import
|
2023-05-02 14:20:38 +00:00
|
|
|
../../waku/v2/waku_relay/protocol,
|
|
|
|
../../waku/v2/waku_core
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
return ctx.finish()
|
|
|
|
|
|
|
|
proc addSignedTopicValidator*(w: WakuRelay, topic: PubsubTopic, publicTopicKey: SkPublicKey) =
|
|
|
|
debug "adding validator to signed topic", topic=topic, publicTopicKey=publicTopicKey
|
|
|
|
|
|
|
|
proc validator(topic: string, message: messages.Message): Future[errors.ValidationResult] {.async.} =
|
|
|
|
let msg = WakuMessage.decode(message.data)
|
2023-05-02 14:20:38 +00:00
|
|
|
var outcome = errors.ValidationResult.Reject
|
|
|
|
|
2023-04-04 08:58:45 +00:00
|
|
|
if msg.isOk():
|
|
|
|
let msgHash = SkMessage(topic.msgHash(msg.get))
|
|
|
|
let recoveredSignature = SkSignature.fromRaw(msg.get.meta)
|
2023-05-02 14:20:38 +00:00
|
|
|
if recoveredSignature.isOk():
|
|
|
|
if recoveredSignature.get.verify(msgHash, publicTopicKey):
|
|
|
|
outcome = errors.ValidationResult.Accept
|
|
|
|
|
|
|
|
waku_msg_validator_signed_outcome.inc(labelValues = [$outcome])
|
|
|
|
return outcome
|
2023-04-04 08:58:45 +00:00
|
|
|
|
|
|
|
w.addValidator(topic, validator)
|