Skip to main content

happy_cracking/crypto/
base58.rs

1use anyhow::{Context, Result};
2use clap::Subcommand;
3use umt_rust::crypto::{umt_decode_base58, umt_encode_base58};
4
5#[derive(Subcommand)]
6pub enum Base58Action {
7    #[command(about = "Encode to Base58")]
8    Encode {
9        #[arg(help = "Input text")]
10        input: String,
11    },
12    #[command(about = "Decode from Base58")]
13    Decode {
14        #[arg(help = "Base58 encoded string")]
15        input: String,
16    },
17}
18
19pub fn run(action: Base58Action) -> Result<()> {
20    match action {
21        Base58Action::Encode { input } => {
22            println!("{}", encode(&input));
23        }
24        Base58Action::Decode { input } => {
25            println!("{}", decode(&input)?);
26        }
27    }
28    Ok(())
29}
30
31pub fn encode(input: &str) -> String {
32    umt_encode_base58(input.as_bytes())
33}
34
35pub fn decode(input: &str) -> Result<String> {
36    let decoded = umt_decode_base58(input.trim()).context("Failed to decode Base58")?;
37    String::from_utf8(decoded).context("Decoded data is not valid UTF-8")
38}