Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MESH-1758 backwards compatible calls #1237

Merged
merged 12 commits into from
May 13, 2022
63 changes: 55 additions & 8 deletions pallets/common/src/traits/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ use frame_support::{
Parameter,
};
use polymesh_primitives::{
secondary_key::SecondaryKey, AuthorizationData, Balance, IdentityClaim, IdentityId,
Permissions, Ticker,
secondary_key::{v1, SecondaryKey},
AuthorizationData, Balance, IdentityClaim, IdentityId, Permissions, Signatory, Ticker,
};
use scale_info::TypeInfo;
use sp_core::H512;
use sp_runtime::traits::{Dispatchable, IdentifyAccount, Member, Verify};
use sp_std::convert::TryFrom;
use sp_std::vec::Vec;

pub type AuthorizationNonce = u64;
Expand Down Expand Up @@ -76,6 +77,32 @@ pub struct SecondaryKeyWithAuth<AccountId> {
pub auth_signature: H512,
}

#[derive(Encode, Decode, TypeInfo)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SecondaryKeyWithAuthV1<AccountId> {
secondary_key: v1::SecondaryKey<AccountId>,
auth_signature: H512,
}

impl<AccountId> TryFrom<SecondaryKeyWithAuthV1<AccountId>> for SecondaryKeyWithAuth<AccountId> {
type Error = ();
fn try_from(auth: SecondaryKeyWithAuthV1<AccountId>) -> Result<Self, Self::Error> {
match auth.secondary_key.signer {
Signatory::Account(key) => Ok(Self {
secondary_key: SecondaryKey {
key,
permissions: auth.secondary_key.permissions,
},
auth_signature: auth.auth_signature,
}),
_ => {
// Unsupported `Signatory::Identity`.
Err(())
}
}
}
}

pub trait WeightInfo {
fn cdd_register_did(i: u32) -> Weight;
fn invalidate_cdd_claims() -> Weight;
Expand Down Expand Up @@ -109,16 +136,36 @@ pub trait WeightInfo {
fn add_investor_uniqueness_claim_v2() -> Weight;
fn revoke_claim_by_index() -> Weight;

/// Add complexity cost of Permissions to `add_secondary_keys_with_authorization` extrinsic.
fn add_secondary_keys_full_v1<AccountId>(
additional_keys: &[SecondaryKeyWithAuthV1<AccountId>],
) -> Weight {
Self::add_secondary_keys_perms_cost(
additional_keys
.iter()
.map(|auth| &auth.secondary_key.permissions),
)
}

/// Add complexity cost of Permissions to `add_secondary_keys_with_authorization` extrinsic.
fn add_secondary_keys_full<AccountId>(
additional_keys: &[SecondaryKeyWithAuth<AccountId>],
) -> Weight {
let perm_cost = additional_keys.iter().fold(0u64, |cost, key| {
cost.saturating_add(Self::permissions_cost_perms(&key.secondary_key.permissions))
});
perm_cost.saturating_add(Self::add_secondary_keys_with_authorization(
additional_keys.len() as u32,
))
Self::add_secondary_keys_perms_cost(
additional_keys
.iter()
.map(|auth| &auth.secondary_key.permissions),
)
}

/// Add complexity cost of Permissions to `add_secondary_keys_with_authorization` extrinsic.
fn add_secondary_keys_perms_cost<'a>(
perms: impl ExactSizeIterator<Item = &'a Permissions>,
) -> Weight {
let len_cost = Self::add_secondary_keys_with_authorization(perms.len() as u32);
perms.fold(len_cost, |cost, key| {
cost.saturating_add(Self::permissions_cost_perms(key))
})
}

/// Add complexity cost of Permissions to `add_authorization` extrinsic.
Expand Down
96 changes: 68 additions & 28 deletions pallets/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ use polymesh_common_utilities::{
protocol_fee::{ChargeProtocolFee, ProtocolOp},
traits::identity::{
AuthorizationNonce, Config, IdentityFnTrait, RawEvent, SecondaryKeyWithAuth,
SecondaryKeyWithAuthV1,
},
SystematicIssuers, GC_DID,
};
Expand Down Expand Up @@ -294,15 +295,15 @@ decl_module! {
Self::base_invalidate_cdd_claims(origin, cdd, disable_from, expiry)?;
}

/// Removes specified secondary keys of a DID if present.
///
/// # Failure
/// It can only called by primary key owner.
///
/// # Weight
/// `950_000_000 + 60_000 * keys_to_remove.len()`
/// Deprecated. Use `remove_secondary_keys` instead.
#[weight = <T as Config>::WeightInfo::remove_secondary_keys(keys_to_remove.len() as u32)]
pub fn remove_secondary_keys(origin, keys_to_remove: Vec<T::AccountId>) {
pub fn remove_secondary_keys_old(origin, keys_to_remove: Vec<Signatory<T::AccountId>>) {
let keys_to_remove = keys_to_remove.into_iter()
.map(|key| match key {
Signatory::Account(key) => Ok(key),
_ => Err(Error::<T>::NotASigner),
})
.collect::<Result<Vec<_>, _>>()?;
Self::base_remove_secondary_keys(origin, keys_to_remove)?;
}

Expand Down Expand Up @@ -381,12 +382,18 @@ decl_module! {
Self::base_revoke_claim(target, claim_type, issuer, scope)
}

/// Sets permissions for an specific `target_key` key.
///
/// Only the primary key of an identity is able to set secondary key permissions.
/// Deprecated. Use `set_secondary_key_permissions` instead.
#[weight = <T as Config>::WeightInfo::set_secondary_key_permissions_full(&perms)]
pub fn set_secondary_key_permissions(origin, key: T::AccountId, perms: Permissions) {
Self::base_set_secondary_key_permissions(origin, key, perms)?;
pub fn set_permission_to_signer(origin, key: Signatory<T::AccountId>, perms: Permissions) {
match key {
Signatory::Account(key) => Self::base_set_secondary_key_permissions(origin, key, perms)?,
_ => Err(Error::<T>::NotASigner)?,
}
}

/// Placeholder for removed `legacy_set_permission_to_signer`.
#[weight = 1_000]
pub fn placeholder_legacy_set_permission_to_signer(_origin) {
}

/// It disables all secondary keys at `did` identity.
Expand Down Expand Up @@ -428,24 +435,17 @@ decl_module! {
Self::base_remove_authorization(origin, target, auth_id)?;
}

/// It adds secondary keys to target identity `id`.
/// Keys are directly added to identity because each of them has an authorization.
///
/// Arguments:
/// - `origin` Primary key of `id` identity.
/// - `id` Identity where new secondary keys will be added.
/// - `additional_keys` New secondary items (and their authorization data) to add to target
/// identity.
///
/// Failure
/// - It can only called by primary key owner.
/// - Keys should be able to linked to any identity.
#[weight = <T as Config>::WeightInfo::add_secondary_keys_full::<T::AccountId>(&additional_keys)]
pub fn add_secondary_keys_with_authorization(
/// Deprecated. Use `add_secondary_keys_with_authorization` instead.
#[weight = <T as Config>::WeightInfo::add_secondary_keys_full_v1::<T::AccountId>(&additional_keys)]
pub fn add_secondary_keys_with_authorization_old(
origin,
additional_keys: Vec<SecondaryKeyWithAuth<T::AccountId>>,
additional_keys: Vec<SecondaryKeyWithAuthV1<T::AccountId>>,
expires_at: T::Moment
) {
let additional_keys = additional_keys.into_iter()
.map(SecondaryKeyWithAuth::<T::AccountId>::try_from)
.collect::<Result<Vec<_>, _>>()
.map_err(|_| Error::<T>::NotASigner)?;
Self::base_add_secondary_keys_with_authorization(origin, additional_keys, expires_at)?;
}

Expand Down Expand Up @@ -528,6 +528,46 @@ decl_module! {
pub fn rotate_primary_key_to_secondary(origin, auth_id:u64, optional_cdd_auth_id: Option<u64>) -> DispatchResult {
Self::base_rotate_primary_key_to_secondary(origin, auth_id, optional_cdd_auth_id)
}

/// Adds secondary keys to target identity `id`.
///
/// Keys are directly added to identity because each of them has an authorization.
///
/// # Arguments:
/// - `origin` which must be the primary key of the identity `id`.
/// - `id` to which new secondary keys will be added.
/// - `additional_keys` which includes secondary keys,
/// coupled with authorization data, to add to target identity.
///
/// # Errors
/// - Can only called by primary key owner.
/// - Keys should be able to linked to any identity.
#[weight = <T as Config>::WeightInfo::add_secondary_keys_full::<T::AccountId>(&additional_keys)]
pub fn add_secondary_keys_with_authorization(
origin,
additional_keys: Vec<SecondaryKeyWithAuth<T::AccountId>>,
expires_at: T::Moment
) {
Self::base_add_secondary_keys_with_authorization(origin, additional_keys, expires_at)?;
}

/// Sets permissions for an specific `target_key` key.
///
/// Only the primary key of an identity is able to set secondary key permissions.
#[weight = <T as Config>::WeightInfo::set_secondary_key_permissions_full(&perms)]
pub fn set_secondary_key_permissions(origin, key: T::AccountId, perms: Permissions) {
Self::base_set_secondary_key_permissions(origin, key, perms)?;
}

/// Removes specified secondary keys of a DID if present.
///
/// # Errors
///
/// The extrinsic can only called by primary key owner.
#[weight = <T as Config>::WeightInfo::remove_secondary_keys(keys_to_remove.len() as u32)]
pub fn remove_secondary_keys(origin, keys_to_remove: Vec<T::AccountId>) {
Self::base_remove_secondary_keys(origin, keys_to_remove)?;
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions polymesh_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@
"Local": "u64"
}
},
"AssetMetadataLockStatus<Moment>": {
"AssetMetadataLockStatus": {
"_enum": {
"Unlocked": "",
"Locked": "",
"LockedUntil": "Moment"
}
},
"AssetMetadataValueDetail<Moment>": {
"AssetMetadataValueDetail": {
"expire": "Option<Moment>",
"lock_status": "AssetMetadataLockStatus<Moment>"
"lock_status": "AssetMetadataLockStatus"
},
"AssetMetadataDescription": "Text",
"AssetMetadataSpec": {
Expand Down
4 changes: 2 additions & 2 deletions primitives/src/secondary_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ pub mod v1 {

/// Old v1 secondary key.
#[derive(Encode, Decode, TypeInfo)]
#[derive(Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct SecondaryKey<AccountId> {
/// Signer.
pub signer: Signatory<AccountId>,
Expand Down
3 changes: 2 additions & 1 deletion scripts/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"start": "npm run build",
"create": "npm run build && npm run test",
"build": "npm run clean:some && tsc -p .",
"build:types": "npm run generate:polymesh-meta && npm run generate:defs && npm run generate:meta",
"build:types": "npm run update:types && npm run generate:polymesh-meta && npm run generate:defs && npm run generate:meta",
"update:types": "node ./util/updateTypes.js",
"generate:defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package polymesh-typegen/interfaces --input ./src/interfaces --endpoint ./polymesh-meta.json",
"generate:meta": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package polymesh-typegen/interfaces --endpoint ./polymesh-meta.json --output ./src/interfaces",
"generate:polymesh-meta": "./chain_metadata.sh",
Expand Down
2 changes: 1 addition & 1 deletion scripts/cli/polymesh-meta.json

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions scripts/cli/src/interfaces/augment-api-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,10 @@ declare module '@polkadot/api-base/types/errors' {
* Multisig is not attached to an identity
**/
MultisigMissingIdentity: AugmentedError<ApiType>;
/**
* Multisig not allowed to add itself as a signer.
**/
MultisigNotAllowedToLinkToItself: AugmentedError<ApiType>;
/**
* A nonce overflow.
**/
Expand Down Expand Up @@ -967,10 +971,6 @@ declare module '@polkadot/api-base/types/errors' {
* Signer is an account key that is already associated with a multisig.
**/
SignerAlreadyLinkedToMultisig: AugmentedError<ApiType>;
/**
* Signer is the multisig. The multisig account can't be a signer to itself.
**/
SignerSameAsMultisig: AugmentedError<ApiType>;
/**
* More signers than required.
**/
Expand Down Expand Up @@ -1777,6 +1777,10 @@ declare module '@polkadot/api-base/types/errors' {
* Proposer's balance is too low.
**/
InsufficientBalance: AugmentedError<ApiType>;
/**
* Invalid identity for disbursement.
**/
InvalidIdentity: AugmentedError<ApiType>;
/**
* Generic error
**/
Expand Down
13 changes: 11 additions & 2 deletions scripts/cli/src/interfaces/augment-api-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1540,11 +1540,20 @@ declare module '@polkadot/api-base/types/events' {
treasury: {
/**
* Disbursement to a target Identity.
* (target identity, amount)
*
* (treasury identity, target identity, target primary key, amount)
**/
TreasuryDisbursement: AugmentedEvent<ApiType, [PolymeshPrimitivesIdentityId, PolymeshPrimitivesIdentityId, u128]>;
TreasuryDisbursement: AugmentedEvent<ApiType, [PolymeshPrimitivesIdentityId, PolymeshPrimitivesIdentityId, AccountId32, u128]>;
/**
* Disbursement to a target Identity failed.
*
* (treasury identity, target identity, target primary key, amount)
**/
TreasuryDisbursementFailed: AugmentedEvent<ApiType, [PolymeshPrimitivesIdentityId, PolymeshPrimitivesIdentityId, AccountId32, u128]>;
/**
* Treasury reimbursement.
*
* (source identity, amount)
**/
TreasuryReimbursement: AugmentedEvent<ApiType, [PolymeshPrimitivesIdentityId, u128]>;
/**
Expand Down
Loading