fedimint_bip39/
lib.rs

1#![deny(clippy::pedantic)]
2
3//! BIP39 client secret support crate
4
5use std::io::{Read, Write};
6
7pub use bip39::{Language, Mnemonic};
8use fedimint_client::secret::RootSecretStrategy;
9use fedimint_core::encoding::{Decodable, DecodeError, Encodable};
10use fedimint_core::module::registry::ModuleRegistry;
11use fedimint_derive_secret::DerivableSecret;
12use rand::{CryptoRng, RngCore};
13
14/// BIP39 root secret encoding strategy allowing retrieval of the seed phrase.
15#[derive(Debug)]
16pub struct Bip39RootSecretStrategy<const WORD_COUNT: usize = 12>;
17
18impl<const WORD_COUNT: usize> RootSecretStrategy for Bip39RootSecretStrategy<WORD_COUNT> {
19    type Encoding = Mnemonic;
20
21    fn to_root_secret(secret: &Self::Encoding) -> DerivableSecret {
22        const FEDIMINT_CLIENT_NONCE: &[u8] = b"Fedimint Client Salt";
23        const EMPTY_PASSPHRASE: &str = "";
24
25        DerivableSecret::new_root(
26            secret.to_seed_normalized(EMPTY_PASSPHRASE).as_ref(),
27            FEDIMINT_CLIENT_NONCE,
28        )
29    }
30
31    fn consensus_encode(secret: &Self::Encoding, writer: &mut impl Write) -> std::io::Result<()> {
32        secret.to_entropy().consensus_encode(writer)
33    }
34
35    fn consensus_decode_partial(
36        reader: &mut impl Read,
37    ) -> Result<Self::Encoding, fedimint_core::encoding::DecodeError> {
38        let bytes = Vec::<u8>::consensus_decode_partial(reader, &ModuleRegistry::default())?;
39        Mnemonic::from_entropy(&bytes).map_err(DecodeError::from_err)
40    }
41
42    fn random<R>(rng: &mut R) -> Self::Encoding
43    where
44        R: RngCore + CryptoRng,
45    {
46        Mnemonic::generate_in_with(rng, Language::English, WORD_COUNT)
47            .expect("Failed to generate mnemonic, bad word count")
48    }
49}