Nostr Development Kit (NDK)

repository·master·Indexed 19 days ago

https://github.com/nostr-dev-kit/ndk

A comprehensive, modular toolkit for building modern Nostr applications. NDK provides core protocol handling, subscription management, and framework-specific integrations for React and Svelte. It includes specialized packages such as @nostr-dev-kit/blossom for file uploads and URL healing, and @nostr-dev-kit/cache-browser for optimized browser caching with automatic fallback between SQLite WASM and IndexedDB.

Tokens
301.4K
Snippets
1K
Records
1.3K
Agent score
66%

What's inside NDK

  1. Overview of NDK Mobile features

    master

    NDK Mobile is a React Native/Expo implementation of the Nostr Development Kit designed for mobile platforms. Key features include:

    • Multiple Signer Implementations: Supports NIP-07, NIP-46, and Private Key signing.
    • Offline Support: Uses SQLite-based caching to enable offline capabilities.
    • Subscription Management: Handles automatic reconnection for Nostr subscriptions.
    • Mobile Integration: Built specifically for React Native and Expo compatibility.
    • State Management: Provides React hooks for easy integration into mobile UI components.
    • Wallet Support: Includes integrated wallet support.
  2. Overview of @nostr-dev-kit/react hooks

    master

    @nostr-dev-kit/react provides React hooks and utilities designed to integrate Nostr functionality into React applications via NDK.

    Key capabilities include:

    • NDK Instance Management: Using useNDKInit and useNDK to manage the NDK lifecycle.
    • User Management: Managing the current user with useNDKCurrentUser and resolving users from different formats with useUser.
    • Profile Management: Accessing specific profile values via useProfileValue.
    • Session & Subscriptions: Handling multi-user sessions and managing event subscriptions using modern callback patterns.
  3. Overview of NDK Packages

    master

    NDK is a modular monorepo. You can install only the specific packages required for your application's needs.

    Core

    • @nostr-dev-kit/ndk: Core library for event handling, subscriptions, and relay management.

    Framework Integration

    • @nostr-dev-kit/svelte: Svelte 5 integration with reactive runes.
    • @nostr-dev-kit/react: React hooks.
    • @nostr-dev-kit/mobile: React Native integration.

    Advanced Features

    • @nostr-dev-kit/messages: NIP-17 DMs and conversation management.
    • @nostr-dev-kit/sessions: Multi-account session management.
    • @nostr-dev-kit/wot: Web of Trust filtering/ranking.
    • @nostr-dev-kit/sync: NIP-77 Negentropy sync.
    • @nostr-dev-kit/wallet: Cashu, NWC, and WebLN integration.
    • @nostr-dev-kit/blossom: Blossom media server protocol.

    Cache Adapters

    • @nostr-dev-kit/cache-memory: In-memory LRU cache.
    • @nostr-dev-kit/cache-dexie: IndexedDB via Dexie.
    • @nostr-dev-kit/cache-redis: Server-side Redis.
    • @nostr-dev-kit/cache-sqlite: SQLite.
    • @nostr-dev-kit/cache-sqlite-wasm: SQLite WASM for web workers.
    • @nostr-dev-kit/cache-nostr: Nostr relay as cache.
  4. Overview of @ndk/svelte component categories

    master

    The @ndk/svelte registry is organized into several categories to help you find the right level of abstraction for your Nostr application:

    • 🧱 Blocks: Complete, ready-to-use UI sections (e.g., login interfaces, thread viewers).
    • 🛠️ Builders: Reusable stateful primitives for composing complex Nostr interactions.
    • 🎨 Components: Individual UI elements for specific Nostr entities (e.g., users, articles, events, hashtags, media).
    • 🎯 UI Primitives: Low-level, composable building blocks for custom layouts.
    • 🛠️ Utilities: Helper functions for formatting, parsing, and data manipulation.
    • 🎭 Icons: Optimized Svelte icon components.
  5. NDK Core Features Overview

    master

    NDK provides a comprehensive set of tools for Nostr development:

    • Relay Management: Connection pool with automatic reconnection and failover.
    • Subscription API: Flexible API with support for caching, batching, and auto-closing.
    • Event Handling: Creation, validation, and wrappers for major NIPs (NIP-01, NIP-04, NIP-07, NIP-18, NIP-49, NIP-57, NIP-60, NIP-61).
    • Signer Adapters: Support for private keys, encrypted keys (NIP-49), browser extensions (NIP-07), and remote signing (NIP-46).
    • Caching: Pluggable cache adapters (Redis, Dexie, SQLite, etc.).
    • NIP Support: Outbox model, Data Vending Machine (NIP-90), and Zap utilities (NIP-57, NIP-61).
    • Framework Integration: Modular packages for Mobile, Svelte (4 and 5), and React.
  6. Core features of NDK Mobile

    master

    NDK Mobile provides a specialized toolkit for building Nostr applications on mobile devices, including:

    • Signer Support: Supports multiple implementations via NDK Core (NIP-07, NIP-46, Private Key) and NDK Mobile (NIP-55).
    • Offline Support: Uses NDKCacheAdapterSqlite for SQLite-based caching.
    • Subscription Management: Handles subscriptions with automatic reconnection logic.
    • State Management: Provides React hooks via @nostr-dev-kit/react such as useNDKStore, useNDKSessions, and useSubscribe.
    • Wallet Integration: Integrated support via @nostr-dev-kit/ndk-wallet.
    • Persistent Sessions: Automatically saves and loads user sessions and signers using expo-secure-store.
  7. NDK-Blossom Features and Protocol Support

    master

    NDK-Blossom provides a high-level interface for the Blossom protocol, supporting several Blossom Upgrade Documents (BUDs):

    • File Upload: Automatic server selection.
    • URL Healing: Finding alternative servers for broken URLs.
    • Blob Management: List, retrieve, and delete blobs.
    • Server Discovery: Using Kind 10063 user preferences.
    • Mirroring: Copying blobs between servers (BUD-04).
    • Media Optimization: Accessing resized or converted versions (BUD-05).
    • Authentication: Using Nostr Kind 24242.
    • Metadata: Support for Nostr File Metadata Tags (BUD-08).
  8. Browse NDK Cookbook recipes

    master

    The NDK Cookbook is a collection of self-contained, practical, and copy-pasteable recipes designed to help you accomplish specific tasks with the Nostr Development Kit (NDK). Recipes are organized by category, package, and difficulty level to help you find the right implementation pattern for your needs.

    Browse by Category

    • Authentication: Login flows, signers, and sessions.
    • Events: Creating, publishing, and handling events.
    • Relays: Connection management, hints, and the outbox model.
    • Payments: Zaps, NWC, Cashu, and Lightning.
    • Testing: Mocks, fixtures, and test patterns.
    • Mobile: React Native specific recipes.

    Browse by Package

    • ndk-core: Core functionality, events, and relays.
    • svelte: Svelte 5 reactive patterns.
    • ndk-mobile: React Native mobile apps.
    • ndk-wallet: Payment and wallet integration.

    Browse by Difficulty

    • Beginner: No prior NDK knowledge required.
    • ⭐⭐ Intermediate: Assumes basic NDK understanding.
    • ⭐⭐⭐ Advanced: Complex concepts involving multiple systems.
  9. Overview of NDK Svelte Registry architecture

    master

    The NDK Svelte Registry is a component library for Svelte 5 applications. It is organized into three hierarchical layers to support progressive enhancement:

    1. UI Primitives (./ui/): Headless, unstyled building blocks. They use a composition pattern and are exported as namespaced objects (e.g., User.Root, User.Avatar). Use these when you need maximum flexibility or are building a custom design system.
    2. Components (./components/): Pre-styled, opinionated implementations built on top of primitives using Tailwind CSS. They are exported as named components (e.g., UserCardPortrait). Use these for quick, standard layouts.
    3. Blocks (./blocks/): Complete, production-ready feature implementations that combine multiple components and primitives (e.g., ThreadViewTwitter). Use these for complex, multi-component layouts like entire application sections.
  10. What is validated in NDK filters?

    master

    NDK validation checks for several types of invalid data within subscription filters:

    1. Undefined Values: Detects or removes undefined in any filter array.
    2. Type Validation:
      • authors: Must be strings and valid 64-character hex pubkeys.
      • ids: Must be strings and valid 64-character hex event IDs.
      • kinds: Must be numbers (integers between 0-65535).
      • Tag filters (#e, #p, etc.): Must be strings. Specifically, #e and #p tags must be valid 64-character hex strings.
    3. Invalid Data:
      • Non-string values in string arrays.
      • Non-numeric values in the kinds array.
      • Invalid hex strings where required.
      • Out-of-range kind numbers.
  11. What is NDKWallet and its components?

    master

    The ndk-wallet package provides a toolkit for managing various wallet types in the Nostr ecosystem. It is built around the NDKWallet interface, which serves as the base for all implementations, providing a common API for:

    • Retrieving balances
    • Sending and receiving payments
    • Interacting with wallet events
    • Managing wallet status

    Key implementations include:

    • NDKCashuWallet: NIP-60 Cashu eCash implementation.
    • NDKNutzapMonitor: Automated monitoring and redemption of NIP-60 nutzaps.
    • NDKWalletNWC: NIP-47 Nostr Wallet Connect client.
  12. What is the Primitives section in the NDK Svelte Registry?

    master

    The primitives section is the designated area for all lower-level technical documentation within a component page. Following the NDK architectural principle, you should avoid scattering technical details across custom sections and instead centralize them here to keep the ComponentPageTemplate focused.

    Use the primitives section for:

    • Builder Pattern Documentation: Explaining how to use state builders (e.g., createReactionAction).
    • UI Primitive Composition: Showing how to combine low-level UI primitives for custom layouts.
    • Anatomy Diagrams: Providing visual breakdowns of a component's internal structure.
    • Low-Level API Documentation: Technical details for advanced usage.
    • Building Block Examples: Demonstrating fundamental composition patterns.

    Page Flow Position:

    1. Showcase: Component variants preview.
    2. Components: Detailed variant documentation.
    3. Recipes: Common composition patterns.
    4. Primitives: All lower-level content (Builders, UI Primitives, Anatomy).