Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
feat: add support for signing (#78)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: publish now takes a callback as it needs to sign messages
  • Loading branch information
jacobheun authored and vasco-santos committed May 7, 2019
1 parent 29d8fd6 commit 4feadeb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"debug": "^4.1.1",
"length-prefixed-stream": "^2.0.0",
"libp2p-crypto": "~0.16.1",
"libp2p-pubsub": "~0.0.4",
"libp2p-pubsub": "~0.1.0",
"protons": "^1.0.1",
"pull-length-prefixed": "^1.3.2",
"pull-pushable": "^2.2.0",
Expand Down
26 changes: 17 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const config = require('./config')
const multicodec = config.multicodec
const ensureArray = utils.ensureArray
const setImmediate = require('async/setImmediate')
const asyncMap = require('async/map')
const noop = () => {}

/**
* FloodSub (aka dumbsub is an implementation of pubsub focused on
Expand Down Expand Up @@ -158,11 +160,13 @@ class FloodSub extends BaseProtocol {
* @override
* @param {Array<string>|string} topics
* @param {Array<any>|any} messages
* @param {function(Error)} callback
* @returns {undefined}
*
*/
publish (topics, messages) {
publish (topics, messages, callback) {
assert(this.started, 'FloodSub is not started')
callback = callback || noop

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

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

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

const buildMessage = (msg) => {
const buildMessage = (msg, cb) => {
const seqno = utils.randomSeqno()
this.seenCache.put(utils.msgId(from, seqno))

return {
this._buildMessage({
from: from,
data: msg,
seqno: seqno,
topicIDs: topics
}
}, cb)
}

const msgObjects = messages.map(buildMessage)
asyncMap(messages, buildMessage, (err, msgObjects) => {
if (err) return callback(err)

// Emit to self if I'm interested
this._emitMessages(topics, msgObjects)

// Emit to self if I'm interested
this._emitMessages(topics, msgObjects)
// send to all the other peers
this._forwardMessages(topics, msgObjects)

// send to all the other peers
this._forwardMessages(topics, msgObjects)
callback(null)
})
}

/**
Expand Down

0 comments on commit 4feadeb

Please sign in to comment.