diff --git a/crates/util/keyfork-mnemonic-util/src/lib.rs b/crates/util/keyfork-mnemonic-util/src/lib.rs index 90190a5..4480443 100644 --- a/crates/util/keyfork-mnemonic-util/src/lib.rs +++ b/crates/util/keyfork-mnemonic-util/src/lib.rs @@ -48,19 +48,26 @@ impl Error for MnemonicGenerationError {} #[derive(Debug, Clone)] pub struct Wordlist(Vec); +static ENGLISH: OnceLock = OnceLock::new(); + impl Default for Wordlist { /// Returns the English wordlist in the Bitcoin BIP-0039 specification. fn default() -> Self { // TODO: English is the only supported language. - let wordlist_file = include_str!("data/wordlist.txt"); - Wordlist( - wordlist_file - .lines() - // skip 1: comment at top of file to point to BIP-0039 source. - .skip(1) - .map(|x| x.trim().to_string()) - .collect(), - ) + ENGLISH + .get_or_init(|| { + let wordlist_file = include_str!("data/wordlist.txt"); + Wordlist( + wordlist_file + .lines() + // skip 1: comment at top of file to point to BIP-0039 source. + .skip(1) + .map(|x| x.trim().to_string()) + .collect(), + ) + .shrank() + }) + .clone() } } @@ -71,6 +78,12 @@ impl Wordlist { Arc::new(self) } + /// Return a shrank version of the Wordlist + pub fn shrank(mut self) -> Self { + self.0.shrink_to_fit(); + self + } + /// Determine whether the Wordlist contains a given word. pub fn contains(&self, word: &str) -> bool { self.0.iter().any(|w| w.as_str() == word)