fedimint_bip39/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#![deny(clippy::pedantic)]

//! BIP39 client secret support crate

use std::io::{Read, Write};

pub use bip39::{Language, Mnemonic};
use fedimint_client::derivable_secret::DerivableSecret;
use fedimint_client::secret::RootSecretStrategy;
use fedimint_core::encoding::{Decodable, DecodeError, Encodable};
use fedimint_core::module::registry::ModuleRegistry;
use rand::{CryptoRng, RngCore};

/// BIP39 root secret encoding strategy allowing retrieval of the seed phrase.
#[derive(Debug)]
pub struct Bip39RootSecretStrategy<const WORD_COUNT: usize = 12>;

impl<const WORD_COUNT: usize> RootSecretStrategy for Bip39RootSecretStrategy<WORD_COUNT> {
    type Encoding = Mnemonic;

    fn to_root_secret(secret: &Self::Encoding) -> DerivableSecret {
        const FEDIMINT_CLIENT_NONCE: &[u8] = b"Fedimint Client Salt";
        const EMPTY_PASSPHRASE: &str = "";

        DerivableSecret::new_root(
            secret.to_seed_normalized(EMPTY_PASSPHRASE).as_ref(),
            FEDIMINT_CLIENT_NONCE,
        )
    }

    fn consensus_encode(
        secret: &Self::Encoding,
        writer: &mut impl Write,
    ) -> std::io::Result<usize> {
        secret.to_entropy().consensus_encode(writer)
    }

    fn consensus_decode(
        reader: &mut impl Read,
    ) -> Result<Self::Encoding, fedimint_core::encoding::DecodeError> {
        let bytes = Vec::<u8>::consensus_decode(reader, &ModuleRegistry::default())?;
        Mnemonic::from_entropy(&bytes).map_err(DecodeError::from_err)
    }

    fn random<R>(rng: &mut R) -> Self::Encoding
    where
        R: RngCore + CryptoRng,
    {
        Mnemonic::generate_in_with(rng, Language::English, WORD_COUNT)
            .expect("Failed to generate mnemonic, bad word count")
    }
}