| … | … | @@ -47,8 +47,21 @@ |
| 47 | 47 | |
| 48 | 48 | import Autobase from 'autobase' |
| 49 | 49 | import Hyperbee from 'hyperbee' |
| 50 | import Hypercore from 'hypercore' |
| 50 | 51 | import ReadyResource from 'ready-resource' |
| 51 | 52 | import b4a from 'b4a' |
| 53 | import crypto from 'hypercore-crypto' |
| 54 | |
| 55 | /** |
| 56 | * Sign a writer-rotation vouch: the voucher (an existing writer) signs the NEW |
| 57 | * writer key, authorizing its admission via the doorbell (see Room.requestWriter |
| 58 | * / _applyRequestWriter). Returns the signature hex. |
| 59 | * @param {string} newWriterKeyHex - the new local writer key being vouched for |
| 60 | * @param {Buffer} voucherSecretKey - the existing writer's hypercore secret key |
| 61 | */ |
| 62 | export function signWriterVouch (newWriterKeyHex, voucherSecretKey) { |
| 63 | return b4a.toString(crypto.sign(b4a.from(newWriterKeyHex, 'hex'), voucherSecretKey), 'hex') |
| 64 | } |
| 52 | 65 | |
| 53 | 66 | export class Room extends ReadyResource { |
| 54 | 67 | /** |
| … | … | @@ -209,6 +222,46 @@ export class Room extends ReadyResource { |
| 209 | 222 | await this._base.append(this._schema.encodeOp(op)) |
| 210 | 223 | } |
| 211 | 224 | |
| 225 | /** |
| 226 | * DOORBELL: a not-yet-admitted peer requests admission of its own new writer |
| 227 | * key, proving authorization by a VOUCH from an existing writer key (keet's |
| 228 | * proof-gated-optimistic admission, using the writer set as the authorization |
| 229 | * record). Used when an existing writer ROTATES its key (e.g. forceResync |
| 230 | * re-join into a fresh corestore): the peer signs its NEW key with its OLD |
| 231 | * (still-admitted) writer key. apply (_applyRequestWriter) admits ONLY if the |
| 232 | * vouch verifies AND the voucher is a current writer — and only ever as a |
| 233 | * PLAIN writer (never indexer/owner: no self-escalation). |
| 234 | * |
| 235 | * Appended OPTIMISTICALLY so a not-yet-writable peer can ring it; it carries |
| 236 | * only the proof (no content), so nothing is lost if no indexer admits it. |
| 237 | * |
| 238 | * autobase writer keys are MANIFEST keys, not raw signing keys, so the vouch |
| 239 | * carries the voucher's raw SIGNER publicKey; apply recomputes the manifest |
| 240 | * key from it (`Hypercore.key({signers:[{publicKey}]})`) to bind it back to a |
| 241 | * current writer. |
| 242 | * |
| 243 | * @param {object} opts |
| 244 | * @param {string} opts.newWriterKey - this peer's new local writer key (hex) |
| 245 | * @param {string} opts.voucherSignerKey - the voucher writer's raw signer publicKey (hex); |
| 246 | * from `oldRoom.base.local.keyPair.publicKey` |
| 247 | * @param {string} opts.voucherSig - signWriterVouch(newWriterKey, voucherSignerSecretKey) |
| 248 | * @param {string} [opts.userId] - writer-set label (default = newWriterKey) |
| 249 | */ |
| 250 | async requestWriter (opts = {}) { |
| 251 | const { newWriterKey, voucherSignerKey, voucherSig, userId } = opts |
| 252 | if (!newWriterKey) throw new Error('requestWriter requires newWriterKey (hex)') |
| 253 | if (!voucherSignerKey || !voucherSig) throw new Error('requestWriter requires voucherSignerKey + voucherSig') |
| 254 | const op = { |
| 255 | type: 'request-writer', |
| 256 | userId: userId || newWriterKey, |
| 257 | newWriterKey, |
| 258 | voucherSignerKey, |
| 259 | voucherSig, |
| 260 | timestamp: Date.now() |
| 261 | } |
| 262 | await this._base.append(this._schema.encodeOp(op), { optimistic: true }) |
| 263 | } |
| 264 | |
| 212 | 265 | /** |
| 213 | 266 | * Append a remove-writer op for the given userId. |
| 214 | 267 | * @param {string} userId |
| … | … | @@ -271,6 +324,10 @@ export class Room extends ReadyResource { |
| 271 | 324 | await this._applyRemoveWriter(batch, op, host) |
| 272 | 325 | continue |
| 273 | 326 | } |
| 327 | if (op.type === 'request-writer') { |
| 328 | await this._applyRequestWriter(view, batch, op, host) |
| 329 | continue |
| 330 | } |
| 274 | 331 | |
| 275 | 332 | // Schema-specific ops |
| 276 | 333 | try { |
| … | … | @@ -306,6 +363,61 @@ export class Room extends ReadyResource { |
| 306 | 363 | } |
| 307 | 364 | } |
| 308 | 365 | |
| 366 | // DOORBELL apply (see requestWriter). Deterministic, proof-gated: admit |
| 367 | // op.newWriterKey ONLY if (1) op.voucherSig is a valid signature of |
| 368 | // op.newWriterKey by op.voucherKey, AND (2) op.voucherKey is a CURRENT |
| 369 | // non-mirror writer in this room. Authorizes a key ROTATION by an existing |
| 370 | // writer; grants a PLAIN writer only (never indexer). Silently no-ops on a |
| 371 | // bad proof — a forged/unauthorized request admits nothing. |
| 372 | async _applyRequestWriter (view, batch, op, host) { |
| 373 | const { newWriterKey, voucherSignerKey, voucherSig } = op |
| 374 | if (!newWriterKey || !voucherSignerKey || !voucherSig) return |
| 375 | |
| 376 | // (1) verify the vouch signature over the new key by the raw signer key |
| 377 | let sigOk = false |
| 378 | try { |
| 379 | sigOk = crypto.verify( |
| 380 | b4a.from(newWriterKey, 'hex'), |
| 381 | b4a.from(voucherSig, 'hex'), |
| 382 | b4a.from(voucherSignerKey, 'hex') |
| 383 | ) |
| 384 | } catch { sigOk = false } |
| 385 | if (!sigOk) return |
| 386 | |
| 387 | // (2) bind the signer to a writer key: recompute the default single-signer |
| 388 | // MANIFEST key from the signer publicKey (autobase writer keys are manifests, |
| 389 | // not raw keys). A non-default manifest simply won't match → safe rejection. |
| 390 | let voucherKey |
| 391 | try { |
| 392 | voucherKey = b4a.toString(Hypercore.key({ signers: [{ publicKey: b4a.from(voucherSignerKey, 'hex') }] }), 'hex') |
| 393 | } catch { return } |
| 394 | |
| 395 | // (3) that manifest key must be a current, non-mirror writer in this room |
| 396 | let voucherIsWriter = false |
| 397 | try { |
| 398 | for await (const entry of view.createReadStream({ gte: 'writer:', lt: 'writer:\xff' })) { |
| 399 | const v = entry.value |
| 400 | if (v && v.writerKey === voucherKey && v.role !== 'mirror') { voucherIsWriter = true; break } |
| 401 | } |
| 402 | } catch { voucherIsWriter = false } |
| 403 | if (!voucherIsWriter) return |
| 404 | |
| 405 | // Admit — PLAIN writer only (no self-escalation to indexer/owner). |
| 406 | const userId = op.userId || newWriterKey |
| 407 | await batch.put(`writer:${userId}`, { |
| 408 | role: 'writer', |
| 409 | writerKey: newWriterKey, |
| 410 | addedBy: 'doorbell:' + voucherKey.slice(0, 16), |
| 411 | addedAt: op.timestamp || 0 |
| 412 | }) |
| 413 | try { |
| 414 | await host.addWriter(b4a.from(newWriterKey, 'hex'), { indexer: false }) |
| 415 | } catch (err) { |
| 416 | // eslint-disable-next-line no-console |
| 417 | console.error('[Room] doorbell addWriter failed:', err.message) |
| 418 | } |
| 419 | } |
| 420 | |
| 309 | 421 | async _applyRemoveWriter (batch, op, host) { |
| 310 | 422 | const existing = await batch.get(`writer:${op.userId}`) |
| 311 | 423 | if (!existing) return |