Skip to content

chore(deps): bump rand to 0.9.1, remove rand_core #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions wallet/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.wasm32-unknown-unknown]
rustflags = ["--cfg", "getrandom_backend=\"wasm_js\""]
6 changes: 4 additions & 2 deletions wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rust-version = "1.63"
workspace = true

[dependencies]
rand_core = { version = "0.6.0" }
rand = { version = "0.9.1" }
miniscript = { version = "12.3.1", features = [ "serde" ], default-features = false }
bitcoin = { version = "0.32.4", features = [ "serde", "base64" ], default-features = false }
serde = { version = "^1.0", features = ["derive"] }
Expand All @@ -27,6 +27,9 @@ bdk_chain = { version = "0.22.0", features = [ "miniscript", "serde" ], default-
bip39 = { version = "2.0", optional = true }
bdk_file_store = { version = "0.20.0", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.3", features = ["wasm_js"] }

[features]
default = ["std"]
std = ["bitcoin/std", "bitcoin/rand-std", "miniscript/std", "bdk_chain/std"]
Expand All @@ -43,7 +46,6 @@ tempfile = "3"
bdk_chain = { version = "0.22.0", features = ["rusqlite"] }
bdk_wallet = { path = ".", features = ["rusqlite", "file_store", "test-utils"] }
anyhow = "1"
rand = "^0.8"

[package.metadata.docs.rs]
all-features = true
Expand Down
10 changes: 5 additions & 5 deletions wallet/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use core::marker::PhantomData;
use core::ops::Deref;
use core::str::FromStr;

use rand_core::{CryptoRng, RngCore};
use rand::CryptoRng;

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

Expand Down Expand Up @@ -645,15 +645,15 @@ pub trait GeneratableKey<Ctx: ScriptContext>: Sized {
/// Uses the thread-local random number generator.
#[cfg(feature = "std")]
fn generate(options: Self::Options) -> Result<GeneratedKey<Self, Ctx>, Self::Error> {
Self::generate_with_aux_rand(options, &mut bitcoin::key::rand::thread_rng())
Self::generate_with_aux_rand(options, &mut rand::rng())
}

/// Generate a key given the options with random entropy.
///
/// Uses a provided random number generator (rng).
fn generate_with_aux_rand(
options: Self::Options,
rng: &mut (impl CryptoRng + RngCore),
rng: &mut impl CryptoRng,
) -> Result<GeneratedKey<Self, Ctx>, Self::Error> {
let mut entropy = Self::Entropy::default();
rng.fill_bytes(entropy.as_mut());
Expand Down Expand Up @@ -682,14 +682,14 @@ where
/// Uses the thread-local random number generator.
#[cfg(feature = "std")]
fn generate_default() -> Result<GeneratedKey<Self, Ctx>, Self::Error> {
Self::generate_with_aux_rand(Default::default(), &mut bitcoin::key::rand::thread_rng())
Self::generate_with_aux_rand(Default::default(), &mut rand::rng())
}

/// Generate a key with the default options and a random entropy
///
/// Uses a provided random number generator (rng).
fn generate_default_with_aux_rand(
rng: &mut (impl CryptoRng + RngCore),
rng: &mut impl CryptoRng,
) -> Result<GeneratedKey<Self, Ctx>, Self::Error> {
Self::generate_with_aux_rand(Default::default(), rng)
}
Expand Down
64 changes: 32 additions & 32 deletions wallet/src/wallet/coin_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//! # use bdk_wallet::*;
//! # use bdk_wallet::coin_selection::decide_change;
//! # use anyhow::Error;
//! # use rand_core::RngCore;
//! # use rand::RngCore;
//! #[derive(Debug)]
//! struct AlwaysSpendEverything;
//!
Expand Down Expand Up @@ -113,7 +113,7 @@ use bitcoin::{Script, Weight};

use core::convert::TryInto;
use core::fmt::{self, Formatter};
use rand_core::RngCore;
use rand::RngCore;

use super::utils::shuffle_slice;
/// Default coin selection algorithm used by [`TxBuilder`](super::tx_builder::TxBuilder) if not
Expand Down Expand Up @@ -737,7 +737,7 @@ mod test {
use crate::types::*;

use rand::prelude::SliceRandom;
use rand::{thread_rng, Rng, RngCore, SeedableRng};
use rand::{rng, Rng, RngCore, SeedableRng};

// signature len (1WU) + signature and sighash (72WU)
// + pubkey len (1WU) + pubkey (33WU)
Expand Down Expand Up @@ -832,13 +832,13 @@ mod test {
))
.unwrap(),
txout: TxOut {
value: Amount::from_sat(rng.gen_range(0..200000000)),
value: Amount::from_sat(rng.random_range(0..200000000)),
script_pubkey: ScriptBuf::new(),
},
keychain: KeychainKind::External,
is_spent: false,
derivation_index: rng.next_u32(),
chain_position: if rng.gen_bool(0.5) {
chain_position: if rng.random_bool(0.5) {
ChainPosition::Confirmed {
anchor: ConfirmationBlockTime {
block_id: chain::BlockId {
Expand Down Expand Up @@ -882,7 +882,7 @@ mod test {
}

fn sum_random_utxos(mut rng: &mut StdRng, utxos: &mut [WeightedUtxo]) -> Amount {
let utxos_picked_len = rng.gen_range(2..utxos.len() / 2);
let utxos_picked_len = rng.random_range(2..utxos.len() / 2);
utxos.shuffle(&mut rng);
utxos[..utxos_picked_len]
.iter()
Expand Down Expand Up @@ -913,7 +913,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand All @@ -935,7 +935,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand All @@ -957,7 +957,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand All @@ -978,7 +978,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
);
assert!(matches!(result, Err(InsufficientFunds { .. })));
}
Expand All @@ -995,7 +995,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1000),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
);
assert!(matches!(result, Err(InsufficientFunds { .. })));
}
Expand All @@ -1013,7 +1013,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand All @@ -1035,7 +1035,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand All @@ -1057,7 +1057,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand All @@ -1078,7 +1078,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
);
assert!(matches!(result, Err(InsufficientFunds { .. })));
}
Expand All @@ -1097,7 +1097,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1000),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
);
assert!(matches!(result, Err(InsufficientFunds { .. })));
}
Expand All @@ -1117,7 +1117,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand All @@ -1139,7 +1139,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand All @@ -1163,7 +1163,7 @@ mod test {
fee_rate,
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand All @@ -1187,7 +1187,7 @@ mod test {
fee_rate,
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng,
);

assert!(
Expand Down Expand Up @@ -1258,7 +1258,7 @@ mod test {
fee_rate,
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand All @@ -1279,7 +1279,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
);

assert!(matches!(result, Err(InsufficientFunds { .. })));
Expand All @@ -1297,7 +1297,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(1000),
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
);
assert!(matches!(result, Err(InsufficientFunds { .. })));
}
Expand All @@ -1317,7 +1317,7 @@ mod test {
fee_rate,
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();

Expand Down Expand Up @@ -1346,7 +1346,7 @@ mod test {
FeeRate::ZERO,
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng,
)
.unwrap();
assert_eq!(result.selected_amount(), target_amount);
Expand Down Expand Up @@ -1515,7 +1515,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(10),
Amount::from_sat(500_000),
&drain_script,
&mut thread_rng(),
&mut rng(),
);

assert_matches!(
Expand All @@ -1542,7 +1542,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(10),
Amount::from_sat(500_000),
&drain_script,
&mut thread_rng(),
&mut rng(),
);

assert_matches!(
Expand All @@ -1565,7 +1565,7 @@ mod test {
FeeRate::from_sat_per_vb_unchecked(10_000),
Amount::from_sat(500_000),
&drain_script,
&mut thread_rng(),
&mut rng(),
);

assert_matches!(
Expand Down Expand Up @@ -1595,7 +1595,7 @@ mod test {
feerate,
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
.unwrap();
assert_eq!(res.selected_amount(), Amount::from_sat(200_000));
Expand Down Expand Up @@ -1652,7 +1652,7 @@ mod test {
fee_rate,
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
)
}
CoinSelectionAlgo::OldestFirst => OldestFirstCoinSelection.coin_select(
Expand All @@ -1661,15 +1661,15 @@ mod test {
fee_rate,
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
),
CoinSelectionAlgo::LargestFirst => LargestFirstCoinSelection.coin_select(
vec![],
optional,
fee_rate,
target_amount,
&drain_script,
&mut thread_rng(),
&mut rng(),
),
};

Expand Down
2 changes: 1 addition & 1 deletion wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use miniscript::{
descriptor::KeyMap,
psbt::{PsbtExt, PsbtInputExt, PsbtInputSatisfier},
};
use rand_core::RngCore;
use rand::RngCore;

mod changeset;
pub mod coin_selection;
Expand Down
6 changes: 3 additions & 3 deletions wallet/src/wallet/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use bitcoin::{
absolute, transaction::Version, Amount, FeeRate, OutPoint, ScriptBuf, Sequence, Transaction,
TxIn, TxOut, Txid, Weight,
};
use rand_core::RngCore;
use rand::RngCore;

use super::coin_selection::CoinSelectionAlgorithm;
use super::utils::shuffle_slice;
Expand Down Expand Up @@ -689,7 +689,7 @@ impl<Cs: CoinSelectionAlgorithm> TxBuilder<'_, Cs> {
/// or more calls to this method before closing the wallet. See [`Wallet::reveal_next_address`].
#[cfg(feature = "std")]
pub fn finish(self) -> Result<Psbt, CreateTxError> {
self.finish_with_aux_rand(&mut bitcoin::key::rand::thread_rng())
self.finish_with_aux_rand(&mut rand::rng())
}

/// Finish building the transaction.
Expand Down Expand Up @@ -804,7 +804,7 @@ impl TxOrdering {
/// Uses the thread-local random number generator (rng).
#[cfg(feature = "std")]
pub fn sort_tx(&self, tx: &mut Transaction) {
self.sort_tx_with_aux_rand(tx, &mut bitcoin::key::rand::thread_rng())
self.sort_tx_with_aux_rand(tx, &mut rand::rng())
}

/// Sort transaction inputs and outputs by [`TxOrdering`] variant.
Expand Down
Loading
Loading