Skip to main content

nintondo_dogecoin/
lib.rs

1// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
2// SPDX-License-Identifier: CC0-1.0
3
4//! # Rust Dogecoin Library
5//!
6//! This is a library that supports the Dogecoin network protocol and associated
7//! primitives. It is designed for Rust programs built to work with the Dogecoin
8//! network.
9//!
10//! It is also written entirely in Rust to illustrate the benefits of strong type
11//! safety, including ownership and lifetime, for financial and/or cryptographic
12//! software.
13//!
14//! See README.md for detailed documentation about development and supported
15//! environments.
16//!
17//! ## Available feature flags
18//!
19//! * `std` - the usual dependency on `std` (default).
20//! * `secp-recovery` - enables calculating public key from a signature and message.
21//! * `base64` - (dependency), enables encoding of PSBTs and message signatures.
22//! * `rand` - (dependency), makes it more convenient to generate random values.
23//! * `serde` - (dependency), implements `serde`-based serialization and
24//!                 deserialization.
25//! * `secp-lowmemory` - optimizations for low-memory devices.
26//! * `no-std` - enables additional features required for this crate to be usable
27//!              without std. Does **not** disable `std`. Depends on `core2`.
28//! * `bitcoinconsensus-std` - enables `std` in `bitcoinconsensus` and communicates it
29//!                            to this crate so it knows how to implement
30//!                            `std::error::Error`. At this time there's a hack to
31//!                            achieve the same without this feature but it could
32//!                            happen the implementations diverge one day.
33
34#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
35// Experimental features we need.
36#![cfg_attr(bench, feature(test))]
37#![cfg_attr(docsrs, feature(doc_cfg))]
38// Coding conventions
39#![warn(missing_docs)]
40// Instead of littering the codebase for non-fuzzing code just globally allow.
41#![cfg_attr(fuzzing, allow(dead_code, unused_imports))]
42
43#[cfg(not(any(feature = "std", feature = "no-std")))]
44compile_error!("at least one of the `std` or `no-std` features must be enabled");
45
46// Disable 16-bit support at least for now as we can't guarantee it yet.
47#[cfg(target_pointer_width = "16")]
48compile_error!(
49    "rust-bitcoin currently only supports architectures with pointers wider than 16 bits, let us
50    know if you want 16-bit support. Note that we do NOT guarantee that we will implement it!"
51);
52
53#[cfg(bench)]
54extern crate test;
55
56#[macro_use]
57extern crate alloc;
58
59#[cfg(feature = "base64")]
60#[cfg_attr(docsrs, doc(cfg(feature = "base64")))]
61pub extern crate base64;
62pub extern crate bech32;
63pub extern crate bitcoin_hashes as hashes;
64#[cfg(feature = "bitcoinconsensus")]
65#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
66pub extern crate bitcoinconsensus;
67pub extern crate hex;
68pub extern crate secp256k1;
69
70#[cfg(feature = "serde")]
71#[macro_use]
72extern crate actual_serde as serde;
73
74#[cfg(test)]
75#[macro_use]
76mod test_macros;
77mod internal_macros;
78mod parse;
79#[cfg(feature = "serde")]
80mod serde_utils;
81
82#[macro_use]
83pub mod network;
84pub mod address;
85pub mod amount;
86pub mod base58;
87pub mod bip152;
88pub mod bip158;
89pub mod bip32;
90pub mod blockdata;
91pub mod consensus;
92// Private until we either make this a crate or flatten it - still to be decided.
93pub(crate) mod crypto;
94pub mod error;
95pub mod hash_types;
96pub mod merkle_tree;
97pub mod policy;
98pub mod pow;
99pub mod psbt;
100pub mod sign_message;
101pub mod string;
102pub mod taproot;
103pub mod util;
104
105// May depend on crate features and we don't want to bother with it
106#[allow(unused)]
107#[cfg(feature = "std")]
108use std::error::Error as StdError;
109#[cfg(feature = "std")]
110use std::io;
111
112#[allow(unused)]
113#[cfg(not(feature = "std"))]
114use core2::error::Error as StdError;
115#[cfg(not(feature = "std"))]
116use core2::io;
117
118pub use crate::address::{Address, AddressType};
119pub use crate::amount::{Amount, Denomination, SignedAmount};
120pub use crate::blockdata::block::{self, Block};
121pub use crate::blockdata::fee_rate::FeeRate;
122pub use crate::blockdata::locktime::{self, absolute, relative};
123pub use crate::blockdata::script::{self, Script, ScriptBuf};
124pub use crate::blockdata::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut};
125pub use crate::blockdata::weight::Weight;
126pub use crate::blockdata::witness::{self, Witness};
127pub use crate::blockdata::{constants, opcodes};
128pub use crate::consensus::encode::VarInt;
129pub use crate::crypto::key::{self, PrivateKey, PublicKey};
130pub use crate::crypto::{ecdsa, sighash};
131pub use crate::error::Error;
132pub use crate::hash_types::{
133    BlockHash, PubkeyHash, ScriptHash, Txid, WPubkeyHash, WScriptHash, Wtxid,
134};
135pub use crate::merkle_tree::MerkleBlock;
136pub use crate::network::constants::Network;
137pub use crate::pow::{CompactTarget, Target, Work};
138
139#[cfg(not(feature = "std"))]
140mod io_extras {
141    /// A writer which will move data into the void.
142    pub struct Sink {
143        _priv: (),
144    }
145
146    /// Creates an instance of a writer which will successfully consume all data.
147    pub const fn sink() -> Sink {
148        Sink { _priv: () }
149    }
150
151    impl core2::io::Write for Sink {
152        #[inline]
153        fn write(&mut self, buf: &[u8]) -> core2::io::Result<usize> {
154            Ok(buf.len())
155        }
156
157        #[inline]
158        fn flush(&mut self) -> core2::io::Result<()> {
159            Ok(())
160        }
161    }
162}
163
164#[rustfmt::skip]
165mod prelude {
166    #[cfg(all(not(feature = "std"), not(test)))]
167    pub use alloc::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc};
168
169    #[cfg(all(not(feature = "std"), not(test), any(not(rust_v_1_60), target_has_atomic = "ptr")))]
170    pub use alloc::sync;
171
172    #[cfg(any(feature = "std", test))]
173    pub use std::{string::{String, ToString}, vec::Vec, boxed::Box, borrow::{Borrow, Cow, ToOwned}, slice, rc, sync};
174
175    #[cfg(all(not(feature = "std"), not(test)))]
176    pub use alloc::collections::{BTreeMap, BTreeSet, btree_map, BinaryHeap};
177
178    #[cfg(any(feature = "std", test))]
179    pub use std::collections::{BTreeMap, BTreeSet, btree_map, BinaryHeap};
180
181    #[cfg(feature = "std")]
182    pub use std::io::sink;
183
184    #[cfg(not(feature = "std"))]
185    pub use crate::io_extras::sink;
186
187    pub use bitcoin_internals::hex::display::DisplayHex;
188}
189
190#[cfg(bench)]
191use bench::EmptyWrite;
192
193#[cfg(bench)]
194mod bench {
195    use core::fmt::Arguments;
196
197    use crate::io::{IoSlice, Result, Write};
198
199    #[derive(Default, Clone, Debug, PartialEq, Eq)]
200    pub struct EmptyWrite;
201
202    impl Write for EmptyWrite {
203        fn write(&mut self, buf: &[u8]) -> Result<usize> {
204            Ok(buf.len())
205        }
206        fn write_vectored(&mut self, bufs: &[IoSlice]) -> Result<usize> {
207            Ok(bufs.iter().map(|s| s.len()).sum())
208        }
209        fn flush(&mut self) -> Result<()> {
210            Ok(())
211        }
212
213        fn write_all(&mut self, _: &[u8]) -> Result<()> {
214            Ok(())
215        }
216        fn write_fmt(&mut self, _: Arguments) -> Result<()> {
217            Ok(())
218        }
219    }
220}