From b5a1220159c9e1cbaee0447102b79950e8dad99a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 16 Jan 2024 17:46:53 -0600 Subject: [PATCH] Publish NWC event from pipeline --- src/pipeline.ts | 28 +++++++++++++++++++++++++--- src/schemas/lnurl.ts | 7 ++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/pipeline.ts b/src/pipeline.ts index 10af61cd..4d763221 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -5,17 +5,19 @@ import { findUser } from '@/db/users.ts'; import { Debug, type Event } from '@/deps.ts'; import { isEphemeralKind } from '@/kinds.ts'; import { isLocallyFollowed } from '@/queries.ts'; -import { lnurlResponseSchema } from '@/schemas/lnurl.ts'; +import { lnurlCallbackResponseSchema, lnurlResponseSchema } from '@/schemas/lnurl.ts'; import { updateStats } from '@/stats.ts'; import { client, eventsDB, memorelay, reqmeister } from '@/storages.ts'; import { Sub } from '@/subs.ts'; import { getTagSet } from '@/tags.ts'; import { type EventData } from '@/types.ts'; import { lnurlDecode } from '@/utils/lnurl.ts'; -import { eventAge, isRelay, nostrDate, Time } from '@/utils.ts'; +import { eventAge, isRelay, nostrDate, nostrNow, Time } from '@/utils.ts'; import { fetchWorker } from '@/workers/fetch.ts'; import { TrendsWorker } from '@/workers/trends.ts'; import { verifySignatureWorker } from '@/workers/verify.ts'; +import { signAdminEvent } from '@/sign.ts'; +import { encryptAdmin } from '@/crypto.ts'; const debug = Debug('ditto:pipeline'); @@ -179,7 +181,27 @@ async function submitZaps(event: Event, data: EventData, signal = AbortSignal.ti params.set('nostr', JSON.stringify(event)); params.set('lnurl', lnurl); callback.search = params.toString(); - await fetchWorker(callback, { signal }); + const response = await fetchWorker(callback, { signal }); + const json = await response.json(); + const { pr } = lnurlCallbackResponseSchema.parse(json); + const nwcRequestEvent = await signAdminEvent({ + kind: 23194, + content: await encryptAdmin( + event.pubkey, + JSON.stringify({ + method: 'pay_invoice', + params: { + invoice: pr, + }, + }), + ), + created_at: nostrNow(), + tags: [ + ['p', event.pubkey], + ['e', event.id], + ], + }); + await handleEvent(nwcRequestEvent); } } catch (e) { debug('lnurl error:', e); diff --git a/src/schemas/lnurl.ts b/src/schemas/lnurl.ts index ec3de2cd..7ee83004 100644 --- a/src/schemas/lnurl.ts +++ b/src/schemas/lnurl.ts @@ -12,4 +12,9 @@ const lnurlResponseSchema = z.object({ nostrPubkey: nostrIdSchema.optional(), }); -export { lnurlResponseSchema }; +const lnurlCallbackResponseSchema = z.object({ + pr: z.string(), + routes: z.unknown().array(), +}); + +export { lnurlCallbackResponseSchema, lnurlResponseSchema };