casper-client 5.0.1

A client library and binary for interacting with the Casper network
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
use std::fs;

use clap::{Arg, ArgAction, ArgMatches};

use casper_client::cli::CliError;
use casper_types::PublicKey;

pub const ARG_PATH: &str = "PATH";
pub const ARG_HEX_STRING: &str = "HEX STRING";
pub const ARG_STRING: &str = "STRING";
pub const ARG_INTEGER: &str = "INTEGER";
pub const DEFAULT_TRANSFER_PAYMENT_AMOUNT: &str = "2500000000";

/// Handles the arg for whether verbose output is required or not.
pub mod verbose {
    use super::*;

    pub const ARG_NAME: &str = "verbose";
    const ARG_NAME_SHORT: char = 'v';
    const ARG_HELP: &str =
        "Generates verbose output, e.g. prints the RPC request.  If repeated by using '-vv' then \
        all output will be extra verbose, meaning that large JSON strings will be shown in full";

    pub fn arg(order: usize) -> Arg {
        Arg::new(ARG_NAME)
            .short(ARG_NAME_SHORT)
            .required(false)
            .action(ArgAction::Count)
            .help(ARG_HELP)
            .display_order(order)
    }

    pub fn get(matches: &ArgMatches) -> u64 {
        match matches.try_get_one::<u8>(ARG_NAME) {
            Ok(maybe_count) => maybe_count.copied().unwrap_or_default() as u64,
            Err(_) => 0,
        }
    }
}

/// Handles providing the arg for and retrieval of the node hostname/IP and port.
pub mod node_address {
    use super::*;

    const ARG_NAME: &str = "node-address";
    const ARG_SHORT: char = 'n';
    const ARG_VALUE_NAME: &str = "HOST:PORT";
    const ARG_DEFAULT: &str = "http://localhost:7777";
    const ARG_HELP: &str = "Hostname or IP and port of node on which HTTP service is running";

    pub fn arg(order: usize) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .required(false)
            .default_value(ARG_DEFAULT)
            .value_name(ARG_VALUE_NAME)
            .help(ARG_HELP)
            .display_order(order)
    }

    pub fn get(matches: &ArgMatches) -> &str {
        matches
            .get_one::<String>(ARG_NAME)
            .map(String::as_str)
            .unwrap_or_else(|| panic!("should have {} arg", ARG_NAME))
    }
}

/// Handles providing the arg for the RPC ID.
pub mod rpc_id {
    use super::*;

    const ARG_NAME: &str = "id";
    const ARG_VALUE_NAME: &str = "STRING OR INTEGER";
    const ARG_HELP: &str =
        "JSON-RPC identifier, applied to the request and returned in the response. If not \
        provided, a random integer will be assigned";

    pub fn arg(order: usize) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .required(false)
            .value_name(ARG_VALUE_NAME)
            .help(ARG_HELP)
            .display_order(order)
    }

    pub fn get(matches: &ArgMatches) -> &str {
        matches
            .get_one::<String>(ARG_NAME)
            .map(String::as_str)
            .unwrap_or_default()
    }
}

/// Handles providing the arg for and retrieval of the secret key.
pub mod secret_key {
    use super::*;

    pub(crate) const ARG_NAME: &str = "secret-key";
    const ARG_SHORT: char = 'k';
    const ARG_VALUE_NAME: &str = ARG_PATH;
    const ARG_HELP: &str = "Path to secret key file";

    pub fn arg(order: usize, extended_help: &str) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .value_name(ARG_VALUE_NAME)
            .help(format!("{}{}", ARG_HELP, extended_help))
            .display_order(order)
    }

    pub fn get(matches: &ArgMatches) -> Option<&str> {
        matches.get_one::<String>(ARG_NAME).map(String::as_str)
    }
}

/// Handles the arg for whether to overwrite existing output file(s).
pub mod force {
    use super::*;

    pub const ARG_NAME: &str = "force";
    const ARG_SHORT: char = 'f';
    const ARG_HELP_SINGULAR: &str =
        "If this flag is passed and the output file already exists, it will be overwritten. \
        Without this flag, if the output file already exists, the command will fail";
    const ARG_HELP_PLURAL: &str =
        "If this flag is passed, any existing output files will be overwritten. Without this flag, \
        if any output file exists, no output files will be generated and the command will fail";

    pub fn arg(order: usize, singular: bool) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .required(false)
            .action(ArgAction::SetTrue)
            .help(if singular {
                ARG_HELP_SINGULAR
            } else {
                ARG_HELP_PLURAL
            })
            .display_order(order)
    }

    pub fn get(matches: &ArgMatches) -> bool {
        matches
            .get_one::<bool>(ARG_NAME)
            .copied()
            .unwrap_or_default()
    }
}

/// Handles providing the arg for and retrieval of the state root hash.
pub mod state_root_hash {
    use super::*;

    pub(crate) const ARG_NAME: &str = "state-root-hash";
    const ARG_SHORT: char = 's';
    const ARG_VALUE_NAME: &str = super::ARG_HEX_STRING;
    const ARG_HELP: &str = "Hex-encoded hash of the state root";

    pub(crate) fn arg(order: usize, is_required: bool) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .required(is_required)
            .value_name(ARG_VALUE_NAME)
            .help(ARG_HELP)
            .display_order(order)
    }

    pub(crate) fn get(matches: &ArgMatches) -> Option<&str> {
        matches.get_one::<String>(ARG_NAME).map(String::as_str)
    }
}

/// Handles providing the arg for and retrieval of the block hash or block height.
pub mod block_identifier {
    use super::*;

    pub(crate) const ARG_NAME: &str = "block-identifier";
    const ARG_SHORT: char = 'b';
    const ARG_VALUE_NAME: &str = "HEX STRING OR INTEGER";
    const ARG_HELP: &str = "Hex-encoded block hash or height of the block";
    const ARG_HELP_WITH_EXTRA_INFO: &str =
        "Hex-encoded block hash or height of the block. If not given, the last block added to the \
        chain as known at the given node will be used";

    pub(crate) fn arg(order: usize, extra_help_string: bool) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .required(false)
            .value_name(ARG_VALUE_NAME)
            .help(if extra_help_string {
                ARG_HELP_WITH_EXTRA_INFO
            } else {
                ARG_HELP
            })
            .display_order(order)
    }

    pub(crate) fn get(matches: &ArgMatches) -> &str {
        matches
            .get_one::<String>(ARG_NAME)
            .map(String::as_str)
            .unwrap_or_default()
    }
}

/// Handles providing the arg for and retrieval of the public key.
pub(super) mod public_key {
    use casper_client::cli::CliError;
    use casper_types::AsymmetricType;

    use super::*;

    pub const ARG_NAME: &str = "public-key";
    const ARG_SHORT: char = 'p';
    const ARG_VALUE_NAME: &str = "FORMATTED STRING or PATH";
    const ARG_HELP: &str =
        "This must be a properly formatted public key. The public key may instead be read in from \
        a file, in which case enter the path to the file as the --public-key argument. The file \
        should be one of the two public key files generated via the `keygen` subcommand; \
        \"public_key_hex\" or \"public_key.pem\"";

    pub fn arg(order: usize, is_required: bool) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .required(is_required)
            .value_name(ARG_VALUE_NAME)
            .help(ARG_HELP)
            .display_order(order)
    }

    pub fn get(matches: &ArgMatches, is_required: bool) -> Result<String, CliError> {
        let value = matches
            .get_one::<String>(ARG_NAME)
            .map(String::as_str)
            .unwrap_or_else(|| {
                if is_required {
                    panic!("should have {} arg", ARG_NAME)
                } else {
                    ""
                }
            });
        try_read_from_file(value)
    }

    /// Treats `value` as a path and tries to read as a PEM-encoded or hex-encoded public key.  If
    /// there is a file at the given path and parsing fails, an error is returned.  If no file is
    /// found, `value` is returned unmodified.
    pub fn try_read_from_file(value: &str) -> Result<String, CliError> {
        // Try to read as a PublicKey PEM file first.
        if let Ok(public_key) = PublicKey::from_file(value) {
            return Ok(public_key.to_hex());
        }

        // Try to read as a hex-encoded PublicKey file next.
        if let Ok(hex_public_key) = fs::read_to_string(value) {
            let _ = PublicKey::from_hex(&hex_public_key).map_err(|error| {
                CliError::FailedToParsePublicKey {
                    context: format!(
                        "Can't parse the contents of {} as a public key: {}",
                        value, error
                    ),
                    error,
                }
            })?;
            return Ok(hex_public_key);
        }

        Ok(value.to_string())
    }
}

pub(super) mod purse_identifier {
    use super::*;

    /// Legacy name of purse identifier argument from when the command was named "get-balance".
    pub(crate) const PURSE_IDENTIFIER_ALIAS: &str = "purse-uref";

    pub(super) const ARG_NAME: &str = "purse-identifier";
    const ARG_SHORT: char = 'p';
    const ARG_VALUE_NAME: &str = "FORMATTED STRING or PATH";
    const ARG_HELP: &str =
        "The identifier for the purse. This can be a public key, account hash or an entity \
        address, implying the main purse of the given account should be used. Alternatively it \
        can be a purse URef. To provide a public key, it must be a properly formatted public key. \
        The public key may be read in from a file, in which case enter the path to the file as \
        the --purse-identifier argument. The file should be one of the two public key files \
        generated via the `keygen` subcommand; \"public_key_hex\" or \"public_key.pem\". To \
        provide an account hash, it must be formatted as \"account-hash-<HEX STRING>\", or \
        for an entity address as \"entity-account-<HEX STRING>\" or for a URef as \
        \"uref-<HEX STRING>-<THREE DIGIT INTEGER>\"";

    pub fn arg(order: usize, is_required: bool) -> Arg {
        Arg::new(ARG_NAME)
            .alias(PURSE_IDENTIFIER_ALIAS)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .required(is_required)
            .value_name(ARG_VALUE_NAME)
            .help(ARG_HELP)
            .display_order(order)
    }

    pub fn get(matches: &ArgMatches) -> Result<String, CliError> {
        let value = matches
            .get_one::<String>(ARG_NAME)
            .map(String::as_str)
            .unwrap_or_default();
        public_key::try_read_from_file(value)
    }
}

pub(super) mod account_identifier {
    use super::*;

    pub(super) const ARG_NAME: &str = "account-identifier";
    const ARG_SHORT: char = 'a';
    const ARG_VALUE_NAME: &str = "FORMATTED STRING or PATH";
    const ARG_HELP: &str =
        "The identifier for an account. This can be a public key or account hash. To provide a \
        public key, it must be a properly formatted public key. The public key may \
        be read in from a file, in which case enter the path to the file as the --account-identifier \
        argument. The file should be one of the two public key files generated via the `keygen` \
        subcommand; \"public_key_hex\" or \"public_key.pem\". To provide an account hash, it must \
        be formatted as \"account-hash-<HEX STRING>\"";

    pub fn arg(order: usize, is_required: bool) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .required(is_required)
            .value_name(ARG_VALUE_NAME)
            .help(ARG_HELP)
            .display_order(order)
    }

    pub fn get(matches: &ArgMatches) -> Result<String, CliError> {
        let value = matches
            .get_one::<String>(ARG_NAME)
            .map(String::as_str)
            .unwrap_or_default();
        public_key::try_read_from_file(value)
    }
}

pub(super) mod entity_identifier {
    use super::*;

    pub(super) const ARG_NAME: &str = "entity-identifier";
    const ARG_SHORT: char = 'e';
    const ARG_VALUE_NAME: &str = "FORMATTED STRING or PATH";
    const ARG_HELP: &str =
        "The identifier for an addressable entity or an account. This can be an entity hash, a public \
        key or an account hash. To provide an entity hash, it must be formatted as \
        \"entity-contract-<HEX STRING>\" or \"entity-account-<HEX STRING>\". \
        To provide a public key, it must be a properly formatted public key. The public key may be \
        read in from a file, in which case enter the path to the file as the --account-identifier \
        argument. The file should be one of the two public key files generated via the `keygen` \
        subcommand; \"public_key_hex\" or \"public_key.pem\". To provide an account hash, it must \
        be formatted as \"account-hash-<HEX STRING>\"";

    pub fn arg(order: usize, is_required: bool) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .required(is_required)
            .value_name(ARG_VALUE_NAME)
            .help(ARG_HELP)
            .display_order(order)
    }

    pub fn get(matches: &ArgMatches) -> Result<String, CliError> {
        let value = matches
            .get_one::<String>(ARG_NAME)
            .map(String::as_str)
            .unwrap_or_default();
        public_key::try_read_from_file(value)
    }
}

/// Handles providing the arg for and retrieval of the purse URef.
pub(super) mod purse_uref {
    use super::*;

    pub const ARG_NAME: &str = "purse-uref";
    const ARG_SHORT: char = 'u';
    const ARG_VALUE_NAME: &str = "FORMATTED STRING";
    const ARG_HELP: &str =
        "The URef under which the purse is stored. This must be a properly formatted URef \
        \"uref-<HEX STRING>-<THREE DIGIT INTEGER>\"";

    pub fn arg(display_order: usize, is_required: bool) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .required(is_required)
            .value_name(ARG_VALUE_NAME)
            .help(ARG_HELP)
            .display_order(display_order)
    }

    pub fn get(matches: &ArgMatches) -> Option<&str> {
        matches.get_one::<String>(ARG_NAME).map(String::as_str)
    }
}

/// Handles providing the arg for and retrieval of the era ID.
pub mod era_identifier {
    use super::*;

    pub(crate) const ARG_NAME: &str = "era-identifier";
    const ARG_SHORT: char = 'e';
    const ARG_VALUE_NAME: &str = "INTEGER";
    const ARG_HELP: &str = "Integer identifying the era";
    const ARG_HELP_WITH_EXTRA_INFO: &str =
        "Integer identifying the era. If not given, the last completed era will be used";

    pub(crate) fn arg(order: usize, extra_help_string: bool) -> Arg {
        Arg::new(ARG_NAME)
            .long(ARG_NAME)
            .short(ARG_SHORT)
            .required(false)
            .value_name(ARG_VALUE_NAME)
            .help(if extra_help_string {
                ARG_HELP_WITH_EXTRA_INFO
            } else {
                ARG_HELP
            })
            .display_order(order)
    }

    pub(crate) fn get(matches: &ArgMatches) -> &str {
        matches
            .get_one::<String>(ARG_NAME)
            .map(String::as_str)
            .unwrap_or_default()
    }
}

/// Handles providing the arg for and retrieval of the deploy hash.
pub mod deploy_hash {
    use super::*;

    const ARG_NAME: &str = "deploy-hash";
    const ARG_VALUE_NAME: &str = "HEX STRING";
    const ARG_HELP: &str = "Hex-encoded deploy hash";

    pub fn arg(display_order: usize) -> Arg {
        Arg::new(ARG_NAME)
            .required(true)
            .value_name(ARG_VALUE_NAME)
            .help(ARG_HELP)
            .display_order(display_order)
    }

    pub fn get(matches: &ArgMatches) -> &str {
        matches
            .get_one::<String>(ARG_NAME)
            .map(String::as_str)
            .unwrap_or_else(|| panic!("should have {} arg", ARG_NAME))
    }
}

/// Handles providing the arg for and retrieval of the transaction hash.
pub mod transaction_hash {
    use super::*;

    const ARG_NAME: &str = "transaction-hash";
    const ARG_VALUE_NAME: &str = "HEX STRING";
    const ARG_HELP: &str = "Hex-encoded transaction hash";

    pub fn arg(display_order: usize) -> Arg {
        Arg::new(ARG_NAME)
            .required(true)
            .value_name(ARG_VALUE_NAME)
            .help(ARG_HELP)
            .display_order(display_order)
    }

    pub fn get(matches: &ArgMatches) -> &str {
        matches
            .get_one::<String>(ARG_NAME)
            .map(String::as_str)
            .unwrap_or_else(|| panic!("should have {} arg", ARG_NAME))
    }
}