use codec::{
Codec,
Decode,
Encode,
};
use core::{
fmt::Debug,
marker::PhantomData,
};
use sp_core::Pair;
use sp_runtime::{
generic::{
Era,
SignedPayload,
UncheckedExtrinsic,
},
traits::{
IdentifyAccount,
SignedExtension,
Verify,
},
transaction_validity::TransactionValidityError,
};
use crate::frame::{
balances::Balances,
system::System,
};
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
pub struct CheckVersion<T: System>(
pub PhantomData<T>,
#[codec(skip)]
pub u32,
);
impl<T> SignedExtension for CheckVersion<T>
where
T: System + Clone + Debug + Eq + Send + Sync,
{
const IDENTIFIER: &'static str = "CheckVersion";
type AccountId = u64;
type Call = ();
type AdditionalSigned = u32;
type Pre = ();
fn additional_signed(
&self,
) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Ok(self.1)
}
}
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
pub struct CheckGenesis<T: System>(
pub PhantomData<T>,
#[codec(skip)]
pub T::Hash,
);
impl<T> SignedExtension for CheckGenesis<T>
where
T: System + Clone + Debug + Eq + Send + Sync,
{
const IDENTIFIER: &'static str = "CheckGenesis";
type AccountId = u64;
type Call = ();
type AdditionalSigned = T::Hash;
type Pre = ();
fn additional_signed(
&self,
) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Ok(self.1)
}
}
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
pub struct CheckEra<T: System>(
pub (Era, PhantomData<T>),
#[codec(skip)]
pub T::Hash,
);
impl<T> SignedExtension for CheckEra<T>
where
T: System + Clone + Debug + Eq + Send + Sync,
{
const IDENTIFIER: &'static str = "CheckEra";
type AccountId = u64;
type Call = ();
type AdditionalSigned = T::Hash;
type Pre = ();
fn additional_signed(
&self,
) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Ok(self.1)
}
}
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
pub struct CheckNonce<T: System>(#[codec(compact)] pub T::Index);
impl<T> SignedExtension for CheckNonce<T>
where
T: System + Clone + Debug + Eq + Send + Sync,
{
const IDENTIFIER: &'static str = "CheckNonce";
type AccountId = u64;
type Call = ();
type AdditionalSigned = ();
type Pre = ();
fn additional_signed(
&self,
) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Ok(())
}
}
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
pub struct CheckWeight<T: System>(pub PhantomData<T>);
impl<T> SignedExtension for CheckWeight<T>
where
T: System + Clone + Debug + Eq + Send + Sync,
{
const IDENTIFIER: &'static str = "CheckWeight";
type AccountId = u64;
type Call = ();
type AdditionalSigned = ();
type Pre = ();
fn additional_signed(
&self,
) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Ok(())
}
}
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
pub struct ChargeTransactionPayment<T: Balances>(#[codec(compact)] pub T::Balance);
impl<T> SignedExtension for ChargeTransactionPayment<T>
where
T: Balances + Clone + Debug + Eq + Send + Sync,
{
const IDENTIFIER: &'static str = "ChargeTransactionPayment";
type AccountId = u64;
type Call = ();
type AdditionalSigned = ();
type Pre = ();
fn additional_signed(
&self,
) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Ok(())
}
}
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
pub struct CheckBlockGasLimit<T: System>(pub PhantomData<T>);
impl<T> SignedExtension for CheckBlockGasLimit<T>
where
T: System + Clone + Debug + Eq + Send + Sync,
{
const IDENTIFIER: &'static str = "CheckBlockGasLimit";
type AccountId = u64;
type Call = ();
type AdditionalSigned = ();
type Pre = ();
fn additional_signed(
&self,
) -> Result<Self::AdditionalSigned, TransactionValidityError> {
Ok(())
}
}
pub trait SignedExtra<T: System> {
type Extra: SignedExtension;
fn new(version: u32, nonce: T::Index, genesis_hash: T::Hash) -> Self;
fn extra(&self) -> Self::Extra;
}
#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug)]
pub struct DefaultExtra<T: System> {
version: u32,
nonce: T::Index,
genesis_hash: T::Hash,
}
impl<T: System + Balances + Clone + Debug + Eq + Send + Sync> SignedExtra<T>
for DefaultExtra<T>
{
type Extra = (
CheckVersion<T>,
CheckGenesis<T>,
CheckEra<T>,
CheckNonce<T>,
CheckWeight<T>,
ChargeTransactionPayment<T>,
CheckBlockGasLimit<T>,
);
fn new(version: u32, nonce: T::Index, genesis_hash: T::Hash) -> Self {
DefaultExtra {
version,
nonce,
genesis_hash,
}
}
fn extra(&self) -> Self::Extra {
(
CheckVersion(PhantomData, self.version),
CheckGenesis(PhantomData, self.genesis_hash),
CheckEra((Era::Immortal, PhantomData), self.genesis_hash),
CheckNonce(self.nonce),
CheckWeight(PhantomData),
ChargeTransactionPayment(<T as Balances>::Balance::default()),
CheckBlockGasLimit(PhantomData),
)
}
}
impl<T: System + Balances + Clone + Debug + Eq + Send + Sync> SignedExtension
for DefaultExtra<T>
{
const IDENTIFIER: &'static str = "DefaultExtra";
type AccountId = T::AccountId;
type Call = ();
type AdditionalSigned =
<<Self as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned;
type Pre = ();
fn additional_signed(
&self,
) -> Result<Self::AdditionalSigned, TransactionValidityError> {
self.extra().additional_signed()
}
}
pub(crate) fn create_and_sign<T: System + Send + Sync, C, P, S, E>(
signer: P,
call: C,
extra: E,
) -> Result<
UncheckedExtrinsic<T::Address, C, S, <E as SignedExtra<T>>::Extra>,
TransactionValidityError,
>
where
P: Pair,
S: Verify + Codec + From<P::Signature>,
S::Signer: From<P::Public> + IdentifyAccount<AccountId = T::AccountId>,
C: Encode,
E: SignedExtra<T> + SignedExtension,
T::Address: From<T::AccountId>,
{
let raw_payload = SignedPayload::new(call, extra.extra())?;
let signature = raw_payload.using_encoded(|payload| signer.sign(payload));
let (call, extra, _) = raw_payload.deconstruct();
let account_id = S::Signer::from(signer.public()).into_account();
Ok(UncheckedExtrinsic::new_signed(
call,
account_id.into(),
signature.into(),
extra,
))
}