From e475caefff1cbcfb3a6e76605f77dff3cfdea120 Mon Sep 17 00:00:00 2001 From: joe bebel <55120843+joebebel@users.noreply.github.com> Date: Mon, 14 Aug 2023 23:56:43 -0700 Subject: [PATCH 1/2] read asset type --- 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..44451524 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 fac323c5..77b27034 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) From ae0ef1c6818fbf6339613c83229a425b38fa6ad7 Mon Sep 17 00:00:00 2001 From: joe bebel <55120843+joebebel@users.noreply.github.com> Date: Wed, 30 Aug 2023 16:44:37 -0700 Subject: [PATCH 2/2] cargo fmt --- masp_primitives/src/asset_type.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/masp_primitives/src/asset_type.rs b/masp_primitives/src/asset_type.rs index 44451524..24a48021 100644 --- a/masp_primitives/src/asset_type.rs +++ b/masp_primitives/src/asset_type.rs @@ -149,14 +149,14 @@ impl AssetType { 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") - }) - } + /// 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 {