casper-cli 0.2.1

CLI wallet manager for Casper: create/recover wallets, derive accounts, and deploy smart contracts.
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
use anyhow::{Context, Result, anyhow, bail};
use clap::{Args, Subcommand};
use comfy_table::{Cell, Table};
use dialoguer::{Input, Select, theme::ColorfulTheme};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs;
use std::io::{self, IsTerminal};
use std::path::{Path, PathBuf};

const CONFIG_FILE_NAME: &str = "config.toml";

#[derive(Args)]
/// Network-related CLI entry point.
pub struct NetworkArgs {
    #[command(subcommand)]
    command: NetworkCommand,
}

#[derive(Subcommand)]
/// Network subcommands.
pub enum NetworkCommand {
    /// Select the active network.
    Use(NetworkUseArgs),
    /// List configured networks and the active one.
    List,
}

#[derive(Args)]
/// Arguments for selecting a network.
pub struct NetworkUseArgs {
    /// Name of the network (key or chain name).
    name: String,
}

#[derive(Serialize, Deserialize)]
struct AppConfig {
    #[serde(default)]
    active: Option<String>,
    #[serde(default)]
    networks: BTreeMap<String, NetworkEntry>,
    #[serde(default)]
    storage: Option<StorageSection>,
}

#[derive(Serialize, Deserialize, Clone)]
struct NetworkEntry {
    chain_name: String,
    rest: String,
    sse: String,
    rpc: String,
    binary_port: String,
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
enum StorageSection {
    File { root_path: String },
    Keyring,
}

#[derive(Clone, Debug)]
pub(crate) enum StorageOverride {
    Keyring,
    File { root_path: String },
}

#[derive(Clone, Debug, Default)]
pub(crate) struct ConfigInitOptions {
    pub(crate) no_interactive: bool,
    pub(crate) storage_override: Option<StorageOverride>,
}

pub(crate) struct ConfigContext {
    path: PathBuf,
    options: ConfigInitOptions,
}

impl ConfigContext {
    pub(crate) fn new(path: PathBuf, options: ConfigInitOptions) -> Self {
        Self { path, options }
    }

    pub(crate) fn path(&self) -> &Path {
        &self.path
    }

    pub(crate) fn options(&self) -> &ConfigInitOptions {
        &self.options
    }
}

pub fn handle(context: &ConfigContext, args: NetworkArgs) -> Result<()> {
    match args.command {
        NetworkCommand::Use(command) => network_use(context, command),
        NetworkCommand::List => network_list(context),
    }
}

fn network_use(context: &ConfigContext, args: NetworkUseArgs) -> Result<()> {
    let config_path = context.path();
    let mut config = load_or_init_config_with_options(config_path, context.options())?;
    let key = resolve_network_key(&config, &args.name)?;
    let entry = config
        .networks
        .get(&key)
        .cloned()
        .ok_or_else(|| anyhow!("network '{key}' not found"))?;
    config.active = Some(key.clone());
    save_config(config_path, &config)?;
    println!("Active network: {key}");
    println!("Chain name: {}", entry.chain_name);
    println!("REST: {}", entry.rest);
    println!("SSE: {}", entry.sse);
    println!("RPC: {}", entry.rpc);
    println!("Binary port: {}", entry.binary_port);
    Ok(())
}

fn network_list(context: &ConfigContext) -> Result<()> {
    let config_path = context.path();
    let config = load_or_init_config_with_options(config_path, context.options())?;

    if config.networks.is_empty() {
        println!("No networks configured.");
        return Ok(());
    }

    let active = config.active.as_deref();
    let mut table = Table::new();
    table.set_header(vec![
        "Name",
        "Chain",
        "Active",
        "REST",
        "SSE",
        "RPC",
        "Binary port",
    ]);
    for (name, entry) in config.networks {
        let is_active = active == Some(name.as_str());
        table.add_row(vec![
            Cell::new(&name),
            Cell::new(&entry.chain_name),
            Cell::new(if is_active { "yes" } else { "" }),
            Cell::new(&entry.rest),
            Cell::new(&entry.sse),
            Cell::new(&entry.rpc),
            Cell::new(&entry.binary_port),
        ]);
    }

    println!("{table}");
    Ok(())
}

fn resolve_network_key(config: &AppConfig, name: &str) -> Result<String> {
    if config.networks.contains_key(name) {
        return Ok(name.to_string());
    }

    let mut matches = config
        .networks
        .iter()
        .filter(|(_key, entry)| entry.chain_name == name)
        .collect::<Vec<_>>();

    if matches.is_empty() {
        let available = config
            .networks
            .keys()
            .cloned()
            .collect::<Vec<_>>()
            .join(", ");
        bail!("unknown network '{name}'. Available: {available}");
    }

    if matches.len() > 1 {
        bail!("ambiguous chain name '{name}'. Use a network key instead.");
    }

    let (key, _entry) = matches.remove(0);
    Ok(key.clone())
}

fn load_or_init_config_with_options(path: &Path, options: &ConfigInitOptions) -> Result<AppConfig> {
    if !path.exists() {
        println!("Missing config file. Initializing new one");
        let override_storage = options
            .storage_override
            .as_ref()
            .map(storage_section_from_override)
            .transpose()?;
        let config = if let Some(storage) = override_storage {
            config_with_storage(storage)
        } else if options.no_interactive {
            bail!(
                "--no-interactive requires --keyring or --file-storage <ROOT_PATH> when config.toml is missing"
            );
        } else if io::stdin().is_terminal() && io::stdout().is_terminal() {
            prompt_default_config()?
        } else {
            println!("No interactive terminal detected; using default storage settings.");
            default_config()?
        };
        let preview = toml::to_string_pretty(&config)?;
        println!("\nGenerated config.toml:\n{preview}");
        println!("Run `casper-cli config edit` to open an editor and you can modify it.");
        save_config(path, &config)?;
        return Ok(config);
    }
    let data =
        fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?;
    let mut config: AppConfig =
        toml::from_str(&data).with_context(|| format!("failed to parse {}", path.display()))?;
    if config.active.is_none() && config.networks.contains_key("devnet") {
        config.active = Some("devnet".to_string());
        save_config(path, &config)?;
    }
    Ok(config)
}

fn save_config(path: &Path, config: &AppConfig) -> Result<()> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("failed to create {}", parent.display()))?;
    }
    let data = toml::to_string_pretty(config)?;
    fs::write(path, data).with_context(|| format!("failed to write {}", path.display()))?;
    Ok(())
}

pub(crate) fn ensure_default_config_with_options(
    path: &Path,
    options: &ConfigInitOptions,
) -> Result<()> {
    if !path.exists() {
        let _config = load_or_init_config_with_options(path, options)?;
    }
    Ok(())
}

pub(crate) fn default_config_path() -> Result<PathBuf> {
    Ok(config_dir()?.join(CONFIG_FILE_NAME))
}

pub(crate) fn active_network_rpc(context: &ConfigContext) -> Result<(String, String)> {
    let config_path = context.path();
    let config = load_or_init_config_with_options(config_path, context.options())?;
    let active = config
        .active
        .clone()
        .ok_or_else(|| anyhow!("active network not set"))?;
    let entry = config
        .networks
        .get(&active)
        .ok_or_else(|| anyhow!("active network '{active}' not found"))?;
    if entry.rpc.trim().is_empty() {
        bail!("active network '{active}' has no rpc endpoint configured");
    }
    Ok((active, entry.rpc.clone()))
}

pub(crate) fn active_network_binary_port(context: &ConfigContext) -> Result<(String, String)> {
    let config_path = context.path();
    let config = load_or_init_config_with_options(config_path, context.options())?;
    let active = config
        .active
        .clone()
        .ok_or_else(|| anyhow!("active network not set"))?;
    let entry = config
        .networks
        .get(&active)
        .ok_or_else(|| anyhow!("active network '{active}' not found"))?;
    let binary_port = entry.binary_port.trim();
    if binary_port.is_empty() {
        bail!("active network '{active}' has no binary port configured");
    }
    Ok((active, binary_port.to_string()))
}

pub(crate) fn active_network_name_and_chain_name(
    context: &ConfigContext,
) -> Result<(String, String)> {
    let config_path = context.path();
    let config = load_or_init_config_with_options(config_path, context.options())?;
    let active = config
        .active
        .clone()
        .ok_or_else(|| anyhow!("active network not set"))?;
    let entry = config
        .networks
        .get(&active)
        .ok_or_else(|| anyhow!("active network '{active}' not found"))?;
    if entry.chain_name.trim().is_empty() {
        bail!("active network '{active}' has no chain name configured");
    }
    Ok((active, entry.chain_name.clone()))
}

pub(crate) fn active_network_chain_name(context: &ConfigContext) -> Result<String> {
    let config_path = context.path();
    let config = load_or_init_config_with_options(config_path, context.options())?;
    let active = config
        .active
        .clone()
        .ok_or_else(|| anyhow!("active network not set"))?;
    let entry = config
        .networks
        .get(&active)
        .ok_or_else(|| anyhow!("active network '{active}' not found"))?;
    if entry.chain_name.trim().is_empty() {
        bail!("active network '{active}' has no chain name configured");
    }
    Ok(entry.chain_name.clone())
}

fn config_dir() -> Result<PathBuf> {
    Ok(dirs::config_dir()
        .or_else(|| std::env::current_dir().ok())
        .context("unable to determine config directory")?
        .join("casper-cli"))
}

fn default_config() -> Result<AppConfig> {
    Ok(config_with_storage(StorageSection::Keyring))
}

fn config_with_storage(storage: StorageSection) -> AppConfig {
    let mut networks = BTreeMap::new();
    networks.insert(
        "devnet".to_string(),
        NetworkEntry {
            chain_name: "casper-dev".to_string(),
            rest: "http://127.0.0.1:14101".to_string(),
            sse: "http://127.0.0.1:18101/events".to_string(),
            rpc: "http://127.0.0.1:11101/rpc".to_string(),
            binary_port: "127.0.0.1:28101".to_string(),
        },
    );

    AppConfig {
        active: Some("devnet".to_string()),
        networks,
        storage: Some(storage),
    }
}

fn prompt_default_config() -> Result<AppConfig> {
    let theme = ColorfulTheme::default();
    let storage = prompt_storage_section(&theme)?;
    Ok(config_with_storage(storage))
}

fn storage_section_from_override(override_choice: &StorageOverride) -> Result<StorageSection> {
    match override_choice {
        StorageOverride::Keyring => Ok(StorageSection::Keyring),
        StorageOverride::File { root_path } => {
            if root_path.trim().is_empty() {
                bail!("--file-storage root_path is empty");
            }
            Ok(StorageSection::File {
                root_path: root_path.clone(),
            })
        }
    }
}

fn prompt_storage_section(theme: &ColorfulTheme) -> Result<StorageSection> {
    let items = [
        "OS-based keyring where master secrets will be securely stored (default)",
        "File based secure storage (good for development, tests, CI, etc.)",
    ];
    let selection = Select::with_theme(theme)
        .with_prompt("Which secret storage backend do you want to use?")
        .items(&items)
        .default(0)
        .interact()
        .context("read storage backend selection")?;
    match selection {
        0 => Ok(StorageSection::Keyring),
        1 => Ok(StorageSection::File {
            root_path: prompt_file_storage_root(theme)?,
        }),
        _ => bail!("invalid storage selection"),
    }
}

fn prompt_file_storage_root(theme: &ColorfulTheme) -> Result<String> {
    let default_root = default_storage_root()?;
    let items = [
        format!("Default location for file storage (~projectdirs based default: {default_root})"),
        "Custom".to_string(),
    ];
    let selection = Select::with_theme(theme)
        .with_prompt("Where should file-based storage live?")
        .items(&items)
        .default(0)
        .interact()
        .context("read storage location selection")?;
    match selection {
        0 => Ok(default_root),
        1 => {
            let prompt = format!(
                "Enter custom storage location (~projectdirs based default: {default_root})"
            );
            let input: String = Input::with_theme(theme)
                .with_prompt(prompt)
                .default(default_root.clone())
                .interact_text()
                .context("read custom storage location")?;
            let trimmed = input.trim();
            if trimmed.is_empty() {
                return Ok(default_root);
            }
            Ok(expand_tilde(trimmed))
        }
        _ => bail!("invalid storage location selection"),
    }
}

fn expand_tilde(path: &str) -> String {
    if let Some(rest) = path.strip_prefix("~/") {
        if let Some(home) = dirs::home_dir() {
            return home.join(rest).display().to_string();
        }
    } else if path == "~"
        && let Some(home) = dirs::home_dir()
    {
        return home.display().to_string();
    }
    path.to_string()
}

fn default_storage_root() -> Result<String> {
    Ok(config_dir()?.display().to_string())
}