@@ -213,9 +213,10 @@ type RouterReady func(rt PubSubRouter, topic string) (bool, error)
213213type ProvideKey func () (crypto.PrivKey , peer.ID )
214214
215215type PublishOptions struct {
216- ready RouterReady
217- customKey ProvideKey
218- local bool
216+ ready RouterReady
217+ customKey ProvideKey
218+ local bool
219+ validatorData any
219220}
220221
221222type PubOpt func (pub * PublishOptions ) error
@@ -308,7 +309,7 @@ func (t *Topic) Publish(ctx context.Context, data []byte, opts ...PubOpt) error
308309 }
309310 }
310311
311- return t .p .val .PushLocal (& Message {m , "" , t .p .host .ID (), nil , pub .local })
312+ return t .p .val .PushLocal (& Message {m , "" , t .p .host .ID (), pub . validatorData , pub .local })
312313}
313314
314315// WithReadiness returns a publishing option for only publishing when the router is ready.
@@ -332,6 +333,15 @@ func WithLocalPublication(local bool) PubOpt {
332333 }
333334}
334335
336+ // WithValidatorData returns a publishing option to set custom validator data for the message.
337+ // This allows users to avoid deserialization of the message data when validating the message locally.
338+ func WithValidatorData (data any ) PubOpt {
339+ return func (pub * PublishOptions ) error {
340+ pub .validatorData = data
341+ return nil
342+ }
343+ }
344+
335345// WithSecretKeyAndPeerId returns a publishing option for providing a custom private key and its corresponding peer ID
336346// This option is useful when we want to send messages from "virtual", never-connectable peers in the network
337347func WithSecretKeyAndPeerId (key crypto.PrivKey , pid peer.ID ) PubOpt {
0 commit comments