diff --git a/sdk/src/browser.ts b/sdk/src/browser.ts index 42e8c1b34..82a5fbe3e 100644 --- a/sdk/src/browser.ts +++ b/sdk/src/browser.ts @@ -114,6 +114,8 @@ export { VerifyingKey, ViewKey, initThreadPool, + initDevMode, + maybeInitDevMode, getOrInitConsensusVersionTestHeights, verifyFunctionExecution, } from "./wasm.js"; diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.ts index 2762cf701..ef2e6c21c 100644 --- a/sdk/src/wasm.ts +++ b/sdk/src/wasm.ts @@ -1,3 +1,7 @@ +import { + initDevMode as initDevModeWasm, +} from "@provablehq/wasm/%%NETWORK%%.js"; + export { Address, Authorization, @@ -51,3 +55,50 @@ export { getOrInitConsensusVersionTestHeights, verifyFunctionExecution, } from "@provablehq/wasm/%%NETWORK%%.js"; + +/** + * Initialize development mode by setting consensus version heights to [0..ConsensusVersion::latest()]. + * + * This function automatically sets up development consensus heights without requiring + * manual specification of the number of consensus versions. It should be called before + * initializing the thread pool when working with a local development network. + * + * @returns An array of block heights at which each consensus version applies. + * + * @example + * import { initDevMode, initThreadPool } from "@provablehq/sdk"; + * + * // Initialize dev mode before the thread pool + * initDevMode(); + * await initThreadPool(); + */ +export function initDevMode(): number[] { + return initDevModeWasm(); +} + +/** + * Check if the ALEO_NETWORK environment variable is set to "local" and if so, + * automatically call initDevMode() to set up development consensus heights. + * + * This function is useful for Node.js applications that want to automatically + * detect when running against a local development network. + * + * @returns An array of block heights if dev mode was initialized, undefined otherwise. + * + * @example + * import { maybeInitDevMode, initThreadPool } from "@provablehq/sdk"; + * + * // Automatically initialize dev mode if ALEO_NETWORK=local + * maybeInitDevMode(); + * await initThreadPool(); + */ +export function maybeInitDevMode(): number[] | undefined { + try { + if (typeof process !== 'undefined' && process.env?.ALEO_NETWORK === 'local') { + return initDevModeWasm(); + } + } catch { + // Ignore errors when process is not available (e.g., in browsers) + } + return undefined; +} diff --git a/wasm/src/utilities/mod.rs b/wasm/src/utilities/mod.rs index e10322a32..fa6db205c 100644 --- a/wasm/src/utilities/mod.rs +++ b/wasm/src/utilities/mod.rs @@ -53,6 +53,34 @@ pub fn get_or_init_consensus_version_heights(heights: Option) -> js_sys: pairs.iter().map(|(_, height)| wasm_bindgen::JsValue::from_f64(*height as f64)).collect::() } +/// Initialize development mode by setting consensus version heights to [0..ConsensusVersion::latest()]. +/// +/// This function automatically sets up development consensus heights without requiring +/// manual specification of the number of consensus versions. It should be called before +/// initializing the thread pool when working with a local development network. +/// +/// @returns {Array} An array of block heights at which each consensus version applies. +/// +/// @example +/// import { initDevMode, initThreadPool } from "@provablehq/sdk"; +/// +/// // Initialize dev mode before the thread pool +/// initDevMode(); +/// await initThreadPool(); +/// // Rest of the code... +#[wasm_bindgen::prelude::wasm_bindgen(js_name = initDevMode)] +pub fn init_dev_mode() -> js_sys::Array { + // Get the latest consensus version index + let latest = snarkvm_console::network::ConsensusVersion::latest(); + let num_versions = latest as u32 + 1; + + // Create a comma-separated string of heights from 0 to num_versions-1 + let heights: String = (0..num_versions).map(|i| i.to_string()).collect::>().join(","); + + // Initialize the consensus version heights + get_or_init_consensus_version_heights(Some(heights)) +} + #[cfg(test)] mod tests { use super::*;