bitcoindevkit / bdk

A modern, lightweight, descriptor-based wallet library written in Rust!
Other
838 stars 301 forks source link

Enable rust-bip39 "rand" feature on `bip39` (optional) dependency. #1546

Open NicolaLS opened 1 month ago

NicolaLS commented 1 month ago

Describe the enhancement
Enable rust-bip39 rand feature by default or using another feature on bdk_wallet. This will make generate_with_in method available.

Use case
Provide the full API of rust-bip39 for developers that want to use bdk_wallet to crate bip39 mnemonics and don't want to worry about creating the entropy themselves. Instead of having to implement logic to create some entropy from 12-24 words, developers could use bdk_wallet::keys::bip39::Mnemonic::generate_in_with(&mut rng, Language::English, 24).

Additional context

Suggested solution Either enable rand per default:

# Optional dependencies
bip39 = { version = "2.0", optional = true features = ["rand"]}

Or enable it with another feature:

[features]
keys-bip39 = ["bip39"]
keys-bip39-rand = ["bip39/rand"]
storopoli commented 1 month ago

Related to #1395.

ValuedMammal commented 2 weeks ago

I think it's possible to have the equivalent of generate_in_with currently in BDK (assuming "keys-bip39" and "std" features are enabled)

fn main() {
    use bdk_wallet::bip39::Mnemonic;
    use bdk_wallet::bitcoin::key::rand::thread_rng;
    use bdk_wallet::keys::bip39::{Language, WordCount};
    use bdk_wallet::keys::{GeneratableKey, GeneratedKey};
    use bdk_wallet::miniscript::Tap;

    let mnemonic: GeneratedKey<Mnemonic, Tap> = Mnemonic::generate_with_aux_rand(
        (WordCount::Words12, Language::English),
        &mut thread_rng(),
    )
    .expect("should work");

    println!("{}", *mnemonic);
}

But since this is not immediately obvious, I'm also in favor of enabling bip39/rand_core by default, so something like

[features]
keys-bip39 = ["bip39/rand_core"]