difftreelog
Merge pull request #238 from UniqueNetwork/fix/sponsoring-checks
in: master
Fix sponsoring token ownership check
3 files changed
pallets/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>,
pallets/nft/src/eth/sponsoring.rsdiffbeforeafterboth--- a/pallets/nft/src/eth/sponsoring.rs
+++ b/pallets/nft/src/eth/sponsoring.rs
@@ -10,7 +10,7 @@
use core::convert::TryInto;
use nft_data_structs::TokenId;
use up_evm_mapping::EvmBackwardsAddressMapping;
-use pallet_evm::AddressMapping;
+use pallet_common::account::CrossAccountId;
use pallet_nonfungible::erc::{UniqueNFTCall, ERC721UniqueExtensionsCall, ERC721Call};
use pallet_fungible::erc::{UniqueFungibleCall, ERC20Call};
@@ -23,7 +23,7 @@
let sponsor = collection.sponsorship.sponsor()?.clone();
let sponsor =
<T as pallet_common::Config>::EvmBackwardsAddressMapping::from_account_id(sponsor);
- let who = <T as pallet_common::Config>::EvmAddressMapping::into_account_id(*who);
+ let who = T::CrossAccountId::from_eth(*who);
let (method_id, mut reader) = AbiReader::new_call(&call.1).ok()?;
match &collection.mode {
crate::CollectionMode::NFT => {
@@ -31,14 +31,19 @@
match call {
UniqueNFTCall::ERC721UniqueExtensions(
ERC721UniqueExtensionsCall::Transfer { token_id, .. },
- )
- | UniqueNFTCall::ERC721(ERC721Call::TransferFrom { token_id, .. }) => {
+ ) => {
let token_id: TokenId = token_id.try_into().ok()?;
withdraw_transfer::<T>(&collection, &who, &token_id).map(|()| sponsor)
}
+ UniqueNFTCall::ERC721(ERC721Call::TransferFrom { token_id, from, .. }) => {
+ let token_id: TokenId = token_id.try_into().ok()?;
+ let from = T::CrossAccountId::from_eth(from);
+ withdraw_transfer::<T>(&collection, &from, &token_id).map(|()| sponsor)
+ }
UniqueNFTCall::ERC721(ERC721Call::Approve { token_id, .. }) => {
let token_id: TokenId = token_id.try_into().ok()?;
- withdraw_approve::<T>(&collection, &who, &token_id).map(|()| sponsor)
+ withdraw_approve::<T>(&collection, who.as_sub(), &token_id)
+ .map(|()| sponsor)
}
_ => None,
}
@@ -47,12 +52,17 @@
let call = UniqueFungibleCall::parse(method_id, &mut reader).ok()??;
#[allow(clippy::single_match)]
match call {
- UniqueFungibleCall::ERC20(
- ERC20Call::Transfer { .. } | ERC20Call::TransferFrom { .. },
- ) => withdraw_transfer::<T>(&collection, &who, &TokenId::default())
- .map(|()| sponsor),
+ UniqueFungibleCall::ERC20(ERC20Call::Transfer { .. }) => {
+ withdraw_transfer::<T>(&collection, &who, &TokenId::default())
+ .map(|()| sponsor)
+ }
+ UniqueFungibleCall::ERC20(ERC20Call::TransferFrom { from, .. }) => {
+ let from = T::CrossAccountId::from_eth(from);
+ withdraw_transfer::<T>(&collection, &from, &TokenId::default())
+ .map(|()| sponsor)
+ }
UniqueFungibleCall::ERC20(ERC20Call::Approve { .. }) => {
- withdraw_approve::<T>(&collection, &who, &TokenId::default())
+ withdraw_approve::<T>(&collection, who.as_sub(), &TokenId::default())
.map(|()| sponsor)
}
_ => None,
pallets/nft/src/sponsorship.rsdiffbeforeafterboth10 storage::{StorageMap, StorageDoubleMap, StorageNMap},10 storage::{StorageMap, StorageDoubleMap, StorageNMap},11};11};12use nft_data_structs::{12use nft_data_structs::{13 CollectionId, FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, NFT_SPONSOR_TRANSFER_TIMEOUT,13 CollectionId, FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, MetaUpdatePermission,14 REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, TokenId,14 NFT_SPONSOR_TRANSFER_TIMEOUT, REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, TokenId,15};15};16use pallet_common::{CollectionHandle};16use pallet_common::{CollectionHandle};17use pallet_common::account::CrossAccountId;17use pallet_common::account::CrossAccountId;181819pub fn withdraw_transfer<T: Config>(19pub fn withdraw_transfer<T: Config>(20 collection: &CollectionHandle<T>,20 collection: &CollectionHandle<T>,21 who: &T::AccountId,21 who: &T::CrossAccountId,22 item_id: &TokenId,22 item_id: &TokenId,23) -> Option<()> {23) -> Option<()> {24 // preliminary sponsoring correctness check24 // preliminary sponsoring correctness check25 match collection.mode {26 CollectionMode::NFT => {25 if !((pallet_nonfungible::TokenData::<T>::get((collection.id, item_id))?.owner).as_sub() == who)27 let owner = pallet_nonfungible::TokenData::<T>::get((collection.id, item_id))?.owner;28 if !owner.conv_eq(who) {29 return None;30 }31 }26 || (pallet_refungible::Owned::<T>::get((32 CollectionMode::Fungible(_) => {33 if item_id != &TokenId::default() {34 return None;35 }27 collection.id,36 if <pallet_fungible::Balance<T>>::get((collection.id, who)) == 0 {28 T::CrossAccountId::from_sub(who.clone()),37 return None;29 item_id,38 }39 }40 CollectionMode::ReFungible => {30 ))) {41 if !<pallet_refungible::Owned<T>>::get((collection.id, who, item_id)) {31 return None;42 return None;32 }43 }44 }45 }334634 // sponsor timeout47 // sponsor timeout35 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;48 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;435644 let last_tx_block = match collection.mode {57 let last_tx_block = match collection.mode {45 CollectionMode::NFT => <NftTransferBasket<T>>::get(collection.id, item_id),58 CollectionMode::NFT => <NftTransferBasket<T>>::get(collection.id, item_id),46 CollectionMode::Fungible(_) => <FungibleTransferBasket<T>>::get(collection.id, who),59 CollectionMode::Fungible(_) => {60 <FungibleTransferBasket<T>>::get(collection.id, who.as_sub())61 }47 CollectionMode::ReFungible => {62 CollectionMode::ReFungible => {48 <ReFungibleTransferBasket<T>>::get((collection.id, item_id, who))63 <ReFungibleTransferBasket<T>>::get((collection.id, item_id, who.as_sub()))49 }64 }50 };65 };516659 match collection.mode {74 match collection.mode {60 CollectionMode::NFT => <NftTransferBasket<T>>::insert(collection.id, item_id, block_number),75 CollectionMode::NFT => <NftTransferBasket<T>>::insert(collection.id, item_id, block_number),61 CollectionMode::Fungible(_) => {76 CollectionMode::Fungible(_) => {62 <FungibleTransferBasket<T>>::insert(collection.id, who, block_number)77 <FungibleTransferBasket<T>>::insert(collection.id, who.as_sub(), block_number)63 }78 }64 CollectionMode::ReFungible => {79 CollectionMode::ReFungible => <ReFungibleTransferBasket<T>>::insert(65 <ReFungibleTransferBasket<T>>::insert((collection.id, item_id, who), block_number)80 (collection.id, item_id, who.as_sub()),66 }81 block_number,82 ),67 };83 };688469 Some(())85 Some(())101}117}102118103pub fn withdraw_set_variable_meta_data<T: Config>(119pub fn withdraw_set_variable_meta_data<T: Config>(104 who: &T::AccountId,120 who: &T::CrossAccountId,105 collection: &CollectionHandle<T>,121 collection: &CollectionHandle<T>,106 item_id: &TokenId,122 item_id: &TokenId,107 data: &[u8],123 data: &[u8],108) -> Option<()> {124) -> Option<()> {109 // preliminary sponsoring correctness check125 // TODO: make it work for admins110 if !((pallet_nonfungible::TokenData::<T>::get((collection.id, item_id))?.owner).as_sub() == who)126 if collection.meta_update_permission != MetaUpdatePermission::ItemOwner {127 return None;128 }129 // preliminary sponsoring correctness check130 match collection.mode {131 CollectionMode::NFT => {132 let owner = pallet_nonfungible::TokenData::<T>::get((collection.id, item_id))?.owner;133 if !owner.conv_eq(who) {134 return None;135 }136 }111 || (pallet_refungible::Owned::<T>::get((137 CollectionMode::Fungible(_) => {138 if item_id != &TokenId::default() {139 return None;140 }112 collection.id,141 if <pallet_fungible::Balance<T>>::get((collection.id, who)) == 0 {113 T::CrossAccountId::from_sub(who.clone()),142 return None;114 item_id,143 }115 ))) {144 }116 return None;145 CollectionMode::ReFungible => {117 }146 if !<pallet_refungible::Owned<T>>::get((collection.id, who, item_id)) {147 return None;148 }149 }150 }118151119 // Can't sponsor fungible collection, this tx will be rejected152 // Can't sponsor fungible collection, this tx will be rejected120 // as invalid153 // as invalid203 collection_id,236 collection_id,204 item_id,237 item_id,205 ..238 ..206 }239 } => {207 | Call::transfer_from {240 let (sponsor, collection) = load(*collection_id)?;208 collection_id,241 withdraw_transfer::<T>(209 item_id,242 &collection,210 ..243 &T::CrossAccountId::from_sub(who.clone()),244 item_id,245 )246 .map(|()| sponsor)247 }248 Call::transfer_from {249 collection_id,250 item_id,251 from,252 ..211 } => {253 } => {212 let (sponsor, collection) = load(*collection_id)?;254 let (sponsor, collection) = load(*collection_id)?;213 withdraw_transfer::<T>(&collection, who, item_id).map(|()| sponsor)255 withdraw_transfer::<T>(&collection, from, item_id).map(|()| sponsor)214 }256 }215 Call::approve {257 Call::approve {216 collection_id,258 collection_id,227 } => {269 } => {228 let (sponsor, collection) = load(*collection_id)?;270 let (sponsor, collection) = load(*collection_id)?;229 withdraw_set_variable_meta_data::<T>(&who, &collection, item_id, data)271 withdraw_set_variable_meta_data::<T>(272 &T::CrossAccountId::from_sub(who.clone()),273 &collection,274 item_id,275 data,276 )230 .map(|()| sponsor)277 .map(|()| sponsor)231 }278 }