UNPKG

askexperts

Version:

AskExperts SDK: build and use AI experts - ask them questions and pay with bitcoin on an open protocol

48 lines 1.55 kB
import { DB } from "./index.js"; import { APP_DB_PATH } from "../common/constants.js"; import { DBClient } from "./DBClient.js"; import { DBRemoteClient } from "./DBRemoteClient.js"; import { getCurrentUserId } from "../common/users.js"; import { hexToBytes } from "nostr-tools/utils"; // Singleton DB instance let dbInstance = null; /** * Get the singleton DB instance * @returns The DB instance */ export function getDB() { if (!dbInstance) { dbInstance = new DB(APP_DB_PATH); } return dbInstance; } /** * Create a DB remote client instance * @param url URL for remote DB client * @returns A DB remote client instance */ export async function createDBRemoteClient(url) { // Get the current user ID const userId = getCurrentUserId(); // Get the user's private key const user = await getDB().getUser(userId); if (!user) { throw new Error(`User with ID ${userId} not found`); } // Convert the private key string to Uint8Array const privkey = hexToBytes(user.privkey); // Return a new DBRemoteClient instance with the new object-based constructor return new DBRemoteClient({ url, privateKey: privkey }); } /** * Create a DB client instance * @param url Optional URL for remote DB client * @returns A DB client instance (DBClient if url is undefined, DBRemoteClient otherwise) */ export async function createDBClient(url) { if (url) return createDBRemoteClient(url); // Return a new DBClient instance return new DBClient(); } //# sourceMappingURL=utils.js.map