@@ -10,6 +10,7 @@ const errcode = require('err-code')
1010
1111const Peer = require ( './peer' )
1212const message = require ( './message' )
13+ const { signMessage } = require ( './message/sign' )
1314const utils = require ( './utils' )
1415
1516const nextTick = require ( 'async/nextTick' )
@@ -22,17 +23,28 @@ class PubsubBaseProtocol extends EventEmitter {
2223 * @param {String } debugName
2324 * @param {String } multicodec
2425 * @param {Object } libp2p libp2p implementation
26+ * @param {Object } options
27+ * @param {boolean } options.signMessages if messages should be signed, defaults to true
2528 * @constructor
2629 */
27- constructor ( debugName , multicodec , libp2p ) {
30+ constructor ( debugName , multicodec , libp2p , options ) {
2831 super ( )
2932
33+ options = {
34+ signMessages : true ,
35+ ...options
36+ }
37+
3038 this . log = debug ( debugName )
3139 this . log . err = debug ( `${ debugName } :error` )
3240 this . multicodec = multicodec
3341 this . libp2p = libp2p
3442 this . started = false
3543
44+ if ( options . signMessages ) {
45+ this . peerId = this . libp2p . peerInfo . id
46+ }
47+
3648 /**
3749 * Map of topics to which peers are subscribed to
3850 *
@@ -225,16 +237,32 @@ class PubsubBaseProtocol extends EventEmitter {
225237 this . _removePeer ( peer )
226238 }
227239
240+ /**
241+ * Normalizes the message and signs it, if signing is enabled
242+ *
243+ * @param {Message } message
244+ * @param {function(Error, Message) } callback
245+ */
246+ _buildMessage ( message , callback ) {
247+ const msg = utils . normalizeOutRpcMessage ( message )
248+ if ( this . peerId ) {
249+ signMessage ( this . peerId , msg , callback )
250+ } else {
251+ nextTick ( callback , null , msg )
252+ }
253+ }
254+
228255 /**
229256 * Overriding the implementation of publish should handle the appropriate algorithms for the publish/subscriber implementation.
230257 * For example, a Floodsub implementation might simply publish each message to each topic for every peer
231258 * @abstract
232259 * @param {Array<string>|string } topics
233260 * @param {Array<any>|any } messages
261+ * @param {function(Error) } callback
234262 * @returns {undefined }
235263 *
236264 */
237- publish ( topics , messages ) {
265+ publish ( topics , messages , callback ) {
238266 throw errcode ( 'publish must be implemented by the subclass' , 'ERR_NOT_IMPLEMENTED' )
239267 }
240268
0 commit comments