Skip to main content

Keystore

Struct Keystore 

Source
pub struct Keystore<K: KeyScheme> { /* private fields */ }
Expand description

A typed, encrypted keystore.

Holds metadata — the on-disk header — but never the plaintext secret until unlock is called. unlock returns a SignerHandle<K> that owns a zeroizing copy of the secret.

§Type parameter

K is the key scheme (see crate::scheme): typically BlsSigning for validator keys, L1WalletBls for Chia L1 wallet keys.

Implementations§

Source§

impl<K: KeyScheme> Keystore<K>

Source

pub fn create( backend: Arc<dyn KeychainBackend>, path: BackendKey, password: Password, plaintext: Option<Zeroizing<Vec<u8>>>, kdf_params: KdfParams, ) -> Result<Self>

Create a new keystore on backend at path.

  • If plaintext is Some, those bytes are used as the secret (length must equal K::SECRET_LEN). Callers who already hold a seed (e.g., from a BIP-39 mnemonic) pass it here.
  • If plaintext is None, a fresh secret is generated via K::generate with an OS-seeded RNG.

Fails with KeystoreError::AlreadyExists if a blob already exists at path — this refuses to silently overwrite an existing key.

Source

pub fn create_with_rng<R: RngCore + CryptoRng>( backend: Arc<dyn KeychainBackend>, path: BackendKey, password: Password, plaintext: Option<Zeroizing<Vec<u8>>>, kdf_params: KdfParams, rng: &mut R, ) -> Result<Self>

Like create but uses a caller-supplied RNG. Primarily for deterministic test fixtures; do not use a predictable RNG for production keys.

Source

pub fn load(backend: Arc<dyn KeychainBackend>, path: BackendKey) -> Result<Self>

Load an existing keystore. Does NOT decrypt — reads and validates the header, verifies CRC32, and returns a handle that unlock can use.

Source

pub fn header(&self) -> KeystoreHeader

Header metadata (magic, scheme id, KDF params, etc).

Source

pub fn path(&self) -> &BackendKey

Backend key this keystore was loaded from.

Source

pub fn cached_public_key(&self) -> Option<K::PublicKey>

If the keystore has been unlocked in this process, returns the cached public key. Otherwise None.

Source

pub fn unlock(&self, password: Password) -> Result<SignerHandle<K>>

Decrypt with password and return a SignerHandle holding the zeroizing secret + derived public key.

§Errors
Source

pub fn change_password(&mut self, old: Password, new: Password) -> Result<()>

Re-encrypt the secret under a new password. The secret itself does not change; only the encryption key derived from the password. A fresh salt + nonce are generated so the output ciphertext differs even with the same password.

Source

pub fn change_password_with_rng<R: RngCore + CryptoRng>( &mut self, old: Password, new: Password, rng: &mut R, ) -> Result<()>

Like change_password but uses a caller-supplied RNG.

Source

pub fn rotate_kdf( &mut self, password: Password, new_params: KdfParams, ) -> Result<()>

Rotate the KDF parameters (e.g., bump to KdfParams::STRONG). Uses the same password throughout; the on-disk file is re-encrypted under a new salt + nonce.

Source

pub fn rotate_kdf_with_rng<R: RngCore + CryptoRng>( &mut self, password: Password, new_params: KdfParams, rng: &mut R, ) -> Result<()>

Like rotate_kdf but uses a caller-supplied RNG.

Source

pub fn delete(self) -> Result<()>

Remove the encrypted blob.

Trait Implementations§

Source§

impl<K: KeyScheme> Debug for Keystore<K>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K> !Freeze for Keystore<K>

§

impl<K> !RefUnwindSafe for Keystore<K>

§

impl<K> Send for Keystore<K>

§

impl<K> Sync for Keystore<K>

§

impl<K> Unpin for Keystore<K>
where <K as KeyScheme>::PublicKey: Unpin,

§

impl<K> UnsafeUnpin for Keystore<K>

§

impl<K> !UnwindSafe for Keystore<K>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.