Skip to content

Commit

Permalink
Remove standalone Orchard address Display, FromStr impls and related …
Browse files Browse the repository at this point in the history
…parts

NU5 proposes Unified Addresses, which would supplant any classic standalone pool address for Orchard.
#1885 (comment)
  • Loading branch information
dconnolly committed Apr 30, 2021
1 parent 72491df commit 48674a9
Showing 1 changed file with 2 additions and 76 deletions.
78 changes: 2 additions & 76 deletions zebra-chain/src/orchard/address.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
//! Orchard shielded payment addresses.
use std::{
fmt,
io::{self, Read, Write},
};

use bech32::{self, FromBase32, ToBase32, Variant};
use std::fmt;

#[cfg(test)]
use proptest::prelude::*;

use crate::{
parameters::Network,
serialization::{ReadZcashExt, SerializationError},
};
use crate::parameters::Network;

use super::keys;

/// Human-Readable Parts for input to bech32 encoding.
mod human_readable_parts {
pub const MAINNET: &str = "zo";
pub const TESTNET: &str = "ztestorchard";
}

/// A Orchard _shielded payment address_.
///
/// Also known as a _diversified payment address_ for Orchard, as
Expand All @@ -46,50 +32,6 @@ impl fmt::Debug for Address {
}
}

impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut bytes = io::Cursor::new(Vec::new());

let _ = bytes.write_all(&<[u8; 11]>::from(self.diversifier));
let _ = bytes.write_all(&<[u8; 32]>::from(self.transmission_key));

let hrp = match self.network {
Network::Mainnet => human_readable_parts::MAINNET,
Network::Testnet => human_readable_parts::TESTNET,
};

bech32::encode_to_fmt(f, hrp, bytes.get_ref().to_base32(), Variant::Bech32).unwrap()
}
}

impl std::str::FromStr for Address {
type Err = SerializationError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match bech32::decode(s) {
Ok((hrp, bytes, Variant::Bech32)) => {
let mut decoded_bytes = io::Cursor::new(Vec::<u8>::from_base32(&bytes).unwrap());

let mut diversifier_bytes = [0; 11];
decoded_bytes.read_exact(&mut diversifier_bytes)?;

let transmission_key_bytes = decoded_bytes.read_32_bytes()?;

Ok(Address {
network: match hrp.as_str() {
human_readable_parts::MAINNET => Network::Mainnet,
human_readable_parts::TESTNET => Network::Testnet,
_ => return Err(SerializationError::Parse("unknown network")),
},
diversifier: keys::Diversifier::from(diversifier_bytes),
transmission_key: keys::TransmissionKey::from(transmission_key_bytes),
})
}
_ => Err(SerializationError::Parse("bech32 decoding error")),
}
}
}

#[cfg(test)]
impl Arbitrary for Address {
type Parameters = ();
Expand Down Expand Up @@ -141,19 +83,3 @@ mod tests {
};
}
}

#[cfg(test)]
proptest! {

#[test]
fn orchard_address_roundtrip(zaddr in any::<Address>()) {
zebra_test::init();

let string = zaddr.to_string();

let zaddr2 = string.parse::<Address>()
.expect("randomized orchard z-addr should deserialize");

prop_assert_eq![zaddr, zaddr2];
}
}

0 comments on commit 48674a9

Please sign in to comment.