Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DVMScheduleEvent Converter #228

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ndk/src/events/kinds/dvm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]];
55 changes: 55 additions & 0 deletions ndk/src/events/kinds/dvm/schedule.test.ts
Original file line number Diff line number Diff line change
@@ -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<NDKEvent> => {
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<NDKEvent> => {
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;
};
36 changes: 36 additions & 0 deletions ndk/src/events/kinds/dvm/schedule.ts
Original file line number Diff line number Diff line change
@@ -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<NDKDVMEventSchedule> {
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);
}
}