moonproof-wallet 0.2.2

Native Rust wallet for the Midnight blockchain — HD derivation, coin balancing, Zswap/unshielded/dust transacting
Documentation
//! Built-in [`NetworkConfig`] presets for the named Midnight networks.
//!
//! Single source of truth for network endpoints across moonproof —
//! CLI's `[networks.*]` defaults, the Flutter SDK's `list_network_presets`
//! bridge fn, and any future host (`UniFFI` etc.) all read from here.
//!
//! Endpoints follow docs.midnight.network's networks guide and
//! `midnight-examples/toolchain.json` (November 2026):
//! - `preview` is Midnight-hosted and needs no credential.
//! - `preprod` and `mainnet` are served by Blockfrost since the
//!   Midnight-hosted endpoints were decommissioned (October/November
//!   2026); every request needs a Blockfrost project id, supplied at
//!   resolution time (see
//!   [`super::network::NetworkConfig::authenticated_from_env`]).
//! - The proof-server URL is never public; always defaults to
//!   `http://127.0.0.1:6300` and is overridable per-host.

use crate::abstractions::NetworkId;

use super::network::NetworkConfig;

/// Default proof-server URL — local in-process is the production
/// expectation; remote proof servers are an opt-in override.
pub const DEFAULT_LOCAL_PROOF_SERVER: &str = "http://127.0.0.1:6300";

/// Names of every built-in preset, in the order surfaced to the UI
/// (least → most production).
///
/// Endpoint URLs verified against Lace wallet's
/// `packages/contract/midnight-context/src/const.ts::DEFAULT_NETWORKS_CONFIG`
/// (the authoritative wallet that ships Midnight to end users).
///
/// `devnet` and `testnet` are NOT presets — the Lace source marks
/// them as "Copy of NetworkId.NetworkId.Undeployed added for type
/// safety" and the dapp-connector explicitly filters them out:
/// `MidnightSDKNetworkId.filter(id => id !== 'devnet' && id !== 'testnet')`.
/// `qanet` is an IOG-internal QA deployment and is omitted by default.
/// Users wanting a non-default network add `[networks.NAME]` to their
/// `moonproof.toml`.
pub const PRESET_NAMES: &[&str] = &[
    "localdev",
    "localdev-emulator",
    "preview",
    "preprod",
    "mainnet",
];

/// Look up a preset by name. Names match `NetworkId` strings except
/// for `localdev` (which carries `NetworkId::Undeployed`).
#[must_use]
pub fn preset_by_name(name: &str) -> Option<NetworkConfig> {
    Some(match name {
        "localdev" | "undeployed" => localdev(),
        "localdev-emulator" => localdev_android_emulator(),
        "preview" => preview(),
        "preprod" => preprod(),
        "mainnet" => mainnet(),
        _ => return None,
    })
}

/// All built-in presets, in the order returned by [`PRESET_NAMES`].
#[must_use]
pub fn all_presets() -> Vec<NetworkConfig> {
    PRESET_NAMES
        .iter()
        .filter_map(|n| preset_by_name(n))
        .collect()
}

/// Local-dev harness — `moonproof dev start` brings up a node +
/// indexer + proof-server at these ports.
#[must_use]
pub fn localdev() -> NetworkConfig {
    NetworkConfig {
        name: "localdev".to_owned(),
        indexer_http: "http://127.0.0.1:8088/api/v4/graphql".to_owned(),
        node_ws: "ws://127.0.0.1:9944".to_owned(),
        proof_server_url: Some(DEFAULT_LOCAL_PROOF_SERVER.to_owned()),
        proof_server_remote_url: None,
        network_id: NetworkId::Undeployed,
        chain_kind: "localdev".to_owned(),
    }
}

/// Local-dev harness reachable from inside the Android emulator.
/// The emulator's `127.0.0.1` is the emulator itself; the host's
/// loopback is `10.0.2.2`. Same ports as [`localdev`], same chain.
/// Pick this preset when running the wallet on an Android emulator
/// and the harness is up on the developer's host.
#[must_use]
pub fn localdev_android_emulator() -> NetworkConfig {
    NetworkConfig {
        name: "localdev-emulator".to_owned(),
        indexer_http: "http://10.0.2.2:8088/api/v4/graphql".to_owned(),
        node_ws: "ws://10.0.2.2:9944".to_owned(),
        proof_server_url: Some("http://10.0.2.2:6300".to_owned()),
        proof_server_remote_url: None,
        network_id: NetworkId::Undeployed,
        chain_kind: "localdev".to_owned(),
    }
}

#[must_use]
pub fn preview() -> NetworkConfig {
    NetworkConfig {
        name: "preview".to_owned(),
        indexer_http: "https://indexer.preview.midnight.network/api/v4/graphql".to_owned(),
        node_ws: "wss://rpc.preview.midnight.network".to_owned(),
        proof_server_url: Some(DEFAULT_LOCAL_PROOF_SERVER.to_owned()),
        proof_server_remote_url: Some("https://proof-server.preview.midnight.network".to_owned()),
        network_id: NetworkId::Preview,
        chain_kind: "preview".to_owned(),
    }
}

#[must_use]
pub fn preprod() -> NetworkConfig {
    NetworkConfig {
        name: "preprod".to_owned(),
        indexer_http: "https://midnight-preprod.blockfrost.io/api/v0".to_owned(),
        node_ws: "wss://rpc.midnight-preprod.blockfrost.io".to_owned(),
        proof_server_url: Some(DEFAULT_LOCAL_PROOF_SERVER.to_owned()),
        proof_server_remote_url: Some("https://proof-server.preprod.midnight.network".to_owned()),
        network_id: NetworkId::PreProd,
        chain_kind: "preprod".to_owned(),
    }
}

/// `mainnet` — production Midnight network, served by Blockfrost.
///
/// There is **no remote proof server** for mainnet; users run a local
/// proof-server at `127.0.0.1:6300` or override per `moonproof.toml`.
#[must_use]
pub fn mainnet() -> NetworkConfig {
    NetworkConfig {
        name: "mainnet".to_owned(),
        indexer_http: "https://midnight-mainnet.blockfrost.io/api/v0".to_owned(),
        node_ws: "wss://rpc.midnight-mainnet.blockfrost.io".to_owned(),
        proof_server_url: Some(DEFAULT_LOCAL_PROOF_SERVER.to_owned()),
        proof_server_remote_url: None,
        network_id: NetworkId::MainNet,
        chain_kind: "mainnet".to_owned(),
    }
}

/// Faucet URL for `name`, when one is published.
///
/// `preview` and `preprod` both have public faucets that follow the
/// `faucet.<NETWORK>.midnight.network/api/drips` pattern. The endpoint
/// requires an `X-Captcha-Token` header (Cloudflare Turnstile) which
/// the moonproof CLI cannot solve programmatically — the
/// `moonproof faucet <ADDRESS>` flow prints the
/// human-solve URL and accepts a `--captcha <TOKEN>` argument once the
/// operator has the token. `mainnet` carries real funds and has no
/// faucet. `localdev` runs against the local-dev harness genesis
/// allocation.
#[must_use]
pub fn faucet_url(name: &str) -> Option<String> {
    let host = match name {
        "preview" => "faucet.preview.midnight.network",
        "preprod" => "faucet.preprod.midnight.network",
        _ => return None,
    };
    Some(format!("https://{host}/api/drips"))
}

/// Browser URL of the captcha-protected faucet UI for `name`, when one
/// is published. CLI `moonproof faucet` prints this so the
/// operator can copy the Turnstile token out of the page.
#[must_use]
pub fn faucet_browser_url(name: &str) -> Option<String> {
    let host = match name {
        "preview" => "faucet.preview.midnight.network",
        "preprod" => "faucet.preprod.midnight.network",
        _ => return None,
    };
    Some(format!("https://{host}/"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn every_preset_name_resolves() {
        for name in PRESET_NAMES {
            assert!(preset_by_name(name).is_some(), "preset `{name}` missing");
        }
    }

    #[test]
    fn all_presets_returns_full_list() {
        assert_eq!(all_presets().len(), PRESET_NAMES.len());
    }

    #[test]
    fn localdev_resolves_via_undeployed_alias() {
        let lhs = preset_by_name("localdev").unwrap();
        let rhs = preset_by_name("undeployed").unwrap();
        assert_eq!(lhs.indexer_http, rhs.indexer_http);
        assert_eq!(lhs.node_ws, rhs.node_ws);
    }

    #[test]
    fn preview_and_preprod_have_faucets() {
        assert!(faucet_url("preview").is_some());
        assert!(faucet_url("preprod").is_some());
    }

    #[test]
    fn mainnet_and_localdev_have_no_faucet() {
        assert!(faucet_url("mainnet").is_none());
        assert!(faucet_url("localdev").is_none());
    }
}