From cf800a1b07133e159f1a65bfbeb50ebaf68cb06d Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 27 Jun 2024 15:37:27 +1000 Subject: [PATCH] Remove bech32 dependency from blockdata We have a single usage of the `bech32` crate inside the `blockdata` module, to convert a `WitnessVersion` to a `Fe32`. We then have a single call site where we use the conversion in the `address` module. This code was written without thinking to hard about the introduced dependency on `bech32`, in hindsite it shouldn't have been added. In preparation for splitting a bunch of code in `blockdata` out into the `primitives` crate remove the `bech32` stuff from the `witness_version` module. --- bitcoin/src/address/mod.rs | 6 ++++-- bitcoin/src/blockdata/script/witness_version.rs | 16 ---------------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 1bb77e1ab..99d539e80 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -33,6 +33,7 @@ use core::marker::PhantomData; use core::str::FromStr; use bech32::primitives::hrp::Hrp; +use bech32::primitives::gf32::Fe32; use hashes::{sha256, HashEngine}; use secp256k1::{Secp256k1, Verification, XOnlyPublicKey}; @@ -168,7 +169,8 @@ impl fmt::Display for AddressInner { } Segwit { program, hrp } => { let hrp = hrp.to_hrp(); - let version = program.version().to_fe(); + let version = Fe32::try_from(program.version().to_num()) + .expect("version nums 0-16 are valid fe32 values"); let program = program.program().as_ref(); if fmt.alternate() { @@ -829,7 +831,7 @@ impl FromStr for Address { fn from_str(s: &str) -> Result, ParseError> { if let Ok((hrp, witness_version, data)) = bech32::segwit::decode(s) { - let version = WitnessVersion::try_from(witness_version)?; + let version = WitnessVersion::try_from(witness_version.to_u8())?; let program = WitnessProgram::new(version, &data) .expect("bech32 guarantees valid program length for witness"); diff --git a/bitcoin/src/blockdata/script/witness_version.rs b/bitcoin/src/blockdata/script/witness_version.rs index 0974da625..826286929 100644 --- a/bitcoin/src/blockdata/script/witness_version.rs +++ b/bitcoin/src/blockdata/script/witness_version.rs @@ -10,7 +10,6 @@ use core::fmt; use core::str::FromStr; -use bech32::Fe32; use internals::write_err; use units::{parse, ParseIntError}; @@ -71,11 +70,6 @@ impl WitnessVersion { /// version in bitcoin script. Thus, there is no function to directly convert witness version /// into a byte since the conversion requires context (bitcoin script or just a version number). pub fn to_num(self) -> u8 { self as u8 } - - /// Converts this witness version to a GF32 field element. - pub fn to_fe(self) -> Fe32 { - Fe32::try_from(self.to_num()).expect("0-16 are valid fe32 values") - } } /// Prints [`WitnessVersion`] number (from 0 to 16) as integer, without any prefix or suffix. @@ -92,12 +86,6 @@ impl FromStr for WitnessVersion { } } -impl TryFrom for WitnessVersion { - type Error = TryFromError; - - fn try_from(value: Fe32) -> Result { Self::try_from(value.to_u8()) } -} - impl TryFrom for WitnessVersion { type Error = TryFromError; @@ -152,10 +140,6 @@ impl<'a> TryFrom> for WitnessVersion { } } -impl From for Fe32 { - fn from(version: WitnessVersion) -> Self { version.to_fe() } -} - impl From for Opcode { fn from(version: WitnessVersion) -> Opcode { match version {