@terminal3/t3n-sdk 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -587,19 +587,34 @@ declare function metamask_get_address(): Promise<string>;
587
587
  */
588
588
  declare function eth_get_address(privateKey: string): string;
589
589
  /**
590
- * Create MlKemPublicKey handler that returns the environment-specific root public key
591
- * Note: The Rust PkWithRho type serializes as an array of bytes, not a base64 string
590
+ * Create an MlKemPublicKey handler that lazily fetches the root public key
591
+ * from `${baseUrl}/status` on first invocation and caches the encoded
592
+ * response for subsequent calls.
593
+ *
594
+ * @param baseUrl - **Required**. The node URL whose `/status` endpoint should
595
+ * serve the ML-KEM public key. Must be the same URL the
596
+ * T3nClient is constructed with — otherwise the handshake
597
+ * encrypts to one node and sends ciphertext to another.
598
+ *
599
+ * Was optional in 0.3.x, where omitting it caused the lazy
600
+ * fetch to silently fall back to `NODE_URLS[currentEnv]` and
601
+ * hit the wrong node. Three downstream consumers (demo.ts,
602
+ * t3-apps dev wallet hooks, t3n-mcp session manager) all
603
+ * hit this trap before we tightened the type.
592
604
  */
593
- declare function createMlKemPublicKeyHandler(): GuestToHostHandler;
605
+ declare function createMlKemPublicKeyHandler(baseUrl: string): GuestToHostHandler;
594
606
  /**
595
607
  * Create Random handler backed by crypto.getRandomValues
596
608
  * Note: The Rust Vec<u8> type serializes as an array of bytes, not a base64 string
597
609
  */
598
610
  declare function createRandomHandler(): GuestToHostHandler;
599
611
  /**
600
- * Create the default handler set required by the T3n handshake
612
+ * Create the default handler set required by the T3n handshake.
613
+ *
614
+ * @param baseUrl - **Required**. Forwarded to `createMlKemPublicKeyHandler`
615
+ * so the lazy /status fetch hits the right node.
601
616
  */
602
- declare function createDefaultHandlers(): GuestToHostHandlers;
617
+ declare function createDefaultHandlers(baseUrl: string): GuestToHostHandlers;
603
618
 
604
619
  /**
605
620
  * Cryptographic utilities for T3n SDK
@@ -724,8 +739,8 @@ type Environment = "local" | "staging" | "production" | "test";
724
739
  interface SdkConfig {
725
740
  /** Environment identifier */
726
741
  environment: Environment;
727
- /** Base64-encoded ML-KEM root public key */
728
- mlKemPublicKey: string;
742
+ /** Resolved node URL (used both for RPC and for fetching the ML-KEM key) */
743
+ nodeUrl: string;
729
744
  /** Configuration version */
730
745
  version: string;
731
746
  }
@@ -738,10 +753,7 @@ interface ConfigValidationResult {
738
753
  }
739
754
 
740
755
  /**
741
- * Configuration loader for T3n SDK
742
- *
743
- * This module provides configuration validation utilities.
744
- * All environment keys are included in the bundle, and runtime selection is done via setEnvironment().
756
+ * Configuration validation for T3n SDK
745
757
  */
746
758
 
747
759
  /**
@@ -752,68 +764,49 @@ declare function validateConfig(config: unknown): ConfigValidationResult;
752
764
  /**
753
765
  * Configuration entry point for T3n SDK
754
766
  *
755
- * This module imports all environment keys as base64 strings. All keys are included
756
- * in the bundle, and the active key is selected at runtime via setEnvironment().
757
- *
758
- * Runtime key selection:
759
- * - All keys (local, staging, production, test) are included in the bundle
760
- * - Default environment is "production"
761
- * - Use setEnvironment(env) to change the active key at runtime
762
- *
763
- * Binary files are converted to base64 strings at build time by the rollup binary plugin.
764
- * In development mode (when running with tsx), files are read from the filesystem.
767
+ * The SDK no longer bundles ML-KEM root public keys. Instead, the active node
768
+ * URL is derived from the current environment (or an explicit override / the
769
+ * client's `baseUrl`), and the ML-KEM public key is fetched lazily from
770
+ * `${nodeUrl}/status` (`encaps_key` field) and cached per-URL.
765
771
  */
766
772
 
767
773
  /**
768
- * Set the active environment for key selection
769
- *
770
- * This function allows engineers to configure which ML-KEM public key
771
- * should be used at runtime. All keys are included in the bundle, so
772
- * switching environments doesn't require rebuilding.
773
- *
774
- * @param env - The environment identifier (local, staging, production, or test)
775
- * @throws Error if the environment is invalid
776
- *
777
- * @example
778
- * ```typescript
779
- * import { setEnvironment } from '@terminal3/t3n-sdk';
780
- *
781
- * // Use staging environment
782
- * setEnvironment('staging');
783
- * ```
774
+ * Default node URLs per environment. Override at runtime via `setNodeUrl()`
775
+ * or by passing `baseUrl` to `T3nClient`.
784
776
  */
785
- declare function setEnvironment(env: Environment): void;
777
+ declare const NODE_URLS: Record<Environment, string>;
786
778
  /**
787
- * Get the current environment identifier
788
- *
789
- * Returns the currently active environment, which defaults to "production".
790
- * Use setEnvironment() to change the active environment.
791
- *
792
- * @returns The current environment identifier
779
+ * Set the active environment. Clears any previous URL override and the key
780
+ * cache so the next fetch uses the new environment's default URL.
793
781
  */
782
+ declare function setEnvironment(env: Environment): void;
794
783
  declare function getEnvironment(): Environment;
784
+ declare function getEnvironmentName(): string;
795
785
  /**
796
- * Load configuration for the current environment
797
- *
798
- * Returns the SDK configuration with the ML-KEM public key for the
799
- * currently active environment (set via setEnvironment()).
786
+ * Override the node URL for the current process. Pass `null` to clear and
787
+ * fall back to the environment default.
800
788
  *
801
- * @returns A minimal SdkConfig object with the ML-KEM public key
789
+ * Always clears the per-URL key cache, including the `setNodeUrl(sameUrl)`
790
+ * case — that's the explicit "force a refresh after a node-side ML-KEM
791
+ * rotation" entry point. Keeping a no-op-call optimization here would
792
+ * silently defeat that contract; an extra fetch on a no-op call is cheap.
802
793
  */
803
- declare function loadConfig(): SdkConfig;
794
+ declare function setNodeUrl(url: string | null): void;
795
+ /** Resolve the active node URL: explicit `baseUrl` > override > env default. */
796
+ declare function getNodeUrl(baseUrl?: string): string;
804
797
  /**
805
- * Get the ML-KEM public key from the current configuration
806
- * This is the main entry point used by handlers
807
- *
808
- * @returns The base64-encoded ML-KEM public key for the current environment
798
+ * Fetch the ML-KEM root public key from `${nodeUrl}/status`. Cached per URL.
799
+ * The node must be in the `Ready` phase and expose `encaps_key`.
809
800
  */
810
- declare function getMlKemPublicKey(): string;
801
+ declare function fetchMlKemPublicKey(baseUrl?: string): Promise<string>;
802
+ /** Clear the cached ML-KEM public keys. Useful in tests. */
803
+ declare function clearKeyCache(): void;
811
804
  /**
812
- * Get the current environment identifier
813
- *
814
- * @returns The current environment name
805
+ * Return the resolved SDK configuration for the current environment.
806
+ * Note: this no longer includes the ML-KEM key — fetch it via
807
+ * `fetchMlKemPublicKey()`.
815
808
  */
816
- declare function getEnvironmentName(): string;
809
+ declare function loadConfig(baseUrl?: string): SdkConfig;
817
810
 
818
- export { AuthMethod, AuthenticationError, HandshakeError, HttpTransport, LogLevel, MockTransport, RpcError, SessionStateError, SessionStatus, T3nClient, T3nError, WasmError, bytesToString, createDefaultHandlers, createEthAuthInput, createLogger, createMlKemPublicKeyHandler, createOidcAuthInput, createRandomHandler, decodeWasmErrorMessage, eth_get_address, extractWasmError, generateRandomString, generateUUID, getEnvironment, getEnvironmentName, getGlobalLogLevel, getLogger, getMlKemPublicKey, getScriptVersion, loadConfig, loadWasmComponent, metamask_get_address, metamask_sign, redactSecrets, redactSecretsFromJson, setEnvironment, setGlobalLogLevel, stringToBytes, validateConfig };
811
+ export { AuthMethod, AuthenticationError, HandshakeError, HttpTransport, LogLevel, MockTransport, NODE_URLS, RpcError, SessionStateError, SessionStatus, T3nClient, T3nError, WasmError, bytesToString, clearKeyCache, createDefaultHandlers, createEthAuthInput, createLogger, createMlKemPublicKeyHandler, createOidcAuthInput, createRandomHandler, decodeWasmErrorMessage, eth_get_address, extractWasmError, fetchMlKemPublicKey, generateRandomString, generateUUID, getEnvironment, getEnvironmentName, getGlobalLogLevel, getLogger, getNodeUrl, getScriptVersion, loadConfig, loadWasmComponent, metamask_get_address, metamask_sign, redactSecrets, redactSecretsFromJson, setEnvironment, setGlobalLogLevel, setNodeUrl, stringToBytes, validateConfig };
819
812
  export type { AuthInput, ClientAuth, ClientHandshake, ConfigValidationResult, Did, Environment, EthAuthInput, GuestToHostHandler, GuestToHostHandlers, HandshakeResult, JsonRpcRequest, JsonRpcResponse, Logger, OidcAuthInput, OidcCredentials, SdkConfig, SessionCrypto, SessionId, T3nClientConfig, Transport, WasmComponent, WasmNextResult };