import { storeJson } from '../fileModels/storeJson' import { sdk } from '../sdk' import { secretHexToPublicHex } from '../nostr' import { randomOperatorSecretHex } from '../utils' // Generate the Nostr operator keypair once, if absent. SpacesOps requires // OPERATOR_SECRET_HEX / OPERATOR_PUBLIC_HEX at startup (it exits otherwise). // Idempotent: only acts when the secret is not yet present. Read with .once() // — never .const() in init (it arms a write-after-const watcher). export const taskOperatorKeys = sdk.setupOnInit(async (effects) => { const existing = await storeJson.read((s) => s.operatorSecretHex).once() if (existing) return const operatorSecretHex = randomOperatorSecretHex() // Derive before persisting; if derivation rejects the (astronomically rare) // out-of-range scalar, nothing is written and StartOS retries init. const operatorPublicHex = secretHexToPublicHex(operatorSecretHex) await storeJson.merge( effects, { operatorSecretHex, operatorPublicHex }, { allowWriteAfterConst: true }, ) })