From e74ea10713f1dda19a349e4dd49e1f8b30e403ca Mon Sep 17 00:00:00 2001 From: joe <55120843+joebebel@users.noreply.github.com> Date: Wed, 30 Aug 2023 17:01:14 -0700 Subject: [PATCH] read asset type (#56) --- masp_primitives/src/asset_type.rs | 9 +++++++++ .../src/transaction/components/amount.rs | 18 +++--------------- .../src/transaction/components/transparent.rs | 14 ++------------ masp_proofs/src/prover.rs | 2 +- 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/masp_primitives/src/asset_type.rs b/masp_primitives/src/asset_type.rs index b36bf46a..24a48021 100644 --- a/masp_primitives/src/asset_type.rs +++ b/masp_primitives/src/asset_type.rs @@ -148,6 +148,15 @@ impl AssetType { pub fn get_nonce(&self) -> Option { self.nonce } + + /// Deserialize an AssetType object + pub fn read(reader: &mut R) -> std::io::Result { + let mut atype = [0; crate::constants::ASSET_IDENTIFIER_LENGTH]; + reader.read_exact(&mut atype)?; + AssetType::from_identifier(&atype).ok_or_else(|| { + std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid asset type") + }) + } } impl PartialEq for AssetType { diff --git a/masp_primitives/src/transaction/components/amount.rs b/masp_primitives/src/transaction/components/amount.rs index 70e03438..6342a398 100644 --- a/masp_primitives/src/transaction/components/amount.rs +++ b/masp_primitives/src/transaction/components/amount.rs @@ -160,13 +160,9 @@ impl ValueSum { /// different assets pub fn read(reader: &mut R) -> std::io::Result { let vec = Vector::read(reader, |reader| { - let mut atype = [0; 32]; + let atype = AssetType::read(reader)?; let mut value = [0; 4]; - reader.read_exact(&mut atype)?; reader.read_exact(&mut value)?; - let atype = AssetType::from_identifier(&atype).ok_or_else(|| { - std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid asset type") - })?; Ok((atype, i32::from_le_bytes(value))) })?; let mut ret = Self::zero(); @@ -195,13 +191,9 @@ impl ValueSum { /// different assets pub fn read(reader: &mut R) -> std::io::Result { let vec = Vector::read(reader, |reader| { - let mut atype = [0; 32]; + let atype = AssetType::read(reader)?; let mut value = [0; 8]; - reader.read_exact(&mut atype)?; reader.read_exact(&mut value)?; - let atype = AssetType::from_identifier(&atype).ok_or_else(|| { - std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid asset type") - })?; Ok((atype, i64::from_le_bytes(value))) })?; let mut ret = Self::zero(); @@ -230,13 +222,9 @@ impl ValueSum { /// different assets pub fn read(reader: &mut R) -> std::io::Result { let vec = Vector::read(reader, |reader| { - let mut atype = [0; 32]; + let atype = AssetType::read(reader)?; let mut value = [0; 16]; - reader.read_exact(&mut atype)?; reader.read_exact(&mut value)?; - let atype = AssetType::from_identifier(&atype).ok_or_else(|| { - std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid asset type") - })?; Ok((atype, i128::from_le_bytes(value))) })?; let mut ret = Self::zero(); diff --git a/masp_primitives/src/transaction/components/transparent.rs b/masp_primitives/src/transaction/components/transparent.rs index 85391e8c..daee2670 100644 --- a/masp_primitives/src/transaction/components/transparent.rs +++ b/masp_primitives/src/transaction/components/transparent.rs @@ -91,12 +91,7 @@ pub struct TxIn { impl TxIn { pub fn read(reader: &mut R) -> io::Result { - let asset_type = { - let mut tmp = [0u8; 32]; - reader.read_exact(&mut tmp)?; - AssetType::from_identifier(&tmp) - } - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "invalid asset identifier"))?; + let asset_type = AssetType::read(reader)?; let value = { let mut tmp = [0u8; 8]; reader.read_exact(&mut tmp)?; @@ -138,12 +133,7 @@ pub struct TxOut { impl TxOut { pub fn read(reader: &mut R) -> io::Result { - let asset_type = { - let mut tmp = [0u8; 32]; - reader.read_exact(&mut tmp)?; - AssetType::from_identifier(&tmp) - } - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "invalid asset identifier"))?; + let asset_type = AssetType::read(reader)?; let value = { let mut tmp = [0u8; 8]; reader.read_exact(&mut tmp)?; diff --git a/masp_proofs/src/prover.rs b/masp_proofs/src/prover.rs index 355fb350..7f7cba20 100644 --- a/masp_proofs/src/prover.rs +++ b/masp_proofs/src/prover.rs @@ -247,7 +247,7 @@ impl TxProver for LocalTxProver { fn binding_sig( &self, ctx: &mut Self::SaplingProvingContext, - assets_and_values: &I128Sum, //&[(AssetType, i64)], + assets_and_values: &I128Sum, sighash: &[u8; 32], ) -> Result { ctx.binding_sig(assets_and_values, sighash)