charms 13.0.0

Programmable assets on Bitcoin and beyond
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
pub mod app;
pub mod server;
pub mod spell;
pub mod tx;
pub mod util;
pub mod wallet;

use crate::{
    cli::{
        server::Server,
        spell::{Check, Prove, SpellCli},
        wallet::{List, WalletCli},
    },
    spell::{CharmsFee, MockProver, ProveSpellTx, ProveSpellTxImpl},
    utils,
    utils::{BoxedSP1Prover, block_on},
};
#[cfg(feature = "prover")]
use crate::{spell::Prover, utils::Shared};
use bitcoin::{Address, Network};
use charms_app_runner::AppRunner;
use charms_client::tx::Chain;
use charms_data::{App, check};
use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum};
use clap_complete::{CompleteEnv, Shell, generate};
use serde::Serialize;
use sp1_sdk::{CpuProver, NetworkProver, ProverClient, install::try_install_circuit_artifacts};
use std::{io, net::IpAddr, path::PathBuf, str::FromStr, sync::Arc};

#[derive(Parser)]
#[command(
    author,
    version,
    about,
    long_about = "Charms CLI: create, prove, and manage programmable assets (charms) on Bitcoin and Cardano using zero-knowledge proofs."
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Args)]
pub struct ServerConfig {
    /// IP address to listen on.
    #[arg(long, default_value = "0.0.0.0")]
    ip: IpAddr,

    /// Port to listen on.
    #[arg(long, default_value = "17784")]
    port: u16,
}

#[derive(Subcommand)]
pub enum Commands {
    /// Start the Charms REST API server (POST /spells/prove, GET /ready).
    Server(#[command(flatten)] ServerConfig),

    /// Create, verify, and prove spells (transaction metadata that defines charm operations).
    Spell {
        #[command(subcommand)]
        command: SpellCommands,
    },

    /// Inspect blockchain transactions for embedded spells.
    Tx {
        #[command(subcommand)]
        command: TxCommands,
    },

    /// Create, build, and inspect Charms apps (WebAssembly programs).
    App {
        #[command(subcommand)]
        command: AppCommands,
    },

    /// List UTXOs with charms in the connected wallet.
    Wallet {
        #[command(subcommand)]
        command: WalletCommands,
    },

    /// Generate shell completion scripts (static).
    ///
    /// For dynamic completions (recommended), run `charms completions --help`.
    #[command(after_long_help = "\
DYNAMIC COMPLETIONS (RECOMMENDED):

  Dynamic completions stay up-to-date automatically when charms is upgraded.

  Bash (add to ~/.bashrc):
    source <(COMPLETE=bash charms)

  Zsh (add to ~/.zshrc):
    source <(COMPLETE=zsh charms)

  Fish (add to ~/.config/fish/completions/charms.fish):
    COMPLETE=fish charms | source

  Elvish (add to ~/.elvish/rc.elv):
    eval (E:COMPLETE=elvish charms | slurp)

STATIC COMPLETIONS:

  Use this if you prefer pre-generated scripts or if dynamic completions
  don't work in your environment.

  Bash (add to ~/.bashrc):
    source <(charms completions bash)

  Zsh (add to ~/.zshrc):
    source <(charms completions zsh)

  Fish (run once):
    charms completions fish > ~/.config/fish/completions/charms.fish

  PowerShell (add to $PROFILE):
    charms completions powershell | Out-String | Invoke-Expression

  Elvish (add to ~/.elvish/rc.elv):
    eval (charms completions elvish | slurp)

After setup, restart your shell or source the config file.
Then type `charms <TAB>` to see available commands and options.")]
    Completions {
        /// Shell to generate completions for
        #[arg(value_enum)]
        shell: Shell,
    },

    /// Utility commands
    Util {
        #[command(subcommand)]
        command: UtilCommands,
    },
}

#[derive(Clone, Debug, ValueEnum)]
pub enum Output {
    #[value(name = "cbor")]
    Cbor,
    #[value(name = "json")]
    Json,
}

#[derive(Args)]
pub struct SpellProveParams {
    /// Path to the spell file (YAML or JSON).
    #[arg(long, default_value = "/dev/stdin")]
    spell: PathBuf,

    /// Output proving API payload instead of calling the API.
    #[arg(long, default_value = "false", hide_env = true)]
    payload: bool,

    /// Output format for payload (JSON or CBOR).
    #[arg(
        long,
        short = 'o',
        default_value = "json",
        value_enum,
        requires = "payload"
    )]
    output: Output,

    /// Path to the private inputs file (YAML or JSON).
    #[arg(long)]
    private_inputs: Option<PathBuf>,

    /// Beamed-from mapping as a YAML/JSON string (e.g. '{0: "txid:vout"}').
    #[arg(long)]
    beamed_from: Option<String>,

    /// Hex-encoded pre-requisite transactions.
    /// Must include the transactions that create the UTXOs spent by the spell.
    /// If the spell has reference UTXOs, transactions creating them must also be included.
    #[arg(long)]
    prev_txs: Vec<String>,

    /// Paths to app Wasm binaries (.wasm files).
    #[arg(long)]
    app_bins: Vec<PathBuf>,

    /// Bitcoin or Cardano address to send the change to.
    #[arg(long)]
    change_address: String,

    /// Fee rate in sats/vB (Bitcoin only).
    #[arg(long, default_value = "2.0")]
    fee_rate: f64,

    /// Target chain.
    #[arg(long, default_value = "bitcoin")]
    chain: Chain,

    /// Use mock mode (skip proof generation).
    #[arg(long, default_value = "false", hide_env = true)]
    mock: bool,

    /// Collateral UTXO for Cardano transactions, as txid:vout (required for --chain cardano).
    #[arg(long, alias = "collateral")]
    collateral_utxo: Option<String>,
}

#[derive(Args)]
pub struct SpellCheckParams {
    /// Path to the spell file (YAML or JSON).
    #[arg(long, default_value = "/dev/stdin")]
    spell: PathBuf,

    /// Path to the private inputs file (YAML or JSON).
    #[arg(long)]
    private_inputs: Option<PathBuf>,

    /// Beamed-from mapping as a YAML/JSON string (e.g. '{0: "txid:vout"}').
    #[arg(long)]
    beamed_from: Option<String>,

    /// Paths to app Wasm binaries (.wasm files).
    #[arg(long)]
    app_bins: Vec<PathBuf>,

    /// Hex-encoded pre-requisite transactions.
    /// Must include the transactions that create the UTXOs spent by the spell.
    /// If the spell has reference UTXOs, transactions creating them must also be included.
    #[arg(long)]
    prev_txs: Option<Vec<String>>,

    /// Target chain.
    #[arg(long, default_value = "bitcoin")]
    chain: Chain,

    /// Use mock mode (skip proof generation).
    #[arg(long, default_value = "false", hide_env = true)]
    mock: bool,
}

#[derive(Args)]
pub struct SpellVkParams {
    /// Use mock mode (show mock verification key).
    #[arg(long, default_value = "false", hide_env = true)]
    mock: bool,
}

const SPELL_DATA_HELP: &str = "\
DATA STRUCTURES:

  Spell file (--spell):
    version: 11                     # protocol version
    tx:
      ins:                          # input UTXOs (txid:vout)
        - deadbeef...:0
      outs:                         # output charms (app_index: value)
        - 0: ~                      # output 0: app 0 has no data
        - 1: 4000000                # output 1: app 1 = 4000000
          2: 10000000               #            app 2 = 10000000
      beamed_outs:                  # (optional) beamed output index -> dest hash
        1: 009fb489...
      coins:                        # native coin outputs
        - amount: 4000000           #   amount in lovelace (Cardano) or sats (Bitcoin)
          dest: 716fc738...         #   hex-encoded destination (use `charms util dest`)
          content:                  #   (optional, Cardano) native tokens
            multiasset:
              <policy_id_hex>:
                <asset_name_hex>: <quantity>
    app_public_inputs:              # map of app -> public input data
      t/<identity_hex>/<vk_hex>:    #   token app (t), NFT (n), or contract (c)
      c/0000...0000/<vk_hex>:       #   value can be null or app-specific data

  Private inputs file (--private-inputs):
    t/<identity_hex>/<vk_hex>: <app-specific data>
    c/0000...0000/<vk_hex>: <app-specific data>

  Previous transactions (--prev-txs):
    Each value is one of:
      - raw hex (auto-detected as Bitcoin or Cardano)
      - YAML-tagged: '!bitcoin <hex>' or '!cardano <hex>'
      - YAML-tagged with finality proof:
          '!cardano {tx: <hex>, signature: <hex>}'
      - JSON: '{\"bitcoin\": \"<hex>\"}' or '{\"cardano\": \"<hex>\"}'

  Beamed-from mapping (--beamed-from):
    YAML/JSON mapping: input_index -> [source_utxo, nonce]
    Example: '{0: [712fcb00...f66c:1, 4538918914141394474]}'
";

#[derive(Subcommand)]
pub enum SpellCommands {
    /// Check spell correctness by running app contracts locally (no proof generation).
    #[command(after_long_help = SPELL_DATA_HELP)]
    Check(#[command(flatten)] SpellCheckParams),
    /// Prove spell correctness and build a ready-to-broadcast transaction.
    ///
    /// Outputs a JSON array of hex-encoded transactions (Bitcoin)
    /// or a Ledger CDDL JSON envelope (Cardano).
    #[command(after_long_help = SPELL_DATA_HELP)]
    Prove(#[command(flatten)] SpellProveParams),
    /// Print the current protocol version and spell verification key (VK) as JSON to stdout.
    Vk(#[command(flatten)] SpellVkParams),
}

#[derive(Args)]
pub struct ShowSpellParams {
    /// Target chain.
    #[arg(long, default_value = "bitcoin")]
    chain: Chain,

    /// Hex-encoded transaction.
    #[arg(long)]
    tx: String,

    /// Output in JSON format (default is YAML).
    #[arg(long)]
    json: bool,

    /// Use mock mode (accept mock proofs).
    #[arg(long, default_value = "false", hide_env = true)]
    mock: bool,
}

#[derive(Subcommand)]
pub enum TxCommands {
    /// Extract and display the spell from a transaction.
    ///
    /// Prints the spell as YAML (default) or JSON if the transaction contains a valid proof.
    ShowSpell(#[command(flatten)] ShowSpellParams),
}

#[derive(Subcommand)]
pub enum AppCommands {
    /// Create a new app from template.
    New {
        /// Name of the app. A directory with this name will be created.
        name: String,
    },

    /// Build the app to WebAssembly (wasm32-wasip1).
    ///
    /// Prints the path to the built .wasm binary to stdout.
    Build,

    /// Show verification key (SHA-256 of Wasm binary) for an app.
    ///
    /// Prints the hex-encoded VK to stdout.
    Vk {
        /// Path to app Wasm binary (builds the app if omitted).
        path: Option<PathBuf>,
    },
}

#[derive(Subcommand)]
pub enum WalletCommands {
    /// List outputs with charms in the user's wallet.
    ///
    /// Outputs YAML (default) or JSON. Requires `bitcoin-cli` to be available.
    List(#[command(flatten)] WalletListParams),
}

#[derive(Args)]
pub struct WalletListParams {
    /// Output in JSON format (default is YAML).
    #[arg(long)]
    json: bool,

    /// Use mock mode (accept mock proofs).
    #[arg(long, default_value = "false", hide_env = true)]
    mock: bool,
}

#[derive(Args)]
pub struct DestParams {
    /// Bitcoin or Cardano address to convert.
    #[arg(long)]
    addr: Option<String>,

    /// Charms apps for proxy script address (format: tag/identity_hex/vk_hex).
    #[arg(long)]
    apps: Vec<App>,

    /// Target chain (auto-detected from address if omitted).
    #[arg(long)]
    chain: Option<Chain>,
}

#[derive(Subcommand)]
pub enum UtilCommands {
    /// Install circuit files.
    #[clap(hide = true)]
    InstallCircuitFiles,

    /// Print hex-encoded `dest` value for use in spell YAML.
    ///
    /// Accepts either --addr (Bitcoin/Cardano address) or --apps (Cardano only).
    /// Prints the hex-encoded destination bytes to stdout.
    Dest(#[command(flatten)] DestParams),
}

pub async fn run() -> anyhow::Result<()> {
    utils::logger::setup_logger();

    CompleteEnv::with_factory(Cli::command).complete();
    let cli = Cli::parse();

    match cli.command {
        Commands::Server(server_config) => {
            let server = server(server_config);
            server.serve().await
        }
        Commands::Spell { command } => {
            let spell_cli = spell_cli();
            match command {
                SpellCommands::Check(params) => spell_cli.check(params),
                SpellCommands::Prove(params) => spell_cli.prove(params).await,
                SpellCommands::Vk(params) => spell_cli.print_vk(params.mock),
            }
        }
        Commands::Tx { command } => match command {
            TxCommands::ShowSpell(params) => tx::tx_show_spell(params),
        },
        Commands::App { command } => match command {
            AppCommands::New { name } => app::new(&name),
            AppCommands::Vk { path } => app::vk(path),
            AppCommands::Build => app::build(),
        },
        Commands::Wallet { command } => {
            let wallet_cli = wallet_cli();
            match command {
                WalletCommands::List(params) => wallet_cli.list(params),
            }
        }
        Commands::Completions { shell } => generate_completions(shell),
        Commands::Util { command } => match command {
            UtilCommands::InstallCircuitFiles => {
                let _ = try_install_circuit_artifacts("groth16");
                Ok(())
            }
            UtilCommands::Dest(params) => util::dest(params),
        },
    }
}

fn server(server_config: ServerConfig) -> Server {
    let prover = ProveSpellTxImpl::new(false);
    Server::new(server_config, prover)
}

pub fn prove_impl(mock: bool) -> Box<dyn crate::spell::Prove> {
    tracing::debug!(mock);
    #[cfg(feature = "prover")]
    match mock {
        false => {
            let app_prover = Arc::new(crate::app::Prover {
                sp1_client: Arc::new(Shared::new(crate::cli::app_sp1_client)),
                runner: AppRunner::new(false),
            });
            let spell_sp1_client = crate::cli::spell_sp1_client(&app_prover.sp1_client);
            Box::new(Prover::new(app_prover, spell_sp1_client))
        }
        true => Box::new(MockProver {
            spell_prover_client: Arc::new(utils::Shared::new(|| Box::new(sp1_cpu_prover()))),
        }),
    }
    #[cfg(not(feature = "prover"))]
    {
        Box::new(MockProver {
            spell_prover_client: Arc::new(utils::Shared::new(|| Box::new(sp1_cpu_prover()))),
        })
    }
}

pub(crate) fn charms_fee_settings() -> Option<CharmsFee> {
    let fee_settings_file = std::env::var("CHARMS_FEE_SETTINGS").ok()?;
    let fee_settings: CharmsFee = serde_yaml::from_reader(
        &std::fs::File::open(fee_settings_file)
            .expect("should be able to open the fee settings file"),
    )
    .expect("should be able to parse the fee settings file");

    assert!(
        fee_settings.fee_addresses[&Chain::Bitcoin]
            .iter()
            .all(|(network, address)| {
                let network = Network::from_core_arg(network)
                    .expect("network should be a valid `bitcoind -chain` argument");
                check!(
                    Address::from_str(address)
                        .is_ok_and(|address| address.is_valid_for_network(network))
                );
                true
            }),
        "a fee address is not valid for the specified network"
    );

    Some(fee_settings)
}

fn spell_cli() -> SpellCli {
    let spell_cli = SpellCli {
        app_runner: AppRunner::new(true),
    };
    spell_cli
}

#[cfg(feature = "prover")]
fn app_sp1_client() -> BoxedSP1Prover {
    let name = std::env::var("APP_SP1_PROVER").unwrap_or_default();
    sp1_named_env_client(name.as_str())
}

#[cfg(feature = "prover")]
fn spell_sp1_client(app_sp1_client: &Arc<Shared<BoxedSP1Prover>>) -> Arc<Shared<BoxedSP1Prover>> {
    let name = std::env::var("SPELL_SP1_PROVER").unwrap_or_default();
    match name.as_str() {
        "app" => app_sp1_client.clone(),
        "network" => Arc::new(Shared::new(sp1_network_client)),
        _ => unreachable!("Only 'app' or 'network' are supported as SPELL_SP1_PROVER values"),
    }
}

#[tracing::instrument(level = "info")]
pub fn sp1_cpu_prover() -> CpuProver {
    block_on(ProverClient::builder().cpu().build())
}

#[tracing::instrument(level = "info")]
pub fn sp1_network_prover() -> NetworkProver {
    block_on(ProverClient::builder().network().build())
}

#[tracing::instrument(level = "info")]
pub fn sp1_network_client() -> BoxedSP1Prover {
    sp1_named_env_client("network")
}

#[tracing::instrument(level = "debug")]
fn sp1_named_env_client(name: &str) -> BoxedSP1Prover {
    let sp1_prover_env_var = std::env::var("SP1_PROVER").unwrap_or_default();
    let name = match name {
        "env" => sp1_prover_env_var.as_str(),
        _ => name,
    };
    match name {
        #[cfg(feature = "prover")]
        "cpu" => Box::new(sp1_cpu_prover()),
        "network" => Box::new(sp1_network_prover()),
        _ => unimplemented!("only 'cuda', 'cpu' and 'network' are supported as prover values"),
    }
}

fn wallet_cli() -> WalletCli {
    let wallet_cli = WalletCli {};
    wallet_cli
}

fn generate_completions(shell: Shell) -> anyhow::Result<()> {
    let cmd = &mut Cli::command();
    generate(shell, cmd, cmd.get_name().to_string(), &mut io::stdout());
    Ok(())
}

fn print_output<T: Serialize>(output: &T, json: bool) -> anyhow::Result<()> {
    match json {
        true => serde_json::to_writer_pretty(io::stdout(), &output)?,
        false => serde_yaml::to_writer(io::stdout(), &output)?,
    };
    Ok(())
}

#[cfg(test)]
mod test {
    #[test]
    fn dummy() {}
}