Skip to content

Commit 4feadeb

Browse files
jacobheunvasco-santos
authored andcommitted
feat: add support for signing (#78)
BREAKING CHANGE: publish now takes a callback as it needs to sign messages
1 parent 29d8fd6 commit 4feadeb

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"debug": "^4.1.1",
6262
"length-prefixed-stream": "^2.0.0",
6363
"libp2p-crypto": "~0.16.1",
64-
"libp2p-pubsub": "~0.0.4",
64+
"libp2p-pubsub": "~0.1.0",
6565
"protons": "^1.0.1",
6666
"pull-length-prefixed": "^1.3.2",
6767
"pull-pushable": "^2.2.0",

src/index.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const config = require('./config')
1111
const multicodec = config.multicodec
1212
const ensureArray = utils.ensureArray
1313
const setImmediate = require('async/setImmediate')
14+
const asyncMap = require('async/map')
15+
const noop = () => {}
1416

1517
/**
1618
* FloodSub (aka dumbsub is an implementation of pubsub focused on
@@ -158,11 +160,13 @@ class FloodSub extends BaseProtocol {
158160
* @override
159161
* @param {Array<string>|string} topics
160162
* @param {Array<any>|any} messages
163+
* @param {function(Error)} callback
161164
* @returns {undefined}
162165
*
163166
*/
164-
publish (topics, messages) {
167+
publish (topics, messages, callback) {
165168
assert(this.started, 'FloodSub is not started')
169+
callback = callback || noop
166170

167171
this.log('publish', topics, messages)
168172

@@ -171,25 +175,29 @@ class FloodSub extends BaseProtocol {
171175

172176
const from = this.libp2p.peerInfo.id.toB58String()
173177

174-
const buildMessage = (msg) => {
178+
const buildMessage = (msg, cb) => {
175179
const seqno = utils.randomSeqno()
176180
this.seenCache.put(utils.msgId(from, seqno))
177181

178-
return {
182+
this._buildMessage({
179183
from: from,
180184
data: msg,
181185
seqno: seqno,
182186
topicIDs: topics
183-
}
187+
}, cb)
184188
}
185189

186-
const msgObjects = messages.map(buildMessage)
190+
asyncMap(messages, buildMessage, (err, msgObjects) => {
191+
if (err) return callback(err)
192+
193+
// Emit to self if I'm interested
194+
this._emitMessages(topics, msgObjects)
187195

188-
// Emit to self if I'm interested
189-
this._emitMessages(topics, msgObjects)
196+
// send to all the other peers
197+
this._forwardMessages(topics, msgObjects)
190198

191-
// send to all the other peers
192-
this._forwardMessages(topics, msgObjects)
199+
callback(null)
200+
})
193201
}
194202

195203
/**

0 commit comments

Comments
 (0)