diff --git a/.github/dependabot.yml b/.github/dependabot.yml
index 0bc3b42..d401a77 100644
--- a/.github/dependabot.yml
+++ b/.github/dependabot.yml
@@ -5,7 +5,7 @@ updates:
schedule:
interval: daily
time: "10:00"
- open-pull-requests-limit: 10
+ open-pull-requests-limit: 20
commit-message:
prefix: "deps"
prefix-development: "deps(dev)"
diff --git a/README.md b/README.md
index bd69a3b..31da734 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,5 @@
+# @libp2p/pubsub-peer-discovery
+
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-pubsub-peer-discovery.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-pubsub-peer-discovery)
@@ -7,6 +9,21 @@
# About
+
+
When the discovery module is started by libp2p it subscribes to the discovery pubsub topic(s)
It will immediately broadcast your peer data via pubsub and repeat the broadcast on the configured `interval`
@@ -104,7 +121,7 @@ $ npm i @libp2p/pubsub-peer-discovery
## Browser `
@@ -118,8 +135,8 @@ Loading this module through a script tag will make it's exports available as `Li
Licensed under either of
-- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / )
-- MIT ([LICENSE-MIT](LICENSE-MIT) / )
+- Apache 2.0, ([LICENSE-APACHE](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/LICENSE-APACHE) / )
+- MIT ([LICENSE-MIT](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/LICENSE-MIT) / )
# Contribution
diff --git a/package.json b/package.json
index f0f2ebd..9da1811 100644
--- a/package.json
+++ b/package.json
@@ -145,24 +145,24 @@
"docs": "aegir docs"
},
"dependencies": {
- "@libp2p/interface": "^1.0.1",
- "@libp2p/interface-internal": "^1.0.1",
- "@libp2p/peer-id": "^4.0.1",
+ "@libp2p/crypto": "^5.0.0",
+ "@libp2p/interface": "^2.0.0",
+ "@libp2p/interface-internal": "^2.0.0",
+ "@libp2p/peer-id": "^5.0.0",
"@multiformats/multiaddr": "^12.0.0",
"protons-runtime": "^5.0.0",
"uint8arraylist": "^2.4.3",
"uint8arrays": "^5.0.2"
},
"devDependencies": {
- "@libp2p/interface-compliance-tests": "^5.0.5",
- "@libp2p/logger": "^4.0.1",
- "@libp2p/peer-id-factory": "^4.0.0",
- "@types/sinon": "^17.0.2",
- "aegir": "^42.2.5",
+ "@libp2p/interface-compliance-tests": "^6.0.0",
+ "@libp2p/logger": "^5.0.0",
+ "@types/sinon": "^17.0.3",
+ "aegir": "^44.1.1",
"p-defer": "^4.0.0",
"p-wait-for": "^5.0.2",
"protons": "^7.3.1",
- "sinon": "^17.0.1",
+ "sinon": "^18.0.1",
"sinon-ts": "^2.0.0"
}
}
diff --git a/src/index.ts b/src/index.ts
index ce24cb1..ca4bb45 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -91,8 +91,9 @@
* The default pubsub topic the module subscribes to is `_peer-discovery._p2p._pubsub`, which is also set on `PubsubPeerDiscovery.TOPIC`.
*/
+import { publicKeyFromProtobuf, publicKeyToProtobuf } from '@libp2p/crypto/keys'
import { TypedEventEmitter, peerDiscoverySymbol } from '@libp2p/interface'
-import { peerIdFromKeys } from '@libp2p/peer-id'
+import { peerIdFromPublicKey } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { Peer as PBPeer } from './peer.js'
import type { PeerDiscovery, PeerDiscoveryEvents, PeerId, PeerInfo, Message, PubSub, Startable, ComponentLogger, Logger } from '@libp2p/interface'
@@ -239,7 +240,7 @@ export class PubSubPeerDiscovery extends TypedEventEmitter
}
const peer = {
- publicKey: peerId.publicKey,
+ publicKey: publicKeyToProtobuf(peerId.publicKey),
addrs: this.components.addressManager.getAddresses().map(ma => ma.bytes)
}
@@ -275,9 +276,11 @@ export class PubSubPeerDiscovery extends TypedEventEmitter
return
}
- const peer = PBPeer.decode(message.data)
+ try {
+ const peer = PBPeer.decode(message.data)
+ const publicKey = publicKeyFromProtobuf(peer.publicKey)
+ const peerId = peerIdFromPublicKey(publicKey)
- void peerIdFromKeys(peer.publicKey).then(peerId => {
// Ignore if we received our own response
if (peerId.equals(this.components.peerId)) {
return
@@ -291,9 +294,9 @@ export class PubSubPeerDiscovery extends TypedEventEmitter
multiaddrs: peer.addrs.map(b => multiaddr(b))
}
})
- }).catch(err => {
- this.log.error(err)
- })
+ } catch (err) {
+ this.log.error('error handling incoming message', err)
+ }
}
}
diff --git a/src/peer.ts b/src/peer.ts
index 7ec7303..3d4dcaa 100644
--- a/src/peer.ts
+++ b/src/peer.ts
@@ -4,7 +4,7 @@
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
/* eslint-disable @typescript-eslint/no-empty-interface */
-import { type Codec, decodeMessage, encodeMessage, message } from 'protons-runtime'
+import { type Codec, decodeMessage, type DecodeOptions, encodeMessage, MaxLengthError, message } from 'protons-runtime'
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
import type { Uint8ArrayList } from 'uint8arraylist'
@@ -38,7 +38,7 @@ export namespace Peer {
if (opts.lengthDelimited !== false) {
w.ldelim()
}
- }, (reader, length) => {
+ }, (reader, length, opts = {}) => {
const obj: any = {
publicKey: uint8ArrayAlloc(0),
addrs: []
@@ -55,6 +55,10 @@ export namespace Peer {
break
}
case 2: {
+ if (opts.limits?.addrs != null && obj.addrs.length === opts.limits.addrs) {
+ throw new MaxLengthError('Decode error - map field "addrs" had too many elements')
+ }
+
obj.addrs.push(reader.bytes())
break
}
@@ -76,7 +80,7 @@ export namespace Peer {
return encodeMessage(obj, Peer.codec())
}
- export const decode = (buf: Uint8Array | Uint8ArrayList): Peer => {
- return decodeMessage(buf, Peer.codec())
+ export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions): Peer => {
+ return decodeMessage(buf, Peer.codec(), opts)
}
}
diff --git a/test/compliance.spec.ts b/test/compliance.spec.ts
index 6717a40..af7a261 100644
--- a/test/compliance.spec.ts
+++ b/test/compliance.spec.ts
@@ -1,9 +1,9 @@
/* eslint-env mocha */
-import { CustomEvent } from '@libp2p/interface'
+import { generateKeyPair, publicKeyToProtobuf } from '@libp2p/crypto/keys'
import tests from '@libp2p/interface-compliance-tests/peer-discovery'
import { defaultLogger } from '@libp2p/logger'
-import { createEd25519PeerId } from '@libp2p/peer-id-factory'
+import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { stubInterface } from 'sinon-ts'
import { pubsubPeerDiscovery, TOPIC } from '../src/index.js'
@@ -16,9 +16,11 @@ describe('compliance tests', () => {
tests({
async setup () {
- const peerId = await createEd25519PeerId()
- const subscriber = await createEd25519PeerId()
- await new Promise(resolve => setTimeout(resolve, 10))
+ const privateKey = await generateKeyPair('Ed25519')
+ const peerId = peerIdFromPrivateKey(privateKey)
+
+ const subscriberPrivateKey = await generateKeyPair('Ed25519')
+ const subscriber = peerIdFromPrivateKey(subscriberPrivateKey)
const addressManager = stubInterface()
addressManager.getAddresses.returns([
@@ -40,7 +42,7 @@ describe('compliance tests', () => {
intervalId = setInterval(() => {
const peer = PBPeer.encode({
- publicKey: subscriber.publicKey,
+ publicKey: publicKeyToProtobuf(subscriber.publicKey),
addrs: [
multiaddr('/ip4/166.10.1.2/tcp/80').bytes
]
diff --git a/test/index.spec.ts b/test/index.spec.ts
index d0dfee7..3d5d375 100644
--- a/test/index.spec.ts
+++ b/test/index.spec.ts
@@ -1,9 +1,9 @@
/* eslint-env mocha */
-import { CustomEvent, start, stop } from '@libp2p/interface'
+import { generateKeyPair, publicKeyFromProtobuf, publicKeyToProtobuf } from '@libp2p/crypto/keys'
+import { start, stop } from '@libp2p/interface'
import { defaultLogger } from '@libp2p/logger'
-import { peerIdFromKeys } from '@libp2p/peer-id'
-import { createEd25519PeerId } from '@libp2p/peer-id-factory'
+import { peerIdFromPrivateKey, peerIdFromPublicKey } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import defer from 'p-defer'
@@ -23,8 +23,11 @@ describe('PubSub Peer Discovery', () => {
let components: PubSubPeerDiscoveryComponents
beforeEach(async () => {
- const peerId = await createEd25519PeerId()
- const subscriber = await createEd25519PeerId()
+ const privateKey = await generateKeyPair('Ed25519')
+ const peerId = peerIdFromPrivateKey(privateKey)
+
+ const subscriberPrivateKey = await generateKeyPair('Ed25519')
+ const subscriber = peerIdFromPrivateKey(subscriberPrivateKey)
mockPubsub = stubInterface({
getSubscribers: () => {
@@ -72,7 +75,7 @@ describe('PubSub Peer Discovery', () => {
}
const peer = PB.Peer.decode(eventData)
- const peerId = await peerIdFromKeys(peer.publicKey)
+ const peerId = peerIdFromPublicKey(publicKeyFromProtobuf(peer.publicKey))
expect(peerId.equals(components.peerId)).to.equal(true)
expect(peer.addrs).to.have.length(1)
peer.addrs.forEach((addr) => {
@@ -97,7 +100,8 @@ describe('PubSub Peer Discovery', () => {
discovery = pubsubPeerDiscovery()(components)
await start(discovery)
- const peerId = await createEd25519PeerId()
+ const privateKey = await generateKeyPair('Ed25519')
+ const peerId = peerIdFromPrivateKey(privateKey)
const expectedPeerData: PeerInfo = {
id: peerId,
multiaddrs: [
@@ -106,7 +110,7 @@ describe('PubSub Peer Discovery', () => {
]
}
const peer = {
- publicKey: peerId.publicKey,
+ publicKey: publicKeyToProtobuf(peerId.publicKey),
addrs: expectedPeerData.multiaddrs.map(ma => multiaddr(ma).bytes)
}