Skip to main content

dig_keystore/
signer.rs

1//! `SignerHandle` — the unlocked, signing-capable handle returned by `Keystore::unlock`.
2//!
3//! A `SignerHandle<K>` owns a `Zeroizing<Vec<u8>>` copy of the decrypted secret
4//! and the derived public key. It exposes `sign` and `public_key`; raw secret
5//! bytes can never be extracted. Drop zeroizes the secret.
6
7use std::marker::PhantomData;
8
9use zeroize::Zeroizing;
10
11use crate::error::Result;
12use crate::scheme::KeyScheme;
13
14/// The unlocked handle. Drop wipes the secret.
15///
16/// Cloning a `SignerHandle` clones the underlying zeroizing buffer — both
17/// copies are independently wiped on drop. This is expensive for high-frequency
18/// signing; prefer sharing an `Arc<SignerHandle<K>>` for that case.
19pub struct SignerHandle<K: KeyScheme> {
20    secret: Zeroizing<Vec<u8>>,
21    public: K::PublicKey,
22    _marker: PhantomData<fn() -> K>,
23}
24
25impl<K: KeyScheme> SignerHandle<K> {
26    pub(crate) fn from_parts(secret: Zeroizing<Vec<u8>>, public: K::PublicKey) -> Self {
27        Self {
28            secret,
29            public,
30            _marker: PhantomData,
31        }
32    }
33
34    /// Borrow the derived public key. Cheap (precomputed at unlock time).
35    pub fn public_key(&self) -> &K::PublicKey {
36        &self.public
37    }
38
39    /// Sign a byte message.
40    pub fn sign(&self, msg: &[u8]) -> K::Signature {
41        // K::sign only errors when the secret length is wrong; we control that.
42        K::sign(&self.secret, msg).expect("signer handle secret length is guaranteed valid")
43    }
44
45    /// Attempt to sign, surfacing any scheme-level errors instead of panicking.
46    pub fn try_sign(&self, msg: &[u8]) -> Result<K::Signature> {
47        K::sign(&self.secret, msg)
48    }
49
50    /// Borrow the raw secret bytes.
51    ///
52    /// # ⚠️ Danger
53    ///
54    /// Prefer [`sign`](Self::sign) whenever possible. This method exists for
55    /// a narrow class of consumers — hierarchical-deterministic (HD) wallets
56    /// and key-derivation libraries — that need the raw seed bytes to
57    /// derive child keys (e.g.
58    /// [`chia_bls::DerivableKey::derive_unhardened`](https://docs.rs/chia-bls)).
59    /// These callers cannot use [`sign`](Self::sign) because they need the
60    /// `SecretKey` itself, not a signature.
61    ///
62    /// The returned slice is borrowed from the handle's internal
63    /// `Zeroizing<Vec<u8>>`, so it wipes automatically when the handle
64    /// drops. **Callers must not copy the bytes into a non-zeroizing
65    /// buffer** without re-wrapping them — doing so would leave the secret
66    /// on the heap past the end of its intended lifetime.
67    ///
68    /// Typical usage:
69    ///
70    /// ```no_run
71    /// # use dig_keystore::{scheme::L1WalletBls, SignerHandle};
72    /// # fn get_signer() -> SignerHandle<L1WalletBls> { unimplemented!() }
73    /// let signer = get_signer();
74    /// let master_sk = chia_bls::SecretKey::from_seed(signer.expose_secret());
75    /// // `master_sk` is now the chia-bls master key; HD-derive as needed.
76    /// ```
77    ///
78    /// If you catch yourself writing `signer.expose_secret().to_vec()` or
79    /// `let copy = signer.expose_secret().to_owned();`, stop and consider
80    /// whether you really need to own the bytes — and if so, wrap the copy
81    /// in `Zeroizing::<Vec<u8>>::from(...)`.
82    pub fn expose_secret(&self) -> &[u8] {
83        &self.secret
84    }
85}
86
87impl<K: KeyScheme> Clone for SignerHandle<K> {
88    fn clone(&self) -> Self {
89        Self {
90            secret: self.secret.clone(),
91            public: self.public.clone(),
92            _marker: PhantomData,
93        }
94    }
95}
96
97impl<K: KeyScheme> std::fmt::Debug for SignerHandle<K> {
98    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99        f.debug_struct("SignerHandle")
100            .field("scheme", &K::NAME)
101            .field("public", &self.public)
102            .field(
103                "secret",
104                &format_args!("<{} bytes zeroized>", self.secret.len()),
105            )
106            .finish()
107    }
108}
109
110// Explicitly NOT implementing AsRef<[u8]>, Deref, or into_raw() on SignerHandle.
111// The secret never leaves the handle by default; `expose_secret` is the one
112// deliberate, clearly-named opt-out for HD-wallet consumers.
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117    use crate::scheme::BlsSigning;
118
119    /// **Proves:** the `Debug` impl of `SignerHandle` does not print the raw
120    /// secret bytes. We construct a handle with secret `0xAA` × 32 and
121    /// assert the formatted output contains the placeholder but not `"AA"`.
122    ///
123    /// **Why it matters:** `SignerHandle` is routinely stored inside the
124    /// validator's `Node` struct which gets `tracing::info!(?self, ...)`ed
125    /// at startup. If the Debug impl leaked the secret, validator keys
126    /// would land in log files on every restart. The test pins the no-leak
127    /// property.
128    ///
129    /// **Catches:** accidentally deriving `Debug` on `SignerHandle` (which
130    /// would print the inner `Zeroizing<Vec<u8>>` content), or a future
131    /// `#[derive(Debug)]` addition that bypasses the custom impl.
132    #[test]
133    fn debug_does_not_leak_secret() {
134        let secret = Zeroizing::new(vec![0xAAu8; 32]);
135        let public = BlsSigning::public_key(&secret).unwrap();
136        let handle: SignerHandle<BlsSigning> = SignerHandle::from_parts(secret, public);
137        let s = format!("{:?}", handle);
138        assert!(s.contains("<32 bytes zeroized>"));
139        assert!(!s.contains("AA"));
140    }
141
142    /// **Proves:** the full in-memory signing path works — construct a
143    /// handle, sign, verify with the public key.
144    ///
145    /// **Why it matters:** Exercises `SignerHandle::sign` without going
146    /// through the encrypted backend. If this ever regresses, `Keystore::unlock`
147    /// would return a handle that produces wrong signatures (catastrophic).
148    ///
149    /// **Catches:** a bug in `sign` (e.g. forwarding the wrong secret field,
150    /// signing the wrong bytes, feeding the public key as the secret key).
151    #[test]
152    fn sign_works() {
153        let secret = Zeroizing::new(vec![0x11u8; 32]);
154        let public = BlsSigning::public_key(&secret).unwrap();
155        let handle: SignerHandle<BlsSigning> = SignerHandle::from_parts(secret, public);
156        let sig = handle.sign(b"message");
157        assert!(chia_bls::verify(&sig, &public, b"message"));
158    }
159
160    /// **Proves:** `try_sign` returns `Ok(sig)` for a valid handle and the
161    /// signature it produces verifies under the handle's public key — i.e. the
162    /// fallible signing path is equivalent to the panicking [`sign`](super::SignerHandle::sign).
163    ///
164    /// **Why it matters:** `try_sign` is the non-panicking entry point for
165    /// callers that prefer to surface scheme-level errors as `Result` rather
166    /// than unwind. Because a `SignerHandle` always holds a length-validated
167    /// secret, `try_sign` must succeed here; the test pins that the `Ok` arm is
168    /// reached and yields a real, verifiable signature (not a default/empty one).
169    ///
170    /// **Catches:** a `try_sign` that forwards the wrong secret/message, or that
171    /// erroneously returns `Err` for a well-formed handle.
172    #[test]
173    fn try_sign_succeeds_and_verifies() {
174        let secret = Zeroizing::new(vec![0x33u8; 32]);
175        let public = BlsSigning::public_key(&secret).unwrap();
176        let handle: SignerHandle<BlsSigning> = SignerHandle::from_parts(secret, public);
177        let sig = handle
178            .try_sign(b"payload")
179            .expect("try_sign should succeed");
180        assert!(chia_bls::verify(&sig, &public, b"payload"));
181        // try_sign and sign agree byte-for-byte.
182        assert_eq!(sig.to_bytes(), handle.sign(b"payload").to_bytes());
183    }
184
185    /// **Proves:** cloning a `SignerHandle` yields an independent copy that
186    /// produces the exact same signature as the original.
187    ///
188    /// **Why it matters:** Validators sometimes clone the handle into a
189    /// per-duty context (so a panic in one duty's signing doesn't poison
190    /// the other's). Both copies must produce identical signatures, which
191    /// they will iff the secret is copied (not shared-and-mutably-rotated).
192    ///
193    /// **Catches:** a regression where `Clone` shares the underlying
194    /// storage via `Arc` without `CoW` semantics — a single rotate would
195    /// then silently corrupt one of the copies.
196    #[test]
197    fn clone_preserves_equality() {
198        let secret = Zeroizing::new(vec![0x11u8; 32]);
199        let public = BlsSigning::public_key(&secret).unwrap();
200        let h1: SignerHandle<BlsSigning> = SignerHandle::from_parts(secret, public);
201        let h2 = h1.clone();
202        let s1 = h1.sign(b"x");
203        let s2 = h2.sign(b"x");
204        assert_eq!(s1.to_bytes(), s2.to_bytes());
205    }
206
207    /// **Proves:** `expose_secret` returns the exact seed bytes that were
208    /// used to construct the handle — byte-for-byte equality.
209    ///
210    /// **Why it matters:** HD-wallet consumers (`dig-l1-wallet`, future
211    /// `apps/wallet`) need the raw seed to feed `chia_bls::SecretKey::from_seed`
212    /// and derive child keys. If `expose_secret` ever returned a transformed
213    /// value (e.g., the derived master `SecretKey` serialised), HD derivation
214    /// in dependent crates would silently produce the wrong child addresses.
215    ///
216    /// **Catches:** a regression that hashes / transforms the secret before
217    /// returning; a bug where `expose_secret` borrows the public key
218    /// instead of the private seed.
219    #[test]
220    fn expose_secret_returns_original_bytes() {
221        let bytes = [0x77u8; 32];
222        let secret = Zeroizing::new(bytes.to_vec());
223        let public = BlsSigning::public_key(&secret).unwrap();
224        let handle: SignerHandle<BlsSigning> = SignerHandle::from_parts(secret, public);
225        assert_eq!(handle.expose_secret(), &bytes);
226    }
227
228    /// **Proves:** the byte slice returned by `expose_secret` is tied to the
229    /// handle's lifetime — once the handle is dropped, the borrow must end.
230    ///
231    /// **Why it matters:** This is really a compile-time property of the
232    /// borrow checker rather than a runtime assertion. The test exists to
233    /// document the intended lifetime contract and to break if someone
234    /// "helpfully" changes the return type to `Vec<u8>` (owned).
235    ///
236    /// **Catches:** a signature change to `expose_secret(&self) -> Vec<u8>`
237    /// that would leak the secret on drop of the returned vec.
238    #[test]
239    fn expose_secret_borrow_scoped_to_handle() {
240        let secret = Zeroizing::new(vec![0x22u8; 32]);
241        let public = BlsSigning::public_key(&secret).unwrap();
242        let handle: SignerHandle<BlsSigning> = SignerHandle::from_parts(secret, public);
243        let borrowed: &[u8] = handle.expose_secret();
244        assert_eq!(borrowed.len(), 32);
245        // The borrow would be rejected by the compiler if `handle` were dropped
246        // before `borrowed` — which is the property we want.
247        drop(handle);
248        // borrowed is now invalid and can't be touched; the compiler proves this.
249    }
250}