Skip to content

Commit 23f26dc

Browse files
authored
feat(bip0039)!: add Language::words and default word_of implementation (#132)
Require Language implementors to provide the full 2048-word list and derive word_of from words() by default. BREAKING CHANGE: custom Language implementations must now implement words().
1 parent ae16942 commit 23f26dc

1 file changed

Lines changed: 12 additions & 20 deletions

File tree

bip0039/src/language/mod.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,37 @@ use self::wordlist::*;
1515
/// Language to be used for the mnemonic phrase.
1616
///
1717
/// Consumers may implement this trait for their own language types by providing:
18+
/// - [`Language::words`]
1819
/// - [`Language::word_of`]
1920
/// - [`Language::index_of`]
2021
///
2122
/// Built-in languages implement a crate-private `WordlistProvider` and automatically
2223
/// get this trait via the blanket impl below.
2324
///
2425
/// # Requirements
25-
///
26-
/// - `word_of(index)` must return a valid word for all indices `0..2048`.
26+
/// - `words()` must return the full underlying word list for this language (2048 words) in BIP-0039
27+
/// order, not just a view of a specific mnemonic, and must be NFKD-normalized and unique.
28+
/// - `word_of(index)` must return a valid word for all indices [0..2048).
2729
/// - `index_of(word)` must return the correct index (BIP-0039 order) for all words in the language
2830
/// wordlist; return `None` for unknown words.
2931
pub trait Language: Sized {
30-
// NOTE (planned breaking change): we intend to add the following method in the next
31-
// minor release (e.g. `0.14.0`), and treat it as a breaking change for external
32-
// `Language` implementations:
33-
//
34-
// /// Returns the full BIP-0039 word list for this language (2048 words) in BIP-0039 order.
35-
// ///
36-
// /// Notes:
37-
// /// - This returns the full underlying word list, not just a view of a specific mnemonic.
38-
// /// - The returned words must be NFKD-normalized and unique.
39-
// fn words() -> &'static [&'static str; 2048];
32+
/// Returns the full BIP-0039 word list for this language (2048 words) in BIP-0039 order.
33+
fn words() -> &'static [&'static str; 2048];
4034

4135
/// Returns the word at `index` (BIP-0039 order).
42-
fn word_of(index: usize) -> &'static str;
36+
fn word_of(index: usize) -> &'static str {
37+
debug_assert!(index < 2048, "Invalid wordlist index");
38+
Self::words()[index]
39+
}
4340

4441
/// Returns the index of `word` in the word list (BIP-0039 order).
4542
fn index_of(word: &str) -> Option<usize>;
4643
}
4744

4845
impl<T: WordlistProvider> Language for T {
49-
// fn words() -> &'static [&'static str; 2048] {
50-
// <T as WordlistProvider>::wordlist().words
51-
// }
52-
5346
#[inline]
54-
fn word_of(index: usize) -> &'static str {
55-
debug_assert!(index < 2048, "Invalid wordlist index");
56-
<T as WordlistProvider>::wordlist().words[index]
47+
fn words() -> &'static [&'static str; 2048] {
48+
<T as WordlistProvider>::wordlist().words
5749
}
5850

5951
#[inline]

0 commit comments

Comments
 (0)