merkleized_metadata/
types.rs

1use alloc::{string::String, vec::Vec};
2use codec::{Compact, Encode};
3
4/// A reference to a type in the registry.
5#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode, Default, Copy)]
6pub enum TypeRef {
7	#[codec(index = 0)]
8	Bool,
9	#[codec(index = 1)]
10	Char,
11	#[codec(index = 2)]
12	Str,
13	#[codec(index = 3)]
14	U8,
15	#[codec(index = 4)]
16	U16,
17	#[codec(index = 5)]
18	U32,
19	#[codec(index = 6)]
20	U64,
21	#[codec(index = 7)]
22	U128,
23	#[codec(index = 8)]
24	U256,
25	#[codec(index = 9)]
26	I8,
27	#[codec(index = 10)]
28	I16,
29	#[codec(index = 11)]
30	I32,
31	#[codec(index = 12)]
32	I64,
33	#[codec(index = 13)]
34	I128,
35	#[codec(index = 14)]
36	I256,
37	#[codec(index = 15)]
38	CompactU8,
39	#[codec(index = 16)]
40	CompactU16,
41	#[codec(index = 17)]
42	CompactU32,
43	#[codec(index = 18)]
44	CompactU64,
45	#[codec(index = 19)]
46	CompactU128,
47	#[codec(index = 20)]
48	CompactU256,
49	#[codec(index = 21)]
50	#[default]
51	Void,
52	#[codec(index = 22)]
53	ById(Compact<u32>),
54}
55
56impl TypeRef {
57	pub fn id(&self) -> Option<u32> {
58		if let Self::ById(id) = self {
59			Some(id.0)
60		} else {
61			None
62		}
63	}
64}
65
66/// The hash type.
67pub type Hash = [u8; 32];
68
69#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
70pub enum TypeDef {
71	/// A composite type (e.g. a struct or a tuple)
72	#[codec(index = 0)]
73	Composite(Vec<Field>),
74	/// An enumeration.
75	#[codec(index = 1)]
76	Enumeration(EnumerationVariant),
77	/// A sequence type with runtime known length.
78	#[codec(index = 2)]
79	Sequence(TypeRef),
80	/// An array type with compile-time known length.
81	#[codec(index = 3)]
82	Array(TypeDefArray),
83	/// A tuple type.
84	#[codec(index = 4)]
85	Tuple(Vec<TypeRef>),
86	/// A type representing a sequence of bits.
87	#[codec(index = 5)]
88	BitSequence(TypeDefBitSequence),
89}
90
91impl TypeDef {
92	/// Returns `self` as [`EnumerationVariant`] or `None` if this isn't an `Enumeration`.
93	pub fn as_enumeration(&self) -> Option<&EnumerationVariant> {
94		if let Self::Enumeration(v) = self {
95			Some(v)
96		} else {
97			None
98		}
99	}
100}
101
102#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
103pub struct Field {
104	pub name: Option<String>,
105	pub ty: TypeRef,
106	pub type_name: Option<String>,
107}
108
109#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
110pub struct EnumerationVariant {
111	pub name: String,
112	pub fields: Vec<Field>,
113	pub index: Compact<u32>,
114}
115
116#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
117pub struct TypeDefArray {
118	pub len: u32,
119	pub type_param: TypeRef,
120}
121
122#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Debug)]
123pub struct TypeDefBitSequence {
124	pub num_bytes: u8,
125	pub least_significant_bit_first: bool,
126}
127
128#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
129pub struct Type {
130	/// The unique path to the type. Can be empty for built-in types
131	pub path: Vec<String>,
132	/// The actual type definition
133	pub type_def: TypeDef,
134	/// The unique id of this type.
135	pub type_id: Compact<u32>,
136}
137
138impl Type {
139	/// Returns the hash of this type.
140	pub fn hash(&self) -> Hash {
141		blake3::hash(&self.encode()).into()
142	}
143}
144
145#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
146pub struct ExtrinsicMetadata {
147	/// Extrinsic version.
148	pub version: u8,
149	pub address_ty: TypeRef,
150	pub call_ty: TypeRef,
151	pub signature_ty: TypeRef,
152	/// The signed extensions in the order they appear in the extrinsic.
153	pub signed_extensions: Vec<SignedExtensionMetadata>,
154}
155
156impl ExtrinsicMetadata {
157	pub fn hash(&self) -> Hash {
158		blake3::hash(&self.encode()).into()
159	}
160}
161
162#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
163pub struct SignedExtensionMetadata {
164	pub identifier: String,
165	pub included_in_extrinsic: TypeRef,
166	pub included_in_signed_data: TypeRef,
167}
168
169/// The metadata digest.
170///
171/// The hash of this digest is the "metadata hash".
172#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Encode)]
173pub enum MetadataDigest {
174	Disabled,
175	V1 {
176		types_tree_root: Hash,
177		extrinsic_metadata_hash: Hash,
178		spec_version: u32,
179		spec_name: String,
180		base58_prefix: u16,
181		decimals: u8,
182		token_symbol: String,
183	},
184}
185
186impl MetadataDigest {
187	/// Returns the hash of this digest.
188	pub fn hash(&self) -> Hash {
189		blake3::hash(&self.encode()).into()
190	}
191}