UNPKG

@nuwa-ai/identity-kit

Version:

SDK for NIP-1 Agent Single DID Multi-Key Model and NIP-3 CADOP (Custodian-Assisted DID Onboarding Protocol)

261 lines (252 loc) 7.47 kB
import { k as KeyTypeInput, V as VerificationRelationship, x as KeyManager, z as IdentityEnv, h as VDRRegistry, a as KeyStore, m as StoredKey } from '../IdentityKit-DE2ZpFFA.cjs'; import { S as SignedData, N as NIP1SignedObject } from '../types-5njySywx.cjs'; import '@roochnetwork/rooch-sdk'; interface ConnectOptions { cadopDomain?: string; keyType?: KeyTypeInput; idFragment?: string; relationships?: VerificationRelationship[]; redirectPath?: string; agentDid?: string; /** Custom session key scopes (for authentication VM) */ scopes?: string[]; } interface AuthResult { success: boolean; agentDid?: string; keyId?: string; error?: string; } /** * Manages deep link authentication flow */ declare class DeepLinkManager { private keyManager; private sessionStorage; constructor(options?: { keyManager?: KeyManager; sessionStorage?: Storage; }); /** * Build a deep link URL for adding a key to a DID */ buildAddKeyUrl(opts?: ConnectOptions): Promise<{ url: string; state: string; privateKeyMultibase: string; publicKeyMultibase: string; }>; /** * Handle the callback from the deep link */ handleCallback(search: string): Promise<AuthResult>; /** * Generate a random state string */ private generateRandomState; } interface IdentityKitWebOptions { /** Application name, used to build key id fragment; optional */ appName?: string; cadopDomain?: string; storage?: 'local' | 'indexeddb' | 'memory'; /** Optional explicit RPC endpoint for Rooch node */ roochRpcUrl?: string; } /** * IdentityKitWeb – High-level Web SDK for Nuwa Identity Kit * Provides a high-level API for web applications */ declare class IdentityKitWeb { private keyManager; private deepLinkManager; private cadopDomain; private appName?; private identityEnv; private constructor(); /** * Initialize the IdentityKitWeb with automatic component initialization */ static init(options?: IdentityKitWebOptions): Promise<IdentityKitWeb>; /** * Advanced factory method for users who need to provide custom components */ static create(options: { keyManager: KeyManager; deepLinkManager?: DeepLinkManager; identityEnv: IdentityEnv; cadopDomain?: string; appName?: string; }): Promise<IdentityKitWeb>; /** * Check if the user is connected */ isConnected(): Promise<boolean>; /** * Get the current DID */ getDid(): Promise<string>; /** * List all key IDs */ listKeyIds(): Promise<string[]>; /** * Connect to Cadop * This will open a new window with the Cadop add-key page */ connect(options?: { scopes?: string[]; }): Promise<void>; /** * Handle the callback from Cadop */ handleCallback(search: string): Promise<void>; /** * Sign an operation payload using DIDAuth v1 * @param payload Object containing `operation` and `params` fields (other fields will be added automatically) */ sign(payload: Omit<SignedData, 'nonce' | 'timestamp'>): Promise<NIP1SignedObject>; /** * Verify a signature */ verify(sig: NIP1SignedObject, opts?: { maxClockSkew?: number; }): Promise<boolean>; /** * Logout (clear all keys) */ logout(): Promise<void>; /** * Generate a readable idFragment based on the application name. * 1. Slugify the provided appName (keep a-z, 0-9, _ and -) * 2. If slug becomes empty (e.g. non-Latin name), fall back to current hostname * 3. If hostname slug is still empty (edge case), use default 'key' * Always append timestamp to ensure uniqueness. */ private generateIdFragment; /** * Get the KeyManager instance */ getKeyManager(): KeyManager; /** * Get the IdentityEnv instance */ getIdentityEnv(): IdentityEnv; /** * Get the DeepLinkManager instance */ getDeepLinkManager(): DeepLinkManager; /** * Get the Cadop domain */ getCadopDomain(): string; /** * Get the app name */ getAppName(): string | undefined; /** * Expose the global VDRRegistry instance */ static get registry(): VDRRegistry; } interface IdentityKitState { isConnected: boolean; isConnecting: boolean; agentDid: string | null; keyId: string | null; error: string | null; } interface IdentityKitHook { state: IdentityKitState; connect: (options?: { scopes?: string[]; }) => Promise<void>; sign: (payload: any) => Promise<NIP1SignedObject>; verify: (sig: NIP1SignedObject) => Promise<boolean>; logout: () => Promise<void>; sdk: IdentityKitWeb | null; } interface UseIdentityKitOptions { appName?: string; cadopDomain?: string; storage?: 'local' | 'indexeddb'; autoConnect?: boolean; roochRpcUrl?: string; } /** * React hook for Nuwa Identity Kit (Web) */ declare function useIdentityKit(options?: UseIdentityKitOptions): IdentityKitHook; /** * Browser LocalStorage implementation of KeyStore */ declare class LocalStorageKeyStore implements KeyStore { private readonly prefix; constructor(options?: { prefix?: string; }); /** * List all key IDs stored in this KeyStore */ listKeyIds(): Promise<string[]>; /** * Load a key by ID, or all keys if no ID is provided */ load(keyId?: string): Promise<StoredKey | null>; /** * Save a key to storage */ save(key: StoredKey): Promise<void>; /** * Clear a key from storage, or all keys if no ID is provided */ clear(keyId?: string): Promise<void>; } /** * IndexedDB implementation of KeyStore * Supports storing CryptoKey objects and direct signing */ declare class IndexedDBKeyStore implements KeyStore { private readonly dbName; private readonly storeName; private db; constructor(options?: { dbName?: string; storeName?: string; }); /** * Initialize the database connection */ private initDB; /** * List all key IDs stored in this KeyStore */ listKeyIds(): Promise<string[]>; /** * Load a key by ID, or all keys if no ID is provided */ load(keyId?: string): Promise<StoredKey | null>; /** * Save a key to storage */ save(key: StoredKey): Promise<void>; /** * Clear a key from storage, or all keys if no ID is provided */ clear(keyId?: string): Promise<void>; } /** * Web-specific functionality for @nuwa-ai/identity-kit * * This module provides browser-specific implementations including: * - LocalStorage and IndexedDB KeyStore implementations * - DeepLink management for CADOP integration * - High-level IdentityKitWeb API * - React hooks (when React is available) * * Note: All exports include runtime environment checks to ensure * they only work in appropriate environments (browser for web features, * React available for hooks, etc.) */ declare const registry: VDRRegistry; export { type AuthResult, type ConnectOptions, DeepLinkManager, type IdentityKitHook, type IdentityKitState, IdentityKitWeb, type IdentityKitWebOptions, IndexedDBKeyStore, LocalStorageKeyStore, type UseIdentityKitOptions, VDRRegistry, registry, useIdentityKit };