2018-10-12 10:22:58 -07:00
|
|
|
//! Generated code for handling light client protobuf structs.
|
|
|
|
|
2020-04-22 22:32:04 -07:00
|
|
|
use ff::PrimeField;
|
2020-07-01 13:26:54 -07:00
|
|
|
use group::GroupEncoding;
|
2021-04-05 10:51:07 -07:00
|
|
|
use std::convert::{TryFrom, TryInto};
|
2020-08-05 13:08:58 -07:00
|
|
|
|
|
|
|
use zcash_primitives::{
|
|
|
|
block::{BlockHash, BlockHeader},
|
|
|
|
consensus::BlockHeight,
|
2021-03-04 13:26:39 -08:00
|
|
|
sapling::Nullifier,
|
2021-05-11 09:54:39 -07:00
|
|
|
transaction::{
|
|
|
|
components::sapling::{CompactOutputDescription, OutputDescription},
|
|
|
|
TxId,
|
|
|
|
},
|
2020-08-05 13:08:58 -07:00
|
|
|
};
|
2019-05-01 05:21:48 -07:00
|
|
|
|
2021-04-05 10:51:07 -07:00
|
|
|
use zcash_note_encryption::COMPACT_NOTE_SIZE;
|
|
|
|
|
2018-10-12 10:22:58 -07:00
|
|
|
pub mod compact_formats;
|
2019-05-01 05:21:48 -07:00
|
|
|
|
|
|
|
impl compact_formats::CompactBlock {
|
|
|
|
/// Returns the [`BlockHash`] for this block.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function will panic if [`CompactBlock.header`] is not set and
|
|
|
|
/// [`CompactBlock.hash`] is not exactly 32 bytes.
|
|
|
|
///
|
|
|
|
/// [`CompactBlock.header`]: #structfield.header
|
|
|
|
/// [`CompactBlock.hash`]: #structfield.hash
|
|
|
|
pub fn hash(&self) -> BlockHash {
|
|
|
|
if let Some(header) = self.header() {
|
|
|
|
header.hash()
|
|
|
|
} else {
|
|
|
|
BlockHash::from_slice(&self.hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the [`BlockHash`] for this block's parent.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function will panic if [`CompactBlock.header`] is not set and
|
|
|
|
/// [`CompactBlock.prevHash`] is not exactly 32 bytes.
|
|
|
|
///
|
|
|
|
/// [`CompactBlock.header`]: #structfield.header
|
|
|
|
/// [`CompactBlock.prevHash`]: #structfield.prevHash
|
|
|
|
pub fn prev_hash(&self) -> BlockHash {
|
|
|
|
if let Some(header) = self.header() {
|
|
|
|
header.prev_block
|
|
|
|
} else {
|
|
|
|
BlockHash::from_slice(&self.prevHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 19:44:06 -07:00
|
|
|
/// Returns the [`BlockHeight`] value for this block
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function will panic if [`CompactBlock.height`] is not
|
|
|
|
/// representable within a u32.
|
|
|
|
pub fn height(&self) -> BlockHeight {
|
|
|
|
self.height.try_into().unwrap()
|
|
|
|
}
|
|
|
|
|
2019-05-01 05:21:48 -07:00
|
|
|
/// Returns the [`BlockHeader`] for this block if present.
|
|
|
|
///
|
|
|
|
/// A convenience method that parses [`CompactBlock.header`] if present.
|
|
|
|
///
|
|
|
|
/// [`CompactBlock.header`]: #structfield.header
|
|
|
|
pub fn header(&self) -> Option<BlockHeader> {
|
|
|
|
if self.header.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
BlockHeader::read(&self.header[..]).ok()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 03:59:07 -07:00
|
|
|
|
2021-05-11 09:54:39 -07:00
|
|
|
impl compact_formats::CompactTx {
|
|
|
|
/// Returns the transaction Id
|
|
|
|
pub fn txid(&self) -> TxId {
|
|
|
|
let mut hash = [0u8; 32];
|
|
|
|
hash.copy_from_slice(&self.hash);
|
|
|
|
TxId::from_bytes(hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 03:59:07 -07:00
|
|
|
impl compact_formats::CompactOutput {
|
|
|
|
/// Returns the note commitment for this output.
|
|
|
|
///
|
|
|
|
/// A convenience method that parses [`CompactOutput.cmu`].
|
|
|
|
///
|
|
|
|
/// [`CompactOutput.cmu`]: #structfield.cmu
|
2020-07-01 13:26:54 -07:00
|
|
|
pub fn cmu(&self) -> Result<bls12_381::Scalar, ()> {
|
|
|
|
let mut repr = [0; 32];
|
2020-04-22 22:32:04 -07:00
|
|
|
repr.as_mut().copy_from_slice(&self.cmu[..]);
|
2020-07-01 13:26:54 -07:00
|
|
|
bls12_381::Scalar::from_repr(repr).ok_or(())
|
2019-08-26 03:59:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the ephemeral public key for this output.
|
|
|
|
///
|
|
|
|
/// A convenience method that parses [`CompactOutput.epk`].
|
|
|
|
///
|
|
|
|
/// [`CompactOutput.epk`]: #structfield.epk
|
2020-09-09 16:39:21 -07:00
|
|
|
pub fn epk(&self) -> Result<jubjub::ExtendedPoint, ()> {
|
|
|
|
let p = jubjub::ExtendedPoint::from_bytes(&self.epk[..].try_into().map_err(|_| ())?);
|
2020-07-01 13:26:54 -07:00
|
|
|
if p.is_some().into() {
|
|
|
|
Ok(p.unwrap())
|
|
|
|
} else {
|
|
|
|
Err(())
|
|
|
|
}
|
2019-08-26 03:59:07 -07:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 09:09:36 -08:00
|
|
|
|
2021-04-05 10:51:07 -07:00
|
|
|
impl From<OutputDescription> for compact_formats::CompactOutput {
|
|
|
|
fn from(out: OutputDescription) -> compact_formats::CompactOutput {
|
|
|
|
let mut result = compact_formats::CompactOutput::new();
|
|
|
|
result.set_cmu(out.cmu.to_repr().to_vec());
|
|
|
|
result.set_epk(out.ephemeral_key.to_bytes().to_vec());
|
|
|
|
result.set_ciphertext(out.enc_ciphertext[..COMPACT_NOTE_SIZE].to_vec());
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<compact_formats::CompactOutput> for CompactOutputDescription {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn try_from(value: compact_formats::CompactOutput) -> Result<Self, Self::Error> {
|
|
|
|
Ok(CompactOutputDescription {
|
|
|
|
cmu: value.cmu()?,
|
|
|
|
epk: value.epk()?,
|
|
|
|
enc_ciphertext: value.ciphertext,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 09:09:36 -08:00
|
|
|
impl compact_formats::CompactSpend {
|
|
|
|
pub fn nf(&self) -> Result<Nullifier, ()> {
|
|
|
|
Nullifier::from_slice(&self.nf).map_err(|_| ())
|
|
|
|
}
|
|
|
|
}
|