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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use core::fmt;
use fuel_types::bytes::padded_len;
use fuel_types::Word;
use strum_macros::EnumString;
pub mod abi_decoder;
#[cfg(not(feature = "no-std"))]
pub mod abi_encoder;
pub mod errors;
pub mod signature;
pub type ByteArray = [u8; 8];
pub type Selector = ByteArray;
pub type Bits256 = [u8; 32];
pub type EnumSelector = (u8, Token);
pub const WORD_SIZE: usize = core::mem::size_of::<Word>();
#[derive(Debug, Clone, EnumString, PartialEq, Eq)]
#[strum(ascii_case_insensitive)]
pub enum ParamType {
U8,
U16,
U32,
U64,
Bool,
Byte,
B256,
Array(Box<ParamType>, usize),
#[strum(serialize = "str")]
String(usize),
#[strum(disabled)]
Struct(Vec<ParamType>),
#[strum(disabled)]
Enum(Vec<ParamType>),
}
impl Default for ParamType {
fn default() -> Self {
ParamType::U8
}
}
impl fmt::Display for ParamType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ParamType::String(size) => {
let t = format!("String({})", size);
write!(f, "{}", t)
}
ParamType::Array(t, size) => {
let boxed_type_str = format!("Box::new(ParamType::{})", t.to_string());
let arr_str = format!("Array({},{})", boxed_type_str, size);
write!(f, "{}", arr_str)
}
ParamType::Struct(inner) => {
let inner_strings: Vec<String> = inner
.iter()
.map(|p| format!("ParamType::{}", p.to_string()))
.collect();
let s = format!("Struct(vec![{}])", inner_strings.join(","));
write!(f, "{}", s)
}
_ => {
write!(f, "{:?}", self)
}
}
}
}
#[derive(Debug, Clone, PartialEq, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum Token {
U8(u8),
U16(u16),
U32(u32),
U64(u64),
Bool(bool),
Byte(u8),
B256(Bits256),
Array(Vec<Token>),
String(String),
Struct(Vec<Token>),
Enum(Box<EnumSelector>),
}
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl<'a> Default for Token {
fn default() -> Self {
Token::U8(0)
}
}
#[derive(Clone, Debug)]
pub struct InvalidOutputType(pub String);
pub trait Tokenizable {
fn from_token(token: Token) -> Result<Self, InvalidOutputType>
where
Self: Sized;
fn into_token(self) -> Token;
}
impl Tokenizable for Token {
fn from_token(token: Token) -> Result<Self, InvalidOutputType> {
Ok(token)
}
fn into_token(self) -> Token {
self
}
}
impl Tokenizable for bool {
fn from_token(token: Token) -> Result<Self, InvalidOutputType> {
match token {
Token::Bool(data) => Ok(data),
other => Err(InvalidOutputType(format!(
"Expected `bool`, got {:?}",
other
))),
}
}
fn into_token(self) -> Token {
Token::Bool(self)
}
}
impl Tokenizable for String {
fn from_token(token: Token) -> Result<Self, InvalidOutputType> {
match token {
Token::String(data) => Ok(data),
other => Err(InvalidOutputType(format!(
"Expected `String`, got {:?}",
other
))),
}
}
fn into_token(self) -> Token {
Token::String(self)
}
}
impl Tokenizable for Bits256 {
fn from_token(token: Token) -> Result<Self, InvalidOutputType> {
match token {
Token::B256(data) => Ok(data),
other => Err(InvalidOutputType(format!(
"Expected `String`, got {:?}",
other
))),
}
}
fn into_token(self) -> Token {
Token::B256(self)
}
}
impl<T: Tokenizable> Tokenizable for Vec<T> {
fn from_token(token: Token) -> Result<Self, InvalidOutputType> {
match token {
Token::Array(data) => {
let mut v: Vec<T> = Vec::new();
for tok in data {
v.push(T::from_token(tok.clone()).unwrap());
}
return Ok(v);
}
other => Err(InvalidOutputType(format!("Expected `T`, got {:?}", other))),
}
}
fn into_token(self) -> Token {
let mut v: Vec<Token> = Vec::new();
for t in self {
let tok = T::into_token(t);
v.push(tok);
}
Token::Array(v)
}
}
impl Tokenizable for u8 {
fn from_token(token: Token) -> Result<Self, InvalidOutputType> {
match token {
Token::U8(data) => Ok(data),
other => Err(InvalidOutputType(format!("Expected `u8`, got {:?}", other))),
}
}
fn into_token(self) -> Token {
Token::U8(self)
}
}
impl Tokenizable for u16 {
fn from_token(token: Token) -> Result<Self, InvalidOutputType> {
match token {
Token::U16(data) => Ok(data),
other => Err(InvalidOutputType(format!(
"Expected `u16`, got {:?}",
other
))),
}
}
fn into_token(self) -> Token {
Token::U16(self)
}
}
impl Tokenizable for u32 {
fn from_token(token: Token) -> Result<Self, InvalidOutputType> {
match token {
Token::U32(data) => Ok(data),
other => Err(InvalidOutputType(format!(
"Expected `u32`, got {:?}",
other
))),
}
}
fn into_token(self) -> Token {
Token::U32(self)
}
}
impl Tokenizable for u64 {
fn from_token(token: Token) -> Result<Self, InvalidOutputType> {
match token {
Token::U64(data) => Ok(data),
other => Err(InvalidOutputType(format!(
"Expected `u64`, got {:?}",
other
))),
}
}
fn into_token(self) -> Token {
Token::U64(self)
}
}
pub trait Detokenize {
fn from_tokens(tokens: Vec<Token>) -> Result<Self, InvalidOutputType>
where
Self: Sized;
}
impl Detokenize for () {
fn from_tokens(_: Vec<Token>) -> std::result::Result<Self, InvalidOutputType>
where
Self: Sized,
{
Ok(())
}
}
impl<T: Tokenizable> Detokenize for T {
fn from_tokens(mut tokens: Vec<Token>) -> Result<Self, InvalidOutputType> {
let token = match tokens.len() {
0 => Token::Struct(vec![]),
1 => tokens.remove(0),
_ => Token::Struct(tokens),
};
Self::from_token(token)
}
}
pub fn pad_u8(value: &u8) -> ByteArray {
let mut padded = ByteArray::default();
padded[7] = *value;
padded
}
pub fn pad_u16(value: &u16) -> ByteArray {
let mut padded = ByteArray::default();
padded[6..].copy_from_slice(&value.to_be_bytes());
padded
}
pub fn pad_u32(value: &u32) -> ByteArray {
let mut padded = [0u8; 8];
padded[4..].copy_from_slice(&value.to_be_bytes());
padded
}
pub fn pad_string(s: &str) -> Vec<u8> {
let pad = padded_len(s.as_bytes()) - s.len();
let mut padded = s.as_bytes().to_owned();
padded.extend_from_slice(&vec![0; pad]);
padded
}