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

Add SetMaintainer trait and implement for signature bridge #304

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion pallets/signature-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use codec::{self, Decode, Encode, EncodeLike, MaxEncodedLen};
use frame_support::{
pallet_prelude::{ensure, DispatchResultWithPostInfo},
traits::{EnsureOrigin, Get},
BoundedVec,
};
use frame_system::{self as system, ensure_root};
pub use pallet::*;
Expand All @@ -71,7 +72,8 @@ use sp_runtime::{
};
use sp_std::{convert::TryInto, prelude::*};
use webb_primitives::{
signing::SigningSystem, utils::compute_chain_id_type, webb_proposals::ResourceId,
signature_bridge::SetMaintainer, signing::SigningSystem, utils::compute_chain_id_type,
webb_proposals::ResourceId,
};
pub use weights::WeightInfo;

Expand All @@ -86,6 +88,7 @@ pub mod pallet {
};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::AtLeast32Bit;
use webb_primitives::signature_bridge::SetMaintainer;

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
Expand Down Expand Up @@ -138,6 +141,7 @@ pub mod pallet {

/// Signature verification utility over public key infrastructure
type SignatureVerifier: SigningSystem;

/// The identifier for this chain.
/// This must be unique and must not collide with existing IDs within a
/// set of bridged chains.
Expand Down Expand Up @@ -589,6 +593,29 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}
}

/// Implements the SetMaintainer post-processing hook for the pallet
impl<T: Config<I>, I: 'static> SetMaintainer<T::MaintainerNonce, T::MaxStringLength>
for Pallet<T, I>
{
fn set_maintainer(
nonce: T::MaintainerNonce,
new_maintainer: BoundedVec<u8, T::MaxStringLength>,
) -> Result<(), DispatchError> {
let next_maintainer_nonce = MaintainerNonce::<T, I>::get() + 1u32.into();
// Nonce should increment by 1
ensure!(next_maintainer_nonce == nonce, Error::<T, I>::InvalidNonce);
// set the new maintainer nonce
MaintainerNonce::<T, I>::put(nonce);
// set the new maintainer
Maintainer::<T, I>::try_mutate(|maintainer| {
let old_maintainer = maintainer.clone();
*maintainer = new_maintainer.clone();
Self::deposit_event(Event::MaintainerSet { old_maintainer, new_maintainer });
Ok(().into())
})
}
}

/// Simple ensure origin for the bridge account
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo, RuntimeDebug)]
pub struct EnsureBridge<T, I>(sp_std::marker::PhantomData<(T, I)>);
Expand Down
1 change: 1 addition & 0 deletions primitives/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub mod key_storage;
pub mod linkable_tree;
pub mod merkle_tree;
pub mod mixer;
pub mod signature_bridge;
pub mod vanchor;
12 changes: 12 additions & 0 deletions primitives/src/traits/signature_bridge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! All the traits exposed to be used in other custom pallets
use frame_support::{dispatch, BoundedVec};

/// Post-processing hook for setting a maintainer after a maintainer is selected
/// in some external process.
pub trait SetMaintainer<N, M> {
/// Set the maintainer of the pallet.
fn set_maintainer(
nonce: N,
maintainer: BoundedVec<u8, M>,
) -> Result<(), dispatch::DispatchError>;
}