Skip to content

Commit

Permalink
fix(bdk): remove rand dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Jun 17, 2024
1 parent 0543801 commit 736e14e
Show file tree
Hide file tree
Showing 20 changed files with 412 additions and 297 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cont_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:
uses: Swatinem/[email protected]
- name: Check bdk wallet
working-directory: ./crates/wallet
run: cargo check --target wasm32-unknown-unknown --no-default-features --features miniscript/no-std,bdk_chain/hashbrown,dev-getrandom-wasm
run: cargo check --target wasm32-unknown-unknown --no-default-features --features miniscript/no-std,bdk_chain/hashbrown
- name: Check esplora
working-directory: ./crates/esplora
run: cargo check --target wasm32-unknown-unknown --no-default-features --features miniscript/no-std,bdk_chain/hashbrown,async
Expand Down
14 changes: 3 additions & 11 deletions crates/wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,31 @@ edition = "2021"
rust-version = "1.63"

[dependencies]
rand = "^0.8"
rand_core = { version = "0.6.0" }
miniscript = { version = "12.0.0", features = ["serde"], default-features = false }
bitcoin = { version = "0.32.0", features = ["serde", "base64", "rand-std"], default-features = false }
bitcoin = { version = "0.32.0", features = ["serde", "base64"], default-features = false }
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1.0" }
bdk_chain = { path = "../chain", version = "0.16.0", features = ["miniscript", "serde"], default-features = false }

# Optional dependencies
bip39 = { version = "2.0", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = "0.2"
js-sys = "0.3"

[features]
default = ["std"]
std = ["bitcoin/std", "miniscript/std", "bdk_chain/std"]
compiler = ["miniscript/compiler"]
all-keys = ["keys-bip39"]
keys-bip39 = ["bip39"]

# This feature is used to run `cargo check` in our CI targeting wasm. It's not recommended
# for libraries to explicitly include the "getrandom/js" feature, so we only do it when
# necessary for running our CI. See: https://docs.rs/getrandom/0.2.8/getrandom/#webassembly-support
dev-getrandom-wasm = ["getrandom/js"]

[dev-dependencies]
lazy_static = "1.4"
assert_matches = "1.5.0"
tempfile = "3"
bdk_sqlite = { path = "../sqlite" }
bdk_file_store = { path = "../file_store" }
anyhow = "1"
rand = "^0.8"

[package.metadata.docs.rs]
all-features = true
Expand Down
3 changes: 2 additions & 1 deletion crates/wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ fn main() {
<!-- use bitcoin::base64; -->
<!-- use bdk_wallet::bitcoin::consensus::serialize; -->
<!-- use bdk_wallet::bitcoin::Network; -->
<!-- use rand::thread_rng(); -->

<!-- fn main() -> Result<(), bdk_wallet::Error> { -->
<!-- let blockchain = ElectrumBlockchain::from(Client::new("ssl://electrum.blockstream.info:60002")?); -->
Expand All @@ -173,7 +174,7 @@ fn main() {
<!-- .enable_rbf() -->
<!-- .do_not_spend_change() -->
<!-- .fee_rate(FeeRate::from_sat_per_vb(5.0)); -->
<!-- builder.finish()? -->
<!-- builder.finish_with_aux_rand(&mut thread_rng())? -->
<!-- }; -->

<!-- println!("Transaction details: {:#?}", details); -->
Expand Down
4 changes: 3 additions & 1 deletion crates/wallet/examples/mnemonic_to_descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use bdk_wallet::descriptor::IntoWalletDescriptor;
use bdk_wallet::keys::bip39::{Language, Mnemonic, WordCount};
use bdk_wallet::keys::{GeneratableKey, GeneratedKey};
use bdk_wallet::miniscript::Tap;
use rand::thread_rng;
use std::str::FromStr;

/// This example demonstrates how to generate a mnemonic phrase
Expand All @@ -25,8 +26,9 @@ fn main() -> Result<(), anyhow::Error> {
// In this example we are generating a 12 words mnemonic phrase
// but it is also possible generate 15, 18, 21 and 24 words
// using their respective `WordCount` variant.
let mut rng = thread_rng();
let mnemonic: GeneratedKey<_, Tap> =
Mnemonic::generate((WordCount::Words12, Language::English))
Mnemonic::generate((WordCount::Words12, Language::English), &mut rng)
.map_err(|_| anyhow!("Mnemonic generation error"))?;

println!("Mnemonic phrase: {}", *mnemonic);
Expand Down
8 changes: 5 additions & 3 deletions crates/wallet/src/keys/bip39.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ mod test {

use bip39::{Language, Mnemonic};

use rand::thread_rng;

use crate::keys::{any_network, GeneratableKey, GeneratedKey};

use super::WordCount;
Expand Down Expand Up @@ -216,12 +218,12 @@ mod test {

#[test]
fn test_keys_generate_bip39_random() {
let mut rng = thread_rng();
let generated_mnemonic: GeneratedKey<_, miniscript::Segwitv0> =
Mnemonic::generate((WordCount::Words12, Language::English)).unwrap();
Mnemonic::generate((WordCount::Words12, Language::English), &mut rng).unwrap();
assert_eq!(generated_mnemonic.valid_networks, any_network());

let generated_mnemonic: GeneratedKey<_, miniscript::Segwitv0> =
Mnemonic::generate((WordCount::Words24, Language::English)).unwrap();
Mnemonic::generate((WordCount::Words24, Language::English), &mut rng).unwrap();
assert_eq!(generated_mnemonic.valid_networks, any_network());
}
}
17 changes: 11 additions & 6 deletions crates/wallet/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use core::marker::PhantomData;
use core::ops::Deref;
use core::str::FromStr;

use rand_core::{CryptoRng, RngCore};

use bitcoin::secp256k1::{self, Secp256k1, Signing};

use bitcoin::bip32;
Expand Down Expand Up @@ -632,11 +634,12 @@ pub trait GeneratableKey<Ctx: ScriptContext>: Sized {
) -> Result<GeneratedKey<Self, Ctx>, Self::Error>;

/// Generate a key given the options with a random entropy
fn generate(options: Self::Options) -> Result<GeneratedKey<Self, Ctx>, Self::Error> {
use rand::{thread_rng, Rng};

fn generate(
options: Self::Options,
rng: &mut (impl CryptoRng + RngCore),
) -> Result<GeneratedKey<Self, Ctx>, Self::Error> {
let mut entropy = Self::Entropy::default();
thread_rng().fill(entropy.as_mut());
rng.fill_bytes(entropy.as_mut());
Self::generate_with_entropy(options, entropy)
}
}
Expand All @@ -657,8 +660,10 @@ where
}

/// Generate a key with the default options and a random entropy
fn generate_default() -> Result<GeneratedKey<Self, Ctx>, Self::Error> {
Self::generate(Default::default())
fn generate_default(
rng: &mut (impl CryptoRng + RngCore),
) -> Result<GeneratedKey<Self, Ctx>, Self::Error> {
Self::generate(Default::default(), rng)
}
}

Expand Down
Loading

0 comments on commit 736e14e

Please sign in to comment.