ethereum/
enveloped.rs

1use bytes::BytesMut;
2
3/// DecoderError for typed transactions.
4#[derive(Clone, Debug, Eq, PartialEq)]
5pub enum EnvelopedDecoderError<T> {
6	UnknownTypeId,
7	Payload(T),
8}
9
10impl<T> From<T> for EnvelopedDecoderError<T> {
11	fn from(e: T) -> Self {
12		Self::Payload(e)
13	}
14}
15
16/// Encodable typed transactions.
17pub trait EnvelopedEncodable {
18	/// Convert self to an owned vector.
19	fn encode(&self) -> BytesMut {
20		let type_id = self.type_id();
21
22		let mut out = BytesMut::new();
23		if let Some(type_id) = type_id {
24			assert!(type_id <= 0x7f);
25			out.extend_from_slice(&[type_id]);
26		}
27
28		out.extend_from_slice(&self.encode_payload()[..]);
29		out
30	}
31
32	/// Type Id of the transaction.
33	fn type_id(&self) -> Option<u8>;
34
35	/// Encode inner payload.
36	fn encode_payload(&self) -> BytesMut;
37}
38
39/// Decodable typed transactions.
40pub trait EnvelopedDecodable: Sized {
41	/// Inner payload decoder error.
42	type PayloadDecoderError;
43
44	/// Decode raw bytes to a Self type.
45	fn decode(bytes: &[u8]) -> Result<Self, EnvelopedDecoderError<Self::PayloadDecoderError>>;
46}