Skip to content

Commit

Permalink
read asset type (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
joe authored Aug 31, 2023
1 parent cf8a90c commit e74ea10
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
9 changes: 9 additions & 0 deletions masp_primitives/src/asset_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ impl AssetType {
pub fn get_nonce(&self) -> Option<u8> {
self.nonce
}

/// Deserialize an AssetType object
pub fn read<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
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 {
Expand Down
18 changes: 3 additions & 15 deletions masp_primitives/src/transaction/components/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,9 @@ impl ValueSum<AssetType, i32> {
/// different assets
pub fn read<R: Read>(reader: &mut R) -> std::io::Result<Self> {
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();
Expand Down Expand Up @@ -195,13 +191,9 @@ impl ValueSum<AssetType, i64> {
/// different assets
pub fn read<R: Read>(reader: &mut R) -> std::io::Result<Self> {
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();
Expand Down Expand Up @@ -230,13 +222,9 @@ impl ValueSum<AssetType, i128> {
/// different assets
pub fn read<R: Read>(reader: &mut R) -> std::io::Result<Self> {
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();
Expand Down
14 changes: 2 additions & 12 deletions masp_primitives/src/transaction/components/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ pub struct TxIn<A: Authorization> {

impl TxIn<Authorized> {
pub fn read<R: Read>(reader: &mut R) -> io::Result<Self> {
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)?;
Expand Down Expand Up @@ -138,12 +133,7 @@ pub struct TxOut {

impl TxOut {
pub fn read<R: Read>(reader: &mut R) -> io::Result<Self> {
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)?;
Expand Down
2 changes: 1 addition & 1 deletion masp_proofs/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Signature, ()> {
ctx.binding_sig(assets_and_values, sighash)
Expand Down

0 comments on commit e74ea10

Please sign in to comment.