OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-store / lib / account-registry.js

42 lines · javascript ref:
// AccountRegistry — the multi-tenant PDS's durable roster of hosted repos.
//
// One classic Hyperbee (same substrate as RepoStore) on a reserved core of the SHARED
// corestore: key = did, value = the account record (handle, storeName, signing key,
// genesis op, active flag). This is what lets a restarted PDS re-open every account it
// hosts instead of forgetting them. The signing key lives here in the PDS custody model
// (the bsky PDS holds account keys); hardening the at-rest storage is a tracked loose
// end (see the hyperident plaintext-seed note in the handoff).

import Hyperbee from 'hyperbee'

export class AccountRegistry {
  constructor (corestore, opts = {}) {
    this._store = corestore
    this._name = opts.name || 'pds-accounts'
    this.bee = null
  }

  async ready () {
    const core = this._store.get({ name: this._name })
    await core.ready()
    // alwaysDuplicate:false → re-putting an unchanged account record is a no-op, not an append. See repo-store.js.
    this.bee = new Hyperbee(core, { keyEncoding: 'utf-8', valueEncoding: 'json', alwaysDuplicate: false })
    await this.bee.ready()
    return this
  }

  async put (did, record) { await this.bee.put(did, record) }

  async get (did) {
    const node = await this.bee.get(did)
    return node ? node.value : null
  }

  // Every persisted account record, in did (key) order.
  async * list () {
    for await (const node of this.bee.createReadStream()) yield node.value
  }

  async close () { if (this.bee) await this.bee.close() }
}