Skip to main content

confium_api/plugin/
keyfmt.rs

1//! `KeyfmtPlugin` trait — the Rust-side counterpart of the key
2//! serialization (keyfmt) v0 wire protocol.
3//!
4//! Plugin authors implement this trait on their key representation
5//! type, then apply `#[plugin_interface(name = "keyfmt", version = 0)]`
6//! to the impl block.
7//!
8//! See `crates/confium-core/src/ffi/keyfmt.rs` for the loader-side
9//! wire types.
10
11use crate::error::PluginResult;
12use crate::options::OptionView;
13
14/// What kind of key this is.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[repr(u32)]
17pub enum KeyKind {
18    /// Secret key (private material present).
19    Secret = 0,
20    /// Public key only (no private material).
21    Public = 1,
22    /// Both secret and public material.
23    Both = 2,
24}
25
26/// Trait implemented by key-format plugins. The type `Self` is the
27/// parsed key representation; `parse` constructs it from bytes and
28/// `serialize` writes it back out.
29pub trait KeyfmtPlugin: Sized {
30    /// Parse `bytes` in the named `format` into a key object. The
31    /// `algorithm_hint` may guide format-specific parsing (e.g. for
32    /// `Raw` format).
33    fn parse(
34        format: &str,
35        algorithm_hint: Option<&str>,
36        bytes: &[u8],
37        opts: Option<OptionView<'_>>,
38    ) -> PluginResult<Self>;
39
40    /// Serialize the key into the named `format`. Returns the bytes.
41    fn serialize(&self, format: &str) -> PluginResult<Vec<u8>>;
42
43    /// Report whether this key is secret, public, or both.
44    fn kind(&self) -> PluginResult<KeyKind>;
45
46    /// Report the algorithm name for this key.
47    fn algorithm(&self) -> PluginResult<String>;
48
49    /// Produce a public-only view of this key (stripping secret
50    /// material). The returned value is a new boxed key object.
51    fn public(&self) -> PluginResult<Self>;
52}