From 4101e023cd2ea2fcecb02bff1cb895d960ef1680 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Mon, 15 Jan 2024 23:01:51 +0200 Subject: [PATCH] connection events has .pubkey field --- lib/connections.js | 26 +++++++++++++++++++++++--- lib/index.js | 1 + test/index.test.js | 5 +++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/connections.js b/lib/connections.js index 59b477f..667ea6b 100644 --- a/lib/connections.js +++ b/lib/connections.js @@ -18,6 +18,7 @@ const IP = require('ip') * | 'disconnecting' * | 'disconnected'; * address: Address; + * pubkey: string | undefined; * details?: any; * }} ConnectionEvent * @typedef {{ @@ -77,6 +78,20 @@ class Connections { return } + /** + * @param {string} addresses + * @returns {string | undefined} + */ + static extractSHSEPubkey(addresses) { + for (const address of addresses.split(';')) { + const [transport, transform] = address.split('~') + const [name, pubkey, extra] = transform.split(':') + if (name === 'shse') { + return pubkey + } + } + } + #assertNotClosed() { if (this.#closed) { throw new Error('This Connections instance is closed, create a new one.') @@ -102,12 +117,13 @@ class Connections { const initiator = weAreClient ? 'we' : 'they' debug('Connected to %s, %s initiated it', address, initiator) + const pubkey = Connections.extractSHSEPubkey(address) this.#rpcs.set(address, rpc) rpc.once('closed', () => { debug('Disconnected from %s', address) this.#rpcs.delete(address) this.#infos.update(address, { state: 'disconnected' }) - this.#notifyEvent({ type: 'disconnected', address }) + this.#notifyEvent({ type: 'disconnected', address, pubkey }) this.#infos.emit() }) @@ -117,6 +133,7 @@ class Connections { this.#notifyEvent({ type: state, address, + pubkey, details: { rpc, weAreClient }, }) this.#infos.emit() @@ -166,8 +183,9 @@ class Connections { case 'disconnected': { debug('Connecting to %s', address) const state = /**@type {Info['state']}*/ ('connecting') + const pubkey = Connections.extractSHSEPubkey(address) this.#infos.update(address, { state }) - this.#notifyEvent({ type: state, address }) + this.#notifyEvent({ type: state, address, pubkey }) this.#infos.emit() const [err, rpc] = await run(this.#peer.connect)(address) @@ -177,6 +195,7 @@ class Connections { this.#notifyEvent({ type: 'connecting-failed', address, + pubkey, details: err, }) this.#infos.emit() @@ -244,8 +263,9 @@ class Connections { debug('Disconnecting from %s', address) const state = /**@type {Info['state']}*/ ('disconnecting') + const pubkey = Connections.extractSHSEPubkey(address) this.#infos.update(address, { state }) - this.#notifyEvent({ type: state, address }) + this.#notifyEvent({ type: state, address, pubkey }) this.#infos.emit() // @ts-ignore await run(rpc.close)(true) diff --git a/lib/index.js b/lib/index.js index c9f7151..5f8c0e0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -181,6 +181,7 @@ function initNet(peer, config) { } exports.name = 'net' +exports.needs = ['shse'] exports.manifest = { start: 'sync', stop: 'sync', diff --git a/test/index.test.js b/test/index.test.js index fca8aff..91cb2e8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -4,8 +4,8 @@ const p = require('node:util').promisify const pull = require('pull-stream') const { createPeer } = require('./util') -const TEST_ADDR = - 'net:localhost:9752~shse:EqTMFv7zm8hpPyAkj789qdJgqtz81AEbcinpAs24RRUC' +const PUBKEY = 'EqTMFv7zm8hpPyAkj789qdJgqtz81AEbcinpAs24RRUC' +const TEST_ADDR = `net:localhost:9752~shse:${PUBKEY}` test('net', async (t) => { await t.test('connect() rejects given unreachable address', async () => { @@ -69,6 +69,7 @@ test('net', async (t) => { if (i === 1) { assert.equal(ev.type, 'connecting', 'event.type ok') assert.equal(ev.address, TEST_ADDR, 'event.address ok') + assert.equal(ev.pubkey, PUBKEY, 'event.pubkey ok') } else if (i === 2) { assert.equal(ev.type, 'connecting-failed', 'event.type ok') assert.equal(ev.address, TEST_ADDR, 'event.address ok')