diff --git a/lib/members.cjs b/lib/members.cjs index 3a0d887..ee09bb0 100644 --- a/lib/members.cjs +++ b/lib/members.cjs @@ -1,9 +1,7 @@ -const Crypto = require('node:crypto') const Path = require('node:path') const AtomicFileRW = require('atomic-file-rw') -const Base58 = require('bs58') -class Tokens { +class Members { static #filePath /** @@ -34,38 +32,36 @@ class Tokens { } const json = typeof buf === 'string' ? buf : buf.toString('utf-8') const arr = JSON.parse(json) - for (const token of arr) { - this.#set.add(token) + for (const pubkey of arr) { + this.#set.add(pubkey) } this.#loaded = true }) } /** - * @param {string} token + * @param {string} pubkey * @returns {boolean} */ - static has(token) { + static has(pubkey) { if (!this.#loaded) { throw new Error('Members not loaded yet, cannot call has()') } - return this.#set.has(token) + return this.#set.has(pubkey) } - static create() { + /** + * @param {string} pubkey + */ + static add(pubkey) { if (!this.#loaded) { throw new Error('Members not loaded yet, cannot call create()') } - let token - do { - token = Base58.encode(Crypto.randomBytes(32)) - } while (this.#set.has(token)) - this.#set.add(token) + this.#set.add(pubkey) this.#save((err, _) => { if (err) console.warn('Problem saving members file:', err) }) - return token } } -module.exports = Tokens +module.exports = Members diff --git a/lib/plugin-hub.cjs b/lib/plugin-hub.cjs index c381d9c..7fdd433 100644 --- a/lib/plugin-hub.cjs +++ b/lib/plugin-hub.cjs @@ -45,13 +45,15 @@ module.exports = { const [clientMetadata, cb] = args const {pubkey, extra} = clientMetadata if (Members.has(pubkey)) { + debug('authorized member %s to connect', pubkey) cb(null, true) } else if (extra && Tokens.has(extra)) { + debug('authorized NEW member %s to connect', pubkey) Tokens.delete(extra) Members.add(pubkey) cb(null, true) } else { - debug('prevented stranger %s from connecting to us', pubkey) + debug('denied stranger %s from connecting', pubkey) cb(new Error('client is a stranger')) } }) diff --git a/lib/tokens.cjs b/lib/tokens.cjs index 9c1d9b4..42f06df 100644 --- a/lib/tokens.cjs +++ b/lib/tokens.cjs @@ -32,7 +32,7 @@ class Tokens { } return } - const json = typeof buf === 'string' ? buf : Buffer.toString(buf, 'utf-8') + const json = typeof buf === 'string' ? buf : buf.toString('utf-8') const arr = JSON.parse(json) for (const token of arr) { this.#set.add(token)