web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit 4c92e1601e509bcd80ea29801cf7e0c16024f9d2
parent e20775cd8d6f915f6d2cbe87b796c12618e6d06f
Author: triesap <triesap@radroots.dev>
Date:   Fri, 21 Nov 2025 01:38:47 +0000

apps-lib: add ndk initialization helper and exported utilities, standardize cache and client configuration across local and global ndk stores

Diffstat:
Mapps-lib/src/lib/index.ts | 1+
Mapps-lib/src/lib/stores/ndk.ts | 8++++----
Aapps-lib/src/lib/utils/nostr/ndk.ts | 13+++++++++++++
3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/apps-lib/src/lib/index.ts b/apps-lib/src/lib/index.ts @@ -18,6 +18,7 @@ export * from "./utils/i18n.js"; export * from "./utils/keyval/idb.js"; export * from "./utils/keyval/lib.js"; export * from "./utils/nostr/lib.js"; +export * from "./utils/nostr/ndk.js"; export { default as Fade } from "./components/fade.svelte"; export { default as Flex } from "./components/flex.svelte"; export { default as Glyph } from "./components/glyph.svelte"; diff --git a/apps-lib/src/lib/stores/ndk.ts b/apps-lib/src/lib/stores/ndk.ts @@ -5,14 +5,14 @@ import NDKSvelte from "@nostr-dev-kit/ndk-svelte"; import { writable } from "svelte/store"; let cache_adapter: NDKCacheAdapter | undefined; -if (typeof window !== `undefined`) cache_adapter = new NDKCacheAdapterDexie({ dbName: _envLib.NDK_CACHE_NAME }); +if (typeof window !== `undefined`) cache_adapter = new NDKCacheAdapterDexie({ dbName: _envLib.NDK_CACHE }); let cache_adapter_global: NDKCacheAdapter | undefined; -if (typeof window !== `undefined`) cache_adapter_global = new NDKCacheAdapterDexie({ dbName: `${_envLib.NDK_CACHE_NAME}-global` }); +if (typeof window !== `undefined`) cache_adapter_global = new NDKCacheAdapterDexie({ dbName: `${_envLib.NDK_CACHE}-global` }); -const _ndk = new NDKSvelte({ cacheAdapter: cache_adapter, clientName: _envLib.NDK_CLIENT_NAME, explicitRelayUrls: [_envLib.RADROOTS_RELAY], autoConnectUserRelays: true, autoFetchUserMutelist: true }); +const _ndk = new NDKSvelte({ cacheAdapter: cache_adapter, clientName: _envLib.NDK_CLIENT, explicitRelayUrls: [_envLib.RADROOTS_RELAY], autoConnectUserRelays: true, autoFetchUserMutelist: true }); export const ndk = writable<NDKSvelte>(_ndk); export const ndk_user = writable<NDKUser>(); -const _ndk_global = new NDKSvelte({ cacheAdapter: cache_adapter_global, clientName: _envLib.NDK_CLIENT_NAME, autoConnectUserRelays: true, autoFetchUserMutelist: true }); +const _ndk_global = new NDKSvelte({ cacheAdapter: cache_adapter_global, clientName: _envLib.NDK_CLIENT, autoConnectUserRelays: true, autoFetchUserMutelist: true }); export const ndk_global = writable<NDKSvelte>(_ndk_global); diff --git a/apps-lib/src/lib/utils/nostr/ndk.ts b/apps-lib/src/lib/utils/nostr/ndk.ts @@ -0,0 +1,12 @@ +import { NDKPrivateKeySigner, type NDKUser } from "@nostr-dev-kit/ndk"; +import NDKSvelte from "@nostr-dev-kit/ndk-svelte"; +import { throw_err } from "@radroots/utils"; + +export const ndk_init = async (ndk: NDKSvelte, secret_key: string): Promise<NDKUser> => { + const signer = new NDKPrivateKeySigner(secret_key); + ndk.signer = signer; + const user = await signer.user(); + if (!user) throw_err("*-user"); + user.ndk = ndk; + return user; +}; +\ No newline at end of file