moonproof-contracts 0.2.2

Node-side host orchestration for the moonproof contract pipeline — providers, deploy/call/maintenance lifecycle, fee balancing, submission, indexer-driven sync
Documentation
//! Debug test: deserialize the on-chain mint tx hex from
//! `/tmp/mint_tx.hex` and try to decrypt every output's ciphertext
//! with the genesis wallet's `ZswapSecretKeys`.
//!
//! This nails down whether the wallet-relevance check the indexer's
//! `wallet-indexer` service performs WOULD succeed for a moonproof-
//! produced mint tx — independent of whether the live wallet-indexer
//! service has actually run the scan yet.
//!
//! - Decryption succeeds → moonproof's pipeline is correct; the
//!   harness wallet-indexer was just stale during the live test.
//! - Decryption fails → there's a real bug in our encryption path
//!   (probably a wire-format mismatch between encryption written by
//!   our prover and consumed by the indexer).
//!
//! Gated `#[ignore]` because it depends on a hex blob captured from a
//! prior live-harness run. Re-capture with:
//!
//! ```bash
//! curl -s -X POST http://127.0.0.1:8088/api/v4/graphql \
//!   -H "Content-Type: application/json" \
//!   -d "{\"query\":\"query { transactions(offset: { hash: \\\"<hash>\\\" }) { ... on RegularTransaction { raw } } }\"}" \
//!   | python3 -c "import json,sys; d=json.load(sys.stdin); print(d['data']['transactions'][0]['raw'])" \
//!   > /tmp/mint_tx.hex
//! ```

use std::fs;

use moonproof_ledger::ledger::structure::{ProofMarker, Transaction};
use moonproof_ledger::storage::db::InMemoryDB;
use moonproof_wallet::abstractions::NetworkId;
use moonproof_wallet::Wallet;

#[tokio::test]
#[ignore = "requires /tmp/mint_tx.hex captured from a live-harness run"]
async fn decrypt_onchain_mint_with_genesis_wallet_keys() {
    let hex_str = fs::read_to_string("/tmp/mint_tx.hex")
        .expect("read /tmp/mint_tx.hex — see module docs for capture command");
    let hex_str = hex_str.trim();
    let bytes = hex::decode(hex_str).expect("hex decode tx bytes");
    eprintln!("on-chain tx: {} bytes", bytes.len());

    // The on-chain tx uses ProofMarker (final proven form) +
    // PureGeneratorPedersen (sealed binding commitment).
    let tx: Transaction<
        moonproof_ledger::signatures::Signature,
        ProofMarker,
        moonproof_ledger::transient_crypto::commitment::PureGeneratorPedersen,
        InMemoryDB,
    > = moonproof_ledger::serialize::tagged_deserialize(&mut bytes.as_slice())
        .expect("deserialize on-chain mint tx");

    // Genesis wallet's ZswapSecretKeys — use the same BIP44 path
    // the live test uses (m/44'/2400'/0'/3/0 from the genesis hex
    // seed), NOT the raw seed bytes. These are different keypairs.
    let sender =
        Wallet::genesis(NetworkId::Undeployed).expect("derive genesis wallet from hex seed");
    let zswap_keys = sender.zswap_secret_keys();

    let stx = match &tx {
        Transaction::Standard(s) => s,
        Transaction::ClaimRewards(_) => {
            panic!("expected Standard tx");
        }
    };

    let mut total_outputs = 0usize;
    let mut decrypted_outputs = 0usize;

    if let Some(offer_sp) = &stx.guaranteed_coins {
        let offer = &**offer_sp;
        for output_sp in offer.outputs.iter() {
            total_outputs += 1;
            let output = &*output_sp;
            if let Some(ciph_sp) = &output.ciphertext {
                let ledger_ciph: moonproof_ledger::transient_crypto::encryption::Ciphertext =
                    (**ciph_sp).clone().into();
                let decrypted: Option<moonproof_ledger::coin_structure::coin::Info> =
                    zswap_keys.encryption_secret_key.decrypt(&ledger_ciph);
                if let Some(coin) = decrypted {
                    decrypted_outputs += 1;
                    eprintln!(
                        "  decrypted output: nonce={:?} type={:?} value={}",
                        coin.nonce, coin.type_, coin.value
                    );
                } else {
                    eprintln!("  output ciphertext present but did NOT decrypt with genesis keys");
                }
            } else {
                eprintln!("  output has no ciphertext (contract-owned)");
            }
        }
    }
    for entry in stx.fallible_coins.iter() {
        let pair = &*entry;
        let offer = &*pair.1;
        for output_sp in offer.outputs.iter() {
            total_outputs += 1;
            let output = &*output_sp;
            if let Some(ciph_sp) = &output.ciphertext {
                let ledger_ciph: moonproof_ledger::transient_crypto::encryption::Ciphertext =
                    (**ciph_sp).clone().into();
                let decrypted: Option<moonproof_ledger::coin_structure::coin::Info> =
                    zswap_keys.encryption_secret_key.decrypt(&ledger_ciph);
                if decrypted.is_some() {
                    decrypted_outputs += 1;
                }
            }
        }
    }

    eprintln!("total outputs: {total_outputs}, decryptable by genesis wallet: {decrypted_outputs}");
    assert!(
        decrypted_outputs > 0,
        "at least one output must decrypt with the genesis wallet's keys — \
         the on-chain mint tx was supposed to address a coin to that wallet. \
         If this fails, the bug is in the encryption pipeline; if this passes \
         but the live wallet-indexer doesn't surface relevance, the harness \
         wallet-indexer is stale."
    );
}