Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read asset type #56

Merged
3 commits merged into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading