import NDK, {NDKPrivateKeySigner} from "@nostr-dev-kit/ndk"; import * as nip19 from 'nostr-tools/nip19' import { createRestAPIClient } from "masto"; import * as masto from "masto"; type MastodonClient = masto.mastodon.rest.Client; export class Adapter { public nickname: string = ""; public protocol: string = ""; public identity: any | null; private _self: NDK | MastodonClient | null = null ; public init(): void {}; public getInbox(): void {}; public publish(): void {}; public getFollowers(): any[] { return [] }; public getFollowing(): any[] { return [] }; public updateMetadata(): void {}; public getMetadata(): any { return {} }; // according to the docs NDK must be a singleton... // this probalby will make having more than one nostr adapter at once problematic private static ndk: NDK | null = null; public static create(): Adapter { return new Adapter(); } public static toNostr(adapter: Adapter, settings: any): Adapter { adapter.identity = { privkey: settings.privkey }; adapter.nickname = settings.nickname; adapter.init = ()=> { if (!Adapter.ndk) { let privkey_raw = nip19.decode(settings.privkey); Adapter.ndk = new NDK({ signer: new NDKPrivateKeySigner(privkey_raw.data), explicitRelayUrls: [ settings.relays ] }); adapter._self = Adapter.ndk; Adapter.ndk.connect(); } else { Adapter.ndk.signer = new NDKPrivateKeySigner(settings.privatekey); for (let i of settings.relays) { Adapter.ndk.addExplicitRelay(i); } } }; adapter.getInbox = async () => { if (Adapter.ndk) { const sub = Adapter.ndk.subscribe({ kinds: [1] }); // Get all kind:1s sub.on("event", (event) => console.log(event.content)); // Show the content sub.on("eose", () => console.log("All relays have reached the end of the event stream")); sub.on("close", () => console.log("Subscription closed")); setTimeout(() => sub.stop(), 10000); // Stop the subscription after 10 seconds } }; return adapter; } public static toMasto(adapter: Adapter, settings: any): Adapter { adapter.identity = { server: settings.server, apiKey: settings.apiKey }; adapter.nickname = settings.nickname; adapter.init = () => { adapter._self = createRestAPIClient({ url: adapter.identity.server, accessToken: adapter.identity.apiKey }); } adapter.getInbox = async () => { let i = 0; for await (const statuses of (adapter._self as MastodonClient).v1.timelines.public.list()) { for (const status of statuses) { console.log(status); i++; } if (i >= 10) break; } } return adapter; } } export default { Adapter }