avail_rust_core/
decoded_transaction.rs

1use super::transaction::{AlreadyEncoded, EXTRINSIC_FORMAT_VERSION, TransactionSigned};
2use crate::TransactionCall;
3use codec::{Compact, Decode, Encode, Error, Input};
4use serde::{Deserialize, Serialize};
5
6pub trait HasTxDispatchIndex {
7	// Pallet ID, Call ID
8	const DISPATCH_INDEX: (u8, u8);
9}
10
11pub trait TransactionConvertible {
12	fn to_call(&self) -> TransactionCall;
13}
14
15pub trait TransactionDecodable {
16	/// Decodes the SCALE encoded Transaction Call
17	///
18	/// If you need to decode hex string call `decode_hex_call`
19	fn decode_call(call: &[u8]) -> Option<Box<Self>>;
20
21	/// Decodes the Hex and SCALE encoded Transaction Call
22	/// This is equal to Hex::decode + Self::decode_call
23	///
24	/// If you need to decode bytes call `decode_call`
25	fn decode_hex_call(call: &str) -> Option<Box<Self>>;
26
27	/// Decodes only the SCALE encoded Transaction Call Data
28	fn decode_call_data(call_data: &[u8]) -> Option<Box<Self>>;
29
30	/// Decodes the whole Hex and SCALE encoded Transaction.
31	/// This is equal to Hex::decode + OpaqueTransaction::try_from + Self::decode_call
32	///
33	/// If you need to decode bytes call `decode_transaction`
34	fn decode_hex_transaction(transaction: &str) -> Option<Box<Self>>;
35
36	/// Decodes the whole SCALE encoded Transaction.
37	/// This is equal to OpaqueTransaction::try_from + Self::decode_call
38	///
39	/// If you need to decode Hex string call `decode_hex_transaction`
40	fn decode_transaction(transaction: &[u8]) -> Option<Box<Self>>;
41}
42
43impl<T: HasTxDispatchIndex + Encode> TransactionConvertible for T {
44	fn to_call(&self) -> TransactionCall {
45		TransactionCall::new(Self::DISPATCH_INDEX.0, Self::DISPATCH_INDEX.1, self.encode())
46	}
47}
48
49impl<T: HasTxDispatchIndex + Decode> TransactionDecodable for T {
50	#[inline(always)]
51	fn decode_hex_transaction(transaction: &str) -> Option<Box<T>> {
52		let opaque = OpaqueTransaction::try_from(transaction).ok()?;
53		Self::decode_call(&opaque.call)
54	}
55
56	#[inline(always)]
57	fn decode_transaction(transaction_bytes: &[u8]) -> Option<Box<T>> {
58		let opaque = OpaqueTransaction::try_from(transaction_bytes).ok()?;
59		Self::decode_call(&opaque.call)
60	}
61
62	#[inline(always)]
63	fn decode_hex_call(call: &str) -> Option<Box<T>> {
64		let hex_decoded = const_hex::decode(call.trim_start_matches("0x")).ok()?;
65		Self::decode_call(&hex_decoded)
66	}
67
68	fn decode_call(call: &[u8]) -> Option<Box<T>> {
69		// This was moved out in order to decrease compilation times
70		if !tx_filter_in(call, Self::DISPATCH_INDEX) {
71			return None;
72		}
73
74		if call.len() <= 2 {
75			try_decode_transaction(&[])
76		} else {
77			try_decode_transaction(&call[2..])
78		}
79	}
80
81	fn decode_call_data(call_data: &[u8]) -> Option<Box<Self>> {
82		// This was moved out in order to decrease compilation times
83		try_decode_transaction(call_data)
84	}
85}
86
87// Purely here to decrease compilation times
88#[inline(never)]
89fn try_decode_transaction<T: Decode>(mut event_data: &[u8]) -> Option<Box<T>> {
90	T::decode(&mut event_data).ok().map(Box::new)
91}
92
93// Purely here to decrease compilation times
94#[inline(never)]
95fn tx_filter_in(call: &[u8], dispatch_index: (u8, u8)) -> bool {
96	if call.len() < 3 {
97		return false;
98	}
99
100	let (pallet_id, call_id) = (call[0], call[1]);
101	if dispatch_index.0 != pallet_id || dispatch_index.1 != call_id {
102		return false;
103	}
104
105	true
106}
107
108#[derive(Clone)]
109pub struct OpaqueTransaction {
110	/// The signature, address, number of extrinsics have come before from
111	/// the same signer and an era describing the longevity of this transaction,
112	/// if this is a signed extrinsic.
113	pub signature: Option<TransactionSigned>,
114	/// The function that should be called.
115	pub call: Vec<u8>,
116}
117
118impl OpaqueTransaction {
119	pub fn pallet_index(&self) -> u8 {
120		self.call[0]
121	}
122
123	pub fn call_index(&self) -> u8 {
124		self.call[1]
125	}
126}
127
128impl TryFrom<Vec<u8>> for OpaqueTransaction {
129	type Error = codec::Error;
130
131	fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
132		Self::try_from(value.as_slice())
133	}
134}
135
136impl TryFrom<&Vec<u8>> for OpaqueTransaction {
137	type Error = codec::Error;
138
139	fn try_from(value: &Vec<u8>) -> Result<Self, Self::Error> {
140		Self::try_from(value.as_slice())
141	}
142}
143
144impl TryFrom<&[u8]> for OpaqueTransaction {
145	type Error = codec::Error;
146
147	fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
148		let mut value = value;
149		Self::decode(&mut value)
150	}
151}
152
153impl TryFrom<String> for OpaqueTransaction {
154	type Error = codec::Error;
155
156	fn try_from(value: String) -> Result<Self, Self::Error> {
157		Self::try_from(value.as_str())
158	}
159}
160
161impl TryFrom<&str> for OpaqueTransaction {
162	type Error = codec::Error;
163
164	fn try_from(value: &str) -> Result<Self, Self::Error> {
165		let Ok(hex_decoded) = const_hex::decode(value.trim_start_matches("0x")) else {
166			return Err("Failed to hex decode transaction".into());
167		};
168
169		Self::decode(&mut hex_decoded.as_slice())
170	}
171}
172
173impl Decode for OpaqueTransaction {
174	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
175		// This is a little more complicated than usual since the binary format must be compatible
176		// with SCALE's generic `Vec<u8>` type. Basically this just means accepting that there
177		// will be a prefix of vector length.
178		let expected_length: Compact<u32> = Decode::decode(input)?;
179		let before_length = input.remaining_len()?;
180
181		let version = input.read_byte()?;
182
183		let is_signed = version & 0b1000_0000 != 0;
184		let version = version & 0b0111_1111;
185		if version != EXTRINSIC_FORMAT_VERSION {
186			return Err("Invalid transaction version".into());
187		}
188
189		let signature = is_signed.then(|| Decode::decode(input)).transpose()?;
190		let call: AlreadyEncoded = Decode::decode(input)?;
191
192		if let Some((before_length, after_length)) = input.remaining_len()?.and_then(|a| before_length.map(|b| (b, a)))
193		{
194			let length = before_length.saturating_sub(after_length);
195
196			if length != expected_length.0 as usize {
197				return Err("Invalid length prefix".into());
198			}
199		}
200
201		Ok(Self { signature, call: call.0 })
202	}
203}
204
205impl Encode for OpaqueTransaction {
206	fn encode_to<T: codec::Output + ?Sized>(&self, dest: &mut T) {
207		let mut encoded_tx_inner = Vec::new();
208		if let Some(signed) = &self.signature {
209			0x84u8.encode_to(&mut encoded_tx_inner);
210			signed.address.encode_to(&mut encoded_tx_inner);
211			signed.signature.encode_to(&mut encoded_tx_inner);
212			signed.tx_extra.encode_to(&mut encoded_tx_inner);
213		} else {
214			0x4u8.encode_to(&mut encoded_tx_inner);
215		}
216
217		encoded_tx_inner.extend(&self.call);
218		let mut encoded_tx = Compact(encoded_tx_inner.len() as u32).encode();
219		encoded_tx.append(&mut encoded_tx_inner);
220
221		dest.write(&encoded_tx)
222	}
223}
224
225impl Serialize for OpaqueTransaction {
226	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
227	where
228		S: serde::Serializer,
229	{
230		let bytes = self.encode();
231		impl_serde::serialize::serialize(&bytes, serializer)
232	}
233}
234
235impl<'a> Deserialize<'a> for OpaqueTransaction {
236	fn deserialize<D>(de: D) -> Result<Self, D::Error>
237	where
238		D: serde::Deserializer<'a>,
239	{
240		let r = impl_serde::serialize::deserialize(de)?;
241		Decode::decode(&mut &r[..]).map_err(|e| serde::de::Error::custom(format!("Decode error: {}", e)))
242	}
243}
244
245#[derive(Debug, Clone)]
246pub struct DecodedTransaction<T: HasTxDispatchIndex + Decode> {
247	pub signature: Option<TransactionSigned>,
248	pub call: Box<T>,
249}
250
251impl<T: HasTxDispatchIndex + Decode> TryFrom<Vec<u8>> for DecodedTransaction<T> {
252	type Error = codec::Error;
253
254	fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
255		Self::try_from(value.as_slice())
256	}
257}
258
259impl<T: HasTxDispatchIndex + Decode> TryFrom<&Vec<u8>> for DecodedTransaction<T> {
260	type Error = codec::Error;
261
262	fn try_from(value: &Vec<u8>) -> Result<Self, Self::Error> {
263		Self::try_from(value.as_slice())
264	}
265}
266
267impl<T: HasTxDispatchIndex + Decode> TryFrom<&[u8]> for DecodedTransaction<T> {
268	type Error = codec::Error;
269
270	fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
271		let opaque = OpaqueTransaction::try_from(value)?;
272		let call = T::decode_call(&opaque.call).ok_or(codec::Error::from("Failed to decode call"))?;
273		Ok(Self { signature: opaque.signature, call })
274	}
275}
276
277impl<T: HasTxDispatchIndex + Decode> TryFrom<String> for DecodedTransaction<T> {
278	type Error = codec::Error;
279
280	fn try_from(value: String) -> Result<Self, Self::Error> {
281		Self::try_from(value.as_str())
282	}
283}
284
285impl<T: HasTxDispatchIndex + Decode> TryFrom<&String> for DecodedTransaction<T> {
286	type Error = codec::Error;
287
288	fn try_from(value: &String) -> Result<Self, Self::Error> {
289		Self::try_from(value.as_str())
290	}
291}
292
293impl<T: HasTxDispatchIndex + Decode> TryFrom<&str> for DecodedTransaction<T> {
294	type Error = codec::Error;
295
296	fn try_from(value: &str) -> Result<Self, Self::Error> {
297		let opaque = OpaqueTransaction::try_from(value)?;
298		let call = T::decode_call(&opaque.call).ok_or(codec::Error::from("Failed to decode call"))?;
299		Ok(Self { signature: opaque.signature, call })
300	}
301}
302
303impl<T: HasTxDispatchIndex + Decode> TryFrom<OpaqueTransaction> for DecodedTransaction<T> {
304	type Error = codec::Error;
305
306	fn try_from(value: OpaqueTransaction) -> Result<Self, Self::Error> {
307		Self::try_from(&value)
308	}
309}
310
311impl<T: HasTxDispatchIndex + Decode> TryFrom<&OpaqueTransaction> for DecodedTransaction<T> {
312	type Error = codec::Error;
313
314	fn try_from(value: &OpaqueTransaction) -> Result<Self, Self::Error> {
315		let call = T::decode_call(&value.call).ok_or(codec::Error::from("Failed to decode call"))?;
316		Ok(Self { signature: value.signature.clone(), call })
317	}
318}
319
320#[cfg(test)]
321pub mod test {
322	use super::TransactionConvertible;
323	use std::borrow::Cow;
324
325	use codec::Encode;
326	use subxt_core::utils::{AccountId32, Era};
327
328	use crate::{
329		MultiAddress, MultiSignature, Transaction, TransactionExtra, avail::data_availability::tx::SubmitData,
330		decoded_transaction::OpaqueTransaction, transaction::TransactionSigned,
331	};
332
333	#[test]
334	fn test_encoding_decoding() {
335		let call = SubmitData { data: vec![0, 1, 2, 3] }.to_call();
336
337		let account_id = AccountId32([1u8; 32]);
338		let signature = [1u8; 64];
339		let signed = TransactionSigned {
340			address: MultiAddress::Id(account_id),
341			signature: MultiSignature::Sr25519(signature),
342			tx_extra: TransactionExtra {
343				era: Era::Mortal { period: 4, phase: 2 },
344				nonce: 1,
345				tip: 2u128,
346				app_id: 3,
347			},
348		};
349
350		let tx = Transaction { signed: Some(signed.clone()), call: Cow::Owned(call.clone()) };
351
352		let encoded_tx = tx.encode();
353
354		// Opaque Transaction
355		let opaque = OpaqueTransaction::try_from(&encoded_tx).unwrap();
356		let opaque_encoded = opaque.encode();
357
358		assert_eq!(encoded_tx, opaque_encoded);
359	}
360
361	#[test]
362	fn test_serialize_deserialize() {
363		let call = SubmitData { data: vec![0, 1, 2, 3] }.to_call();
364
365		let account_id = AccountId32([1u8; 32]);
366		let signature = [1u8; 64];
367		let signed = TransactionSigned {
368			address: MultiAddress::Id(account_id),
369			signature: MultiSignature::Sr25519(signature),
370			tx_extra: TransactionExtra {
371				era: Era::Mortal { period: 4, phase: 2 },
372				nonce: 1,
373				tip: 2u128,
374				app_id: 3,
375			},
376		};
377
378		let tx = Transaction { signed: Some(signed.clone()), call: Cow::Owned(call.clone()) };
379
380		let encoded_tx = tx.encode();
381		let expected_serialized = std::format!("0x{}", const_hex::encode(&encoded_tx));
382
383		// Transaction Serialized
384		let serialized = serde_json::to_string(&tx).unwrap();
385		assert_eq!(serialized.trim_matches('"'), expected_serialized);
386
387		// Transaction Deserialized
388		let tx_deserialized: Transaction = serde_json::from_str(&serialized).unwrap();
389		assert_eq!(encoded_tx, tx_deserialized.encode());
390
391		// Opaque Serialized
392		let opaque = OpaqueTransaction::try_from(&encoded_tx).unwrap();
393		let serialized = serde_json::to_string(&opaque).unwrap();
394		assert_eq!(serialized.trim_matches('"'), expected_serialized);
395
396		// Opaque Deserialized
397		let opaque_deserialized: OpaqueTransaction = serde_json::from_str(&serialized).unwrap();
398		assert_eq!(encoded_tx, opaque_deserialized.encode());
399	}
400}