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
use signature::short_signature;
use {Param, Token, Result, ErrorKind, Bytes, decode, ParamType, encode};
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct Function {
pub name: String,
pub inputs: Vec<Param>,
pub outputs: Vec<Param>,
#[serde(default)]
pub constant: bool,
}
impl Function {
fn input_param_types(&self) -> Vec<ParamType> {
self.inputs.iter()
.map(|p| p.kind.clone())
.collect()
}
fn output_param_types(&self) -> Vec<ParamType> {
self.outputs.iter()
.map(|p| p.kind.clone())
.collect()
}
pub fn encode_input(&self, tokens: &[Token]) -> Result<Bytes> {
let params = self.input_param_types();
if !Token::types_check(tokens, ¶ms) {
return Err(ErrorKind::InvalidData.into());
}
let signed = short_signature(&self.name, ¶ms).to_vec();
let encoded = encode(tokens);
Ok(signed.into_iter().chain(encoded.into_iter()).collect())
}
pub fn decode_output(&self, data: &[u8]) -> Result<Vec<Token>> {
decode(&self.output_param_types(), &data)
}
}
#[cfg(test)]
mod tests {
use hex::FromHex;
use {Token, Param, Function, ParamType};
#[test]
fn test_function_encode_call() {
let interface = Function {
name: "baz".to_owned(),
inputs: vec![Param {
name: "a".to_owned(),
kind: ParamType::Uint(32),
}, Param {
name: "b".to_owned(),
kind: ParamType::Bool,
}],
outputs: vec![],
constant: false,
};
let func = Function::from(interface);
let mut uint = [0u8; 32];
uint[31] = 69;
let encoded = func.encode_input(&[Token::Uint(uint.into()), Token::Bool(true)]).unwrap();
let expected = "cdcd77c000000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001".from_hex().unwrap();
assert_eq!(encoded, expected);
}
}