Skip to content

Commit

Permalink
Remove internal use of reexports of types have been moved to other cr…
Browse files Browse the repository at this point in the history
…ates.

This commit temporarily removes the reexported types to simplify the
removal process; the reexports will be reintroduced with deprecation
annotations in the subsequent commit.
  • Loading branch information
nuttycom committed Dec 20, 2024
1 parent 84fdb1f commit f6c0534
Show file tree
Hide file tree
Showing 97 changed files with 1,316 additions and 1,385 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 62 additions & 42 deletions components/zcash_address/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use core::fmt;
#[cfg(feature = "std")]
use std::error::Error;

use crate::{kind::*, AddressKind, Network, ZcashAddress};
use zcash_protocol::consensus::NetworkType;

use crate::{kind::*, AddressKind, ZcashAddress};

/// An error indicating that an address type is not supported for conversion.
#[derive(Debug)]
Expand All @@ -19,7 +21,10 @@ impl fmt::Display for UnsupportedAddress {
#[derive(Debug)]
pub enum ConversionError<E> {
/// The address is for the wrong network.
IncorrectNetwork { expected: Network, actual: Network },
IncorrectNetwork {
expected: NetworkType,
actual: NetworkType,
},
/// The address type is not supported by the target type.
Unsupported(UnsupportedAddress),
/// A conversion error returned by the target type.
Expand Down Expand Up @@ -60,15 +65,16 @@ impl<E: Error + 'static> Error for ConversionError<E> {

/// A helper trait for converting a [`ZcashAddress`] into a network-agnostic type.
///
/// A blanket implementation of [`TryFromAddress`] is provided for `(Network, T)` where
/// A blanket implementation of [`TryFromAddress`] is provided for `(NetworkType, T)` where
/// `T: TryFromRawAddress`.
///
/// [`ZcashAddress`]: crate::ZcashAddress
///
/// # Examples
///
/// ```
/// use zcash_address::{ConversionError, Network, TryFromRawAddress, UnsupportedAddress, ZcashAddress};
/// use zcash_address::{ConversionError, TryFromRawAddress, UnsupportedAddress, ZcashAddress};
/// use zcash_protocol::consensus::NetworkType;
///
/// #[derive(Debug, PartialEq)]
/// struct MySapling([u8; 43]);
Expand All @@ -90,15 +96,15 @@ impl<E: Error + 'static> Error for ConversionError<E> {
///
/// // You can use `ZcashAddress::convert_if_network` to get your type directly.
/// let addr: ZcashAddress = addr_string.parse().unwrap();
/// let converted = addr.convert_if_network::<MySapling>(Network::Main);
/// let converted = addr.convert_if_network::<MySapling>(NetworkType::Main);
/// assert!(converted.is_ok());
/// assert_eq!(converted.unwrap(), MySapling([0; 43]));
///
/// // Using `ZcashAddress::convert` gives us the tuple `(network, converted_addr)`.
/// let addr: ZcashAddress = addr_string.parse().unwrap();
/// let converted = addr.convert::<(_, MySapling)>();
/// assert!(converted.is_ok());
/// assert_eq!(converted.unwrap(), (Network::Main, MySapling([0; 43])));
/// assert_eq!(converted.unwrap(), (NetworkType::Main, MySapling([0; 43])));
///
/// // For an unsupported address type, we get an error.
/// let addr: ZcashAddress = "t1Hsc1LR8yKnbbe3twRp88p6vFfC5t7DLbs".parse().unwrap();
Expand Down Expand Up @@ -158,7 +164,8 @@ pub trait TryFromRawAddress: Sized {
/// # Examples
///
/// ```
/// use zcash_address::{ConversionError, Network, TryFromAddress, UnsupportedAddress, ZcashAddress};
/// use zcash_address::{ConversionError, TryFromAddress, UnsupportedAddress, ZcashAddress};
/// use zcash_protocol::consensus::NetworkType;
///
/// #[derive(Debug)]
/// struct MySapling([u8; 43]);
Expand All @@ -171,7 +178,7 @@ pub trait TryFromRawAddress: Sized {
/// type Error = &'static str;
///
/// fn try_from_sapling(
/// net: Network,
/// net: NetworkType,
/// data: [u8; 43],
/// ) -> Result<Self, ConversionError<Self::Error>> {
/// Ok(MySapling(data))
Expand All @@ -197,29 +204,32 @@ pub trait TryFromAddress: Sized {
/// [`Self::try_from_sapling`] as a valid Sapling address).
type Error;

fn try_from_sprout(net: Network, data: [u8; 64]) -> Result<Self, ConversionError<Self::Error>> {
fn try_from_sprout(
net: NetworkType,
data: [u8; 64],
) -> Result<Self, ConversionError<Self::Error>> {
let _ = (net, data);
Err(ConversionError::Unsupported(UnsupportedAddress("Sprout")))
}

fn try_from_sapling(
net: Network,
net: NetworkType,
data: [u8; 43],
) -> Result<Self, ConversionError<Self::Error>> {
let _ = (net, data);
Err(ConversionError::Unsupported(UnsupportedAddress("Sapling")))
}

fn try_from_unified(
net: Network,
net: NetworkType,
data: unified::Address,
) -> Result<Self, ConversionError<Self::Error>> {
let _ = (net, data);
Err(ConversionError::Unsupported(UnsupportedAddress("Unified")))
}

fn try_from_transparent_p2pkh(
net: Network,
net: NetworkType,
data: [u8; 20],
) -> Result<Self, ConversionError<Self::Error>> {
let _ = (net, data);
Expand All @@ -229,7 +239,7 @@ pub trait TryFromAddress: Sized {
}

fn try_from_transparent_p2sh(
net: Network,
net: NetworkType,
data: [u8; 20],
) -> Result<Self, ConversionError<Self::Error>> {
let _ = (net, data);
Expand All @@ -238,50 +248,59 @@ pub trait TryFromAddress: Sized {
)))
}

fn try_from_tex(net: Network, data: [u8; 20]) -> Result<Self, ConversionError<Self::Error>> {
fn try_from_tex(
net: NetworkType,
data: [u8; 20],
) -> Result<Self, ConversionError<Self::Error>> {
let _ = (net, data);
Err(ConversionError::Unsupported(UnsupportedAddress(
"transparent-source restricted P2PKH",
)))
}
}

impl<T: TryFromRawAddress> TryFromAddress for (Network, T) {
impl<T: TryFromRawAddress> TryFromAddress for (NetworkType, T) {
type Error = T::Error;

fn try_from_sprout(net: Network, data: [u8; 64]) -> Result<Self, ConversionError<Self::Error>> {
fn try_from_sprout(
net: NetworkType,
data: [u8; 64],
) -> Result<Self, ConversionError<Self::Error>> {
T::try_from_raw_sprout(data).map(|addr| (net, addr))
}

fn try_from_sapling(
net: Network,
net: NetworkType,
data: [u8; 43],
) -> Result<Self, ConversionError<Self::Error>> {
T::try_from_raw_sapling(data).map(|addr| (net, addr))
}

fn try_from_unified(
net: Network,
net: NetworkType,
data: unified::Address,
) -> Result<Self, ConversionError<Self::Error>> {
T::try_from_raw_unified(data).map(|addr| (net, addr))
}

fn try_from_transparent_p2pkh(
net: Network,
net: NetworkType,
data: [u8; 20],
) -> Result<Self, ConversionError<Self::Error>> {
T::try_from_raw_transparent_p2pkh(data).map(|addr| (net, addr))
}

fn try_from_transparent_p2sh(
net: Network,
net: NetworkType,
data: [u8; 20],
) -> Result<Self, ConversionError<Self::Error>> {
T::try_from_raw_transparent_p2sh(data).map(|addr| (net, addr))
}

fn try_from_tex(net: Network, data: [u8; 20]) -> Result<Self, ConversionError<Self::Error>> {
fn try_from_tex(
net: NetworkType,
data: [u8; 20],
) -> Result<Self, ConversionError<Self::Error>> {
T::try_from_raw_tex(data).map(|addr| (net, addr))
}
}
Expand All @@ -298,88 +317,89 @@ impl<T: TryFromRawAddress> TryFromAddress for (Network, T) {
/// # Examples
///
/// ```
/// use zcash_address::{ToAddress, Network, ZcashAddress};
/// use zcash_address::{ToAddress, ZcashAddress};
/// use zcash_protocol::consensus::NetworkType;
///
/// #[derive(Debug)]
/// struct MySapling([u8; 43]);
///
/// impl MySapling {
/// /// Encodes this Sapling address for the given network.
/// fn encode(&self, net: Network) -> ZcashAddress {
/// fn encode(&self, net: NetworkType) -> ZcashAddress {
/// ZcashAddress::from_sapling(net, self.0)
/// }
/// }
///
/// let addr = MySapling([0; 43]);
/// let encoded = addr.encode(Network::Main);
/// let encoded = addr.encode(NetworkType::Main);
/// assert_eq!(
/// encoded.to_string(),
/// "zs1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpq6d8g",
/// );
/// ```
pub trait ToAddress: private::Sealed {
fn from_sprout(net: Network, data: [u8; 64]) -> Self;
fn from_sprout(net: NetworkType, data: [u8; 64]) -> Self;

fn from_sapling(net: Network, data: [u8; 43]) -> Self;
fn from_sapling(net: NetworkType, data: [u8; 43]) -> Self;

fn from_unified(net: Network, data: unified::Address) -> Self;
fn from_unified(net: NetworkType, data: unified::Address) -> Self;

fn from_transparent_p2pkh(net: Network, data: [u8; 20]) -> Self;
fn from_transparent_p2pkh(net: NetworkType, data: [u8; 20]) -> Self;

fn from_transparent_p2sh(net: Network, data: [u8; 20]) -> Self;
fn from_transparent_p2sh(net: NetworkType, data: [u8; 20]) -> Self;

fn from_tex(net: Network, data: [u8; 20]) -> Self;
fn from_tex(net: NetworkType, data: [u8; 20]) -> Self;
}

impl ToAddress for ZcashAddress {
fn from_sprout(net: Network, data: [u8; 64]) -> Self {
fn from_sprout(net: NetworkType, data: [u8; 64]) -> Self {
ZcashAddress {
net: if let Network::Regtest = net {
Network::Test
net: if let NetworkType::Regtest = net {
NetworkType::Test
} else {
net
},
kind: AddressKind::Sprout(data),
}
}

fn from_sapling(net: Network, data: [u8; 43]) -> Self {
fn from_sapling(net: NetworkType, data: [u8; 43]) -> Self {
ZcashAddress {
net,
kind: AddressKind::Sapling(data),
}
}

fn from_unified(net: Network, data: unified::Address) -> Self {
fn from_unified(net: NetworkType, data: unified::Address) -> Self {
ZcashAddress {
net,
kind: AddressKind::Unified(data),
}
}

fn from_transparent_p2pkh(net: Network, data: [u8; 20]) -> Self {
fn from_transparent_p2pkh(net: NetworkType, data: [u8; 20]) -> Self {
ZcashAddress {
net: if let Network::Regtest = net {
Network::Test
net: if let NetworkType::Regtest = net {
NetworkType::Test
} else {
net
},
kind: AddressKind::P2pkh(data),
}
}

fn from_transparent_p2sh(net: Network, data: [u8; 20]) -> Self {
fn from_transparent_p2sh(net: NetworkType, data: [u8; 20]) -> Self {
ZcashAddress {
net: if let Network::Regtest = net {
Network::Test
net: if let NetworkType::Regtest = net {
NetworkType::Test
} else {
net
},
kind: AddressKind::P2sh(data),
}
}

fn from_tex(net: Network, data: [u8; 20]) -> Self {
fn from_tex(net: NetworkType, data: [u8; 20]) -> Self {
ZcashAddress {
net,
kind: AddressKind::Tex(data),
Expand Down
Loading

0 comments on commit f6c0534

Please sign in to comment.