Allow transparent-only unified addresses and viewing keys.

This commit is contained in:
Kris Nuttycombe 2024-04-22 12:27:31 -06:00
parent cab4d84464
commit fa225d479a
5 changed files with 28 additions and 18 deletions

View File

@ -561,7 +561,6 @@ pub(crate) mod private {
) -> Result<Self, ParseError> {
assert!(u32::from(Typecode::P2SH) == u32::from(Typecode::P2PKH) + 1);
let mut only_transparent = true;
let mut prev_code = None; // less than any Some
for item in &items {
let t = item.typecode();
@ -576,16 +575,11 @@ pub(crate) mod private {
return Err(ParseError::BothP2phkAndP2sh);
} else {
prev_code = t_code;
only_transparent = only_transparent && item.is_transparent_data_item();
}
}
if only_transparent {
Err(ParseError::OnlyTransparent)
} else {
// All checks pass!
Ok(Self::from_inner(revision, items))
}
// All checks pass!
Ok(Self::from_inner(revision, items))
}
fn parse_internal<T: Into<Vec<u8>>>(hrp: &str, buf: T) -> Result<Self, ParseError> {

View File

@ -357,9 +357,6 @@ mod tests {
0xf4, 0xf5, 0x16, 0xef, 0x5c, 0xe0, 0x26, 0xbc, 0x23, 0x73, 0x76, 0x3f, 0x4b,
];
assert_eq!(
Ufvk::parse_internal(Ufvk::MAINNET_R0, &encoded[..]),
Err(ParseError::OnlyTransparent)
);
assert_matches!(Ufvk::parse_internal(Ufvk::MAINNET_R0, &encoded[..]), Ok(_));
}
}

View File

@ -339,9 +339,6 @@ mod tests {
0xbd, 0xfe, 0xa4, 0xb7, 0x47, 0x20, 0x92, 0x6, 0xf0, 0x0, 0xf9, 0x64,
];
assert_eq!(
Uivk::parse_internal(Uivk::MAINNET_R0, &encoded[..]),
Err(ParseError::OnlyTransparent)
);
assert_matches!(Uivk::parse_internal(Uivk::MAINNET_R0, &encoded[..]), Ok(_));
}
}

View File

@ -278,7 +278,10 @@ impl UnifiedAddress {
.chain(self.expiry_time.map(unified::MetadataItem::ExpiryTime));
let ua = unified::Address::try_from_items(
if self.expiry_height().is_some() || self.expiry_time().is_some() {
if self.expiry_height().is_some()
|| self.expiry_time().is_some()
|| !(self.has_orchard() || self.has_sapling())
{
Revision::R1
} else {
Revision::R0

View File

@ -855,7 +855,10 @@ impl UnifiedFullViewingKey {
.chain(self.expiry_time.map(unified::MetadataItem::ExpiryTime));
zcash_address::unified::Ufvk::try_from_items(
if self.expiry_height().is_some() || self.expiry_time().is_some() {
if self.expiry_height().is_some()
|| self.expiry_time().is_some()
|| !(self.has_sapling() || self.has_orchard())
{
Revision::R1
} else {
Revision::R0
@ -901,12 +904,28 @@ impl UnifiedFullViewingKey {
self.sapling.as_ref()
}
/// Returns whether this UFVK contains a Sapling item.
pub fn has_sapling(&self) -> bool {
#[cfg(feature = "sapling")]
return self.sapling.is_some();
#[cfg(not(feature = "sapling"))]
return false;
}
/// Returns the Orchard full viewing key component of this unified key.
#[cfg(feature = "orchard")]
pub fn orchard(&self) -> Option<&orchard::keys::FullViewingKey> {
self.orchard.as_ref()
}
/// Returns whether this UFVK contains an Orchard item.
pub fn has_orchard(&self) -> bool {
#[cfg(feature = "orchard")]
return self.orchard.is_some();
#[cfg(not(feature = "orchard"))]
return false;
}
/// Returns any unknown data items parsed from the encoded form of the key.
pub fn unknown_data(&self) -> &[(u32, Vec<u8>)] {
self.unknown_data.as_ref()