diff --git a/src/controllers/api/pleroma.ts b/src/controllers/api/pleroma.ts index a500161..40e0c2b 100644 --- a/src/controllers/api/pleroma.ts +++ b/src/controllers/api/pleroma.ts @@ -1,8 +1,8 @@ import { type AppController } from '@/app.ts'; import { Conf } from '@/config.ts'; -import { decryptAdmin, encryptAdmin } from '@/crypto.ts'; import { z } from '@/deps.ts'; import { configSchema, elixirTupleSchema } from '@/schemas/pleroma-api.ts'; +import { AdminSigner } from '@/signers/AdminSigner.ts'; import { eventsDB } from '@/storages.ts'; import { createAdminEvent } from '@/utils/api.ts'; import { jsonSchema } from '@/schema.ts'; @@ -18,7 +18,7 @@ const frontendConfigController: AppController = async (c) => { }], { signal }); const configs = jsonSchema.pipe(z.array(configSchema)).catch([]).parse( - event?.content ? await decryptAdmin(Conf.pubkey, event.content) : '', + event?.content ? await new AdminSigner().nip04.decrypt(Conf.pubkey, event.content) : '', ); const frontendConfig = configs.find(({ group, key }) => group === ':pleroma' && key === ':frontend_configurations'); @@ -47,7 +47,7 @@ const configController: AppController = async (c) => { }], { signal }); const configs = jsonSchema.pipe(z.array(configSchema)).catch([]).parse( - event?.content ? await decryptAdmin(pubkey, event.content) : '', + event?.content ? await new AdminSigner().nip04.decrypt(pubkey, event.content) : '', ); return c.json({ configs, need_reboot: false }); @@ -66,7 +66,7 @@ const updateConfigController: AppController = async (c) => { }], { signal }); const configs = jsonSchema.pipe(z.array(configSchema)).catch([]).parse( - event?.content ? await decryptAdmin(pubkey, event.content) : '', + event?.content ? await await new AdminSigner().nip04.decrypt(pubkey, event.content) : '', ); const { configs: newConfigs } = z.object({ configs: z.array(configSchema) }).parse(await c.req.json()); @@ -82,7 +82,7 @@ const updateConfigController: AppController = async (c) => { await createAdminEvent({ kind: 30078, - content: await encryptAdmin(pubkey, JSON.stringify(configs)), + content: await await new AdminSigner().nip04.encrypt(pubkey, JSON.stringify(configs)), tags: [['d', 'pub.ditto.pleroma.config']], }, c); diff --git a/src/crypto.ts b/src/crypto.ts deleted file mode 100644 index 9cafb51..0000000 --- a/src/crypto.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Conf } from '@/config.ts'; -import { nip04 } from '@/deps.ts'; - -/** Encrypt a message as the Ditto server account. */ -function encryptAdmin(targetPubkey: string, message: string): Promise { - return nip04.encrypt(Conf.seckey, targetPubkey, message); -} - -/** Decrypt a message as the Ditto server account. */ -function decryptAdmin(targetPubkey: string, message: string): Promise { - return nip04.decrypt(Conf.seckey, targetPubkey, message); -} - -export { decryptAdmin, encryptAdmin }; diff --git a/src/deps.ts b/src/deps.ts index 201c16a..3e0cf0a 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -17,7 +17,6 @@ export { getPublicKey, matchFilter, matchFilters, - nip04, nip05, nip13, nip19, diff --git a/src/pipeline.ts b/src/pipeline.ts index caa58fb..ead0044 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -1,5 +1,4 @@ import { Conf } from '@/config.ts'; -import { encryptAdmin } from '@/crypto.ts'; import { addRelays } from '@/db/relays.ts'; import { deleteAttachedMedia } from '@/db/unattached-media.ts'; import { Debug, LNURL, type NostrEvent } from '@/deps.ts'; @@ -198,7 +197,7 @@ async function payZap(event: DittoEvent, signal: AbortSignal) { const nwcRequestEvent = await signer.signEvent({ kind: 23194, - content: await encryptAdmin( + content: await signer.nip04.encrypt( event.pubkey, JSON.stringify({ method: 'pay_invoice', params: { invoice: pr } }), ), diff --git a/src/signers/APISigner.ts b/src/signers/APISigner.ts index 3e20370..cc4cde3 100644 --- a/src/signers/APISigner.ts +++ b/src/signers/APISigner.ts @@ -1,9 +1,9 @@ import { type AppContext } from '@/app.ts'; import { Conf } from '@/config.ts'; -import { decryptAdmin, encryptAdmin } from '@/crypto.ts'; import { HTTPException, type NostrEvent, type NostrSigner, NSecSigner, Stickynotes } from '@/deps.ts'; import { connectResponseSchema } from '@/schemas/nostr.ts'; import { jsonSchema } from '@/schema.ts'; +import { AdminSigner } from '@/signers/AdminSigner.ts'; import { Sub } from '@/subs.ts'; import { eventMatchesTemplate } from '@/utils.ts'; import { createAdminEvent } from '@/utils/api.ts'; @@ -63,7 +63,7 @@ export class APISigner implements NostrSigner { createAdminEvent({ kind: 24133, - content: await encryptAdmin( + content: await new AdminSigner().nip04.encrypt( pubkey, JSON.stringify({ id: messageId, @@ -93,7 +93,7 @@ export class APISigner implements NostrSigner { this.#c.req.raw.signal.addEventListener('abort', close); for await (const event of sub) { - const decrypted = await decryptAdmin(event.pubkey, event.content); + const decrypted = await new AdminSigner().nip04.decrypt(event.pubkey, event.content); const result = jsonSchema .pipe(connectResponseSchema)