fedimint_cli/
command_encoding.rs1use std::str::FromStr;
2
3use fedimint_core::base32::{self, FEDIMINT_PREFIX};
4use fedimint_core::config::FederationIdPrefix;
5use fedimint_core::db::DatabaseValue;
6use fedimint_core::invite_code::InviteCode;
7use fedimint_mint_client::OOBNotes;
8
9use crate::cli::{DecodeType, EncodeType, OOBNotesJson, Opts};
10use crate::{CliError, CliOutput, CliOutputResult, CliResultExt, FedimintCli};
11
12impl FedimintCli {
13 pub(super) async fn handle_decode(
16 &mut self,
17 cli: &Opts,
18 decode_type: DecodeType,
19 ) -> CliOutputResult {
20 match decode_type {
21 DecodeType::InviteCode { invite_code, .. } => {
22 let invite_code =
23 InviteCode::from_str(&invite_code.expect("secret inputs were resolved"))
24 .map_err(|_| CliError {
25 error: "Invalid invite code".to_owned(),
26 })?;
27 Ok(CliOutput::DecodeInviteCode {
28 url: invite_code.url(),
29 federation_id: invite_code.federation_id(),
30 })
31 }
32 DecodeType::Notes { notes, .. } => {
33 let notes = OOBNotes::from_str(¬es.expect("secret inputs were resolved"))
34 .map_err(|_| CliError {
35 error: "Invalid notes".to_owned(),
36 })?;
37 let notes_json = notes
38 .notes_json()
39 .map_err_cli_msg("failed to decode notes")?;
40 Ok(CliOutput::Raw(notes_json))
41 }
42 DecodeType::Transaction { hex_string } => {
43 let bytes: Vec<u8> = hex::FromHex::from_hex(&hex_string)
44 .map_err_cli_msg("failed to decode transaction")?;
45 let client = self.client_open(cli).await?;
46 let tx =
47 fedimint_core::transaction::Transaction::from_bytes(&bytes, client.decoders())
48 .map_err_cli_msg("failed to decode transaction")?;
49 Ok(CliOutput::DecodeTransaction {
50 transaction: format!("{tx:?}"),
51 })
52 }
53 DecodeType::SetupCode { setup_code } => {
54 let setup_code = base32::decode_prefixed(FEDIMINT_PREFIX, &setup_code)
55 .map_err_cli_msg("failed to decode setup code")?;
56 Ok(CliOutput::SetupCode { setup_code })
57 }
58 }
59 }
60}
61
62pub(super) fn handle_encode(encode_type: EncodeType) -> CliOutputResult {
65 match encode_type {
66 EncodeType::InviteCode {
67 url,
68 federation_id,
69 peer,
70 api_secret,
71 ..
72 } => Ok(CliOutput::InviteCode {
73 invite_code: InviteCode::new(url, peer, federation_id, api_secret),
74 }),
75 EncodeType::Notes { notes_json, .. } => {
76 let notes = serde_json::from_str::<OOBNotesJson>(
77 ¬es_json.expect("secret inputs were resolved"),
78 )
79 .map_err(|_| CliError {
80 error: "Invalid JSON for notes".to_owned(),
81 })?;
82 let prefix =
83 FederationIdPrefix::from_str(¬es.federation_id_prefix).map_err(|_| {
84 CliError {
85 error: "Invalid federation prefix in notes".to_owned(),
86 }
87 })?;
88 let notes = OOBNotes::new(prefix, notes.notes);
89 Ok(CliOutput::Raw(notes.to_string().into()))
90 }
91 }
92}