git.delta.rocks / unique-network / refs/commits / b6f32fd78e2b

difftreelog

fix sponsoring token ownership check

Yaroslav Bolyukin2021-11-23parent: #fbf3e9a.patch.diff
in: master

3 files changed

modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -52,7 +52,7 @@
 		StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u128, QueryKind = ValueQuery>;
 
 	#[pallet::storage]
-	pub(super) type Balance<T: Config> = StorageNMap<
+	pub type Balance<T: Config> = StorageNMap<
 		Key = (
 			Key<Twox64Concat, CollectionId>,
 			Key<Blake2_128Concat, T::CrossAccountId>,
modifiedpallets/nft/src/eth/sponsoring.rsdiffbeforeafterboth
before · pallets/nft/src/eth/sponsoring.rs
1//! Implements EVM sponsoring logic via OnChargeEVMTransaction23use crate::{Config, sponsorship::*};4use evm_coder::{Call, abi::AbiReader};5use pallet_common::{CollectionHandle, eth::map_eth_to_id};6use sp_core::H160;7use sp_std::prelude::*;8use up_sponsorship::SponsorshipHandler;9use core::marker::PhantomData;10use core::convert::TryInto;11use nft_data_structs::TokenId;12use up_evm_mapping::EvmBackwardsAddressMapping;13use pallet_evm::AddressMapping;1415use pallet_nonfungible::erc::{UniqueNFTCall, ERC721UniqueExtensionsCall, ERC721Call};16use pallet_fungible::erc::{UniqueFungibleCall, ERC20Call};1718pub struct NftEthSponsorshipHandler<T: Config>(PhantomData<*const T>);19impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for NftEthSponsorshipHandler<T> {20	fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {21		let collection_id = map_eth_to_id(&call.0)?;22		let collection = <CollectionHandle<T>>::new(collection_id)?;23		let sponsor = collection.sponsorship.sponsor()?.clone();24		let sponsor =25			<T as pallet_common::Config>::EvmBackwardsAddressMapping::from_account_id(sponsor);26		let who = <T as pallet_common::Config>::EvmAddressMapping::into_account_id(*who);27		let (method_id, mut reader) = AbiReader::new_call(&call.1).ok()?;28		match &collection.mode {29			crate::CollectionMode::NFT => {30				let call = UniqueNFTCall::parse(method_id, &mut reader).ok()??;31				match call {32					UniqueNFTCall::ERC721UniqueExtensions(33						ERC721UniqueExtensionsCall::Transfer { token_id, .. },34					)35					| UniqueNFTCall::ERC721(ERC721Call::TransferFrom { token_id, .. }) => {36						let token_id: TokenId = token_id.try_into().ok()?;37						withdraw_transfer::<T>(&collection, &who, &token_id).map(|()| sponsor)38					}39					UniqueNFTCall::ERC721(ERC721Call::Approve { token_id, .. }) => {40						let token_id: TokenId = token_id.try_into().ok()?;41						withdraw_approve::<T>(&collection, &who, &token_id).map(|()| sponsor)42					}43					_ => None,44				}45			}46			crate::CollectionMode::Fungible(_) => {47				let call = UniqueFungibleCall::parse(method_id, &mut reader).ok()??;48				#[allow(clippy::single_match)]49				match call {50					UniqueFungibleCall::ERC20(51						ERC20Call::Transfer { .. } | ERC20Call::TransferFrom { .. },52					) => withdraw_transfer::<T>(&collection, &who, &TokenId::default())53						.map(|()| sponsor),54					UniqueFungibleCall::ERC20(ERC20Call::Approve { .. }) => {55						withdraw_approve::<T>(&collection, &who, &TokenId::default())56							.map(|()| sponsor)57					}58					_ => None,59				}60			}61			_ => None,62		}63	}64}
modifiedpallets/nft/src/sponsorship.rsdiffbeforeafterboth
--- a/pallets/nft/src/sponsorship.rs
+++ b/pallets/nft/src/sponsorship.rs
@@ -10,25 +10,38 @@
 	storage::{StorageMap, StorageDoubleMap, StorageNMap},
 };
 use nft_data_structs::{
-	CollectionId, FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, NFT_SPONSOR_TRANSFER_TIMEOUT,
-	REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, TokenId,
+	CollectionId, FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, MetaUpdatePermission,
+	NFT_SPONSOR_TRANSFER_TIMEOUT, REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, TokenId,
 };
 use pallet_common::{CollectionHandle};
 use pallet_common::account::CrossAccountId;
 
 pub fn withdraw_transfer<T: Config>(
 	collection: &CollectionHandle<T>,
-	who: &T::AccountId,
+	who: &T::CrossAccountId,
 	item_id: &TokenId,
 ) -> Option<()> {
 	// preliminary sponsoring correctness check
-	if !((pallet_nonfungible::TokenData::<T>::get((collection.id, item_id))?.owner).as_sub() == who)
-		|| (pallet_refungible::Owned::<T>::get((
-			collection.id,
-			T::CrossAccountId::from_sub(who.clone()),
-			item_id,
-		))) {
-		return None;
+	match collection.mode {
+		CollectionMode::NFT => {
+			let owner = pallet_nonfungible::TokenData::<T>::get((collection.id, item_id))?.owner;
+			if !owner.conv_eq(who) {
+				return None;
+			}
+		}
+		CollectionMode::Fungible(_) => {
+			if item_id != &TokenId::default() {
+				return None;
+			}
+			if <pallet_fungible::Balance<T>>::get((collection.id, who)) == 0 {
+				return None;
+			}
+		}
+		CollectionMode::ReFungible => {
+			if !<pallet_refungible::Owned<T>>::get((collection.id, who, item_id)) {
+				return None;
+			}
+		}
 	}
 
 	// sponsor timeout
@@ -43,9 +56,11 @@
 
 	let last_tx_block = match collection.mode {
 		CollectionMode::NFT => <NftTransferBasket<T>>::get(collection.id, item_id),
-		CollectionMode::Fungible(_) => <FungibleTransferBasket<T>>::get(collection.id, who),
+		CollectionMode::Fungible(_) => {
+			<FungibleTransferBasket<T>>::get(collection.id, who.as_sub())
+		}
 		CollectionMode::ReFungible => {
-			<ReFungibleTransferBasket<T>>::get((collection.id, item_id, who))
+			<ReFungibleTransferBasket<T>>::get((collection.id, item_id, who.as_sub()))
 		}
 	};
 
@@ -59,11 +74,12 @@
 	match collection.mode {
 		CollectionMode::NFT => <NftTransferBasket<T>>::insert(collection.id, item_id, block_number),
 		CollectionMode::Fungible(_) => {
-			<FungibleTransferBasket<T>>::insert(collection.id, who, block_number)
-		}
-		CollectionMode::ReFungible => {
-			<ReFungibleTransferBasket<T>>::insert((collection.id, item_id, who), block_number)
+			<FungibleTransferBasket<T>>::insert(collection.id, who.as_sub(), block_number)
 		}
+		CollectionMode::ReFungible => <ReFungibleTransferBasket<T>>::insert(
+			(collection.id, item_id, who.as_sub()),
+			block_number,
+		),
 	};
 
 	Some(())
@@ -101,20 +117,37 @@
 }
 
 pub fn withdraw_set_variable_meta_data<T: Config>(
-	who: &T::AccountId,
+	who: &T::CrossAccountId,
 	collection: &CollectionHandle<T>,
 	item_id: &TokenId,
 	data: &[u8],
 ) -> Option<()> {
-	// preliminary sponsoring correctness check
-	if !((pallet_nonfungible::TokenData::<T>::get((collection.id, item_id))?.owner).as_sub() == who)
-		|| (pallet_refungible::Owned::<T>::get((
-			collection.id,
-			T::CrossAccountId::from_sub(who.clone()),
-			item_id,
-		))) {
+	// TODO: make it work for admins
+	if collection.meta_update_permission != MetaUpdatePermission::ItemOwner {
 		return None;
 	}
+	// preliminary sponsoring correctness check
+	match collection.mode {
+		CollectionMode::NFT => {
+			let owner = pallet_nonfungible::TokenData::<T>::get((collection.id, item_id))?.owner;
+			if !owner.conv_eq(who) {
+				return None;
+			}
+		}
+		CollectionMode::Fungible(_) => {
+			if item_id != &TokenId::default() {
+				return None;
+			}
+			if <pallet_fungible::Balance<T>>::get((collection.id, who)) == 0 {
+				return None;
+			}
+		}
+		CollectionMode::ReFungible => {
+			if !<pallet_refungible::Owned<T>>::get((collection.id, who, item_id)) {
+				return None;
+			}
+		}
+	}
 
 	// Can't sponsor fungible collection, this tx will be rejected
 	// as invalid
@@ -203,14 +236,23 @@
 				collection_id,
 				item_id,
 				..
+			} => {
+				let (sponsor, collection) = load(*collection_id)?;
+				withdraw_transfer::<T>(
+					&collection,
+					&T::CrossAccountId::from_sub(who.clone()),
+					item_id,
+				)
+				.map(|()| sponsor)
 			}
-			| Call::transfer_from {
+			Call::transfer_from {
 				collection_id,
 				item_id,
+				from,
 				..
 			} => {
 				let (sponsor, collection) = load(*collection_id)?;
-				withdraw_transfer::<T>(&collection, who, item_id).map(|()| sponsor)
+				withdraw_transfer::<T>(&collection, from, item_id).map(|()| sponsor)
 			}
 			Call::approve {
 				collection_id,
@@ -226,8 +268,13 @@
 				data,
 			} => {
 				let (sponsor, collection) = load(*collection_id)?;
-				withdraw_set_variable_meta_data::<T>(&who, &collection, item_id, data)
-					.map(|()| sponsor)
+				withdraw_set_variable_meta_data::<T>(
+					&T::CrossAccountId::from_sub(who.clone()),
+					&collection,
+					item_id,
+					data,
+				)
+				.map(|()| sponsor)
 			}
 			_ => None,
 		}