pallet_nfts/
common_functions.rs1use crate::*;
21use alloc::vec::Vec;
22use frame_support::pallet_prelude::*;
23
24impl<T: Config<I>, I: 'static> Pallet<T, I> {
25 pub fn owner(collection: T::CollectionId, item: T::ItemId) -> Option<T::AccountId> {
27 Item::<T, I>::get(collection, item).map(|i| i.owner)
28 }
29
30 pub fn collection_owner(collection: T::CollectionId) -> Option<T::AccountId> {
32 Collection::<T, I>::get(collection).map(|i| i.owner)
33 }
34
35 pub fn validate_signature(
42 data: &Vec<u8>,
43 signature: &T::OffchainSignature,
44 signer: &T::AccountId,
45 ) -> DispatchResult {
46 if signature.verify(&**data, &signer) {
47 return Ok(())
48 }
49
50 let prefix = b"<Bytes>";
53 let suffix = b"</Bytes>";
54 let mut wrapped: Vec<u8> = Vec::with_capacity(data.len() + prefix.len() + suffix.len());
55 wrapped.extend(prefix);
56 wrapped.extend(data);
57 wrapped.extend(suffix);
58
59 ensure!(signature.verify(&*wrapped, &signer), Error::<T, I>::WrongSignature);
60
61 Ok(())
62 }
63
64 pub(crate) fn set_next_collection_id(collection: T::CollectionId) {
65 let next_id = collection.increment();
66 NextCollectionId::<T, I>::set(next_id);
67 Self::deposit_event(Event::NextCollectionIdIncremented { next_id });
68 }
69
70 #[cfg(any(test, feature = "runtime-benchmarks"))]
71 pub fn set_next_id(id: T::CollectionId) {
72 NextCollectionId::<T, I>::set(Some(id));
73 }
74
75 #[cfg(test)]
76 pub fn get_next_id() -> T::CollectionId {
77 NextCollectionId::<T, I>::get()
78 .or(T::CollectionId::initial_value())
79 .expect("Failed to get next collection ID")
80 }
81}