Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bip0039/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ spanish = []
[dependencies]
hmac = { version = "0.13", default-features = false }
pbkdf2 = { version = "0.13", default-features = false }
phf = { version = "0.13", default-features = false }
phf = { version = "0.13", default-features = false, features = ["macros"] }
rand = { version = "0.10", optional = true }
sha2 = { version = "0.11", default-features = false }
unicode-normalization = { version = "0.1", default-features = false }
zeroize = { version = "1.8", default-features = false, features = ["alloc"] }

[build-dependencies]
anyhow = "1.0"
phf_codegen = "0.13"

[dev-dependencies]
const-hex = "1.17"
Expand Down
22 changes: 7 additions & 15 deletions bip0039/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Build script to generate BIP-0039 wordlists and PHF indices from `words/*.txt`.
//! Build script to generate BIP-0039 wordlists and PHF macro input from `words/*.txt`.
//!
//! This script generates one Rust source file per language into `OUT_DIR`:
//! - `bip0039_wordlist_<lang>.rs`
Expand Down Expand Up @@ -77,18 +77,6 @@ fn generate_one(out_dir: &Path, lang: &str, input_path: &Path) -> Result<()> {
words.len()
);

// Create PHF map (word -> index).
//
// `phf_codegen::Map::entry` stores borrowed `&str` values internally while we build it.
// So we must ensure backing strings live long enough and are not moved/reallocated
// during insertion. We do that by precomputing all value strings first, then inserting.
let index_values: Vec<String> = (0..words.len()).map(|i| format!("{i}u16")).collect();

let mut index_map = phf_codegen::Map::new();
for (w, v) in words.iter().zip(index_values.iter()) {
index_map.entry(w, v.as_str());
}

let out_name = format!("bip0039_wordlist_{lang}.rs");
let out_path = out_dir.join(&out_name);

Expand All @@ -113,8 +101,12 @@ fn generate_one(out_dir: &Path, lang: &str, input_path: &Path) -> Result<()> {
writeln!(f, "];\n").context("failed writing WORDS footer")?;

// INDEX
writeln!(f, "pub static INDEX: ::phf::Map<&'static str, u16> = {};\n", index_map.build())
.context("failed writing INDEX")?;
writeln!(f, "pub static INDEX: ::phf::Map<&'static str, u16> = ::phf::phf_map! {{")
.context("failed writing INDEX header")?;
for (i, w) in words.iter().enumerate() {
writeln!(f, " {:?} => {i}u16,", w).context("failed writing an INDEX entry")?;
}
writeln!(f, "}};\n").context("failed writing INDEX footer")?;

// WORDLIST
writeln!(
Expand Down
Loading