1pub mod extrinsic_params;
12pub mod polkadot;
13pub mod substrate;
14
15use codec::{Decode, Encode};
16use core::fmt::Debug;
17use serde::{de::DeserializeOwned, Serialize};
18
19pub use extrinsic_params::ExtrinsicParams;
20pub use polkadot::PolkadotConfig;
21pub use substrate::SubstrateConfig;
22
23pub trait Config: 'static {
28 type Index: Debug + Copy + Decode + Into<u64>;
31
32 type Hash: Debug
34 + Copy
35 + Send
36 + Sync
37 + Decode
38 + AsRef<[u8]>
39 + Serialize
40 + DeserializeOwned
41 + Encode
42 + PartialEq;
43
44 type AccountId: Debug + Clone + Encode;
46
47 type Address: Debug + Encode + From<Self::AccountId>;
49
50 type Signature: Debug + Encode;
52
53 type Hasher: Debug + Hasher<Output = Self::Hash>;
55
56 type Header: Debug + Header<Hasher = Self::Hasher> + Sync + Send + DeserializeOwned;
58
59 type ExtrinsicParams: extrinsic_params::ExtrinsicParams<Self::Index, Self::Hash>;
61}
62
63pub trait Hasher {
66 type Output;
68
69 fn hash(s: &[u8]) -> Self::Output;
71
72 fn hash_of<S: Encode>(s: &S) -> Self::Output {
74 let out = s.encode();
75 Self::hash(&out)
76 }
77}
78
79pub trait Header: Sized + Encode {
81 type Number: Into<u64>;
83 type Hasher: Hasher;
85
86 fn number(&self) -> Self::Number;
88
89 fn hash(&self) -> <Self::Hasher as Hasher>::Output {
91 Self::Hasher::hash_of(self)
92 }
93}
94
95pub struct WithExtrinsicParams<T: Config, E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>> {
99 _marker: std::marker::PhantomData<(T, E)>,
100}
101
102impl<T: Config, E: extrinsic_params::ExtrinsicParams<T::Index, T::Hash>> Config
103 for WithExtrinsicParams<T, E>
104{
105 type Index = T::Index;
106 type Hash = T::Hash;
107 type AccountId = T::AccountId;
108 type Address = T::Address;
109 type Signature = T::Signature;
110 type Hasher = T::Hasher;
111 type Header = T::Header;
112 type ExtrinsicParams = E;
113}
114
115#[cfg(feature = "substrate-compat")]
117mod substrate_impls {
118 use super::*;
119 use primitive_types::{H256, U256};
120
121 impl<N, H> Header for sp_runtime::generic::Header<N, H>
122 where
123 Self: Encode,
124 N: Copy + Into<U256> + Into<u64> + TryFrom<U256>,
125 H: sp_runtime::traits::Hash + Hasher,
126 {
127 type Number = N;
128 type Hasher = H;
129
130 fn number(&self) -> Self::Number {
131 self.number
132 }
133 }
134
135 impl Hasher for sp_core::Blake2Hasher {
136 type Output = H256;
137
138 fn hash(s: &[u8]) -> Self::Output {
139 <Self as sp_core::Hasher>::hash(s)
140 }
141 }
142
143 impl Hasher for sp_core::KeccakHasher {
144 type Output = H256;
145
146 fn hash(s: &[u8]) -> Self::Output {
147 <Self as sp_core::Hasher>::hash(s)
148 }
149 }
150}