Disallow decoding of phantom asset (#6597)

* Disallow decoding of phantom asset

* Remove pub
This commit is contained in:
Gavin Wood
2023-01-20 15:15:33 -03:00
committed by GitHub
parent 7a09c3f6a2
commit 808ff7d3f8
+18 -1
View File
@@ -235,7 +235,7 @@ impl TryFrom<AssetInstance> for u128 {
}
/// Classification of whether an asset is fungible or not, along with a mandatory amount or instance.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, Decode, TypeInfo, MaxEncodedLen)]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Encode, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum Fungibility {
/// A fungible asset; we record a number of units, as a `u128` in the inner item.
@@ -245,6 +245,23 @@ pub enum Fungibility {
NonFungible(AssetInstance),
}
#[derive(Decode)]
enum UncheckedFungibility {
Fungible(#[codec(compact)] u128),
NonFungible(AssetInstance),
}
impl Decode for Fungibility {
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, parity_scale_codec::Error> {
match UncheckedFungibility::decode(input)? {
UncheckedFungibility::Fungible(a) if a != 0 => Ok(Self::Fungible(a)),
UncheckedFungibility::NonFungible(i) => Ok(Self::NonFungible(i)),
UncheckedFungibility::Fungible(_) =>
Err("Fungible asset of zero amount is not allowed".into()),
}
}
}
impl Fungibility {
pub fn is_kind(&self, w: WildFungibility) -> bool {
use Fungibility::*;