Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Trait for deriving sovereign accounts of a pallet #8858

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 39 additions & 0 deletions frame/support/src/traits/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,42 @@ pub trait GetPalletVersion {
/// this function returns `None`.
fn storage_version() -> Option<PalletVersion>;
}

/// Conversion trait to allow a pallet instance to be converted into an account ID.
///
/// The same account ID will be returned as long as the pallet remains with the same name and index
/// within the runtime construction.
pub trait PalletIntoAccount {
/// Convert into an account ID. This is infallible.
fn into_account<AccountId: Default + Encode + Decode>() -> AccountId { Self::into_sub_account(&()) }

/// Convert this value amalgamated with the a secondary "sub" value into an account ID. This is
/// infallible.
///
/// NOTE: The account IDs from this and from `into_account` are *not* guaranteed to be distinct
/// for any given value of `self`, nor are different invocations to this with different types
/// `T`. For example, the following will all encode to the same account ID value:
/// - `self.into_sub_account(0u32)`
/// - `self.into_sub_account(vec![0u8; 0])`
/// - `self.into_account()`
fn into_sub_account<AccountId: Default + Encode + Decode, S: Encode>(sub: S) -> AccountId;
}

impl<T: PalletInfoAccess> PalletIntoAccount for T {
fn into_sub_account<AccountId: Default + Encode + Decode, S: Encode>(sub: S) -> AccountId {
sp_runtime::traits::AccountIdConversion::into_sub_account(&PalletId::new::<T>(), sub)
Copy link
Contributor

@gui1117 gui1117 May 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this easily go beyond 64 bit or 128 bit no ?

for the pallet "RandomnessCollectiveFlip", the pallet id is 4 + 1 + 24 byte, its type id is 4 byte. So the front part is 33 byte.
So the sub part will always be silent (or only on some runtime, depending on what is the pallet name).

Currently AccountIdConversion has some concern #8564
I think they should considered here.

}
}

#[derive(Encode, Decode)]
pub struct PalletId(u32, sp_runtime::RuntimeString);

impl sp_core::TypeId for PalletId {
const TYPE_ID: [u8; 4] = *b"PALI";
}

impl PalletId {
pub fn new<T: PalletInfoAccess>() -> Self {
Self(T::index() as u32, T::name().into())
}
}