diff --git a/ndk/src/events/kinds/dvm/index.ts b/ndk/src/events/kinds/dvm/index.ts index 2c4f5fde..2c167759 100644 --- a/ndk/src/events/kinds/dvm/index.ts +++ b/ndk/src/events/kinds/dvm/index.ts @@ -2,5 +2,6 @@ export * from "./request"; export * from "./NDKTranscriptionDVM"; export * from "./result"; export * from "./feedback"; +export * from "./schedule"; export type NDKDvmParam = [string, string, ...string[]]; diff --git a/ndk/src/events/kinds/dvm/schedule.test.ts b/ndk/src/events/kinds/dvm/schedule.test.ts new file mode 100644 index 00000000..0c2f39e9 --- /dev/null +++ b/ndk/src/events/kinds/dvm/schedule.test.ts @@ -0,0 +1,55 @@ +import type { NostrEvent } from "../.."; +import { NDKEvent } from "../.."; +import { NDK } from "../../../ndk"; +import { NDKPrivateKeySigner } from "../../../signers/private-key"; +import { NDKKind } from ".."; +import { NDKDVMEventSchedule } from "./schedule"; + +let ndk: NDK; +const TEST_ACCOUNT_PK = "npub1es6q5vpvf42kv88thswctggfgzxty9amgcmhn6ytym8674n8fgpqtxsyua"; +const SHIPYARD_PK = "85c20d3760ef4e1976071a569fb363f4ff086ca907669fb95167cdc5305934d1"; +const SIGNER = NDKPrivateKeySigner.generate(); + +beforeAll(() => ndk = new NDK({ signer: SIGNER })); + +describe("creates an NDK event", () => { + it("able to decrypt", async () => { + const eventToPublish = await createTextEvent(); + const dmvInput = ["i", JSON.stringify(eventToPublish.rawEvent()), "text"]; + const mockEvent = await createScheduleEvent(dmvInput); + const event = await NDKDVMEventSchedule.from(mockEvent); + + expect(event.created_at).toEqual(mockEvent.created_at); + expect(JSON.parse(event.content)).toEqual([dmvInput]); + expect(event.kind).toBe(NDKKind.DVMEventSchedule); + }); +}); + +const createScheduleEvent = async (dvmInput: string[]): Promise => { + const scheduleNostrEvent: NostrEvent = { + kind: NDKKind.DVMEventSchedule, + created_at: Date.now() / 1000, + pubkey: TEST_ACCOUNT_PK, + tags: [ + ["p", SHIPYARD_PK], + ["encrypted"] + ], + content: JSON.stringify([dvmInput]) + }; + const dvm = ndk.getUser({ pubkey: SHIPYARD_PK }); + const scheduleEvent = new NDKEvent(ndk, scheduleNostrEvent); + await scheduleEvent.encrypt(dvm); + await scheduleEvent.sign(); + return scheduleEvent; +}; + +const createTextEvent = async (): Promise => { + const ndkEvent = new NDKEvent(ndk, { + created_at: Date.now() / 1000 + 120, + content: "test", + kind: NDKKind.Text, + pubkey: TEST_ACCOUNT_PK + } as NostrEvent); + await ndkEvent.sign(SIGNER); + return ndkEvent; +}; \ No newline at end of file diff --git a/ndk/src/events/kinds/dvm/schedule.ts b/ndk/src/events/kinds/dvm/schedule.ts new file mode 100644 index 00000000..8728fb56 --- /dev/null +++ b/ndk/src/events/kinds/dvm/schedule.ts @@ -0,0 +1,36 @@ +import type { NostrEvent } from "../../index.js"; +import type { NDK } from "../../../ndk/index.js"; +import { NDKKind } from "../index.js"; +import { NDKEvent } from "../../index.js"; +import { NDKUser } from "../../../user/index.js"; + +export class NDKDVMEventSchedule extends NDKEvent { + constructor(ndk?: NDK, event?: NostrEvent) { + super(ndk, event); + this.kind = NDKKind.DVMEventSchedule; + } + + static async from(event: NDKEvent): Promise { + const e = new NDKDVMEventSchedule(event.ndk, event.rawEvent()); + if (e.encrypted) { + const serviceProvider = new NDKUser({ + pubkey: event.tagValue("p"), + }); + await e.decrypt(serviceProvider); + } + return e; + } + + get encrypted(): boolean { + return !!this.getMatchingTags("encrypted")[0]; + } + + static parseEventToPublish(scheduleEvent: NDKDVMEventSchedule): NDKEvent | undefined { + const parsed = JSON.parse(scheduleEvent.content); + const input = parsed.find((el: string[]) => el[0] === 'i'); + if (!input) return; + + const payload = JSON.parse(input[1]); + return new NDKEvent(scheduleEvent.ndk, payload); + } +} \ No newline at end of file