diff --git a/bin/node/cli/src/chain_spec.rs b/bin/node/cli/src/chain_spec.rs index 57d367e7c49a8..50f77345bc945 100644 --- a/bin/node/cli/src/chain_spec.rs +++ b/bin/node/cli/src/chain_spec.rs @@ -21,9 +21,9 @@ use grandpa_primitives::AuthorityId as GrandpaId; use kitchensink_runtime::{ constants::currency::*, wasm_binary_unwrap, BabeConfig, BalancesConfig, Block, CouncilConfig, - DemocracyConfig, ElectionsConfig, ImOnlineConfig, IndicesConfig, MaxNominations, - NominationPoolsConfig, SessionConfig, SessionKeys, SocietyConfig, StakerStatus, StakingConfig, - SudoConfig, SystemConfig, TechnicalCommitteeConfig, + DemocracyConfig, ElectionsConfig, IdentityConfig, ImOnlineConfig, IndicesConfig, + MaxNominations, NominationPoolsConfig, SessionConfig, SessionKeys, SocietyConfig, StakerStatus, + StakingConfig, SudoConfig, SystemConfig, TechnicalCommitteeConfig, }; use pallet_im_online::sr25519::AuthorityId as ImOnlineId; use sc_chain_spec::ChainSpecExtension; @@ -298,6 +298,7 @@ pub fn testnet_genesis( balances: BalancesConfig { balances: endowed_accounts.iter().cloned().map(|x| (x, ENDOWMENT)).collect(), }, + identity: IdentityConfig { registrars: vec![] }, indices: IndicesConfig { indices: vec![] }, session: SessionConfig { keys: initial_authorities diff --git a/bin/node/testing/src/genesis.rs b/bin/node/testing/src/genesis.rs index 8063b8ef45708..d631f6b7f1bb1 100644 --- a/bin/node/testing/src/genesis.rs +++ b/bin/node/testing/src/genesis.rs @@ -21,12 +21,12 @@ use crate::keyring::*; use kitchensink_runtime::{ constants::currency::*, wasm_binary_unwrap, AccountId, AssetsConfig, BabeConfig, - BalancesConfig, GluttonConfig, GrandpaConfig, IndicesConfig, RuntimeGenesisConfig, - SessionConfig, SocietyConfig, StakerStatus, StakingConfig, SystemConfig, + BalancesConfig, GluttonConfig, GrandpaConfig, IdentityConfig, IndicesConfig, + RuntimeGenesisConfig, SessionConfig, SocietyConfig, StakerStatus, StakingConfig, SystemConfig, BABE_GENESIS_EPOCH_CONFIG, }; use sp_keyring::{Ed25519Keyring, Sr25519Keyring}; -use sp_runtime::Perbill; +use sp_runtime::{BoundedVec, Perbill}; /// Create genesis runtime configuration for tests. pub fn config(code: Option<&[u8]>) -> RuntimeGenesisConfig { @@ -52,6 +52,12 @@ pub fn config_endowed(code: Option<&[u8]>, extra_endowed: Vec) -> Run code: code.map(|x| x.to_vec()).unwrap_or_else(|| wasm_binary_unwrap().to_vec()), ..Default::default() }, + identity: IdentityConfig { + identities: vec![ + (alice(), BoundedVec::try_from("Alice".to_string().as_bytes().to_vec()).unwrap()), + (bob(), BoundedVec::try_from("Bob".to_string().as_bytes().to_vec()).unwrap()), + ], + }, indices: IndicesConfig { indices: vec![] }, balances: BalancesConfig { balances: endowed }, session: SessionConfig { diff --git a/frame/identity/src/lib.rs b/frame/identity/src/lib.rs index f192ee2b461b3..b3784f9f047a5 100644 --- a/frame/identity/src/lib.rs +++ b/frame/identity/src/lib.rs @@ -99,6 +99,7 @@ type AccountIdLookupOf = <::Lookup as StaticLookup #[frame_support::pallet] pub mod pallet { use super::*; + use enumflags2::BitFlags; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; @@ -201,6 +202,55 @@ pub mod pallet { ValueQuery, >; + #[pallet::genesis_config] + pub struct GenesisConfig { + pub registrars: Vec<(T::AccountId, Vec<(T::AccountId, BoundedVec>)>)>, + } + + impl Default for GenesisConfig { + fn default() -> Self { + GenesisConfig { registrars: Default::default() } + } + } + + #[pallet::genesis_build] + impl BuildGenesisConfig for GenesisConfig { + fn build(&self) { + for (registrar, identities) in &self.registrars { + >::put( + BoundedVec::try_from(vec![Some(RegistrarInfo { + account: registrar.clone(), + fee: Zero::zero(), + fields: IdentityFields(>::all()), + })]) + .unwrap(), + ); + for (account, name) in identities { + let judgements = + BoundedVec::try_from(vec![(0, Judgement::KnownGood); 1]).unwrap(); + >::insert( + account, + Registration { + info: IdentityInfo { + display: Data::Raw(name.clone()), + twitter: Data::None, + riot: Data::None, + email: Data::None, + pgp_fingerprint: None, + image: Data::None, + legal: Data::None, + web: Data::None, + additional: BoundedVec::default(), + }, + judgements, + deposit: Zero::zero(), + }, + ); + } + } + } + } + #[pallet::error] pub enum Error { /// Too many subs-accounts. diff --git a/frame/identity/src/tests.rs b/frame/identity/src/tests.rs index 1532980574c2a..6cbee2e75b812 100644 --- a/frame/identity/src/tests.rs +++ b/frame/identity/src/tests.rs @@ -619,3 +619,32 @@ fn test_has_identity() { )); }); } + +#[test] +fn test_genesis_config_should_register_identities() { + let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + pallet_identity::GenesisConfig:: { + identities: vec![ + (1, BoundedVec::try_from("One".to_string().as_bytes().to_vec()).unwrap()), + (2, BoundedVec::try_from("Two".to_string().as_bytes().to_vec()).unwrap()), + (3, BoundedVec::try_from("Three".to_string().as_bytes().to_vec()).unwrap()), + ], + } + .assimilate_storage(&mut t) + .unwrap(); + let mut ext: sp_io::TestExternalities = t.into(); + ext.execute_with(|| { + assert_eq!( + Identity::identity(1).unwrap().info.display, + Data::Raw(b"One".to_vec().try_into().unwrap()) + ); + assert_eq!( + Identity::identity(2).unwrap().info.display, + Data::Raw(b"Two".to_vec().try_into().unwrap()) + ); + assert_eq!( + Identity::identity(3).unwrap().info.display, + Data::Raw(b"Three".to_vec().try_into().unwrap()) + ); + }); +}