Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ gcloud-md5-ingress.yml
collator-data
relay-data
.vscode/settings.json
lcov.info
2 changes: 2 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"args": [
"run",
"--release",
"--bin",
"hashed",
"--",
"--dev"
],
Expand Down
1 change: 1 addition & 0 deletions pallets/fruniques/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ std = [
"frame-system/std",
"frame-benchmarking/std",
"sp-runtime/std",
"pallet-rbac/std",
]

runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
Expand Down
107 changes: 102 additions & 5 deletions pallets/fruniques/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::*;

use crate::types::*;
use frame_system::pallet_prelude::*;

use frame_support::sp_io::hashing::blake2_256;
use frame_support::traits::tokens::nonfungibles::Inspect;
use frame_system::pallet_prelude::*;
use scale_info::prelude::string::String;

use pallet_rbac::types::*;
Expand Down Expand Up @@ -82,7 +82,63 @@ impl<T: Config> Pallet<T> {
}

pub fn do_initial_setup() -> DispatchResult {
let _pallet_id = Self::pallet_id();

let pallet: IdOrVec = Self::pallet_id();

let owner_role_ids = T::Rbac::create_and_set_roles(
pallet.clone(),
FruniqueRole::get_owner_roles())?;

for owner_role in owner_role_ids {
T::Rbac::create_and_set_permissions(
pallet.clone(),
owner_role,
Permission::owner_permissions())?;
}

let admin_role_ids = T::Rbac::create_and_set_roles(
pallet.clone(),
FruniqueRole::get_admin_roles())?;

for admin_role in admin_role_ids {
T::Rbac::create_and_set_permissions(
pallet.clone(),
admin_role,
Permission::admin_permissions())?;
}

let collaborator_role_ids = T::Rbac::create_and_set_roles(
pallet.clone(),
FruniqueRole::get_collaborator_roles())?;

for collaborator_role in collaborator_role_ids {
T::Rbac::create_and_set_permissions(
pallet.clone(),
collaborator_role,
Permission::collaborator_permissions())?;
}

let collector_role_ids = T::Rbac::create_and_set_roles(
pallet.clone(),
FruniqueRole::get_collector_roles())?;

for collector_role in collector_role_ids {
T::Rbac::create_and_set_permissions(
pallet.clone(),
collector_role,
Permission::collector_permissions())?;
}

let holder_role_ids = T::Rbac::create_and_set_roles(
pallet.clone(),
FruniqueRole::get_holder_roles())?;

for holder_role in holder_role_ids {
T::Rbac::create_and_set_permissions(
pallet.clone(),
holder_role,
Permission::holder_permissions())?;
}

Ok(())
}
Expand Down Expand Up @@ -150,6 +206,11 @@ impl<T: Config> Pallet<T> {
) -> DispatchResult {
let owner = T::CreateOrigin::ensure_origin(origin.clone(), &class_id)?;

let scope_id = class_id.using_encoded(blake2_256);
T::Rbac::create_scope(Self::pallet_id(), scope_id)?;

Self::insert_auth_in_frunique_collection(owner.clone(), class_id, FruniqueRole::Owner)?;

pallet_uniques::Pallet::<T>::do_create_collection(
class_id,
owner.clone(),
Expand Down Expand Up @@ -197,12 +258,18 @@ impl<T: Config> Pallet<T> {
origin: OriginFor<T>,
collection: T::CollectionId,
item: T::ItemId,
owner: <T::Lookup as sp_runtime::traits::StaticLookup>::Source,
owner: T::AccountId,
metadata: CollectionDescription<T>,
attributes: Option<Attributes<T>>,
) -> DispatchResult {
ensure!(Self::collection_exists(&collection), <Error<T>>::CollectionNotFound);
pallet_uniques::Pallet::<T>::mint(origin.clone(), collection, item, owner)?;
let user: T::AccountId = ensure_signed(origin.clone())?;
Self::is_authorized(user, collection, Permission::Mint)?;

// pallet_uniques::Pallet::<T>::do_mint(collection, item, owner, |_| Ok(()))?;
pallet_uniques::Pallet::<T>::do_mint(collection, item, owner, |_| {
Ok(())
})?;

pallet_uniques::Pallet::<T>::set_metadata(
origin.clone(),
Expand All @@ -227,7 +294,37 @@ impl<T: Config> Pallet<T> {
Ok(())
}

/// Helper functions to interact with the RBAC module
pub fn pallet_id() -> IdOrVec {
IdOrVec::Vec(Self::module_name().as_bytes().to_vec())
}

pub fn insert_auth_in_frunique_collection(
user: T::AccountId,
class_id: T::CollectionId,
role: FruniqueRole,
) -> DispatchResult {
T::Rbac::assign_role_to_user(
user,
Self::pallet_id(),
&class_id.using_encoded(blake2_256),
role.id(),
)?;

Ok(())
}

fn is_authorized(
user: T::AccountId,
collection_id: T::CollectionId,
permission: Permission,
) -> DispatchResult {
let scope_id = collection_id.using_encoded(blake2_256);
<T as pallet::Config>::Rbac::is_authorized(
user,
Self::pallet_id(),
&scope_id,
&permission.id(),
)
}
}
37 changes: 33 additions & 4 deletions pallets/fruniques/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub mod pallet {
use crate::types::*;
use frame_support::{pallet_prelude::*, transactional};
use frame_system::pallet_prelude::*;

use pallet_rbac::types::RoleBasedAccessControl;
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config + pallet_uniques::Config {
Expand All @@ -30,6 +32,8 @@ pub mod pallet {
/// Maximum number of children a Frunique can have
#[pallet::constant]
type ChildMaxLen: Get<u32>;

type Rbac : RoleBasedAccessControl<Self::AccountId>;
}

#[pallet::pallet]
Expand All @@ -47,6 +51,8 @@ pub mod pallet {
FruniqueDivided(T::AccountId, T::AccountId, T::CollectionId, T::ItemId),
// A frunique has been verified.
FruniqueVerified(T::AccountId, CollectionId, ItemId),
// A user has been invited to collaborate on a collection.
InvitedToCollaborate(T::AccountId, T::AccountId, T::CollectionId),
// Counter should work?
NextFrunique(u32),
}
Expand Down Expand Up @@ -158,7 +164,7 @@ pub mod pallet {
#[pallet::weight(10_000 + T::DbWeight::get().writes(10))]
pub fn initial_setup(origin: OriginFor<T>) -> DispatchResult {
T::RemoveOrigin::ensure_origin(origin.clone())?;
// Self::do_initial_setup()?;
Self::do_initial_setup()?;
Ok(())
}

Expand Down Expand Up @@ -263,7 +269,6 @@ pub mod pallet {
}

let owner: T::AccountId = ensure_signed(origin.clone())?;
let account_id = Self::account_id_to_lookup_source(&owner);

let instance_id: ItemId = <NextFrunique<T>>::try_get(class_id).unwrap_or(0);
<NextFrunique<T>>::insert(class_id, instance_id + 1);
Expand All @@ -282,7 +287,7 @@ pub mod pallet {
<FruniqueChild<T>>::insert(class_id, instance_id, Some(child_info));
}

Self::do_spawn(origin, class_id, instance_id, account_id, metadata, attributes)?;
Self::do_spawn(origin, class_id, instance_id, owner.clone(), metadata, attributes)?;

Self::deposit_event(Event::FruniqueCreated(
owner.clone(),
Expand Down Expand Up @@ -317,6 +322,30 @@ pub mod pallet {
Ok(())
}

/// ## Invite a user to become a collaborator in a collection.
/// ### Parameters:
/// - `origin` must be signed by the owner of the frunique.
/// - `class_id` must be a valid class of the asset class.
/// - `invitee` must be a valid user.
/// ### Considerations:
/// This functions enables the owner of a collection to invite a user to become a collaborator.
/// The user will be able to create NFTs in the collection.
/// The user will be able to add attributes to the NFTs in the collection.

#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
pub fn invite(
origin: OriginFor<T>,
class_id: CollectionId,
invitee: T::AccountId,
) -> DispatchResult {

let owner: T::AccountId = ensure_signed(origin.clone())?;
Self::insert_auth_in_frunique_collection(invitee.clone(), class_id, FruniqueRole::Collaborator)?;

Self::deposit_event(Event::InvitedToCollaborate(owner, invitee, class_id));
Ok(())
}

/// ## Force set counter
/// ### Parameters:
/// `origin` must be signed by the Root origin.
Expand Down Expand Up @@ -391,7 +420,7 @@ pub mod pallet {
let _ = <NextFrunique<T>>::clear(1000, None);
let _ = <FruniqueParent<T>>::clear(1000, None);
let _ = <FruniqueChild<T>>::clear(1000, None);

T::Rbac::remove_pallet_storage(Self::pallet_id())?;
Ok(())
}
}
Expand Down
34 changes: 34 additions & 0 deletions pallets/fruniques/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ construct_runtime!(
Uniques: pallet_uniques::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Fruniques: pallet_fruniques::{Pallet, Call, Storage, Event<T>},
RBAC: pallet_rbac::{Pallet, Call, Storage, Event<T>},
}
);
parameter_types! {
Expand Down Expand Up @@ -60,6 +61,7 @@ impl pallet_fruniques::Config for Test {
type Event = Event;
type RemoveOrigin = EnsureRoot<Self::AccountId>;
type ChildMaxLen = ChildMaxLen;
type Rbac = RBAC;
}

parameter_types! {
Expand Down Expand Up @@ -112,7 +114,39 @@ impl pallet_balances::Config for Test {
type ReserveIdentifier = [u8; 8];
}

parameter_types! {
pub const MaxScopesPerPallet: u32 = 2;
pub const MaxRolesPerPallet: u32 = 6;
pub const RoleMaxLen: u32 = 25;
pub const PermissionMaxLen: u32 = 25;
pub const MaxPermissionsPerRole: u32 = 11;
pub const MaxRolesPerUser: u32 = 2;
pub const MaxUsersPerRole: u32 = 2;
}
impl pallet_rbac::Config for Test {
type Event = Event;
type MaxScopesPerPallet = MaxScopesPerPallet;
type MaxRolesPerPallet = MaxRolesPerPallet;
type RoleMaxLen = RoleMaxLen;
type PermissionMaxLen = PermissionMaxLen;
type MaxPermissionsPerRole = MaxPermissionsPerRole;
type MaxRolesPerUser = MaxRolesPerUser;
type MaxUsersPerRole = MaxUsersPerRole;
}
// Build genesis storage according to the mock runtime.
// pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
// frame_system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
// }
// Build genesis storage according to the mock runtime.


pub fn new_test_ext() -> sp_io::TestExternalities {
let balance_amount = 1_000_000 as u64;
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, balance_amount), (2, balance_amount), (3, balance_amount)],
}.assimilate_storage(&mut t).expect("assimilate_storage failed");
let mut t: sp_io::TestExternalities = t.into();
t.execute_with(|| Fruniques::do_initial_setup().expect("Error on configuring initial setup"));
t
}
Loading