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::derivable_secret::DerivableSecret;
9use fedimint_client::secret::RootSecretStrategy;
10use fedimint_core::encoding::{Decodable, DecodeError, Encodable};
11use fedimint_core::module::registry::ModuleRegistry;
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(
32        secret: &Self::Encoding,
33        writer: &mut impl Write,
34    ) -> std::io::Result<usize> {
35        secret.to_entropy().consensus_encode(writer)
36    }
37
38    fn consensus_decode_partial(
39        reader: &mut impl Read,
40    ) -> Result<Self::Encoding, fedimint_core::encoding::DecodeError> {
41        let bytes = Vec::<u8>::consensus_decode_partial(reader, &ModuleRegistry::default())?;
42        Mnemonic::from_entropy(&bytes).map_err(DecodeError::from_err)
43    }
44
45    fn random<R>(rng: &mut R) -> Self::Encoding
46    where
47        R: RngCore + CryptoRng,
48    {
49        Mnemonic::generate_in_with(rng, Language::English, WORD_COUNT)
50            .expect("Failed to generate mnemonic, bad word count")
51    }
52}