stellar_strkey/unredacted.rs
1/// Wrapper that opts a value in to formatting or serialization that would
2/// otherwise expose private-key bytes.
3///
4/// Wrap an [`ed25519::PrivateKey`](crate::ed25519::PrivateKey) in `Unredacted`
5/// for any of:
6///
7/// - [`Display`](core::fmt::Display) / `to_string` / `write_string` — renders
8/// the encoded strkey string (`S…`).
9/// - [`Debug`](core::fmt::Debug) — prints the raw 32-byte seed as hex
10/// (`PrivateKey(<hex>)`). Bare `PrivateKey`'s `Debug` redacts.
11/// - `serde::Serialize` (under the `serde` feature) — serializes as the
12/// strkey string form.
13/// - [`Decoded`](crate::Decoded)`<Unredacted<&PrivateKey>>` (under the
14/// `serde-decoded` feature) — serializes the raw seed as hex inside a
15/// JSON object.
16///
17/// # Zeroize
18///
19/// `Display`, `to_string`, and `serde::Serialize` materialize the encoded
20/// bytes into a non-zeroizing intermediate (the returned `String`, the
21/// formatter, or the serializer's internal buffer). For the strongest
22/// guarantees, use [`Unredacted::write_string`] to write directly into a
23/// caller-provided [`Zeroizing`](zeroize::Zeroizing) buffer.
24#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
25pub struct Unredacted<T>(pub T);
26
27#[cfg(feature = "serde")]
28impl<T> serde::Serialize for Unredacted<T>
29where
30 Self: core::fmt::Display,
31{
32 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
33 serializer.collect_str(self)
34 }
35}