1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use sp_std::prelude::*;
#[cfg(feature = "std")]
use std::fmt;
use codec::{Compact, Decode, Encode, Error, Input};
use sp_core::blake2_256;
use sp_core::H256;
use sp_runtime::{generic::Era, MultiSignature};
pub use sp_runtime::{AccountId32 as AccountId, MultiAddress};
pub type AccountIndex = u64;
pub type GenericAddress = sp_runtime::MultiAddress<AccountId, ()>;
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Decode, Encode, Clone, Eq, PartialEq)]
pub struct GenericExtra(Era, Compact<u32>, Compact<u128>);
impl GenericExtra {
pub fn new(era: Era, nonce: u32) -> GenericExtra {
GenericExtra(era, Compact(nonce), Compact(0_u128))
}
}
impl Default for GenericExtra {
fn default() -> Self {
Self::new(Era::Immortal, 0)
}
}
pub type AdditionalSigned = (u32, u32, H256, H256, (), (), ());
#[derive(Encode, Clone)]
pub struct SignedPayload<Call>((Call, GenericExtra, AdditionalSigned));
impl<Call> SignedPayload<Call>
where
Call: Encode,
{
pub fn from_raw(call: Call, extra: GenericExtra, additional_signed: AdditionalSigned) -> Self {
Self((call, extra, additional_signed))
}
pub fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(|payload| {
if payload.len() > 256 {
f(&blake2_256(payload)[..])
} else {
f(payload)
}
})
}
}
#[derive(Clone, PartialEq)]
pub struct UncheckedExtrinsicV4<Call> {
pub signature: Option<(GenericAddress, MultiSignature, GenericExtra)>,
pub function: Call,
}
impl<Call> UncheckedExtrinsicV4<Call>
where
Call: Encode,
{
pub fn new_signed(
function: Call,
signed: GenericAddress,
signature: MultiSignature,
extra: GenericExtra,
) -> Self {
UncheckedExtrinsicV4 {
signature: Some((signed, signature, extra)),
function,
}
}
#[cfg(feature = "std")]
pub fn hex_encode(&self) -> String {
let mut hex_str = hex::encode(self.encode());
hex_str.insert_str(0, "0x");
hex_str
}
}
#[cfg(feature = "std")]
impl<Call> fmt::Debug for UncheckedExtrinsicV4<Call>
where
Call: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"UncheckedExtrinsic({:?}, {:?})",
self.signature.as_ref().map(|x| (&x.0, &x.2)),
self.function
)
}
}
const V4: u8 = 4;
impl<Call> Encode for UncheckedExtrinsicV4<Call>
where
Call: Encode,
{
fn encode(&self) -> Vec<u8> {
encode_with_vec_prefix::<Self, _>(|v| {
match self.signature.as_ref() {
Some(s) => {
v.push(V4 | 0b1000_0000);
s.encode_to(v);
}
None => {
v.push(V4 & 0b0111_1111);
}
}
self.function.encode_to(v);
})
}
}
impl<Call> Decode for UncheckedExtrinsicV4<Call>
where
Call: Decode + Encode,
{
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let _length_do_not_remove_me_see_above: Vec<()> = Decode::decode(input)?;
let version = input.read_byte()?;
let is_signed = version & 0b1000_0000 != 0;
let version = version & 0b0111_1111;
if version != V4 {
return Err("Invalid transaction version".into());
}
Ok(UncheckedExtrinsicV4 {
signature: if is_signed {
Some(Decode::decode(input)?)
} else {
None
},
function: Decode::decode(input)?,
})
}
}
fn encode_with_vec_prefix<T: Encode, F: Fn(&mut Vec<u8>)>(encoder: F) -> Vec<u8> {
let size = sp_std::mem::size_of::<T>();
let reserve = match size {
0..=0b0011_1111 => 1,
0b0100_0000..=0b0011_1111_1111_1111 => 2,
_ => 4,
};
let mut v = Vec::with_capacity(reserve + size);
v.resize(reserve, 0);
encoder(&mut v);
let mut length: Vec<()> = Vec::new();
length.resize(v.len() - reserve, ());
length.using_encoded(|s| {
v.splice(0..reserve, s.iter().cloned());
});
v
}
#[cfg(test)]
mod tests {
use super::*;
use crate::extrinsic::xt_primitives::{GenericAddress, GenericExtra};
use sp_runtime::MultiSignature;
#[test]
fn encode_decode_roundtrip_works() {
let xt = UncheckedExtrinsicV4::new_signed(
vec![1, 1, 1],
GenericAddress::default(),
MultiSignature::default(),
GenericExtra::default(),
);
let xt_enc = xt.encode();
assert_eq!(xt, Decode::decode(&mut xt_enc.as_slice()).unwrap())
}
}