Update SproutShieldedAddress types

Use magics module.
Stub out key types, including a TransmissionKey alias for x25519_dalek::PublicKey
This commit is contained in:
Deirdre Connolly 2020-03-17 18:49:15 -04:00 committed by Deirdre Connolly
parent 01afa09575
commit c3700c1a37
2 changed files with 21 additions and 13 deletions

View File

@ -3,9 +3,7 @@
use std::{fmt, io}; use std::{fmt, io};
use bs58; use bs58;
use ripemd160::{Digest, Ripemd160};
use sha2::Sha256; use sha2::Sha256;
use x25519_dalek;
#[cfg(test)] #[cfg(test)]
use proptest::{arbitrary::Arbitrary, collection::vec, prelude::*}; use proptest::{arbitrary::Arbitrary, collection::vec, prelude::*};
@ -18,6 +16,13 @@ use crate::{
Network, Network,
}; };
/// Magic numbers used to identify what networks Sprout Shielded
/// Addresses are associated with.
mod magics {
pub const MAINNET: [u8; 2] = [0x16, 0x9A];
pub const TESTNET: [u8; 2] = [0x16, 0xB6];
}
/// Sprout Shielded Payment Addresses /// Sprout Shielded Payment Addresses
/// ///
/// In Bitcoin a single byte is used for the version field identifying /// In Bitcoin a single byte is used for the version field identifying
@ -29,11 +34,11 @@ use crate::{
/// to a Bitcoin address just by removing the “t”.) /// to a Bitcoin address just by removing the “t”.)
/// ///
/// https://zips.z.cash/protocol/protocol.pdf#transparentaddrencoding /// https://zips.z.cash/protocol/protocol.pdf#transparentaddrencoding
#[derive(Clone, Eq, PartialEq)] #[derive(Copy, Clone)]
pub struct SproutShieldedAddress { pub struct SproutShieldedAddress {
network: Network, network: Network,
paying_key: sprout::PayingKey, paying_key: sprout::PayingKey,
transmission_key: x25519_dalek::PublicKey, transmission_key: sprout::TransmissionKey,
} }
impl fmt::Debug for SproutShieldedAddress { impl fmt::Debug for SproutShieldedAddress {
@ -50,13 +55,11 @@ impl fmt::Debug for SproutShieldedAddress {
impl ZcashSerialize for SproutShieldedAddress { impl ZcashSerialize for SproutShieldedAddress {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> { fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
if self.network == Network::Mainnet { if self.network == Network::Mainnet {
writer.write_all(&[0x16, 0x9A][..])? writer.write_all(&magics::MAINNET[..])?
} else { } else {
writer.write_all(&[0x16, 0xB6][..])? writer.write_all(&magics::TESTNET[..])?
} }
writer.write_all(&self.paying_key.0[..])?; writer.write_all(&self.paying_key.0[..])?;
// XXX revisit to see if we want to impl ZcashSerialize on
// x25519_dalek::PublicKey
writer.write_all(self.transmission_key.as_bytes())?; writer.write_all(self.transmission_key.as_bytes())?;
Ok(()) Ok(())
@ -69,8 +72,8 @@ impl ZcashDeserialize for SproutShieldedAddress {
reader.read_exact(&mut version_bytes)?; reader.read_exact(&mut version_bytes)?;
let network = match version_bytes { let network = match version_bytes {
[0x16, 0x9A] => Network::Mainnet, magics::MAINNET => Network::Mainnet,
[0x16, 0xB6] => Network::Testnet, magics::TESTNET => Network::Testnet,
_ => panic!(SerializationError::Parse( _ => panic!(SerializationError::Parse(
"bad sprout shielded addr version/type", "bad sprout shielded addr version/type",
)), )),
@ -79,7 +82,7 @@ impl ZcashDeserialize for SproutShieldedAddress {
Ok(SproutShieldedAddress { Ok(SproutShieldedAddress {
network, network,
paying_key: sprout::PayingKey(reader.read_32_bytes()?), paying_key: sprout::PayingKey(reader.read_32_bytes()?),
transmission_key: sprout::TransmissionKey(reader.read_32_bytes()?), transmission_key: sprout::TransmissionKey::from(reader.read_32_bytes()?),
}) })
} }
} }

View File

@ -21,12 +21,17 @@ use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}
/// All other Sprout key types derive from the SpendingKey value. /// All other Sprout key types derive from the SpendingKey value.
pub struct SpendingKey; pub struct SpendingKey;
///
pub struct ReceivingKey; pub struct ReceivingKey;
pub struct PayingKey; ///
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct PayingKey(pub [u8; 32]);
pub struct TransmissionKey; ///
pub type TransmissionKey = x25519_dalek::PublicKey;
///
pub struct IncomingViewingKey { pub struct IncomingViewingKey {
paying_key: PayingKey, paying_key: PayingKey,
receiving_key: ReceivingKey, receiving_key: ReceivingKey,