connection events has .pubkey field

This commit is contained in:
Andre Staltz 2024-01-15 23:01:51 +02:00
parent 0ce68f2770
commit 4101e023cd
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 27 additions and 5 deletions

View File

@ -18,6 +18,7 @@ const IP = require('ip')
* | 'disconnecting' * | 'disconnecting'
* | 'disconnected'; * | 'disconnected';
* address: Address; * address: Address;
* pubkey: string | undefined;
* details?: any; * details?: any;
* }} ConnectionEvent * }} ConnectionEvent
* @typedef {{ * @typedef {{
@ -77,6 +78,20 @@ class Connections {
return 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() { #assertNotClosed() {
if (this.#closed) { if (this.#closed) {
throw new Error('This Connections instance is closed, create a new one.') throw new Error('This Connections instance is closed, create a new one.')
@ -102,12 +117,13 @@ class Connections {
const initiator = weAreClient ? 'we' : 'they' const initiator = weAreClient ? 'we' : 'they'
debug('Connected to %s, %s initiated it', address, initiator) debug('Connected to %s, %s initiated it', address, initiator)
const pubkey = Connections.extractSHSEPubkey(address)
this.#rpcs.set(address, rpc) this.#rpcs.set(address, rpc)
rpc.once('closed', () => { rpc.once('closed', () => {
debug('Disconnected from %s', address) debug('Disconnected from %s', address)
this.#rpcs.delete(address) this.#rpcs.delete(address)
this.#infos.update(address, { state: 'disconnected' }) this.#infos.update(address, { state: 'disconnected' })
this.#notifyEvent({ type: 'disconnected', address }) this.#notifyEvent({ type: 'disconnected', address, pubkey })
this.#infos.emit() this.#infos.emit()
}) })
@ -117,6 +133,7 @@ class Connections {
this.#notifyEvent({ this.#notifyEvent({
type: state, type: state,
address, address,
pubkey,
details: { rpc, weAreClient }, details: { rpc, weAreClient },
}) })
this.#infos.emit() this.#infos.emit()
@ -166,8 +183,9 @@ class Connections {
case 'disconnected': { case 'disconnected': {
debug('Connecting to %s', address) debug('Connecting to %s', address)
const state = /**@type {Info['state']}*/ ('connecting') const state = /**@type {Info['state']}*/ ('connecting')
const pubkey = Connections.extractSHSEPubkey(address)
this.#infos.update(address, { state }) this.#infos.update(address, { state })
this.#notifyEvent({ type: state, address }) this.#notifyEvent({ type: state, address, pubkey })
this.#infos.emit() this.#infos.emit()
const [err, rpc] = await run(this.#peer.connect)(address) const [err, rpc] = await run(this.#peer.connect)(address)
@ -177,6 +195,7 @@ class Connections {
this.#notifyEvent({ this.#notifyEvent({
type: 'connecting-failed', type: 'connecting-failed',
address, address,
pubkey,
details: err, details: err,
}) })
this.#infos.emit() this.#infos.emit()
@ -244,8 +263,9 @@ class Connections {
debug('Disconnecting from %s', address) debug('Disconnecting from %s', address)
const state = /**@type {Info['state']}*/ ('disconnecting') const state = /**@type {Info['state']}*/ ('disconnecting')
const pubkey = Connections.extractSHSEPubkey(address)
this.#infos.update(address, { state }) this.#infos.update(address, { state })
this.#notifyEvent({ type: state, address }) this.#notifyEvent({ type: state, address, pubkey })
this.#infos.emit() this.#infos.emit()
// @ts-ignore // @ts-ignore
await run(rpc.close)(true) await run(rpc.close)(true)

View File

@ -181,6 +181,7 @@ function initNet(peer, config) {
} }
exports.name = 'net' exports.name = 'net'
exports.needs = ['shse']
exports.manifest = { exports.manifest = {
start: 'sync', start: 'sync',
stop: 'sync', stop: 'sync',

View File

@ -4,8 +4,8 @@ const p = require('node:util').promisify
const pull = require('pull-stream') const pull = require('pull-stream')
const { createPeer } = require('./util') const { createPeer } = require('./util')
const TEST_ADDR = const PUBKEY = 'EqTMFv7zm8hpPyAkj789qdJgqtz81AEbcinpAs24RRUC'
'net:localhost:9752~shse:EqTMFv7zm8hpPyAkj789qdJgqtz81AEbcinpAs24RRUC' const TEST_ADDR = `net:localhost:9752~shse:${PUBKEY}`
test('net', async (t) => { test('net', async (t) => {
await t.test('connect() rejects given unreachable address', async () => { await t.test('connect() rejects given unreachable address', async () => {
@ -69,6 +69,7 @@ test('net', async (t) => {
if (i === 1) { if (i === 1) {
assert.equal(ev.type, 'connecting', 'event.type ok') assert.equal(ev.type, 'connecting', 'event.type ok')
assert.equal(ev.address, TEST_ADDR, 'event.address ok') assert.equal(ev.address, TEST_ADDR, 'event.address ok')
assert.equal(ev.pubkey, PUBKEY, 'event.pubkey ok')
} else if (i === 2) { } else if (i === 2) {
assert.equal(ev.type, 'connecting-failed', 'event.type ok') assert.equal(ev.type, 'connecting-failed', 'event.type ok')
assert.equal(ev.address, TEST_ADDR, 'event.address ok') assert.equal(ev.address, TEST_ADDR, 'event.address ok')