pub mod extrinsic_params;
pub mod polkadot;
pub mod substrate;
use codec::{Decode, Encode};
use core::fmt::Debug;
use serde::{de::DeserializeOwned, Serialize};
pub use extrinsic_params::ExtrinsicParams;
pub use polkadot::PolkadotConfig;
pub use substrate::SubstrateConfig;
pub trait Config: 'static {
type Index: Debug + Copy + Decode + Into<u64>;
type Hash: Debug
+ Copy
+ Send
+ Sync
+ Decode
+ AsRef<[u8]>
+ Serialize
+ DeserializeOwned
+ Encode
+ PartialEq;
type AccountId: Debug + Clone + Encode;
type Address: Debug + Encode + From<Self::AccountId>;
type Signature: Debug + Encode;
type Hasher: Debug + Hasher<Output = Self::Hash>;
type Header: Debug + Header<Hasher = Self::Hasher> + Sync + Send + DeserializeOwned;
type ExtrinsicParams: extrinsic_params::ExtrinsicParams<Self::Index, Self::Hash>;
}
pub trait Hasher {
type Output;
fn hash(s: &[u8]) -> Self::Output;
fn hash_of<S: Encode>(s: &S) -> Self::Output {
let out = s.encode();
Self::hash(&out)
}
}
pub trait Header: Sized + Encode {
type Number: Into<u64>;
type Hasher: Hasher;
fn number(&self) -> Self::Number;
fn hash(&self) -> <Self::Hasher as Hasher>::Output {
Self::Hasher::hash_of(self)
}
}
pub struct WithExtrinsicParams<T: Config, E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>> {
_marker: std::marker::PhantomData<(T, E)>,
}
impl<T: Config, E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>> Config
for WithExtrinsicParams<T, E>
{
type Index = T::Index;
type Hash = T::Hash;
type AccountId = T::AccountId;
type Address = T::Address;
type Signature = T::Signature;
type Hasher = T::Hasher;
type Header = T::Header;
type ExtrinsicParams = E;
}
#[cfg(feature = "substrate-compat")]
mod substrate_impls {
use super::*;
use primitive_types::{H256, U256};
impl<N, H> Header for sp_runtime::generic::Header<N, H>
where
Self: Encode,
N: Copy + Into<U256> + Into<u64> + TryFrom<U256>,
H: sp_runtime::traits::Hash + Hasher,
{
type Number = N;
type Hasher = H;
fn number(&self) -> Self::Number {
self.number
}
}
impl Hasher for sp_core::Blake2Hasher {
type Output = H256;
fn hash(s: &[u8]) -> Self::Output {
<Self as sp_core::Hasher>::hash(s)
}
}
impl Hasher for sp_core::KeccakHasher {
type Output = H256;
fn hash(s: &[u8]) -> Self::Output {
<Self as sp_core::Hasher>::hash(s)
}
}
}