Introduce bitcoin-internals crate

Add a new crate `bitcoin-internals` to be used for internal code needed
by multiple soon-to-be-created crates.

Add the `write_err` macro to `bitcoin-internals`, nothing else.

This patch uses a `path` dependency which means `rust-bitcoin` cannot be
released in its current state, will need to be changed once we release
the `bitcoin-internals` crate on `crates.io`.
This commit is contained in:
Tobin C. Harding 2022-09-05 12:19:28 +10:00
parent 12c5fb042e
commit 834bbf461f
24 changed files with 120 additions and 41 deletions

View File

@ -1,3 +1,3 @@
[workspace]
members = ["bitcoin"]
members = ["bitcoin", "internals"]
exclude = ["embedded", "fuzz"]

View File

@ -25,7 +25,7 @@ secp-recovery = ["secp256k1/recovery"]
# The no-std feature doesn't disable std - you need to turn off the std feature for that by disabling default.
# Instead no-std enables additional features required for this crate to be usable without std.
# As a result, both can be enabled without conflict.
std = ["secp256k1/std", "bitcoin_hashes/std", "bech32/std"]
std = ["secp256k1/std", "bitcoin_hashes/std", "bech32/std", "bitcoin-internals/std"]
no-std = ["hashbrown", "core2/alloc", "bitcoin_hashes/alloc", "secp256k1/alloc"]
[package.metadata.docs.rs]
@ -33,6 +33,7 @@ features = [ "std", "secp-recovery", "base64", "rand", "serde", "bitcoinconsensu
rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
bitcoin-internals = { path = "../internals" }
bech32 = { version = "0.9.0", default-features = false }
bitcoin_hashes = { version = "0.11.0", default-features = false }
secp256k1 = { version = "0.24.0", default-features = false, features = ["bitcoin_hashes"] }

View File

@ -27,6 +27,7 @@ use core::fmt;
use core::str::FromStr;
use bech32;
use bitcoin_internals::write_err;
use secp256k1::{Secp256k1, Verification, XOnlyPublicKey};
use crate::blockdata::constants::{
@ -38,7 +39,7 @@ use crate::blockdata::{opcodes, script};
use crate::error::ParseIntError;
use crate::hash_types::{PubkeyHash, ScriptHash};
use crate::hashes::{sha256, Hash, HashEngine};
use crate::internal_macros::{serde_string_impl, write_err};
use crate::internal_macros::serde_string_impl;
use crate::network::constants::Network;
use crate::prelude::*;
use crate::util::base58;

View File

@ -42,6 +42,8 @@
use core::cmp::{self, Ordering};
use core::fmt::{self, Display, Formatter};
use bitcoin_internals::write_err;
use crate::blockdata::block::Block;
use crate::blockdata::script::Script;
use crate::blockdata::transaction::OutPoint;
@ -49,7 +51,6 @@ use crate::consensus::encode::VarInt;
use crate::consensus::{Decodable, Encodable};
use crate::hash_types::{BlockHash, FilterHash, FilterHeader};
use crate::hashes::{siphash24, Hash};
use crate::internal_macros::write_err;
use crate::io;
use crate::prelude::*;
use crate::util::endian;
@ -273,7 +274,8 @@ impl GcsFilterReader {
let reader = &mut decoder;
// map hashes to [0, n_elements << grp]
let nm = n_elements.0 * self.m;
let mut mapped = query.map(|e| map_to_range(self.filter.hash(e.borrow()), nm)).collect::<Vec<_>>();
let mut mapped =
query.map(|e| map_to_range(self.filter.hash(e.borrow()), nm)).collect::<Vec<_>>();
// sort
mapped.sort_unstable();
if mapped.is_empty() {
@ -317,7 +319,8 @@ impl GcsFilterReader {
let reader = &mut decoder;
// map hashes to [0, n_elements << grp]
let nm = n_elements.0 * self.m;
let mut mapped = query.map(|e| map_to_range(self.filter.hash(e.borrow()), nm)).collect::<Vec<_>>();
let mut mapped =
query.map(|e| map_to_range(self.filter.hash(e.borrow()), nm)).collect::<Vec<_>>();
// sort
mapped.sort_unstable();
mapped.dedup();

View File

@ -12,11 +12,12 @@ use core::cmp::{PartialOrd, Ordering};
use core::convert::TryFrom;
use core::str::FromStr;
use bitcoin_internals::write_err;
use crate::consensus::encode::{self, Decodable, Encodable};
use crate::error::ParseIntError;
use crate::io::{self, Read, Write};
use crate::prelude::*;
use crate::internal_macros::write_err;
use crate::parse::{self, impl_parse_str_through_int};
/// The Threshold for deciding whether a lock time value is a height or a time (see [Bitcoin Core]).

View File

@ -20,7 +20,7 @@ use core::{fmt, default::Default};
use core::ops::Index;
use crate::internal_macros::debug_from_display;
#[cfg(feature = "bitcoinconsensus")]
use crate::internal_macros::write_err;
use bitcoin_internals::write_err;
#[cfg(feature = "serde")] use serde;

View File

@ -18,6 +18,8 @@ use crate::io;
use core::{fmt, str, default::Default};
use core::convert::TryFrom;
use bitcoin_internals::write_err;
use crate::hashes::{self, Hash, sha256d};
use crate::hashes::hex::FromHex;
@ -30,7 +32,7 @@ use crate::blockdata::locktime::relative;
use crate::consensus::{encode, Decodable, Encodable};
use crate::hash_types::{Sighash, Txid, Wtxid};
use crate::VarInt;
use crate::internal_macros::{impl_consensus_encoding, serde_struct_human_string_impl, write_err};
use crate::internal_macros::{impl_consensus_encoding, serde_struct_human_string_impl};
use crate::parse::impl_parse_str_through_int;
#[cfg(doc)]

View File

@ -20,9 +20,10 @@ use crate::prelude::*;
use core::{fmt, mem, u32, convert::From};
use bitcoin_internals::write_err;
use crate::hashes::{sha256d, Hash, sha256};
use crate::hash_types::{BlockHash, FilterHash, TxMerkleNode, FilterHeader};
use crate::internal_macros::write_err;
use crate::io::{self, Cursor, Read};
use crate::util::endian;

View File

@ -626,26 +626,6 @@ macro_rules! user_enum {
}
pub(crate) use user_enum;
/// Formats error. If `std` feature is OFF appends error source (delimited by `: `). We do this
/// because `e.source()` is only available in std builds, without this macro the error source is
/// lost for no-std builds.
macro_rules! write_err {
($writer:expr, $string:literal $(, $args:expr)*; $source:expr) => {
{
#[cfg(feature = "std")]
{
let _ = &$source; // Prevents clippy warnings.
write!($writer, $string $(, $args)*)
}
#[cfg(not(feature = "std"))]
{
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
}
}
}
}
pub(crate) use write_err;
/// Asserts a boolean expression at compile time.
macro_rules! const_assert {
($x:expr) => {{

View File

@ -2,8 +2,9 @@ use core::convert::TryFrom;
use core::fmt;
use core::str::FromStr;
use bitcoin_internals::write_err;
use crate::error::impl_std_error;
use crate::internal_macros::write_err;
use crate::prelude::*;
/// Error with rich context returned when a string can't be parsed as an integer.

View File

@ -11,11 +11,11 @@ use crate::prelude::*;
use core::{fmt, str, iter, slice};
use bitcoin_internals::write_err;
use crate::hashes::{sha256d, Hash, hex};
use secp256k1;
use crate::util::{endian, key};
use crate::internal_macros::write_err;
/// An error that might occur during base58 decoding
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]

View File

@ -14,6 +14,7 @@ use core::{fmt, str::FromStr, default::Default};
use core::ops::Index;
#[cfg(feature = "serde")] use serde;
use bitcoin_internals::write_err;
use crate::hash_types::XpubIdentifier;
use crate::hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine, hex};
use secp256k1::{self, Secp256k1, XOnlyPublicKey};
@ -21,7 +22,7 @@ use secp256k1::{self, Secp256k1, XOnlyPublicKey};
use crate::network::constants::Network;
use crate::util::{base58, endian, key};
use crate::util::key::{PublicKey, PrivateKey, KeyPair};
use crate::internal_macros::{impl_array_newtype, impl_bytes_newtype, serde_string_impl, write_err};
use crate::internal_macros::{impl_array_newtype, impl_bytes_newtype, serde_string_impl};
/// A chain code
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]

View File

@ -8,11 +8,11 @@
use core::str::FromStr;
use core::{fmt, iter};
use bitcoin_internals::write_err;
use secp256k1;
use crate::prelude::*;
use crate::hashes::hex::{self, FromHex};
use crate::internal_macros::write_err;
use crate::util::sighash::{EcdsaSighashType, NonStandardSighashType};
/// An ECDSA signature with the corresponding hash type.

View File

@ -11,6 +11,7 @@ use crate::prelude::*;
use core::{ops, str::FromStr};
use core::fmt::{self, Write};
use bitcoin_internals::write_err;
pub use secp256k1::{self, Secp256k1, XOnlyPublicKey, KeyPair};
use crate::io;
@ -18,7 +19,6 @@ use crate::network::constants::Network;
use crate::hashes::{Hash, hash160, hex, hex::FromHex};
use crate::hash_types::{PubkeyHash, WPubkeyHash};
use crate::util::base58;
use crate::internal_macros::write_err;
/// A key-related error.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]

View File

@ -27,13 +27,13 @@ mod message_signing {
use core::fmt;
use bitcoin_internals::write_err;
use crate::hashes::sha256d;
use secp256k1;
use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
use crate::util::key::PublicKey;
use crate::address::{Address, AddressType};
use crate::internal_macros::write_err;
/// An error used for dealing with Bitcoin Signed Messages.
#[cfg_attr(docsrs, doc(cfg(feature = "secp-recovery")))]

View File

@ -28,8 +28,9 @@ use crate::prelude::*;
use crate::io;
use core::fmt;
use bitcoin_internals::write_err;
use crate::consensus::encode;
use crate::internal_macros::write_err;
/// A trait which allows numbers to act as fixed-size bit arrays
pub trait BitArray {

View File

@ -4,13 +4,14 @@ use crate::prelude::*;
use core::fmt;
use bitcoin_internals::write_err;
use crate::blockdata::transaction::Transaction;
use crate::consensus::encode;
use crate::util::psbt::raw;
use crate::hashes;
use crate::util::bip32::ExtendedPubKey;
use crate::internal_macros::write_err;
/// Enum for marking psbt hash error.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]

View File

@ -207,7 +207,7 @@ mod display_from_str {
use core::str::FromStr;
use crate::consensus::encode::{Error, self};
use base64::display::Base64Display;
use crate::internal_macros::write_err;
use bitcoin_internals::write_err;
/// Error encountered during PSBT decoding from Base64 string.
#[derive(Debug)]

View File

@ -9,12 +9,13 @@
use core::fmt;
use bitcoin_internals::write_err;
use crate::prelude::*;
use secp256k1::{self, Secp256k1, Verification, constants};
use crate::util::taproot::{TapBranchHash, TapTweakHash};
use crate::SchnorrSighashType;
use crate::internal_macros::write_err;
/// Deprecated re-export of [`secp256k1::XOnlyPublicKey`]
#[deprecated(since = "0.28.0", note = "Please use `util::key::XOnlyPublicKey` instead")]

View File

@ -7,6 +7,7 @@
use crate::prelude::*;
use crate::io;
use bitcoin_internals::write_err;
use secp256k1::{self, Secp256k1, Scalar};
use core::convert::TryFrom;
@ -17,7 +18,6 @@ use crate::hashes::{sha256, sha256t_hash_newtype, Hash, HashEngine};
use crate::schnorr::{TweakedPublicKey, UntweakedPublicKey, TapTweak};
use crate::util::key::XOnlyPublicKey;
use crate::Script;
use crate::internal_macros::write_err;
use crate::consensus::Encodable;
/// The SHA-256 midstate value for the TapLeaf hash.

25
internals/Cargo.toml Normal file
View File

@ -0,0 +1,25 @@
[package]
name = "bitcoin-internals"
version = "0.1.0"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>", "The Rust Bitcoin developers"]
license = "CC0-1.0"
repository = "https://github.com/rust-bitcoin/rust-bitcoin/"
documentation = "https://docs.rs/bitcoin-internals"
description = "Internal types and macros used by rust-bitcoin ecosystem"
categories = ["cryptography::cryptocurrencies"]
keywords = ["internal"]
readme = "README.md"
edition = "2018"
# Please don't forget to add relevant features to docs.rs below.
[features]
default = []
std = []
[package.metadata.docs.rs]
features = ["std"]
rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
[dev-dependencies]

7
internals/README.md Normal file
View File

@ -0,0 +1,7 @@
Rust Bitcoin Internals
======================
This crate is only meant to be used internally by crates in the
[rust-bitcoin](https://github.com/rust-bitcoin) ecosystem.
This crate will never be stabilized, depend on it at your own risk.

29
internals/src/error.rs Normal file
View File

@ -0,0 +1,29 @@
// Written by the Rust Bitcoin developers.
// SPDX-License-Identifier: CC0-1.0
//! # Error
//!
//! Error handling macros and helpers.
//!
/// Formats error.
///
/// If `std` feature is OFF appends error source (delimited by `: `). We do this because
/// `e.source()` is only available in std builds, without this macro the error source is lost for
/// no-std builds.
#[macro_export]
macro_rules! write_err {
($writer:expr, $string:literal $(, $args:expr)*; $source:expr) => {
{
#[cfg(feature = "std")]
{
let _ = &$source; // Prevents clippy warnings.
write!($writer, $string $(, $args)*)
}
#[cfg(not(feature = "std"))]
{
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
}
}
}
}

24
internals/src/lib.rs Normal file
View File

@ -0,0 +1,24 @@
// Written by the Rust Bitcoin developers.
// SPDX-License-Identifier: CC0-1.0
//! # Rust Bitcoin Internal
//!
//! This crate is only meant to be used internally by crates in the
//! [rust-bitcoin](https://github.com/rust-bitcoin) ecosystem.
//!
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_cfg))]
// Coding conventions
#![forbid(unsafe_code)]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![deny(dead_code)]
#![deny(unused_imports)]
#![deny(missing_docs)]
#![deny(unused_must_use)]
pub mod error;