1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// This file is part of the Polymesh distribution (https://github.com/PolymathNetwork/Polymesh).
// Copyright (c) 2020 Polymath

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.

// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use crate::{DispatchableName, IdentityId, PalletName, PortfolioId, SubsetRestriction, Ticker};
use codec::{Decode, Encode};
#[cfg(feature = "std")]
use sp_runtime::{Deserialize, Serialize};
use sp_std::{
    cmp::{Ord, Ordering, PartialOrd},
    iter,
};

/// Asset permissions.
pub type AssetPermissions = SubsetRestriction<Ticker>;

/// A permission to call
///
/// - specific functions, using `SubsetRestriction(Some(_))`, or
///
/// - all functions, using `SubsetRestriction(None)`
///
/// within a given pallet `pallet_name`.
#[derive(Decode, Encode, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct PalletPermissions {
    /// The name of a pallet.
    pub pallet_name: PalletName,
    /// A subset of function names within the pallet.
    pub dispatchable_names: SubsetRestriction<DispatchableName>,
}

impl PalletPermissions {
    /// Constructs new pallet permissions from given arguments.
    pub fn new(
        pallet_name: PalletName,
        dispatchable_names: SubsetRestriction<DispatchableName>,
    ) -> Self {
        PalletPermissions {
            pallet_name,
            dispatchable_names,
        }
    }

    /// Constructs new pallet permissions for full access to pallet `pallet_name`.
    pub fn entire_pallet(pallet_name: PalletName) -> Self {
        PalletPermissions {
            pallet_name,
            dispatchable_names: SubsetRestriction(None),
        }
    }
}

/// Extrinsic permissions.
pub type ExtrinsicPermissions = SubsetRestriction<PalletPermissions>;

/// Portfolio permissions.
pub type PortfolioPermissions = SubsetRestriction<PortfolioId>;

/// Signing key permissions.
///
/// Common cases of permissions:
/// - `Permissions::empty()`: no permissions,
/// - `Permissions::default()`: full permissions.
#[derive(Encode, Decode, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Permissions {
    /// The subset of assets under management.
    pub asset: AssetPermissions,
    /// The subset of callable extrinsics.
    pub extrinsic: ExtrinsicPermissions,
    /// The subset of portfolios management.
    pub portfolio: PortfolioPermissions,
}

impl Permissions {
    /// The empty permissions.
    pub fn empty() -> Self {
        Self {
            asset: SubsetRestriction::empty(),
            extrinsic: SubsetRestriction::empty(),
            portfolio: SubsetRestriction::empty(),
        }
    }

    /// Empty permissions apart from given extrinsic permissions.
    pub fn from_pallet_permissions(
        pallet_permissions: impl IntoIterator<Item = PalletPermissions>,
    ) -> Self {
        Self {
            asset: SubsetRestriction::empty(),
            extrinsic: SubsetRestriction(Some(pallet_permissions.into_iter().collect())),
            portfolio: SubsetRestriction::empty(),
        }
    }

    /// Adds extra extrinsic permissions to `self` for just one pallet. The result is stored in
    /// `self`.
    pub fn add_pallet_permissions(&mut self, pallet_permissions: PalletPermissions) {
        self.extrinsic = self.extrinsic.union(&SubsetRestriction(Some(
            iter::once(pallet_permissions).collect(),
        )));
    }
}

/// It supports different elements as a signer.
#[allow(missing_docs)]
#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum Signatory<AccountId> {
    Identity(IdentityId),
    Account(AccountId),
}

impl<AccountId> Default for Signatory<AccountId> {
    fn default() -> Self {
        Signatory::Identity(IdentityId::default())
    }
}

impl<AccountId> From<IdentityId> for Signatory<AccountId> {
    fn from(v: IdentityId) -> Self {
        Signatory::Identity(v)
    }
}

impl<AccountId> PartialEq<IdentityId> for Signatory<AccountId> {
    fn eq(&self, other: &IdentityId) -> bool {
        match self {
            Signatory::Identity(ref id) => id == other,
            _ => false,
        }
    }
}

impl<AccountId> Signatory<AccountId>
where
    AccountId: PartialEq,
{
    /// Checks if Signatory is either a particular Identity or a particular key
    pub fn eq_either(&self, other_identity: &IdentityId, other_key: &AccountId) -> bool {
        match self {
            Signatory::Account(ref key) => key == other_key,
            Signatory::Identity(ref id) => id == other_identity,
        }
    }

    /// This signatory as `IdentityId` or None.
    pub fn as_identity(&self) -> Option<&IdentityId> {
        match self {
            Signatory::Identity(id) => Some(id),
            _ => None,
        }
    }

    /// This signatory as `AccountId` or None.
    pub fn as_account(&self) -> Option<&AccountId> {
        match self {
            Signatory::Account(key) => Some(key),
            _ => None,
        }
    }
}

impl<AccountId> PartialOrd for Signatory<AccountId>
where
    AccountId: Ord,
{
    /// Any key is less than any Identity.
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<AccountId> Ord for Signatory<AccountId>
where
    AccountId: Ord,
{
    fn cmp(&self, other: &Self) -> Ordering {
        match self {
            Signatory::Identity(id) => match other {
                Signatory::Identity(other_id) => id.cmp(other_id),
                Signatory::Account(..) => Ordering::Greater,
            },
            Signatory::Account(key) => match other {
                Signatory::Account(other_key) => key.cmp(other_key),
                Signatory::Identity(..) => Ordering::Less,
            },
        }
    }
}

/// A secondary key is a signatory with defined permissions.
#[derive(Encode, Decode, Default, Clone, Eq, Debug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct SecondaryKey<AccountId: Encode + Decode> {
    /// The account or identity that is the signatory of this key.
    pub signer: Signatory<AccountId>,
    /// The access permissions of the signing key.
    pub permissions: Permissions,
}

impl<AccountId> SecondaryKey<AccountId>
where
    AccountId: Encode + Decode,
{
    /// Creates a [`SecondaryKey`].
    pub fn new(signer: Signatory<AccountId>, permissions: Permissions) -> Self {
        Self {
            signer,
            permissions,
        }
    }

    /// Creates a [`SecondaryKey`] from an `AccountId`.
    pub fn from_account_id(s: AccountId) -> Self {
        Self {
            signer: Signatory::Account(s),
            // Full permissions.
            permissions: Permissions::empty(),
        }
    }

    /// Checks if the given key has permission to access the given asset.
    pub fn has_asset_permission(&self, asset: Ticker) -> bool {
        self.permissions.asset.ge(&SubsetRestriction::elem(asset))
    }

    /// Checks if the given key has permission to call the given extrinsic.
    pub fn has_extrinsic_permission(
        &self,
        pallet_name: &PalletName,
        dispatchable_name: &DispatchableName,
    ) -> bool {
        match &self.permissions.extrinsic.0 {
            None => true,
            Some(pallet_perms) => pallet_perms
                .iter()
                .find(|perm| {
                    if &perm.pallet_name != pallet_name {
                        return false;
                    }
                    match &perm.dispatchable_names.0 {
                        None => true,
                        Some(funcs) => funcs.contains(dispatchable_name),
                    }
                })
                .is_some(),
        }
    }

    /// Checks if the given key has permission to access all given portfolios.
    pub fn has_portfolio_permission(&self, it: impl IntoIterator<Item = PortfolioId>) -> bool {
        self.permissions.portfolio.ge(&SubsetRestriction::elems(it))
    }
}

impl<AccountId> From<IdentityId> for SecondaryKey<AccountId>
where
    AccountId: Encode + Decode,
{
    fn from(id: IdentityId) -> Self {
        Self {
            signer: Signatory::Identity(id),
            permissions: Permissions::empty(),
        }
    }
}

impl<AccountId> PartialEq for SecondaryKey<AccountId>
where
    AccountId: Encode + Decode + PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.signer == other.signer && self.permissions == other.permissions
    }
}

impl<AccountId> PartialEq<IdentityId> for SecondaryKey<AccountId>
where
    AccountId: Encode + Decode,
{
    fn eq(&self, other: &IdentityId) -> bool {
        if let Signatory::Identity(id) = self.signer {
            id == *other
        } else {
            false
        }
    }
}

impl<AccountId> PartialOrd for SecondaryKey<AccountId>
where
    AccountId: Encode + Decode + Ord,
{
    /// Any key is less than any Identity.
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.signer.partial_cmp(&other.signer)
    }
}

impl<AccountId> Ord for SecondaryKey<AccountId>
where
    AccountId: Encode + Decode + Ord,
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.signer.cmp(&other.signer)
    }
}

/// Hacks to workaround substrate and Polkadot.js restrictions/bugs.
pub mod api {
    use super::{
        AssetPermissions, ExtrinsicPermissions, PalletPermissions, Permissions,
        PortfolioPermissions,
    };
    use crate::{DispatchableName, PalletName, Signatory, SubsetRestriction};
    use codec::{Decode, Encode};
    #[cfg(feature = "std")]
    use sp_runtime::{Deserialize, Serialize};
    use sp_std::vec::Vec;

    /// A permission to call functions within a given pallet.
    #[derive(Decode, Encode, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
    #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
    pub struct LegacyPalletPermissions {
        /// The name of a pallet.
        pub pallet_name: PalletName,
        /// A workaround for https://github.com/polkadot-js/apps/issues/3632.
        ///
        /// - `total == false` - only the functions listed in `dispatchable_names` are allowed to be
        /// called.
        ///
        /// - `total == true` - `dispatchable_names` is ignored. Such permissions allow any function in
        /// `pallet_name` to be called.
        pub total: bool,
        /// A subset of function names within the pallet taken into account when `total == false`.
        pub dispatchable_names: Vec<DispatchableName>,
    }

    /// Extrinsic permissions.
    #[derive(Encode, Decode, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
    #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
    pub struct LegacyExtrinsicPermissions(pub Option<Vec<LegacyPalletPermissions>>);

    /// Signing key permissions.
    #[derive(Encode, Decode, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
    #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
    pub struct LegacyPermissions {
        /// The subset of assets under management.
        pub asset: AssetPermissions,
        /// The subset of callable extrinsics.
        pub extrinsic: LegacyExtrinsicPermissions,
        /// The subset of portfolios management.
        pub portfolio: PortfolioPermissions,
    }

    impl LegacyPermissions {
        /// The empty permissions.
        pub fn empty() -> Self {
            Self {
                asset: SubsetRestriction::empty(),
                extrinsic: LegacyExtrinsicPermissions(Some(Vec::new())),
                portfolio: SubsetRestriction::empty(),
            }
        }
    }

    /// The same secondary key object without the extra trait constraints.
    /// It is needed because it's not possible to define `decl_event!`
    /// with the required restrictions on `AccountId`
    #[derive(Encode, Decode, Default, Clone, PartialEq, Eq, Debug)]
    #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
    pub struct SecondaryKey<AccountId> {
        /// The account or identity that is the signatory of this key.
        pub signer: Signatory<AccountId>,
        /// The access permissions of the signing key.
        pub permissions: Permissions,
    }

    impl From<PalletPermissions> for LegacyPalletPermissions {
        fn from(p: PalletPermissions) -> LegacyPalletPermissions {
            LegacyPalletPermissions {
                pallet_name: p.pallet_name,
                total: p.dispatchable_names.0.is_none(),
                dispatchable_names: if let Some(elems) = p.dispatchable_names.0 {
                    elems.into_iter().collect()
                } else {
                    Default::default()
                },
            }
        }
    }

    impl From<ExtrinsicPermissions> for LegacyExtrinsicPermissions {
        fn from(p: ExtrinsicPermissions) -> LegacyExtrinsicPermissions {
            LegacyExtrinsicPermissions(
                p.0.map(|elems| elems.into_iter().map(|e| e.into()).collect()),
            )
        }
    }

    impl From<Permissions> for LegacyPermissions {
        fn from(p: Permissions) -> LegacyPermissions {
            LegacyPermissions {
                asset: p.asset,
                extrinsic: p.extrinsic.into(),
                portfolio: p.portfolio,
            }
        }
    }

    impl<AccountId> From<super::SecondaryKey<AccountId>> for SecondaryKey<AccountId>
    where
        AccountId: Encode + Decode,
    {
        fn from(k: super::SecondaryKey<AccountId>) -> SecondaryKey<AccountId> {
            SecondaryKey {
                signer: k.signer,
                permissions: k.permissions,
            }
        }
    }

    impl From<LegacyPalletPermissions> for PalletPermissions {
        fn from(p: LegacyPalletPermissions) -> PalletPermissions {
            PalletPermissions {
                pallet_name: p.pallet_name,
                dispatchable_names: SubsetRestriction(if !p.total {
                    Some(p.dispatchable_names.into_iter().collect())
                } else {
                    Default::default()
                }),
            }
        }
    }

    impl From<LegacyExtrinsicPermissions> for ExtrinsicPermissions {
        fn from(p: LegacyExtrinsicPermissions) -> ExtrinsicPermissions {
            SubsetRestriction(p.0.map(|elems| elems.into_iter().map(|e| e.into()).collect()))
        }
    }

    impl From<LegacyPermissions> for Permissions {
        fn from(p: LegacyPermissions) -> Permissions {
            Permissions {
                asset: p.asset.into(),
                extrinsic: p.extrinsic.into(),
                portfolio: p.portfolio.into(),
            }
        }
    }

    impl<AccountId> From<SecondaryKey<AccountId>> for super::SecondaryKey<AccountId>
    where
        AccountId: Encode + Decode,
    {
        fn from(k: SecondaryKey<AccountId>) -> super::SecondaryKey<AccountId> {
            super::SecondaryKey {
                signer: k.signer,
                permissions: k.permissions,
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Permissions, PortfolioId, SecondaryKey, Signatory, SubsetRestriction};
    use crate::{IdentityId, Ticker};
    use sp_core::sr25519::Public;
    use std::convert::{From, TryFrom};

    #[test]
    fn build_test() {
        let key = Public::from_raw([b'A'; 32]);
        let rk1 = SecondaryKey::new(Signatory::Account(key.clone()), Permissions::empty());
        let rk2 = SecondaryKey::from_account_id(key.clone());
        assert_eq!(rk1, rk2);

        let rk3_permissions = Permissions {
            asset: SubsetRestriction::elem(Ticker::try_from(&[1][..]).unwrap()),
            extrinsic: SubsetRestriction(None),
            portfolio: SubsetRestriction::elem(PortfolioId::default_portfolio(IdentityId::from(
                1u128,
            ))),
        };
        let rk3 = SecondaryKey::new(Signatory::Account(key.clone()), rk3_permissions.clone());
        assert_ne!(rk1, rk3);

        let mut rk4 = SecondaryKey::from_account_id(key);
        rk4.permissions = rk3_permissions;
        assert_eq!(rk3, rk4);

        let si1 = SecondaryKey::from(IdentityId::from(1u128));
        let si2 = SecondaryKey::from(IdentityId::from(1u128));
        assert_eq!(si1, si2);

        let si3 = SecondaryKey::from(IdentityId::from(2u128));
        assert_ne!(si1, si3);

        assert_ne!(si1, rk1);
    }

    #[test]
    fn has_permission_test() {
        let key = Public::from_raw([b'A'; 32]);
        let ticker1 = Ticker::try_from(&[1][..]).unwrap();
        let ticker2 = Ticker::try_from(&[2][..]).unwrap();
        let portfolio1 = PortfolioId::user_portfolio(IdentityId::default(), 1.into());
        let portfolio2 = PortfolioId::user_portfolio(IdentityId::default(), 2.into());
        let permissions = Permissions {
            asset: SubsetRestriction::elem(ticker1),
            extrinsic: SubsetRestriction(None),
            portfolio: SubsetRestriction::elem(portfolio1),
        };
        let free_key = SecondaryKey::new(Signatory::Account(key.clone()), Permissions::default());
        let restricted_key = SecondaryKey::new(Signatory::Account(key), permissions.clone());
        assert!(free_key.has_asset_permission(ticker2));
        assert!(free_key
            .has_extrinsic_permission(&b"pallet".as_ref().into(), &b"function".as_ref().into()));
        assert!(free_key.has_portfolio_permission(vec![portfolio1]));
        assert!(restricted_key.has_asset_permission(ticker1));
        assert!(!restricted_key.has_asset_permission(ticker2));
        assert!(restricted_key
            .has_extrinsic_permission(&b"pallet".as_ref().into(), &b"function".as_ref().into()));
        assert!(restricted_key.has_portfolio_permission(vec![portfolio1]));
        assert!(!restricted_key.has_portfolio_permission(vec![portfolio2]));
    }

    #[test]
    fn signer_build_and_eq_tests() {
        let key = Public::from_raw([b'A'; 32]);
        let iden = IdentityId::try_from(
            "did:poly:f1d273950ddaf693db228084d63ef18282e00f91997ae9df4f173f09e86d0976",
        )
        .unwrap();
        let iden_sig: Signatory<sp_core::sr25519::Public> = Signatory::from(iden);
        assert_ne!(Signatory::Account(key), iden_sig);
        assert_eq!(iden_sig, iden);
        assert_ne!(iden_sig, Signatory::Account(key));
    }
}