Skip to content

Commit

Permalink
feat: encode dev command for notes and invite-code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed Mar 6, 2024
1 parent 433d0bd commit ef0ae5b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
65 changes: 48 additions & 17 deletions fedimint-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ enum CliOutput {
},

DecodeNotes {
federation_id_prefix: FederationIdPrefix,
federation_id_prefix: String,
notes: TieredMulti<SpendableNote>,
},

Expand Down Expand Up @@ -372,6 +372,27 @@ enum DecodeType {
Notes { notes: OOBNotes },
}

#[derive(Debug, Clone, Subcommand)]
enum EncodeType {
/// Encode connection info from its constituent parts
InviteCode {
#[clap(long = "url")]
url: SafeUrl,
#[clap(long = "federation_id")]
federation_id: FederationId,
#[clap(long = "peer")]
peer: PeerId,
},

/// Encode a JSON string of notes to an ecash string
Notes {
#[clap(long = "federation_id_prefix")]
federation_id_prefix: String,
#[clap(long = "notes")]
notes: String,
},
}

#[derive(Debug, Clone, Subcommand)]
enum DevCmd {
/// Send direct method call to the API. If you specify --peer-id, it will
Expand Down Expand Up @@ -412,14 +433,10 @@ Examples:
decode_type: DecodeType,
},

/// Encode connection info from its constituent parts
EncodeInviteCode {
#[clap(long = "url")]
url: SafeUrl,
#[clap(long = "federation_id")]
federation_id: FederationId,
#[clap(long = "peer")]
peer: PeerId,
/// Encode an invite code or ecash notes into binary
Encode {
#[clap(subcommand)]
encode_type: EncodeType,
},

/// Gets the current fedimint AlephBFT block count
Expand Down Expand Up @@ -783,17 +800,31 @@ impl FedimintCli {
federation_id: invite_code.federation_id(),
}),
DecodeType::Notes { notes } => Ok(CliOutput::DecodeNotes {
federation_id_prefix: notes.federation_id_prefix(),
federation_id_prefix: notes.federation_id_prefix().to_string(),
notes: notes.notes().clone(),
}),
},
Command::Dev(DevCmd::EncodeInviteCode {
url,
federation_id,
peer,
}) => Ok(CliOutput::InviteCode {
invite_code: InviteCode::new(url, peer, federation_id),
}),
Command::Dev(DevCmd::Encode { encode_type }) => match encode_type {
EncodeType::InviteCode {
url,
federation_id,
peer,
} => Ok(CliOutput::InviteCode {
invite_code: InviteCode::new(url, peer, federation_id),
}),
EncodeType::Notes {
federation_id_prefix,
notes,
} => {
let federation_id_prefix = FederationIdPrefix::from_str(&federation_id_prefix)
.map_err_cli_msg(CliErrorKind::InvalidValue, "invalid federation id")?;
let notes = serde_json::from_str(&notes).map_err_cli_general()?;
let notes = OOBNotes::new(federation_id_prefix, notes);
Ok(CliOutput::Raw(
serde_json::to_value(notes).map_err_cli_general()?,
))
}
},
Command::Dev(DevCmd::SessionCount) => {
let client = self.client_open(&cli).await?;
let count = client.api().session_count().await?;
Expand Down
10 changes: 10 additions & 0 deletions fedimint-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,16 @@ impl Display for FederationId {
}
}

impl FromStr for FederationIdPrefix {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Vec::from_hex(s)?.try_into().map_err(
|bytes: Vec<u8>| hex::Error::InvalidLength(4, bytes.len()),
)?))
}
}

/// Display as a hex encoding
impl FederationId {
/// Random dummy id for testing
Expand Down

0 comments on commit ef0ae5b

Please sign in to comment.