Skip to main content

kdk_mnemonic/
dice.rs

1use kdk_entropy::{dice_to_entropy, Dice, EntropyError};
2use kdk_zeroize::SensitiveBytes;
3
4use crate::error::MnemonicError;
5use crate::game::{GameMnemonic, SensitiveGame};
6
7/// Dice-roll BIP39 mnemonic. `FACES` is the die size (e.g. 6, 20);
8/// `W` is the word count `in [12, 15, 18, 21, 24]`.
9pub type DiceMnemonic<const FACES: u8, const W: u8> = GameMnemonic<Dice<FACES>, W>;
10
11impl<const FACES: u8> SensitiveGame for Dice<FACES> {
12    fn to_entropy<const N: usize>(input: &[u8]) -> Result<SensitiveBytes<N, Self>, EntropyError> {
13        dice_to_entropy::<FACES, N>(input)
14    }
15}
16
17/// Dice rolls to BIP39 mnemonic. `FACES` is the die size and `W` the
18/// word count; the matching `words-<W>` Cargo feature must be enabled.
19///
20/// Requires the `dice` and `words-<W>` features.
21///
22/// # Example
23///
24/// ```
25/// use kdk_mnemonic::{dice_mnemonic, DiceMnemonic};
26///
27/// let rolls = [1u8; 50];
28/// let _: DiceMnemonic<6, 12> = dice_mnemonic::<6, 12>(&rolls).unwrap();
29/// ```
30pub fn dice_mnemonic<const FACES: u8, const W: u8>(
31    rolls: &[u8],
32) -> Result<DiceMnemonic<FACES, W>, MnemonicError> {
33    <Dice<FACES> as SensitiveGame>::mnemonic::<W>(rolls)
34}