From 43b1ed1b86c111d378cca0b61b11f73d44ce81c6 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 12 Dec 2023 08:12:39 +1100 Subject: [PATCH] Fully encapsulate bitcoinconsensus The `bitcoinconsensus` crate is not fully under our control because it exposes code from Core, so we cannot guarantee its stability across versions. To make our semver compliance easier we can fully encapsulate the `bitcoinconsensus` crate so it does not appear in our public API. However, it is useful to have the crate itself exported, here we add an "unstable" feature and only publicly export the `bitcoinconsensus` crate if the "unstable" feature is enabled. --- bitcoin/src/consensus/validation.rs | 8 ++------ bitcoin/src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/bitcoin/src/consensus/validation.rs b/bitcoin/src/consensus/validation.rs index 988e1837..883bec6c 100644 --- a/bitcoin/src/consensus/validation.rs +++ b/bitcoin/src/consensus/validation.rs @@ -51,13 +51,13 @@ pub fn verify_script_with_flags>( spending_tx: &[u8], flags: F, ) -> Result<(), BitcoinconsensusError> { - Ok(bitcoinconsensus::verify_with_flags( + bitcoinconsensus::verify_with_flags( script.as_bytes(), amount.to_sat(), spending_tx, index, flags.into(), - )?) + ).map_err(BitcoinconsensusError) } /// Verifies that this transaction is able to spend its inputs. @@ -198,10 +198,6 @@ impl std::error::Error for BitcoinconsensusError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } } -impl From for BitcoinconsensusError { - fn from(e: bitcoinconsensus::Error) -> Self { Self(e) } -} - /// An error during transaction validation. #[derive(Debug, Clone, PartialEq, Eq)] #[non_exhaustive] diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index d65fe1e6..8cd93f02 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -59,9 +59,9 @@ pub extern crate base64; /// Encodes and decodes the Bech32 forrmat. pub extern crate bech32; -#[cfg(feature = "bitcoinconsensus")] /// Bitcoin's libbitcoinconsensus with Rust binding. -pub extern crate bitcoinconsensus; +#[cfg(feature = "bitcoinconsensus")] +extern crate bitcoinconsensus; /// Rust implementation of cryptographic hash function algorithems. pub extern crate hashes;