Files
featherChat/warzone/crates/warzone-protocol/src/mnemonic.rs
Siavash Sameni 651396fa13 Scaffold Rust workspace: warzone-protocol, server, client, mule
4 crates, all compile. 16/17 tests pass.

warzone-protocol (core crypto):
- Seed-based identity (Ed25519 + X25519 from 32-byte seed via HKDF)
- BIP39 mnemonic encode/decode (24 words)
- Fingerprint type (SHA-256 truncated, displayed as xxxx:xxxx:xxxx:xxxx)
- ChaCha20-Poly1305 AEAD encrypt/decrypt with random nonce
- HKDF-SHA256 key derivation
- Pre-key bundle generation with Ed25519 signatures
- X3DH key exchange (simplified, needs X25519 identity key in bundle)
- Double Ratchet: full implementation with DH ratchet, chain ratchet,
  out-of-order message handling via skipped keys cache
- Message format (WarzoneMessage envelope + RatchetHeader)
- Session type with ratchet state
- Storage trait definitions (PreKeyStore, SessionStore, MessageQueue)

warzone-server (axum):
- sled database (keys, messages, one-time pre-keys)
- Routes: /v1/health, /v1/keys/register, /v1/keys/{fp},
  /v1/messages/send, /v1/messages/poll/{fp}, /v1/messages/{id}/ack

warzone-client (CLI):
- `warzone init` — generate seed, show mnemonic, save to ~/.warzone/
- `warzone recover <words>` — restore from mnemonic
- `warzone info` — show fingerprint and keys
- Seed storage at ~/.warzone/identity.seed (600 perms)
- Stubs for send, recv, chat commands

warzone-mule: Phase 4 placeholder

Known issue: X3DH test fails (initiate/respond use different DH ops
due to missing X25519 identity key in bundle). Fix in next step.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 21:27:48 +04:00

38 lines
1.1 KiB
Rust

use bip39::Mnemonic;
use crate::errors::ProtocolError;
/// Encode 32 bytes as a BIP39 mnemonic (24 words).
pub fn seed_to_mnemonic(seed: &[u8; 32]) -> String {
// BIP39 with 256 bits of entropy = 24 words
let mnemonic = Mnemonic::from_entropy(seed).expect("32 bytes is valid BIP39 entropy");
mnemonic.to_string()
}
/// Decode a BIP39 mnemonic back to 32 bytes.
pub fn mnemonic_to_seed(words: &str) -> Result<[u8; 32], ProtocolError> {
let mnemonic: Mnemonic = words.parse().map_err(|_| ProtocolError::InvalidMnemonic)?;
let entropy = mnemonic.to_entropy();
if entropy.len() != 32 {
return Err(ProtocolError::InvalidSeedLength);
}
let mut seed = [0u8; 32];
seed.copy_from_slice(&entropy);
Ok(seed)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrip() {
let seed = [0xab; 32];
let words = seed_to_mnemonic(&seed);
let word_count = words.split_whitespace().count();
assert_eq!(word_count, 24);
let recovered = mnemonic_to_seed(&words).unwrap();
assert_eq!(seed, recovered);
}
}