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.
This commit is contained in:
Tobin C. Harding 2024-06-27 15:37:27 +10:00
parent 554a5eb40c
commit cf800a1b07
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 4 additions and 18 deletions

View File

@ -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<NetworkUnchecked> {
fn from_str(s: &str) -> Result<Address<NetworkUnchecked>, 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");

View File

@ -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<bech32::Fe32> for WitnessVersion {
type Error = TryFromError;
fn try_from(value: Fe32) -> Result<Self, Self::Error> { Self::try_from(value.to_u8()) }
}
impl TryFrom<u8> for WitnessVersion {
type Error = TryFromError;
@ -152,10 +140,6 @@ impl<'a> TryFrom<Instruction<'a>> for WitnessVersion {
}
}
impl From<WitnessVersion> for Fe32 {
fn from(version: WitnessVersion) -> Self { version.to_fe() }
}
impl From<WitnessVersion> for Opcode {
fn from(version: WitnessVersion) -> Opcode {
match version {