import { storeJson } from '../fileModels/storeJson' import { i18n } from '../i18n' import { isValidSecretHex, secretHexToPublicHex } from '../nostr' import { sdk } from '../sdk' const { InputSpec, Value } = sdk const inputSpec = InputSpec.of({ secretHex: Value.text({ name: i18n('Operator Secret Key (hex)'), description: i18n( 'A 64-character hex-encoded secp256k1 / Nostr secret key. The public key is derived automatically.', ), warning: null, footnote: null, default: null, required: true, masked: true, placeholder: '64 hexadecimal characters', minLength: 64, maxLength: 64, patterns: [ { regex: '^[0-9a-fA-F]{64}$', description: i18n('Must be exactly 64 hexadecimal characters.'), }, ], }), }) export const importOperatorKey = sdk.Action.withInput( // id 'import-operator-key', // metadata async ({ effects }) => ({ name: i18n('Import Operator Key'), description: i18n( 'Replace the operator keypair with one you provide (hex secret key).', ), warning: i18n( 'This changes the operator identity SpacesOps signs events with. Events already published under the old key stay under it. The service restarts to apply the new key.', ), allowedStatuses: 'any', group: null, visibility: 'enabled', }), // input inputSpec, // prefill — never pre-populate a secret async ({ effects }) => {}, // run async ({ effects, input }) => { const secretHex = input.secretHex.trim().toLowerCase() if (!isValidSecretHex(secretHex)) { return { version: '1', title: i18n('Failure'), message: i18n( 'The secret key must be exactly 64 hexadecimal characters.', ), result: null, } } let operatorPublicHex: string try { operatorPublicHex = secretHexToPublicHex(secretHex) } catch (e) { return { version: '1', title: i18n('Failure'), message: i18n( 'Could not derive a public key from that secret: ${error}', { error: (e as Error).message, }, ), result: null, } } await storeJson.merge(effects, { operatorSecretHex: secretHex, operatorPublicHex, }) return { version: '1', title: i18n('Success'), message: i18n( 'Operator key imported. The service is restarting to apply the new identity.', ), result: null, } }, )