39 lines
1.1 KiB
Rust
39 lines
1.1 KiB
Rust
use std::io::Write;
|
|
use base58::{FromBase58, ToBase58};
|
|
use crate::CmdExec;
|
|
use clap::Args;
|
|
use rust_util::{opt_result, simple_error, XResult};
|
|
use crate::util::read_file_or_stdin;
|
|
|
|
#[derive(Debug, Args)]
|
|
pub struct CmdBase58 {
|
|
/// Encode
|
|
#[arg(long, short = 'e')]
|
|
pub encode: bool,
|
|
/// Decode
|
|
#[arg(long, short = 'd')]
|
|
pub decode: bool,
|
|
/// Input file
|
|
#[arg(long, short = 'i')]
|
|
pub r#in: Option<String>,
|
|
}
|
|
|
|
impl CmdExec for CmdBase58 {
|
|
fn exec(&mut self) -> XResult<()> {
|
|
if self.encode && self.decode {
|
|
return simple_error!("Encode and decode cannot both assigned.");
|
|
}
|
|
|
|
let input = read_file_or_stdin(&self.r#in)?;
|
|
|
|
if self.decode {
|
|
let s = opt_result!(String::from_utf8(input), "Decode UTF8 failed: {}");
|
|
let decoded = opt_result!(s.trim().from_base58(), "Decode base58 failed: {:?}");
|
|
std::io::stdout().write_all(&decoded)?;
|
|
} else {
|
|
let encoded = input.to_base58();
|
|
print!("{}", encoded);
|
|
}
|
|
Ok(())
|
|
}
|
|
} |