Skip to content

Commit

Permalink
Unslab publicKey and topic in PeerInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote committed Jul 29, 2024
1 parent 04b2004 commit 942baf5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { EventEmitter } = require('events')
const DHT = require('hyperdht')
const spq = require('shuffled-priority-queue')
const b4a = require('b4a')
const unslab = require('unslab')

const PeerInfo = require('./lib/peer-info')
const RetryTimer = require('./lib/retry-timer')
Expand Down Expand Up @@ -355,7 +356,7 @@ module.exports = class Hyperswarm extends EventEmitter {
}

peerInfo = new PeerInfo({
publicKey,
publicKey: unslab(publicKey),
relayAddresses
})

Expand Down Expand Up @@ -423,6 +424,8 @@ module.exports = class Hyperswarm extends EventEmitter {
// TODO: When you rejoin, it should reannounce + bump lookup priority
join (topic, opts = {}) {
if (!topic) throw new Error(ERR_MISSING_TOPIC)
topic = unslab(topic)

const topicString = b4a.toString(topic, 'hex')

let discovery = this._discovery.get(topicString)
Expand Down
43 changes: 43 additions & 0 deletions test/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,49 @@ test('peerDiscovery has unslabbed closestNodes', async (t) => {
t.is(hasUnslabbeds, false, 'sanity check: all are unslabbed')
})

test('topic and peer get unslabbed in PeerInfo', async (t) => {
const { bootstrap } = await createTestnet(3, t.teardown)

const swarm1 = new Hyperswarm({ bootstrap })
const swarm2 = new Hyperswarm({ bootstrap })

t.plan(3)

t.teardown(async () => {
await swarm1.destroy()
await swarm2.destroy()
})

swarm2.on('connection', (conn) => {
t.is(
[...swarm2.peers.values()][0].publicKey.buffer.byteLength,
32,
'unslabbed publicKey in peerInfo'
)
t.is([...swarm2.peers.values()][0].topics[0].buffer.byteLength,
32,
'unslabbed topic in peerInfo'
)

conn.on('error', noop)
conn.end()
})
swarm1.on('connection', (conn) => {
t.is(
[...swarm1.peers.values()][0].publicKey.buffer.byteLength,
32,
'unslabbed publicKey in peerInfo'
)

conn.on('error', noop)
conn.end()
})

const topic = Buffer.alloc(32).fill('hello world')
await swarm1.join(topic, { server: true, client: false }).flushed()
swarm2.join(topic, { client: true, server: false })
})

function noop () {}

function eventFlush () {
Expand Down

0 comments on commit 942baf5

Please sign in to comment.