Skip to content
Snippets Groups Projects
Unverified Commit 2a6b8c07 authored by bgallois's avatar bgallois
Browse files

fix #245

parent 6dec13d1
No related branches found
No related tags found
No related merge requests found
Pipeline #37904 failed
...@@ -243,6 +243,7 @@ impl pallet_identity::Config for Test { ...@@ -243,6 +243,7 @@ impl pallet_identity::Config for Test {
type IdtyNameValidator = IdtyNameValidatorTestImpl; type IdtyNameValidator = IdtyNameValidatorTestImpl;
type OnNewIdty = (); type OnNewIdty = ();
type OnRemoveIdty = (); type OnRemoveIdty = ();
type OwnerKeyChangePermission = ();
type RuntimeEvent = RuntimeEvent; type RuntimeEvent = RuntimeEvent;
type Signature = TestSignature; type Signature = TestSignature;
type Signer = UintAuthorityId; type Signer = UintAuthorityId;
......
...@@ -123,6 +123,7 @@ impl pallet_identity::Config for Test { ...@@ -123,6 +123,7 @@ impl pallet_identity::Config for Test {
type IdtyNameValidator = IdtyNameValidatorTestImpl; type IdtyNameValidator = IdtyNameValidatorTestImpl;
type OnNewIdty = DuniterWot; type OnNewIdty = DuniterWot;
type OnRemoveIdty = DuniterWot; type OnRemoveIdty = DuniterWot;
type OwnerKeyChangePermission = ();
type RuntimeEvent = RuntimeEvent; type RuntimeEvent = RuntimeEvent;
type Signature = TestSignature; type Signature = TestSignature;
type Signer = UintAuthorityId; type Signer = UintAuthorityId;
......
...@@ -132,6 +132,9 @@ pub mod pallet { ...@@ -132,6 +132,9 @@ pub mod pallet {
/// The type used to check account worthiness. /// The type used to check account worthiness.
type CheckAccountWorthiness: CheckAccountWorthiness<Self>; type CheckAccountWorthiness: CheckAccountWorthiness<Self>;
/// Handler that checks the necessary permissions for an identity's owner key change.
type OwnerKeyChangePermission: CheckKeyChangeAllowed<Self>;
/// Custom data to store in each identity. /// Custom data to store in each identity.
type IdtyData: Clone type IdtyData: Clone
+ Codec + Codec
...@@ -453,6 +456,12 @@ pub mod pallet { ...@@ -453,6 +456,12 @@ pub mod pallet {
Error::<T>::OwnerKeyAlreadyUsed Error::<T>::OwnerKeyAlreadyUsed
); );
// Ensure that the key is not currently as a validator
ensure!(
T::OwnerKeyChangePermission::check_allowed(&idty_index),
Error::<T>::OwnerKeyUsedAsValidator
);
let block_number = frame_system::Pallet::<T>::block_number(); let block_number = frame_system::Pallet::<T>::block_number();
let maybe_old_old_owner_key = let maybe_old_old_owner_key =
if let Some((old_owner_key, last_change)) = idty_value.old_owner_key { if let Some((old_owner_key, last_change)) = idty_value.old_owner_key {
...@@ -690,6 +699,8 @@ pub mod pallet { ...@@ -690,6 +699,8 @@ pub mod pallet {
AccountNotExist, AccountNotExist,
/// Insufficient balance to create an identity. /// Insufficient balance to create an identity.
InsufficientBalance, InsufficientBalance,
/// Owner key currently used as validator.
OwnerKeyUsedAsValidator,
} }
// INTERNAL FUNCTIONS // // INTERNAL FUNCTIONS //
......
...@@ -116,6 +116,7 @@ impl pallet_identity::Config for Test { ...@@ -116,6 +116,7 @@ impl pallet_identity::Config for Test {
type IdtyNameValidator = IdtyNameValidatorTestImpl; type IdtyNameValidator = IdtyNameValidatorTestImpl;
type OnNewIdty = (); type OnNewIdty = ();
type OnRemoveIdty = (); type OnRemoveIdty = ();
type OwnerKeyChangePermission = ();
type RuntimeEvent = RuntimeEvent; type RuntimeEvent = RuntimeEvent;
type Signature = Signature; type Signature = Signature;
type Signer = AccountPublic; type Signer = AccountPublic;
......
...@@ -94,3 +94,15 @@ impl<AccountId, IdtyIndex> LinkIdty<AccountId, IdtyIndex> for () { ...@@ -94,3 +94,15 @@ impl<AccountId, IdtyIndex> LinkIdty<AccountId, IdtyIndex> for () {
Ok(()) Ok(())
} }
} }
/// Trait for checking whether a key change is allowed for a given identity.
pub trait CheckKeyChangeAllowed<T: Config> {
/// Determines if a key change is allowed for the given identity.
fn check_allowed(account_id: &T::IdtyIndex) -> bool;
}
impl<T: Config> CheckKeyChangeAllowed<T> for () {
fn check_allowed(_: &T::IdtyIndex) -> bool {
true
}
}
...@@ -157,6 +157,7 @@ impl pallet_identity::Config for Test { ...@@ -157,6 +157,7 @@ impl pallet_identity::Config for Test {
type IdtyNameValidator = IdtyNameValidatorTestImpl; type IdtyNameValidator = IdtyNameValidatorTestImpl;
type OnNewIdty = (); type OnNewIdty = ();
type OnRemoveIdty = (); type OnRemoveIdty = ();
type OwnerKeyChangePermission = ();
type RuntimeEvent = RuntimeEvent; type RuntimeEvent = RuntimeEvent;
type Signature = Signature; type Signature = Signature;
type Signer = AccountPublic; type Signer = AccountPublic;
......
...@@ -165,3 +165,17 @@ where ...@@ -165,3 +165,17 @@ where
} }
} }
} }
/// Runtime handler OwnerKeyChangePermission.
pub struct OwnerKeyChangePermissionHandler<Runtime>(core::marker::PhantomData<Runtime>);
impl<
Runtime: frame_system::Config
+ pallet_identity::Config<IdtyIndex = IdtyIndex>
+ pallet_authority_members::Config<MemberId = IdtyIndex>,
> pallet_identity::traits::CheckKeyChangeAllowed<Runtime>
for OwnerKeyChangePermissionHandler<Runtime>
{
fn check_allowed(idty_index: &IdtyIndex) -> bool {
!pallet_authority_members::Pallet::<Runtime>::online().contains(idty_index)
}
}
...@@ -470,6 +470,7 @@ macro_rules! pallets_config { ...@@ -470,6 +470,7 @@ macro_rules! pallets_config {
type IdtyNameValidator = IdtyNameValidatorImpl; type IdtyNameValidator = IdtyNameValidatorImpl;
type OnNewIdty = OnNewIdtyHandler<Runtime>; type OnNewIdty = OnNewIdtyHandler<Runtime>;
type OnRemoveIdty = OnRemoveIdtyHandler<Runtime>; type OnRemoveIdty = OnRemoveIdtyHandler<Runtime>;
type OwnerKeyChangePermission = OwnerKeyChangePermissionHandler<Runtime>;
type RuntimeEvent = RuntimeEvent; type RuntimeEvent = RuntimeEvent;
type Signature = Signature; type Signature = Signature;
type Signer = <Signature as sp_runtime::traits::Verify>::Signer; type Signer = <Signature as sp_runtime::traits::Verify>::Signer;
......
...@@ -1361,6 +1361,31 @@ fn test_link_account() { ...@@ -1361,6 +1361,31 @@ fn test_link_account() {
}) })
} }
/// test change owner key
#[test]
fn test_change_owner_key_validator_online() {
ExtBuilder::new(1, 3, 4).build().execute_with(|| {
let genesis_hash = System::block_hash(0);
let alice = AccountKeyring::Alice.to_account_id();
let ferdie = AccountKeyring::Ferdie.to_account_id();
let payload = (b"icok", genesis_hash, 1u32, alice.clone()).encode();
let signature = AccountKeyring::Alice.sign(&payload);
// Alice is an online validator
assert!(pallet_authority_members::OnlineAuthorities::<Runtime>::get().contains(&1));
// As an online validator she cannot change key
assert_noop!(
Identity::change_owner_key(
frame_system::RawOrigin::Signed(alice.clone()).into(),
ferdie.clone(),
signature.into()
),
pallet_identity::Error::<gdev_runtime::Runtime>::OwnerKeyUsedAsValidator
);
})
}
/// test change owner key /// test change owner key
#[test] #[test]
fn test_change_owner_key() { fn test_change_owner_key() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment