Commit df49312f authored by Éloïs's avatar Éloïs
Browse files

feat(identity): add IdtyIndex

parent e1b772a4
......@@ -30,10 +30,13 @@ mod benchmarking;*/
pub use pallet::*;
use crate::traits::*;
use codec::Codec;
use frame_support::dispatch::Weight;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::traits::{AtLeast32BitUnsigned, One, Saturating, Zero};
use sp_std::collections::btree_map::BTreeMap;
use sp_std::fmt::Debug;
use sp_std::prelude::*;
#[frame_support::pallet]
......@@ -60,6 +63,16 @@ pub mod pallet {
type IdtyData: IdtyData;
/// Identity decentralized identifier
type IdtyDid: IdtyDid;
/// A short identity index.
type IdtyIndex: Parameter
+ Member
+ AtLeast32BitUnsigned
+ Codec
+ Default
+ Copy
+ MaybeSerializeDeserialize
+ Debug
+ MaxEncodedLen;
/// Origin allowed to validate identity
type IdtyValidationOrigin: EnsureOrigin<Self::Origin>;
/// Rights that an identity can have
......@@ -115,6 +128,7 @@ pub mod pallet {
#[cfg_attr(feature = "std", derive(Deserialize, Serialize))]
#[derive(Encode, Decode, Clone, PartialEq, Eq)]
pub struct IdtyValue<T: Config> {
pub index: T::IdtyIndex,
pub owner_key: T::AccountId,
pub removable_on: Option<T::BlockNumber>,
pub rights: Vec<(T::IdtyRight, Option<T::AccountId>)>,
......@@ -124,6 +138,7 @@ pub mod pallet {
impl<T: Config> IdtyValue<T> {
pub fn new_valid(owner_key: T::AccountId, rights: Vec<T::IdtyRight>) -> Self {
Self {
index: Default::default(),
owner_key,
removable_on: None,
rights: rights.into_iter().map(|right| (right, None)).collect(),
......@@ -135,6 +150,7 @@ pub mod pallet {
impl<T: Config> Default for IdtyValue<T> {
fn default() -> Self {
Self {
index: Default::default(),
owner_key: Default::default(),
removable_on: None,
rights: Default::default(),
......@@ -172,6 +188,15 @@ pub mod pallet {
pub type Identities<T: Config> =
StorageMap<_, Blake2_128Concat, T::IdtyDid, IdtyValue<T>, ValueQuery>;
/// IdentitiesByIndex
#[pallet::storage]
#[pallet::getter(fn identity_did_of_index)]
pub type IdentitiesByIndex<T: Config> =
StorageMap<_, Blake2_128Concat, T::IdtyIndex, T::IdtyDid, ValueQuery>;
#[pallet::storage]
pub(super) type NextIdtyIndex<T: Config> = StorageValue<_, T::IdtyIndex, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn identities_count)]
pub(super) type IdentitiesCount<T: Config> = StorageValue<_, u64, ValueQuery>;
......@@ -298,14 +323,17 @@ pub mod pallet {
let block_number = frame_system::pallet::Pallet::<T>::block_number();
let removable_on = block_number + T::ConfirmPeriod::get();
let idty_index = Self::get_next_idty_index();
<Identities<T>>::insert(
idty_did,
IdtyValue {
index: idty_index,
owner_key: owner_key.clone(),
removable_on: Some(removable_on),
..Default::default()
},
);
<IdentitiesByIndex<T>>::insert(idty_index, idty_did);
IdentitiesRemovableOn::<T>::append(removable_on, (idty_did, IdtyStatus::Created));
Self::inc_identities_counter();
Self::deposit_event(Event::IdtyCreated(idty_did, owner_key));
......@@ -591,6 +619,15 @@ pub mod pallet {
} else {
panic!("storage corrupted")
}
} //NextIdtyIndex
fn get_next_idty_index() -> T::IdtyIndex {
if let Ok(next_index) = <NextIdtyIndex<T>>::try_get() {
<NextIdtyIndex<T>>::put(next_index.saturating_add(T::IdtyIndex::one()));
next_index
} else {
<NextIdtyIndex<T>>::put(T::IdtyIndex::one());
T::IdtyIndex::zero()
}
}
fn inc_identities_counter() {
if let Ok(counter) = <IdentitiesCount<T>>::try_get() {
......
......@@ -113,6 +113,7 @@ impl pallet_identity::Config for Test {
type EnsureIdtyCallAllowed = ();
type IdtyData = ();
type IdtyDid = IdtyDid;
type IdtyIndex = u64;
type IdtyValidationOrigin = system::EnsureRoot<AccountId>;
type IdtyRight = IdtyRight;
type OnIdtyConfirmed = ();
......
......@@ -37,19 +37,23 @@ fn test_two_identities() {
identities.insert(
Did(1),
crate::IdtyValue {
index: 0,
owner_key: 1,
removable_on: None,
rights: vec![(Right::Right2, Some(10))],
status: crate::IdtyStatus::Validated,
data: (),
},
);
identities.insert(
Did(2),
crate::IdtyValue {
index: 1,
owner_key: 2,
removable_on: None,
rights: vec![(Right::Right1, Some(20))],
status: crate::IdtyStatus::Validated,
data: (),
},
);
new_test_ext(IdentityConfig { identities }).execute_with(|| {
......
[package]
authors = ['librelois <c@elo.tf>']
description = 'FRAME pallet strong certifications.'
edition = '2018'
homepage = 'https://substrate.dev'
license = 'AGPL-3.0'
name = 'pallet-strong-certs'
readme = 'README.md'
repository = 'https://git.duniter.org/nodes/rust/lc-core-substrate'
version = '3.0.0'
[features]
default = ['std']
runtime-benchmarks = ['frame-benchmarking']
std = [
'codec/std',
'frame-support/std',
'frame-system/std',
'frame-benchmarking/std',
'serde',
'sp-core/std',
'sp-runtime/std',
'sp-std/std',
]
try-runtime = ['frame-support/try-runtime']
[dependencies.codec]
default-features = false
features = ['derive']
package = 'parity-scale-codec'
version = '2.1.0'
[dependencies.frame-benchmarking]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
optional = true
tag = 'monthly-2021-07'
[dependencies.frame-support]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
tag = 'monthly-2021-07'
[dependencies.frame-system]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
tag = 'monthly-2021-07'
[dependencies.serde]
version = "1.0.101"
optional = true
features = ["derive"]
[dependencies.sp-core]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
tag = 'monthly-2021-07'
[dependencies.sp-runtime]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
tag = 'monthly-2021-07'
[dependencies.sp-std]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
tag = 'monthly-2021-07'
### DOC ###
[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']
[dev-dependencies.serde]
version = '1.0.119'
### DEV ###
[dev-dependencies.sp-io]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
tag = 'monthly-2021-07'
......@@ -41,7 +41,7 @@ frame_support::construct_runtime!(
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
UniversalDividend: pallet_universal_dividend::{Pallet, Call, Storage, Config<T>, Event<T>},
UniversalDividend: pallet_universal_dividend::{Pallet, Storage, Config<T>, Event<T>},
}
);
......
......@@ -82,6 +82,9 @@ pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::Account
/// Balance of an account.
pub type Balance = u64;
/// Index of an identity
pub type IdtyIndex = u64;
/// Index of a transaction in the chain.
pub type Index = u32;
......@@ -331,6 +334,7 @@ impl pallet_identity::Config for Runtime {
type EnsureIdtyCallAllowed = crate::authorizations::EnsureIdtyCallAllowedImpl;
type IdtyData = ();
type IdtyDid = IdtyDid;
type IdtyIndex = IdtyIndex;
type IdtyValidationOrigin = EnsureRoot<Self::AccountId>;
type IdtyRight = IdtyRight;
type OnIdtyConfirmed = ();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment