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
use hex::FromHex;
use token::Tokenizer;
use errors::{Error, ErrorKind};
pub struct StrictTokenizer;
impl Tokenizer for StrictTokenizer {
fn tokenize_address(value: &str) -> Result<[u8; 20], Error> {
let hex = try!(value.from_hex());
match hex.len() == 20 {
false => Err(ErrorKind::InvalidData.into()),
true => {
let mut address = [0u8; 20];
address.copy_from_slice(&hex);
Ok(address)
}
}
}
fn tokenize_string(value: &str) -> Result<String, Error> {
Ok(value.to_owned())
}
fn tokenize_bool(value: &str) -> Result<bool, Error> {
match value {
"true" | "1" => Ok(true),
"false" | "0" => Ok(false),
_ => Err(ErrorKind::InvalidData.into()),
}
}
fn tokenize_bytes(value: &str) -> Result<Vec<u8>, Error> {
let hex = try!(value.from_hex());
Ok(hex)
}
fn tokenize_fixed_bytes(value: &str, len: usize) -> Result<Vec<u8>, Error> {
let hex = try!(value.from_hex());
match hex.len() == len {
true => Ok(hex),
false => Err(ErrorKind::InvalidData.into()),
}
}
fn tokenize_uint(value: &str) -> Result<[u8; 32], Error> {
let hex = try!(value.from_hex());
match hex.len() == 32 {
true => {
let mut uint = [0u8; 32];
uint.copy_from_slice(&hex);
Ok(uint)
},
false => Err(ErrorKind::InvalidData.into())
}
}
fn tokenize_int(value: &str) -> Result<[u8; 32], Error> {
let hex = try!(value.from_hex());
match hex.len() == 32 {
true => {
let mut int = [0u8; 32];
int.copy_from_slice(&hex);
Ok(int)
},
false => Err(ErrorKind::InvalidData.into())
}
}
}
#[cfg(test)]
mod tests {
use ParamType;
use token::{Token, Tokenizer, StrictTokenizer};
#[test]
fn tokenize_address() {
assert_eq!(StrictTokenizer::tokenize(&ParamType::Address, "1111111111111111111111111111111111111111").unwrap(), Token::Address([0x11u8; 20].into()));
assert_eq!(StrictTokenizer::tokenize(&ParamType::Address, "2222222222222222222222222222222222222222").unwrap(), Token::Address([0x22u8; 20].into()));
}
#[test]
fn tokenize_string() {
assert_eq!(StrictTokenizer::tokenize(&ParamType::String, "gavofyork").unwrap(), Token::String("gavofyork".to_owned()));
assert_eq!(StrictTokenizer::tokenize(&ParamType::String, "hello").unwrap(), Token::String("hello".to_owned()));
}
#[test]
fn tokenize_bool() {
assert_eq!(StrictTokenizer::tokenize(&ParamType::Bool, "true").unwrap(), Token::Bool(true));
assert_eq!(StrictTokenizer::tokenize(&ParamType::Bool, "1").unwrap(), Token::Bool(true));
assert_eq!(StrictTokenizer::tokenize(&ParamType::Bool, "false").unwrap(), Token::Bool(false));
assert_eq!(StrictTokenizer::tokenize(&ParamType::Bool, "0").unwrap(), Token::Bool(false));
}
#[test]
fn tokenize_bytes() {
assert_eq!(StrictTokenizer::tokenize(&ParamType::Bytes, "123456").unwrap(), Token::Bytes(vec![0x12, 0x34, 0x56]));
assert_eq!(StrictTokenizer::tokenize(&ParamType::Bytes, "0017").unwrap(), Token::Bytes(vec![0x00, 0x17]));
}
#[test]
fn tokenize_fixed_bytes() {
assert_eq!(StrictTokenizer::tokenize(&ParamType::FixedBytes(3), "123456").unwrap(), Token::FixedBytes(vec![0x12, 0x34, 0x56]));
assert_eq!(StrictTokenizer::tokenize(&ParamType::FixedBytes(2), "0017").unwrap(), Token::FixedBytes(vec![0x00, 0x17]));
}
#[test]
fn tokenize_uint() {
assert_eq!(
StrictTokenizer::tokenize(&ParamType::Uint(256), "1111111111111111111111111111111111111111111111111111111111111111").unwrap(),
Token::Uint([0x11u8; 32].into())
);
assert_eq!(
StrictTokenizer::tokenize(&ParamType::Uint(256), "2222222222222222222222222222222222222222222222222222222222222222").unwrap(),
Token::Uint([0x22u8; 32].into())
);
}
#[test]
fn tokenize_int() {
assert_eq!(
StrictTokenizer::tokenize(&ParamType::Int(256), "1111111111111111111111111111111111111111111111111111111111111111").unwrap(),
Token::Int([0x11u8; 32].into())
);
assert_eq!(
StrictTokenizer::tokenize(&ParamType::Int(256), "2222222222222222222222222222222222222222222222222222222222222222").unwrap(),
Token::Int([0x22u8; 32].into())
);
}
#[test]
fn tokenize_empty_array() {
assert_eq!(
StrictTokenizer::tokenize(&ParamType::Array(Box::new(ParamType::Bool)), "[]").unwrap(),
Token::Array(vec![])
);
}
#[test]
fn tokenize_bool_array() {
assert_eq!(
StrictTokenizer::tokenize(&ParamType::Array(Box::new(ParamType::Bool)), "[true,1,0,false]").unwrap(),
Token::Array(vec![Token::Bool(true), Token::Bool(true), Token::Bool(false), Token::Bool(false)])
);
}
#[test]
fn tokenize_bool_array_of_arrays() {
assert_eq!(
StrictTokenizer::tokenize(&ParamType::Array(Box::new(ParamType::Array(Box::new(ParamType::Bool)))), "[[true,1,0],[false]]").unwrap(),
Token::Array(vec![
Token::Array(vec![Token::Bool(true), Token::Bool(true), Token::Bool(false)]),
Token::Array(vec![Token::Bool(false)])
])
);
}
}