|
9 | 9 |
|
10 | 10 | use std::fmt; |
11 | 11 |
|
12 | | -use bip39::rand::rngs::OsRng; |
13 | | -use bip39::{Language, Mnemonic as Bip39Mnemonic}; |
14 | | - |
15 | 12 | use crate::config::WALLET_KEYS_SEED_LEN; |
16 | | -use crate::ffi::{maybe_deref, maybe_wrap}; |
| 13 | +use crate::ffi::maybe_deref; |
17 | 14 | use crate::io; |
18 | 15 |
|
19 | 16 | #[cfg(not(feature = "uniffi"))] |
20 | | -type Mnemonic = Bip39Mnemonic; |
| 17 | +type Mnemonic = bip39::Mnemonic; |
21 | 18 | #[cfg(feature = "uniffi")] |
22 | 19 | type Mnemonic = std::sync::Arc<crate::ffi::Mnemonic>; |
23 | 20 |
|
@@ -124,89 +121,3 @@ impl fmt::Debug for NodeEntropy { |
124 | 121 | write!(f, "NODE ENTROPY") |
125 | 122 | } |
126 | 123 | } |
127 | | - |
128 | | -/// Generates a random [BIP 39] mnemonic with the specified word count. |
129 | | -/// |
130 | | -/// If no word count is specified, defaults to 24 words (256-bit entropy). |
131 | | -/// |
132 | | -/// The result may be used to initialize the [`NodeEntropy`], i.e., can be given to |
133 | | -/// [`NodeEntropy::from_bip39_mnemonic`]. |
134 | | -/// |
135 | | -/// [BIP 39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki |
136 | | -/// [`Node`]: crate::Node |
137 | | -pub fn generate_entropy_mnemonic(word_count: Option<WordCount>) -> Mnemonic { |
138 | | - let word_count = word_count.unwrap_or(WordCount::Words24).word_count(); |
139 | | - let mnemonic = Bip39Mnemonic::generate_in_with(&mut OsRng, Language::English, word_count) |
140 | | - .expect("Failed to generate mnemonic"); |
141 | | - maybe_wrap(mnemonic) |
142 | | -} |
143 | | - |
144 | | -/// Supported BIP39 mnemonic word counts for entropy generation. |
145 | | -#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
146 | | -#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))] |
147 | | -pub enum WordCount { |
148 | | - /// 12-word mnemonic (128-bit entropy) |
149 | | - Words12, |
150 | | - /// 15-word mnemonic (160-bit entropy) |
151 | | - Words15, |
152 | | - /// 18-word mnemonic (192-bit entropy) |
153 | | - Words18, |
154 | | - /// 21-word mnemonic (224-bit entropy) |
155 | | - Words21, |
156 | | - /// 24-word mnemonic (256-bit entropy) |
157 | | - Words24, |
158 | | -} |
159 | | - |
160 | | -impl WordCount { |
161 | | - /// Returns the word count as a usize value. |
162 | | - pub fn word_count(&self) -> usize { |
163 | | - match self { |
164 | | - WordCount::Words12 => 12, |
165 | | - WordCount::Words15 => 15, |
166 | | - WordCount::Words18 => 18, |
167 | | - WordCount::Words21 => 21, |
168 | | - WordCount::Words24 => 24, |
169 | | - } |
170 | | - } |
171 | | -} |
172 | | - |
173 | | -#[cfg(test)] |
174 | | -mod tests { |
175 | | - use super::*; |
176 | | - |
177 | | - #[test] |
178 | | - fn mnemonic_to_entropy_to_mnemonic() { |
179 | | - // Test default (24 words) |
180 | | - let mnemonic = generate_entropy_mnemonic(None); |
181 | | - let mnemonic_inner = maybe_deref(&mnemonic); |
182 | | - let entropy = mnemonic_inner.to_entropy(); |
183 | | - assert_eq!(mnemonic_inner, &Bip39Mnemonic::from_entropy(&entropy).unwrap()); |
184 | | - assert_eq!(mnemonic_inner.word_count(), 24); |
185 | | - |
186 | | - // Test with different word counts |
187 | | - let word_counts = [ |
188 | | - WordCount::Words12, |
189 | | - WordCount::Words15, |
190 | | - WordCount::Words18, |
191 | | - WordCount::Words21, |
192 | | - WordCount::Words24, |
193 | | - ]; |
194 | | - |
195 | | - for word_count in word_counts { |
196 | | - let mnemonic = generate_entropy_mnemonic(Some(word_count)); |
197 | | - let mnemonic_inner = maybe_deref(&mnemonic); |
198 | | - let entropy = mnemonic_inner.to_entropy(); |
199 | | - assert_eq!(mnemonic_inner, &Bip39Mnemonic::from_entropy(&entropy).unwrap()); |
200 | | - |
201 | | - // Verify expected word count |
202 | | - let expected_words = match word_count { |
203 | | - WordCount::Words12 => 12, |
204 | | - WordCount::Words15 => 15, |
205 | | - WordCount::Words18 => 18, |
206 | | - WordCount::Words21 => 21, |
207 | | - WordCount::Words24 => 24, |
208 | | - }; |
209 | | - assert_eq!(mnemonic_inner.word_count(), expected_words); |
210 | | - } |
211 | | - } |
212 | | -} |
0 commit comments