zei 0.0.10

Zei: Confidential Assets
// Copyright 2019 Stichting Organism
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Zei Confidential Assets

use mohan::{
    ser,
    dalek::{
        ristretto::CompressedRistretto,
        scalar::Scalar
    },
    hash::H256
};
use crate::core::AssetId;
use crate::Lockbox;
use serde::{ Serialize, Deserialize };


/// Description of an asset issuance in a transaction input
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct AssetIssuance {
    /// Zero for a new asset issuance; otherwise a blinding factor for the input
    pub asset_blinding_nonce: CompressedRistretto,
    /// Freeform entropy field
    pub asset_entropy: H256,
    /// Amount of asset to issue
    pub amount: CompressedRistretto,
    /// Amount of inflation keys to issue
    pub inflation_keys: CompressedRistretto,
}


impl ser::Writeable for AssetIssuance {
	fn write<W: ser::Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
        self.asset_blinding_nonce.write(writer)?;
		self.asset_entropy.write(writer)?;
		self.amount.write(writer)?;
        self.inflation_keys.write(writer)?;
		Ok(())
	}
}

impl ser::Readable for AssetIssuance {
	fn read(reader: &mut dyn ser::Reader) -> Result<AssetIssuance, ser::Error> {
		Ok(AssetIssuance {
            asset_blinding_nonce: CompressedRistretto::read(reader)?,
			asset_entropy: H256::read(reader)?,
			amount: CompressedRistretto::read(reader)?,
            inflation_keys: CompressedRistretto::read(reader)?
		})
	}
}


/// Unblinded Asset Record for local visability and proofs
#[derive(Clone, Default, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct OpenAssetOut {
    pub blinded: AssetOut,
    pub value: u64,
    pub value_blind: Scalar,
    pub flavor_id: AssetId,
    pub flavor_blind: Scalar
}

impl ser::Writeable for OpenAssetOut {
	fn write<W: ser::Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
        self.blinded.write(writer)?;
		writer.write_u64(self.value)?;
		self.value_blind.write(writer)?;
        self.flavor_id.write(writer)?;
        self.flavor_blind.write(writer)?;
		Ok(())
	}
}

impl ser::Readable for OpenAssetOut {
	fn read(reader: &mut dyn ser::Reader) -> Result<OpenAssetOut, ser::Error> {
		Ok(OpenAssetOut {
            blinded: AssetOut::read(reader)?,
			value: reader.read_u64()?,
            value_blind: Scalar::read(reader)?,
			flavor_id: AssetId::read(reader)?,
            flavor_blind: Scalar::read(reader)?,
		})
	}
}

/// Transaction output
#[derive(Clone, Default, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct AssetOut {
    /// Committed asset
    pub flavor: CompressedRistretto,
    /// Committed amount
    pub value: CompressedRistretto,
    /// Encypted Amount and Blinding Scalar, 40 Bytes (8 byte u64, 32 byte Scalar)
    pub vessel: Lockbox
}


impl ser::Writeable for AssetOut {
	fn write<W: ser::Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
        self.flavor.write(writer)?;
		self.value.write(writer)?;
		self.vessel.write(writer)?;
		Ok(())
	}
}

impl ser::Readable for AssetOut {
	fn read(reader: &mut dyn ser::Reader) -> Result<AssetOut, ser::Error> {
		Ok(AssetOut {
            flavor: CompressedRistretto::read(reader)?,
			value: CompressedRistretto::read(reader)?,
			vessel: Lockbox::read(reader)?,
		})
	}
}