diff --git a/Cargo.toml b/Cargo.toml index bda2d3eb..22a42590 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,8 @@ getrandom = "0.2" js-sys = "0.3" [features] +default = ["std"] +std = [] compiler = ["miniscript/compiler"] all-keys = ["keys-bip39"] keys-bip39 = ["bip39"] @@ -66,6 +68,7 @@ required-features = ["compiler"] [[example]] name = "policy" path = "examples/policy.rs" +required-features = ["std"] [[example]] name = "mnemonic_to_descriptors" diff --git a/bdk_test_client/src/lib.rs b/bdk_test_client/src/lib.rs index aebcb457..6ae446bf 100644 --- a/bdk_test_client/src/lib.rs +++ b/bdk_test_client/src/lib.rs @@ -5,15 +5,15 @@ use bitcoin::{Address, PackedLockTime, Script, Sequence, Transaction, Txid, Witn pub use bitcoincore_rpc::bitcoincore_rpc_json::AddressType; use bitcoincore_rpc::jsonrpc::serde_json::{self, json}; pub use bitcoincore_rpc::{Auth, Client as RpcClient, Error as RpcError, RpcApi}; +use core::ops::Deref; use core::str::FromStr; +use core::time::Duration; use electrsd::bitcoind::BitcoinD; use electrsd::{bitcoind, ElectrsD}; pub use electrum_client::{Client as ElectrumClient, ElectrumApi}; #[allow(unused_imports)] use log::{debug, error, info, log_enabled, trace, Level}; use std::env; -use std::ops::Deref; -use std::time::Duration; pub struct TestClient { pub bitcoind: BitcoinD, diff --git a/src/descriptor/checksum.rs b/src/descriptor/checksum.rs index b4ba0e8f..07120ab7 100644 --- a/src/descriptor/checksum.rs +++ b/src/descriptor/checksum.rs @@ -15,6 +15,7 @@ //! checksum of a descriptor use crate::descriptor::DescriptorError; +use alloc::string::String; const INPUT_CHARSET: &[u8] = b"0123456789()[],'/*abcdefgh@:$%{}IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~ijklmnopqrstuvwxyzABCDEFGH`#\"\\ "; const CHECKSUM_CHARSET: &[u8] = b"qpzry9x8gf2tvdw0s3jn54khce6mua7l"; @@ -170,7 +171,7 @@ mod test { #[test] fn test_calc_checksum_invalid_character() { - let sparkle_heart = unsafe { std::str::from_utf8_unchecked(&[240, 159, 146, 150]) }; + let sparkle_heart = unsafe { core::str::from_utf8_unchecked(&[240, 159, 146, 150]) }; let invalid_desc = format!("wpkh(tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcL{}fjdi5qUvw3VDfgYiH5mNsj5izuiu2N/1/2/*)", sparkle_heart); assert_matches!( diff --git a/src/descriptor/dsl.rs b/src/descriptor/dsl.rs index 67ef6705..fdd78a46 100644 --- a/src/descriptor/dsl.rs +++ b/src/descriptor/dsl.rs @@ -23,7 +23,7 @@ macro_rules! impl_top_level_sh { }; ( $inner_struct:ident, $constructor:ident, $sortedmulti_constructor:ident, $ctx:ident, sortedmulti $( $inner:tt )* ) => {{ - use std::marker::PhantomData; + use core::marker::PhantomData; use $crate::miniscript::descriptor::{$inner_struct, Descriptor, DescriptorPublicKey}; use $crate::miniscript::$ctx; @@ -35,7 +35,7 @@ macro_rules! impl_top_level_sh { $crate::impl_sortedmulti!(build_desc, sortedmulti $( $inner )*) }}; ( $inner_struct:ident, $constructor:ident, $sortedmulti_constructor:ident, $ctx:ident, sortedmulti_vec $( $inner:tt )* ) => {{ - use std::marker::PhantomData; + use core::marker::PhantomData; use $crate::miniscript::descriptor::{$inner_struct, Descriptor, DescriptorPublicKey}; use $crate::miniscript::$ctx; @@ -203,8 +203,8 @@ macro_rules! impl_node_opcode_two { a_keymap.extend(b_keymap.into_iter()); let minisc = $crate::miniscript::Miniscript::from_ast($crate::miniscript::miniscript::decode::Terminal::$terminal_variant( - std::sync::Arc::new(a_minisc), - std::sync::Arc::new(b_minisc), + alloc::sync::Arc::new(a_minisc), + alloc::sync::Arc::new(b_minisc), ))?; minisc.check_miniscript()?; @@ -234,9 +234,9 @@ macro_rules! impl_node_opcode_three { let networks = $crate::keys::merge_networks(&networks, &c_networks); let minisc = $crate::miniscript::Miniscript::from_ast($crate::miniscript::miniscript::decode::Terminal::$terminal_variant( - std::sync::Arc::new(a_minisc), - std::sync::Arc::new(b_minisc), - std::sync::Arc::new(c_minisc), + alloc::sync::Arc::new(a_minisc), + alloc::sync::Arc::new(b_minisc), + alloc::sync::Arc::new(c_minisc), ))?; minisc.check_miniscript()?; @@ -263,7 +263,7 @@ macro_rules! impl_sortedmulti { )* ]; - keys.into_iter().collect::, _>>() + keys.into_iter().collect::, _>>() .map_err($crate::descriptor::DescriptorError::Key) .and_then(|keys| $crate::keys::make_sortedmulti($thresh, keys, $build_desc, &secp)) }); @@ -274,7 +274,7 @@ macro_rules! impl_sortedmulti { #[macro_export] macro_rules! parse_tap_tree { ( @merge $tree_a:expr, $tree_b:expr) => {{ - use std::sync::Arc; + use alloc::sync::Arc; use $crate::miniscript::descriptor::TapTree; $tree_a @@ -318,7 +318,7 @@ macro_rules! parse_tap_tree { // Single leaf ( $op:ident ( $( $minisc:tt )* ) ) => {{ - use std::sync::Arc; + use alloc::sync::Arc; use $crate::miniscript::descriptor::TapTree; $crate::fragment!( $op ( $( $minisc )* ) ) @@ -337,7 +337,7 @@ macro_rules! apply_modifier { .and_then(|(minisc, keymap, networks)| { let minisc = $crate::miniscript::Miniscript::from_ast( $crate::miniscript::miniscript::decode::Terminal::$terminal_variant( - std::sync::Arc::new(minisc), + alloc::sync::Arc::new(minisc), ), )?; @@ -374,8 +374,8 @@ macro_rules! apply_modifier { $inner.and_then(|(a_minisc, a_keymap, a_networks)| { $crate::impl_leaf_opcode_value_two!( AndV, - std::sync::Arc::new(a_minisc), - std::sync::Arc::new($crate::fragment!(true).unwrap().0) + alloc::sync::Arc::new(a_minisc), + alloc::sync::Arc::new($crate::fragment!(true).unwrap().0) ) .map(|(minisc, _, _)| (minisc, a_keymap, a_networks)) }) @@ -384,8 +384,8 @@ macro_rules! apply_modifier { $inner.and_then(|(a_minisc, a_keymap, a_networks)| { $crate::impl_leaf_opcode_value_two!( OrI, - std::sync::Arc::new($crate::fragment!(false).unwrap().0), - std::sync::Arc::new(a_minisc) + alloc::sync::Arc::new($crate::fragment!(false).unwrap().0), + alloc::sync::Arc::new(a_minisc) ) .map(|(minisc, _, _)| (minisc, a_keymap, a_networks)) }) @@ -394,8 +394,8 @@ macro_rules! apply_modifier { $inner.and_then(|(a_minisc, a_keymap, a_networks)| { $crate::impl_leaf_opcode_value_two!( OrI, - std::sync::Arc::new(a_minisc), - std::sync::Arc::new($crate::fragment!(false).unwrap().0) + alloc::sync::Arc::new(a_minisc), + alloc::sync::Arc::new($crate::fragment!(false).unwrap().0) ) .map(|(minisc, _, _)| (minisc, a_keymap, a_networks)) }) @@ -599,7 +599,7 @@ macro_rules! group_multi_keys { )* ]; - keys.into_iter().collect::, _>>() + keys.into_iter().collect::, _>>() .map_err($crate::descriptor::DescriptorError::Key) }}; } @@ -744,8 +744,8 @@ macro_rules! fragment { ( thresh_vec ( $thresh:expr, $items:expr ) ) => ({ use $crate::miniscript::descriptor::KeyMap; - let (items, key_maps_networks): (Vec<_>, Vec<_>) = $items.into_iter().map(|(a, b, c)| (a, (b, c))).unzip(); - let items = items.into_iter().map(std::sync::Arc::new).collect(); + let (items, key_maps_networks): (alloc::vec::Vec<_>, alloc::vec::Vec<_>) = $items.into_iter().map(|(a, b, c)| (a, (b, c))).unzip(); + let items = items.into_iter().map(alloc::sync::Arc::new).collect(); let (key_maps, valid_networks) = key_maps_networks.into_iter().fold((KeyMap::default(), $crate::keys::any_network()), |(mut keys_acc, net_acc), (key, net)| { keys_acc.extend(key.into_iter()); @@ -760,7 +760,7 @@ macro_rules! fragment { ( thresh ( $thresh:expr, $( $inner:tt )* ) ) => ({ let items = $crate::fragment_internal!( @v $( $inner )* ); - items.into_iter().collect::, _>>() + items.into_iter().collect::, _>>() .and_then(|items| $crate::fragment!(thresh_vec($thresh, items))) }); ( multi_vec ( $thresh:expr, $keys:expr ) ) => ({ @@ -793,12 +793,13 @@ macro_rules! fragment { #[cfg(test)] mod test { + use alloc::string::ToString; use bitcoin::hashes::hex::ToHex; use bitcoin::secp256k1::Secp256k1; use miniscript::descriptor::{DescriptorPublicKey, KeyMap}; use miniscript::{Descriptor, Legacy, Segwitv0}; - use std::str::FromStr; + use core::str::FromStr; use crate::descriptor::{DescriptorError, DescriptorMeta}; use crate::keys::{DescriptorKey, IntoDescriptorKey, ValidNetworks}; diff --git a/src/descriptor/error.rs b/src/descriptor/error.rs index d558c926..83efb01a 100644 --- a/src/descriptor/error.rs +++ b/src/descriptor/error.rs @@ -76,6 +76,7 @@ impl std::fmt::Display for Error { } } +#[cfg(feature = "std")] impl std::error::Error for Error {} impl_error!(bitcoin::util::bip32::Error, Bip32); diff --git a/src/descriptor/mod.rs b/src/descriptor/mod.rs index dd53f9f9..295135f6 100644 --- a/src/descriptor/mod.rs +++ b/src/descriptor/mod.rs @@ -14,7 +14,9 @@ //! This module contains generic utilities to work with descriptors, plus some re-exported types //! from [`miniscript`]. -use std::collections::BTreeMap; +use crate::collections::BTreeMap; +use alloc::string::String; +use alloc::vec::Vec; use bitcoin::util::bip32::{ChildNumber, DerivationPath, ExtendedPubKey, Fingerprint, KeySource}; use bitcoin::util::{psbt, taproot}; @@ -581,7 +583,8 @@ impl DescriptorMeta for ExtendedDescriptor { #[cfg(test)] mod test { - use std::str::FromStr; + use alloc::string::ToString; + use core::str::FromStr; use assert_matches::assert_matches; use bitcoin::consensus::encode::deserialize; diff --git a/src/descriptor/policy.rs b/src/descriptor/policy.rs index 6923ce3f..96889ffa 100644 --- a/src/descriptor/policy.rs +++ b/src/descriptor/policy.rs @@ -36,9 +36,11 @@ //! # Ok::<(), bdk::Error>(()) //! ``` -use std::cmp::max; -use std::collections::{BTreeMap, HashSet, VecDeque}; -use std::fmt; +use crate::collections::{BTreeMap, HashSet, VecDeque}; +use alloc::string::String; +use alloc::vec::Vec; +use core::cmp::max; +use core::fmt; use serde::ser::SerializeMap; use serde::{Serialize, Serializer}; @@ -523,6 +525,7 @@ impl fmt::Display for PolicyError { } } +#[cfg(feature = "std")] impl std::error::Error for PolicyError {} impl Policy { @@ -1146,12 +1149,12 @@ mod test { use crate::descriptor::policy::SatisfiableItem::{EcdsaSignature, Multisig, Thresh}; use crate::keys::{DescriptorKey, IntoDescriptorKey}; use crate::wallet::signer::SignersContainer; + use alloc::{string::ToString, sync::Arc}; use assert_matches::assert_matches; use bitcoin::secp256k1::Secp256k1; use bitcoin::util::bip32; use bitcoin::Network; - use std::str::FromStr; - use std::sync::Arc; + use core::str::FromStr; const TPRV0_STR:&str = "tprv8ZgxMBicQKsPdZXrcHNLf5JAJWFAoJ2TrstMRdSKtEggz6PddbuSkvHKM9oKJyFgZV1B7rw8oChspxyYbtmEXYyg1AjfWbL3ho3XHDpHRZf"; const TPRV1_STR:&str = "tprv8ZgxMBicQKsPdpkqS7Eair4YxjcuuvDPNYmKX3sCniCf16tHEVrjjiSXEkFRnUH77yXc6ZcwHHcLNfjdi5qUvw3VDfgYiH5mNsj5izuiu2N"; @@ -1444,12 +1447,12 @@ mod test { .into_wallet_descriptor(&secp, Network::Testnet) .unwrap(); let signers_container = Arc::new(SignersContainer::build(keymap, &wallet_desc, &secp)); - let policy = wallet_desc + let _policy = wallet_desc .extract_policy(&signers_container, BuildSatisfaction::None, &secp) .unwrap() .unwrap(); - println!("desc policy = {:?}", policy); // TODO remove - // TODO how should this fail with mixed timelocks? + // println!("desc policy = {:?}", policy); // TODO remove + // TODO how should this fail with mixed timelocks? } // - multiple timelocks of the same type should be correctly merged together @@ -1469,12 +1472,12 @@ mod test { .into_wallet_descriptor(&secp, Network::Testnet) .unwrap(); let signers_container = Arc::new(SignersContainer::build(keymap, &wallet_desc, &secp)); - let policy = wallet_desc + let _policy = wallet_desc .extract_policy(&signers_container, BuildSatisfaction::None, &secp) .unwrap() .unwrap(); - println!("desc policy = {:?}", policy); // TODO remove - // TODO how should this merge timelocks? + // println!("desc policy = {:?}", policy); // TODO remove + // TODO how should this merge timelocks? let (prvkey1, _pubkey1, _fingerprint1) = setup_keys(TPRV0_STR, PATH, &secp); let locktime_seconds0 = 500000100; let locktime_seconds1 = 500000200; @@ -1487,12 +1490,12 @@ mod test { .into_wallet_descriptor(&secp, Network::Testnet) .unwrap(); let signers_container = Arc::new(SignersContainer::build(keymap, &wallet_desc, &secp)); - let policy = wallet_desc + let _policy = wallet_desc .extract_policy(&signers_container, BuildSatisfaction::None, &secp) .unwrap() .unwrap(); - println!("desc policy = {:?}", policy); // TODO remove + // println!("desc policy = {:?}", policy); // TODO remove // TODO how should this merge timelocks? } diff --git a/src/descriptor/template.rs b/src/descriptor/template.rs index 996c357a..d6697bec 100644 --- a/src/descriptor/template.rs +++ b/src/descriptor/template.rs @@ -418,7 +418,7 @@ macro_rules! expand_make_bipxx { keychain: KeychainKind, network: Network, ) -> Result, DescriptorError> { - let mut derivation_path = Vec::with_capacity(4); + let mut derivation_path = alloc::vec::Vec::with_capacity(4); derivation_path.push(bip32::ChildNumber::from_hardened_idx(bip)?); match network { @@ -478,7 +478,8 @@ expand_make_bipxx!(segwit_v0, Segwitv0); mod test { // test existing descriptor templates, make sure they are expanded to the right descriptors - use std::str::FromStr; + use alloc::{string::ToString, vec::Vec}; + use core::str::FromStr; use super::*; use crate::descriptor::{DescriptorError, DescriptorMeta}; diff --git a/src/error.rs b/src/error.rs index 707a0433..8cb349a5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,11 +9,11 @@ // You may not use this file except in accordance with one or both of these // licenses. -use std::fmt; - use crate::bitcoin::Network; use crate::{descriptor, wallet}; +use alloc::{string::String, vec::Vec}; use bitcoin::{OutPoint, Txid}; +use core::fmt; /// Errors that can be thrown by the [`Wallet`](crate::wallet::Wallet) #[derive(Debug)] @@ -249,6 +249,7 @@ impl fmt::Display for Error { } } +#[cfg(feature = "std")] impl std::error::Error for Error {} macro_rules! impl_error { @@ -256,7 +257,7 @@ macro_rules! impl_error { impl_error!($from, $to, Error); }; ( $from:ty, $to:ident, $impl_for:ty ) => { - impl std::convert::From<$from> for $impl_for { + impl core::convert::From<$from> for $impl_for { fn from(err: $from) -> Self { <$impl_for>::$to(err) } diff --git a/src/keys/bip39.rs b/src/keys/bip39.rs index c79683ac..78f54493 100644 --- a/src/keys/bip39.rs +++ b/src/keys/bip39.rs @@ -14,6 +14,7 @@ // TODO: maybe write our own implementation of bip39? Seems stupid to have an extra dependency for // something that should be fairly simple to re-implement. +use alloc::string::String; use bitcoin::util::bip32; use bitcoin::Network; @@ -150,7 +151,8 @@ impl GeneratableKey for Mnemonic { #[cfg(test)] mod test { - use std::str::FromStr; + use alloc::string::ToString; + use core::str::FromStr; use bitcoin::util::bip32; diff --git a/src/keys/mod.rs b/src/keys/mod.rs index 84c447fe..0f5e386e 100644 --- a/src/keys/mod.rs +++ b/src/keys/mod.rs @@ -11,11 +11,13 @@ //! Key formats -use std::any::TypeId; -use std::collections::HashSet; -use std::marker::PhantomData; -use std::ops::Deref; -use std::str::FromStr; +use crate::collections::HashSet; +use alloc::string::{String, ToString}; +use alloc::vec::Vec; +use core::any::TypeId; +use core::marker::PhantomData; +use core::ops::Deref; +use core::str::FromStr; use bitcoin::secp256k1::{self, Secp256k1, Signing}; @@ -277,7 +279,7 @@ impl ExtScriptContext for Ctx { /// /// ```compile_fail /// use bdk::bitcoin::PublicKey; -/// use std::str::FromStr; +/// use core::str::FromStr; /// /// use bdk::keys::{DescriptorKey, IntoDescriptorKey, KeyError}; /// @@ -619,7 +621,7 @@ pub trait GeneratableKey: Sized { /// Extra options required by the `generate_with_entropy` type Options; /// Returned error in case of failure - type Error: std::fmt::Debug; + type Error: core::fmt::Debug; /// Generate a key given the extra options and the entropy fn generate_with_entropy( @@ -946,6 +948,7 @@ impl std::fmt::Display for KeyError { } } +#[cfg(feature = "std")] impl std::error::Error for KeyError {} #[cfg(test)] diff --git a/src/lib.rs b/src/lib.rs index 280001fc..6a208919 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ // , at your option. // You may not use this file except in accordance with one or both of these // licenses. - +// // rustdoc will warn if there are missing docs #![warn(missing_docs)] // only enables the `doc_cfg` feature when @@ -147,7 +147,7 @@ fn main() -> Result<(), bdk::Error> { //! ## Sign a transaction //! //! ```no_run -//! use std::str::FromStr; +//! use core::str::FromStr; //! //! use bitcoin::util::psbt::PartiallySignedTransaction as Psbt; //! @@ -188,6 +188,14 @@ fn main() -> Result<(), bdk::Error> { //! * `keys-bip39`: [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) mnemonic codes for generating deterministic keys //! +#![no_std] +#[cfg(feature = "std")] +#[macro_use] +extern crate std; + +#[macro_use] +extern crate alloc; + pub extern crate bitcoin; #[cfg(feature = "hardware-signer")] pub extern crate hwi; @@ -225,3 +233,4 @@ pub fn version() -> &'static str { } pub use bdk_chain as chain; +pub(crate) use bdk_chain::collections; diff --git a/src/psbt/mod.rs b/src/psbt/mod.rs index 6f986056..62ef0a79 100644 --- a/src/psbt/mod.rs +++ b/src/psbt/mod.rs @@ -12,6 +12,7 @@ //! Additional functions on the `rust-bitcoin` `PartiallySignedTransaction` structure. use crate::FeeRate; +use alloc::vec::Vec; use bitcoin::util::psbt::PartiallySignedTransaction as Psbt; use bitcoin::TxOut; @@ -85,7 +86,7 @@ mod test { use crate::wallet::AddressIndex; use crate::wallet::AddressIndex::New; use crate::{psbt, FeeRate, SignOptions}; - use std::str::FromStr; + use core::str::FromStr; // from bip 174 const PSBT_STR: &str = "cHNidP8BAKACAAAAAqsJSaCMWvfEm4IS9Bfi8Vqz9cM9zxU4IagTn4d6W3vkAAAAAAD+////qwlJoIxa98SbghL0F+LxWrP1wz3PFTghqBOfh3pbe+QBAAAAAP7///8CYDvqCwAAAAAZdqkUdopAu9dAy+gdmI5x3ipNXHE5ax2IrI4kAAAAAAAAGXapFG9GILVT+glechue4O/p+gOcykWXiKwAAAAAAAEHakcwRAIgR1lmF5fAGwNrJZKJSGhiGDR9iYZLcZ4ff89X0eURZYcCIFMJ6r9Wqk2Ikf/REf3xM286KdqGbX+EhtdVRs7tr5MZASEDXNxh/HupccC1AaZGoqg7ECy0OIEhfKaC3Ibi1z+ogpIAAQEgAOH1BQAAAAAXqRQ1RebjO4MsRwUPJNPuuTycA5SLx4cBBBYAFIXRNTfy4mVAWjTbr6nj3aAfuCMIAAAA"; diff --git a/src/types.rs b/src/types.rs index 47d1c2b8..db33ed22 100644 --- a/src/types.rs +++ b/src/types.rs @@ -9,8 +9,9 @@ // You may not use this file except in accordance with one or both of these // licenses. -use std::convert::AsRef; -use std::ops::Sub; +use alloc::boxed::Box; +use core::convert::AsRef; +use core::ops::Sub; use bdk_chain::ConfirmationTime; use bitcoin::blockdata::transaction::{OutPoint, Transaction, TxOut}; @@ -124,7 +125,7 @@ impl FeeRate { } } -impl std::default::Default for FeeRate { +impl Default for FeeRate { fn default() -> Self { FeeRate::default_min_relay_fee() } @@ -249,13 +250,13 @@ pub struct TransactionDetails { } impl PartialOrd for TransactionDetails { - fn partial_cmp(&self, other: &Self) -> Option { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for TransactionDetails { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { + fn cmp(&self, other: &Self) -> core::cmp::Ordering { self.confirmation_time .cmp(&other.confirmation_time) .then_with(|| self.txid.cmp(&other.txid)) @@ -287,8 +288,8 @@ impl Balance { } } -impl std::fmt::Display for Balance { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl core::fmt::Display for Balance { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!( f, "{{ immature: {}, trusted_pending: {}, untrusted_pending: {}, confirmed: {} }}", @@ -297,7 +298,7 @@ impl std::fmt::Display for Balance { } } -impl std::ops::Add for Balance { +impl core::ops::Add for Balance { type Output = Self; fn add(self, other: Self) -> Self { @@ -310,7 +311,7 @@ impl std::ops::Add for Balance { } } -impl std::iter::Sum for Balance { +impl core::iter::Sum for Balance { fn sum>(iter: I) -> Self { iter.fold( Balance { diff --git a/src/wallet/coin_selection.rs b/src/wallet/coin_selection.rs index 7f2861cb..bfd1fe57 100644 --- a/src/wallet/coin_selection.rs +++ b/src/wallet/coin_selection.rs @@ -99,16 +99,16 @@ use crate::wallet::utils::IsDust; use crate::WeightedUtxo; use crate::{error::Error, Utxo}; +use alloc::vec::Vec; use bitcoin::consensus::encode::serialize; use bitcoin::Script; #[cfg(test)] use assert_matches::assert_matches; +use core::convert::TryInto; use rand::seq::SliceRandom; #[cfg(not(test))] use rand::thread_rng; -#[cfg(test)] -use std::convert::TryInto; /// Default coin selection algorithm used by [`TxBuilder`](super::tx_builder::TxBuilder) if not /// overridden @@ -177,7 +177,7 @@ impl CoinSelectionResult { /// selection algorithm when it creates transactions. /// /// For an example see [this module](crate::wallet::coin_selection)'s documentation. -pub trait CoinSelectionAlgorithm: std::fmt::Debug { +pub trait CoinSelectionAlgorithm: core::fmt::Debug { /// Perform the coin selection /// /// - `database`: a reference to the wallet's database that can be used to lookup additional @@ -695,7 +695,7 @@ impl BranchAndBoundCoinSelection { #[cfg(test)] mod test { - use std::str::FromStr; + use core::str::FromStr; use bdk_chain::ConfirmationTime; use bitcoin::{OutPoint, Script, TxOut}; diff --git a/src/wallet/export.rs b/src/wallet/export.rs index f5d552b2..9db268eb 100644 --- a/src/wallet/export.rs +++ b/src/wallet/export.rs @@ -59,8 +59,9 @@ //! # Ok::<_, bdk::Error>(()) //! ``` -use std::str::FromStr; +use core::str::FromStr; +use alloc::string::{String, ToString}; use bdk_chain::sparse_chain::ChainPosition; use serde::{Deserialize, Serialize}; @@ -217,7 +218,7 @@ impl FullyNodedExport { #[cfg(test)] mod test { - use std::str::FromStr; + use core::str::FromStr; use bdk_chain::{BlockId, ConfirmationTime}; use bitcoin::hashes::Hash; diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index 8e7e1198..8884553e 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -12,14 +12,18 @@ //! Wallet //! //! This module defines the [`Wallet`] structure. -use bdk_chain::chain_graph; -use bdk_chain::{keychain::KeychainTracker, sparse_chain, BlockId, ConfirmationTime}; +use crate::collections::{BTreeMap, HashMap, HashSet}; +use alloc::{ + boxed::Box, + string::{String, ToString}, + sync::Arc, + vec::Vec, +}; +use bdk_chain::{keychain::KeychainTracker, sparse_chain, BlockId, ConfirmationTime, chain_graph}; use bitcoin::secp256k1::Secp256k1; +use core::fmt; +use core::ops::Deref; use core::convert::TryInto; -use std::collections::{BTreeMap, HashMap, HashSet}; -use std::fmt; -use std::ops::Deref; -use std::sync::Arc; use bitcoin::consensus::encode::serialize; use bitcoin::util::psbt; @@ -551,7 +555,7 @@ impl Wallet { /// [`TxBuilder`]: crate::TxBuilder pub fn build_tx(&mut self) -> TxBuilder<'_, DefaultCoinSelectionAlgorithm, CreateTx> { TxBuilder { - wallet: std::rc::Rc::new(core::cell::RefCell::new(self)), + wallet: alloc::rc::Rc::new(core::cell::RefCell::new(self)), params: TxParams::default(), coin_selection: DefaultCoinSelectionAlgorithm::default(), phantom: core::marker::PhantomData, @@ -1067,7 +1071,7 @@ impl Wallet { }; Ok(TxBuilder { - wallet: std::rc::Rc::new(std::cell::RefCell::new(self)), + wallet: alloc::rc::Rc::new(core::cell::RefCell::new(self)), params, coin_selection: DefaultCoinSelectionAlgorithm::default(), phantom: core::marker::PhantomData, @@ -1846,7 +1850,7 @@ pub(crate) mod test { macro_rules! from_str { ($e:expr, $t:ty) => {{ - use std::str::FromStr; + use core::str::FromStr; <$t>::from_str($e).unwrap() }}; @@ -2344,7 +2348,7 @@ pub(crate) mod test { #[test] fn test_create_tx_input_hd_keypaths() { use bitcoin::util::bip32::{DerivationPath, Fingerprint}; - use std::str::FromStr; + use core::str::FromStr; let (mut wallet, _) = get_funded_wallet("wpkh([d34db33f/44'/0'/0']tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)"); let addr = wallet.get_address(New); @@ -2365,7 +2369,7 @@ pub(crate) mod test { #[test] fn test_create_tx_output_hd_keypaths() { use bitcoin::util::bip32::{DerivationPath, Fingerprint}; - use std::str::FromStr; + use core::str::FromStr; let (mut wallet, _) = get_funded_wallet("wpkh([d34db33f/44'/0'/0']tpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)"); @@ -4118,8 +4122,8 @@ pub(crate) mod test { #[test] fn test_get_address_no_reuse_single_descriptor() { + use crate::collections::HashSet; use crate::descriptor::template::Bip84; - use std::collections::HashSet; let key = bitcoin::util::bip32::ExtendedPrivKey::from_str("tprv8ZgxMBicQKsPcx5nBGsR63Pe8KnRUqmbJNENAfGftF3yuXoMMoVJJcYeUw5eVkm9WBPjWYt6HMWYJNesB5HaNVBaFc1M6dRjWSYnmewUMYy").unwrap(); let mut wallet = diff --git a/src/wallet/signer.rs b/src/wallet/signer.rs index ff54cfa8..51106d02 100644 --- a/src/wallet/signer.rs +++ b/src/wallet/signer.rs @@ -15,8 +15,8 @@ //! through the [`Wallet::add_signer`](super::Wallet::add_signer) function. //! //! ``` -//! # use std::sync::Arc; -//! # use std::str::FromStr; +//! # use alloc::sync::Arc; +//! # use core::str::FromStr; //! # use bitcoin::secp256k1::{Secp256k1, All}; //! # use bitcoin::*; //! # use bitcoin::util::psbt; @@ -80,11 +80,12 @@ //! # Ok::<_, bdk::Error>(()) //! ``` -use std::cmp::Ordering; -use std::collections::BTreeMap; -use std::fmt; -use std::ops::{Bound::Included, Deref}; -use std::sync::Arc; +use crate::collections::BTreeMap; +use alloc::sync::Arc; +use alloc::vec::Vec; +use core::cmp::Ordering; +use core::fmt; +use core::ops::{Bound::Included, Deref}; use bitcoin::blockdata::opcodes; use bitcoin::blockdata::script::Builder as ScriptBuilder; @@ -199,6 +200,7 @@ impl fmt::Display for SignerError { } } +#[cfg(feature = "std")] impl std::error::Error for SignerError {} /// Signing context @@ -560,7 +562,7 @@ fn sign_psbt_schnorr( #[derive(Debug, Clone, PartialOrd, PartialEq, Ord, Eq)] pub struct SignerOrdering(pub usize); -impl std::default::Default for SignerOrdering { +impl Default for SignerOrdering { fn default() -> Self { SignerOrdering(100) } @@ -1017,8 +1019,8 @@ mod signers_container_tests { use bitcoin::secp256k1::{All, Secp256k1}; use bitcoin::util::bip32; use bitcoin::Network; + use core::str::FromStr; use miniscript::ScriptContext; - use std::str::FromStr; fn is_equal(this: &Arc, that: &Arc) -> bool { let secp = Secp256k1::new(); diff --git a/src/wallet/tx_builder.rs b/src/wallet/tx_builder.rs index 04833682..64212db8 100644 --- a/src/wallet/tx_builder.rs +++ b/src/wallet/tx_builder.rs @@ -36,12 +36,11 @@ //! # Ok::<(), bdk::Error>(()) //! ``` -use std::cell::RefCell; -use std::collections::BTreeMap; -use std::collections::HashSet; -use std::default::Default; -use std::marker::PhantomData; -use std::rc::Rc; +use crate::collections::BTreeMap; +use crate::collections::HashSet; +use alloc::{boxed::Box, rc::Rc, string::String, vec::Vec}; +use core::cell::RefCell; +use core::marker::PhantomData; use bitcoin::util::psbt::{self, PartiallySignedTransaction as Psbt}; use bitcoin::{LockTime, OutPoint, Script, Sequence, Transaction}; @@ -53,7 +52,7 @@ use crate::{ }; use crate::{Error, Utxo, Wallet}; /// Context in which the [`TxBuilder`] is valid -pub trait TxBuilderContext: std::fmt::Debug + Default + Clone {} +pub trait TxBuilderContext: core::fmt::Debug + Default + Clone {} /// Marker type to indicate the [`TxBuilder`] is being used to create a new transaction (as opposed /// to bumping the fee of an existing one). @@ -163,7 +162,7 @@ pub(crate) enum FeePolicy { FeeAmount(u64), } -impl std::default::Default for FeePolicy { +impl Default for FeePolicy { fn default() -> Self { FeePolicy::FeeRate(FeeRate::default_min_relay_fee()) } @@ -827,7 +826,7 @@ mod test { #[test] fn test_output_ordering_bip69() { - use std::str::FromStr; + use core::str::FromStr; let original_tx = ordering_test_tx!(); let mut tx = original_tx; diff --git a/src/wallet/utils.rs b/src/wallet/utils.rs index 163d417e..86f7d2fe 100644 --- a/src/wallet/utils.rs +++ b/src/wallet/utils.rs @@ -120,7 +120,7 @@ mod test { use super::{check_nsequence_rbf, IsDust}; use crate::bitcoin::{Address, Sequence}; - use std::str::FromStr; + use core::str::FromStr; #[test] fn test_is_dust() {