Skip to content

Commit

Permalink
asset + identity: dedup some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Oct 16, 2020
1 parent d87dae6 commit 9659e27
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
93 changes: 46 additions & 47 deletions pallets/asset/src/lib.rs
Expand Up @@ -86,6 +86,7 @@ pub mod ethereum;

use arrayvec::ArrayVec;
use codec::{Decode, Encode};
use core::mem;
use core::result::Result as StdResult;
use currency::*;
use frame_support::{
Expand Down Expand Up @@ -618,12 +619,9 @@ decl_module! {
/// * `ticker` - the ticker of the token.
#[weight = T::DbWeight::get().reads_writes(4, 1) + 300_000_000]
pub fn freeze(origin, ticker: Ticker) -> DispatchResult {
let sender_did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;

// verify the ownership of the token
ensure!(Self::is_owner(&ticker, sender_did), Error::<T>::Unauthorized);
ensure!(<Tokens<T>>::contains_key(&ticker), Error::<T>::NoSuchAsset);

// Verify the ownership of the token
let sender_did = Self::ensure_perms_owner(origin, &ticker)?;
Self::ensure_asset_exists(&ticker)?;
ensure!(!Self::frozen(&ticker), Error::<T>::AlreadyFrozen);
Frozen::insert(&ticker, true);
Self::deposit_event(RawEvent::AssetFrozen(sender_did, ticker));
Expand All @@ -637,12 +635,9 @@ decl_module! {
/// * `ticker` - the ticker of the frozen token.
#[weight = T::DbWeight::get().reads_writes(4, 1) + 300_000_000]
pub fn unfreeze(origin, ticker: Ticker) -> DispatchResult {
let sender_did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;

// verify the ownership of the token
ensure!(Self::is_owner(&ticker, sender_did), Error::<T>::Unauthorized);
ensure!(<Tokens<T>>::contains_key(&ticker), Error::<T>::NoSuchAsset);

// Verify the ownership of the token
let sender_did = Self::ensure_perms_owner(origin, &ticker)?;
Self::ensure_asset_exists(&ticker)?;
ensure!(Self::frozen(&ticker), Error::<T>::NotFrozen);
Frozen::insert(&ticker, false);
Self::deposit_event(RawEvent::AssetUnfrozen(sender_did, ticker));
Expand All @@ -657,12 +652,9 @@ decl_module! {
/// * `name` - the new name of the token.
#[weight = T::DbWeight::get().reads_writes(2, 1) + 300_000_000]
pub fn rename_asset(origin, ticker: Ticker, name: AssetName) -> DispatchResult {
let sender_did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;

// verify the ownership of the token
ensure!(Self::is_owner(&ticker, sender_did), Error::<T>::Unauthorized);
ensure!(<Tokens<T>>::contains_key(&ticker), Error::<T>::NoSuchAsset);

// Verify the ownership of the token
let sender_did = Self::ensure_perms_owner(origin, &ticker)?;
Self::ensure_asset_exists(&ticker)?;
<Tokens<T>>::mutate(&ticker, |token| token.name = name.clone());
Self::deposit_event(RawEvent::AssetRenamed(sender_did, ticker, name));
Ok(())
Expand All @@ -676,9 +668,7 @@ decl_module! {
/// * `ticker` Ticker of the token.
#[weight = T::DbWeight::get().reads_writes(3, 2) + 400_000_000]
pub fn create_checkpoint(origin, ticker: Ticker) -> DispatchResult {
let did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;

ensure!(Self::is_owner(&ticker, did), Error::<T>::Unauthorized);
let did = Self::ensure_perms_owner(origin, &ticker)?;
let now_as_secs = T::UnixTime::now().as_secs().saturated_into::<u64>();
let _ = Self::_create_checkpoint(&ticker, now_as_secs)?;
Self::deposit_event(RawEvent::CheckpointCreated(
Expand Down Expand Up @@ -708,8 +698,7 @@ decl_module! {
ticker: Ticker,
schedule: CheckpointSchedule
) -> DispatchResult {
let primary_did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;
ensure!(Self::is_owner(&ticker, primary_did), Error::<T>::Unauthorized);
let primary_did = Self::ensure_perms_owner(origin, &ticker)?;
ensure!(
!CheckpointSchedules::contains_key(&ticker),
Error::<T>::CheckpointScheduleAlreadyExists
Expand Down Expand Up @@ -749,8 +738,7 @@ decl_module! {
origin,
ticker: Ticker,
) -> DispatchResult {
let primary_did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;
ensure!(Self::is_owner(&ticker, primary_did), Error::<T>::Unauthorized);
let primary_did = Self::ensure_perms_owner(origin, &ticker)?;
ensure!(
CheckpointSchedules::contains_key(&ticker),
Error::<T>::NoCheckpointSchedule
Expand Down Expand Up @@ -778,12 +766,12 @@ decl_module! {
pub fn issue(origin, ticker: Ticker, value: T::Balance) -> DispatchResult {
let PermissionedCallOriginData {
sender,
primary_did: did,
primary_did,
..
} = Identity::<T>::ensure_origin_call_permissions(origin)?;

// Ensure that the sender is the PIA or the token owner and returns the PIA address.
let beneficiary = Self::ensure_pia_or_owner(&ticker, did)?;
let beneficiary = Self::ensure_pia_or_owner(&ticker, primary_did)?;
Self::_mint(&ticker, sender, beneficiary, value, Some(ProtocolOp::AssetIssue))
}

Expand Down Expand Up @@ -860,9 +848,7 @@ decl_module! {
/// * `ticker` Ticker of the token.
#[weight = T::DbWeight::get().reads_writes(2, 1) + 300_000_000]
pub fn make_divisible(origin, ticker: Ticker) -> DispatchResult {
let did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;

ensure!(Self::is_owner(&ticker, did), Error::<T>::Unauthorized);
let did = Self::ensure_perms_owner(origin, &ticker)?;
// Read the token details
let mut token = Self::token_details(&ticker);
ensure!(!token.divisible, Error::<T>::AssetAlreadyDivisible);
Expand Down Expand Up @@ -952,8 +938,7 @@ decl_module! {
ticker: Ticker,
identifiers: Vec<AssetIdentifier>
) -> DispatchResult {
let did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;
ensure!(Self::is_owner(&ticker, did), Error::<T>::Unauthorized);
let did = Self::ensure_perms_owner(origin, &ticker)?;
let identifiers: Vec<AssetIdentifier> = identifiers
.into_iter()
.filter_map(|identifier| identifier.validate())
Expand All @@ -971,8 +956,7 @@ decl_module! {
/// * `extension_details` - Details of the smart extension.
#[weight = T::DbWeight::get().reads_writes(2, 2) + 600_000_000]
pub fn add_extension(origin, ticker: Ticker, extension_details: SmartExtension<T::AccountId>) -> DispatchResult {
let my_did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;
ensure!(Self::is_owner(&ticker, my_did), Error::<T>::Unauthorized);
let my_did = Self::ensure_perms_owner(origin, &ticker)?;

// Verify the details of smart extension & store it
ensure!(!<ExtensionDetails<T>>::contains_key((ticker, &extension_details.extension_id)), Error::<T>::ExtensionAlreadyPresent);
Expand Down Expand Up @@ -1040,14 +1024,9 @@ decl_module! {
origin,
ticker: Ticker,
) -> DispatchResult {
let did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;
ensure!(Self::is_owner(&ticker, did), Error::<T>::Unauthorized);
let mut old_primary_issuance_agent = None;
<Tokens<T>>::mutate(&ticker, |token| {
old_primary_issuance_agent = token.primary_issuance_agent;
token.primary_issuance_agent = None
});
Self::deposit_event(RawEvent::PrimaryIssuanceAgentTransferred(did, ticker, old_primary_issuance_agent, None));
let did = Self::ensure_perms_owner(origin, &ticker)?;
let old_pia = <Tokens<T>>::mutate(&ticker, |t| mem::replace(&mut t.primary_issuance_agent, None));
Self::deposit_event(RawEvent::PrimaryIssuanceAgentTransferred(did, ticker, old_pia, None));
Ok(())
}

Expand Down Expand Up @@ -1429,6 +1408,28 @@ impl<T: Trait> AssetSubTrait for Module<T> {
/// Public functions can be called from other modules e.g.: lock and unlock (being called from the tcr module)
/// All functions in the impl module section are not part of public interface because they are not part of the Call enum.
impl<T: Trait> Module<T> {
/// Ensure that `origin` is permissioned for this call and that its identity is `ticker`'s owner.
pub fn ensure_perms_owner(
origin: T::Origin,
ticker: &Ticker,
) -> Result<IdentityId, DispatchError> {
let did = Identity::<T>::ensure_perms(origin)?;
Self::ensure_owner(ticker, did)?;
Ok(did)
}

/// Ensure that `did` is the owner of `ticker`.
pub fn ensure_owner(ticker: &Ticker, did: IdentityId) -> DispatchResult {
ensure!(Self::is_owner(ticker, did), Error::<T>::Unauthorized);
Ok(())
}

/// Ensure that `ticker` is a valid created asset.
fn ensure_asset_exists(ticker: &Ticker) -> DispatchResult {
ensure!(<Tokens<T>>::contains_key(&ticker), Error::<T>::NoSuchAsset);
Ok(())
}

// Public immutables
pub fn _is_owner(ticker: &Ticker, did: IdentityId) -> bool {
Self::token_details(ticker).owner_did == did
Expand Down Expand Up @@ -1999,7 +2000,7 @@ impl<T: Trait> Module<T> {
_ => return Err(Error::<T>::NotTickerOwnershipTransferAuth.into()),
};

ensure!(<Tokens<T>>::contains_key(&ticker), Error::<T>::NoSuchAsset);
Self::ensure_asset_exists(&ticker)?;
Self::consume_auth_by_owner(&ticker, to_did, auth_id)?;

let ticker_details = Self::ticker_registration(&ticker);
Expand All @@ -2008,8 +2009,7 @@ impl<T: Trait> Module<T> {
<AssetOwnershipRelations>::insert(to_did, ticker, AssetOwnershipRelation::AssetOwned);

<Tickers<T>>::mutate(&ticker, |tr| tr.owner = to_did);
let owner =
<Tokens<T>>::mutate(&ticker, |tr| core::mem::replace(&mut tr.owner_did, to_did));
let owner = <Tokens<T>>::mutate(&ticker, |tr| mem::replace(&mut tr.owner_did, to_did));

Self::deposit_event(RawEvent::AssetOwnershipTransferred(to_did, ticker, owner));

Expand Down Expand Up @@ -2290,8 +2290,7 @@ impl<T: Trait> Module<T> {
ticker: &Ticker,
id: &T::AccountId,
) -> Result<IdentityId, DispatchError> {
let did = Identity::<T>::ensure_origin_call_permissions(origin)?.primary_did;
ensure!(Self::is_owner(ticker, did), Error::<T>::Unauthorized);
let did = Self::ensure_perms_owner(origin, ticker)?;
ensure!(
<ExtensionDetails<T>>::contains_key((ticker, id)),
Error::<T>::NoSuchSmartExtension
Expand Down
5 changes: 5 additions & 0 deletions pallets/identity/src/lib.rs
Expand Up @@ -2094,6 +2094,11 @@ impl<T: Trait> Module<T> {
Ok(origin_data)
}

/// Ensure `origin` is signed and permissioned for this call, returning its DID.
pub fn ensure_perms(origin: T::Origin) -> Result<IdentityId, DispatchError> {
Self::ensure_origin_call_permissions(origin).map(|x| x.primary_did)
}

/// Ensures that the did is an active CDD Provider.
fn ensure_authorized_cdd_provider(did: IdentityId) -> DispatchResult {
ensure!(
Expand Down

0 comments on commit 9659e27

Please sign in to comment.