// This file is part of Bizinikiwi. // Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute // SPDX-License-Identifier: MIT-0 // Permission is hereby granted, free of charge, to any person obtaining a copy of // this software and associated documentation files (the "Software"), to deal in // the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. //! # Authorization Transaction Extension Example Pezpallet //! //! **This pezpallet serves as an example and is not meant to be used in production.** //! //! FRAME Transaction Extension reference implementation, origin mutation, origin authorization and //! integration in a `TransactionExtension` pipeline. //! //! The [TransactionExtension](pezsp_runtime::traits::TransactionExtension) used in this example is //! [AuthorizeCoownership](extensions::AuthorizeCoownership). If activated, the extension will //! authorize 2 signers as coowners, with a [coowner origin](pezpallet_coownership::Origin) specific //! to the [coownership example pezpallet](pezpallet_coownership), by validating a signature of the //! rest of the transaction from each party. This means any extensions after ours in the pipeline, //! their implicits and the actual call. The extension pipeline used in our example checks the //! genesis hash, transaction version and mortality of the transaction after the //! `AuthorizeCoownership` runs as we want these transactions to run regardless of what origin //! passes through them and/or we want their implicit data in any signature authorization happening //! earlier in the pipeline. //! //! In this example, aside from the [AuthorizeCoownership](extensions::AuthorizeCoownership) //! extension, we use the following pallets: //! - [pezpallet_coownership] - provides a coowner origin and the functionality to authorize it. //! - [pezpallet_assets] - a dummy asset pezpallet that tracks assets, identified by an //! [AssetId](pezpallet_assets::AssetId), and their respective owners, which can be either an //! [account](pezpallet_assets::Owner::Single) or a [pair of //! owners](pezpallet_assets::Owner::Double). //! //! Assets are created in [pezpallet_assets] using the //! [create_asset](pezpallet_assets::Call::create_asset) call, which accepts traditionally signed //! origins (a single account) or coowner origins, authorized through the //! [CoownerOrigin](pezpallet_assets::Config::CoownerOrigin) type. //! //! ### Example runtime setup #![doc = docify::embed!("src/mock.rs", example_runtime)] //! //! ### Example usage #![doc = docify::embed!("src/tests.rs", create_coowned_asset_works)] //! //! This example does not focus on any pezpallet logic or syntax, but rather on //! `TransactionExtension` functionality. The pallets used are just skeletons to provide storage //! state and custom origin choices and requirements, as shown in the examples. Any weight and/or //! transaction fee is out of scope for this example. #![cfg_attr(not(feature = "std"), no_std)] pub mod extensions; #[cfg(test)] mod mock; #[cfg(test)] mod tests; extern crate alloc; use pezframe_support::pezpallet_prelude::*; use pezframe_system::pezpallet_prelude::*; #[pezframe_support::pezpallet(dev_mode)] pub mod pezpallet_coownership { use super::*; use pezframe_support::traits::OriginTrait; #[pezpallet::config] pub trait Config: pezframe_system::Config { /// The aggregated origin which the dispatch will take. type RuntimeOrigin: OriginTrait + From + IsType<::RuntimeOrigin>; /// The caller origin, overarching type of all pallets origins. type PalletsOrigin: From> + TryInto, Error = Self::PalletsOrigin>; } #[pezpallet::pezpallet] pub struct Pezpallet(_); /// Origin that this pezpallet can authorize. For the purposes of this example, it's just two /// accounts that own something together. #[pezpallet::origin] #[derive( Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, DecodeWithMemTracking, MaxEncodedLen, TypeInfo, )] pub enum Origin { Coowners(T::AccountId, T::AccountId), } } #[pezframe_support::pezpallet(dev_mode)] pub mod pezpallet_assets { use super::*; pub type AssetId = u32; /// Type that describes possible owners of a particular asset. #[derive(Clone, PartialEq, Eq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)] pub enum Owner { Single(AccountId), Double(AccountId, AccountId), } #[pezpallet::config] pub trait Config: pezframe_system::Config { /// Type that can authorize an account pair coowner origin. type CoownerOrigin: EnsureOrigin< Self::RuntimeOrigin, Success = (Self::AccountId, Self::AccountId), >; } /// Map that holds the owner information for each asset it manages. #[pezpallet::storage] pub type AssetOwners = StorageMap<_, Blake2_128Concat, AssetId, Owner<::AccountId>>; #[pezpallet::pezpallet] pub struct Pezpallet(_); #[pezpallet::error] pub enum Error { /// Asset already exists. AlreadyExists, } #[pezpallet::call] impl Pezpallet { /// Simple call that just creates an asset with a specific `AssetId`. This call will fail if /// there is already an asset with the same `AssetId`. /// /// The origin is either a single account (traditionally signed origin) or a coowner origin. #[pezpallet::call_index(0)] pub fn create_asset(origin: OriginFor, asset_id: AssetId) -> DispatchResult { let owner: Owner = match T::CoownerOrigin::try_origin(origin) { Ok((first, second)) => Owner::Double(first, second), Err(origin) => ensure_signed(origin).map(|account| Owner::Single(account))?, }; AssetOwners::::try_mutate(asset_id, |maybe_owner| { if maybe_owner.is_some() { return Err(Error::::AlreadyExists); } *maybe_owner = Some(owner); Ok(()) })?; Ok(()) } } }