From c1a40b1ac01efce1ad2d363fe0212c2e2e6fcb6f Mon Sep 17 00:00:00 2001 From: Anton Yemelyanov Date: Wed, 20 Sep 2023 20:15:11 +0300 Subject: [PATCH] add bip32 word list iterator --- wallet/bip32/src/mnemonic/language.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/wallet/bip32/src/mnemonic/language.rs b/wallet/bip32/src/mnemonic/language.rs index 68b276692..b06207477 100644 --- a/wallet/bip32/src/mnemonic/language.rs +++ b/wallet/bip32/src/mnemonic/language.rs @@ -54,6 +54,29 @@ impl WordList { pub fn get_word(&self, bits: Bits11) -> &'static str { self.inner[bits.bits() as usize] } + + pub fn iter(&self) -> WordListIterator<'_> { + WordListIterator { wordlist: self, index: 0 } + } +} + +pub struct WordListIterator<'a> { + wordlist: &'a WordList, + index: usize, +} + +impl Iterator for WordListIterator<'_> { + type Item = &'static str; + + fn next(&mut self) -> Option { + if self.index < self.wordlist.inner.len() { + let word = self.wordlist.inner[self.index]; + self.index += 1; + Some(word) + } else { + None + } + } } // TODO(tarcieri): use `const fn` instead of `Lazy`