zei 0.0.10

Zei: Confidential Assets
Documentation
// 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.
//
// Rust Elements Library
// Written in 2019 by
//   The Elements developers
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

//! Asset ID

use mohan::{
    ser,
    hash::H256,
    dalek::scalar::Scalar
};
use mohan::fast_merkle_root;
use std::iter::FromIterator;
use serde::{ Serialize, Deserialize };

/// An issued asset ID.
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash, Debug, Serialize, Deserialize)]
pub struct AssetId(H256);

impl AssetId {
    /// Create an [AssetId] from its inner type.
    pub fn from_inner(inner: H256) -> AssetId {
        AssetId(inner)
    }

    /// Convert the [AssetId] into its inner type.
    pub fn into_inner(self) -> H256 {
        self.0
    }

    /// Convert the [AssetId] into Scalar inner type.
    pub fn into_scalar(self) -> Scalar {
        self.0.into_scalar()
    }

    /// Generate the asset entropy from the issuance prevout and the pubkey hash.
    pub fn generate_asset_entropy(
        prevout_hash: H256,
        pubkey_hash: H256,
    ) -> H256 {
        // E : entropy
        // I : prevout
        // C : contract
        // E = H( H(I) || H(C) )
        fast_merkle_root(vec![prevout_hash, pubkey_hash])
    }

    /// Calculate the asset ID from the asset entropy.
    pub fn from_entropy(entropy: H256) -> AssetId {
        // H_a : asset tag
        // E   : entropy
        // H_a = H( E || 0 )
        AssetId(fast_merkle_root(vec![entropy, H256::zero()]))
    }

    /// Calculate the reissuance token asset ID from the asset entropy.
    pub fn reissuance_token_from_entropy(entropy: H256, confidential: bool) -> AssetId {
        // H_a : asset reissuance tag
        // E   : entropy
        // if not Confidential:
        //     H_a = H( E || 1 )
        // else
        //     H_a = H( E || 2 )
        let second = match confidential {
            false => H256::from_vec(&vec![1]), 
            true => H256::from_vec(&vec![2]), 
        };

        AssetId(fast_merkle_root(vec![entropy, second]))
    }
}



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

impl ser::Readable for AssetId {
	fn read(reader: &mut dyn ser::Reader) -> Result<AssetId, ser::Error> {
		let id = H256::read(reader)?;
		Ok(AssetId(id))
	}
}


impl ::std::fmt::Display for AssetId {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        ::std::fmt::Display::fmt(&self.0, f)
    }
}