rage icon indicating copy to clipboard operation
rage copied to clipboard

feat(age): add BIP39 mnemonic support for Identity

Open RyderFreeman4Logos opened this issue 1 year ago • 0 comments

Summary

This PR introduces support for BIP39 mnemonics to the age crate, enabling users to backup and restore x25519::Identity using a 24-word phrase. This facilitates deterministic key generation and easier key management.

Changes

age Crate

  • Added Identity::from_secret_bytes: A public method to construct an Identity from a raw 32-byte array.
    • Note: Added a strong # Security section to the documentation warning users to only provide high-entropy inputs (CSPRNG or KDF derived).
  • Added bip39 Feature:
    • Introduced an optional dependency on bip39 (v2.0).
    • Added a bip39 feature flag to Cargo.toml.
  • BIP39 Implementation:
    • Implemented Identity::to_mnemonic(&self) -> Mnemonic: Serializes the secret key into a 24-word English mnemonic.
    • Implemented Identity::from_mnemonic(&Mnemonic) -> Result<Self, &'static str>: Restores an identity from a mnemonic.
    • Security: Intermediate entropy vectors are explicitly zeroized using the Zeroize trait before being dropped.

Testing

  • Added unit tests for the new functionality:
    • identity_from_secret_bytes: Verifies raw byte deserialization.
    • mnemonic_round_trip: Verifies that an identity can be converted to a mnemonic and restored back correctly (guarded by bip39 feature).
    • invalid_mnemonic_length: Ensures that mnemonics with insufficient entropy (e.g., 12 words/128 bits) are rejected safely.

Verification

Run the tests including the new feature:

cargo test --package age --lib --features bip39

RyderFreeman4Logos avatar Dec 04 '25 10:12 RyderFreeman4Logos