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
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(not(feature = "std"))]
use crate::no_std_prelude::*;
use crate::{
decode, encode, errors,
signature::{long_signature, short_signature},
Bytes, Hash, Param, ParamType, Result, Token,
};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct Error {
#[cfg_attr(feature = "serde", serde(deserialize_with = "crate::util::sanitize_name::deserialize"))]
pub name: String,
pub inputs: Vec<Param>,
}
impl Error {
fn param_types(&self) -> Vec<ParamType> {
self.inputs.iter().map(|p| p.kind.clone()).collect()
}
pub fn signature(&self) -> Hash {
long_signature(&self.name, &self.param_types())
}
pub fn encode(&self, tokens: &[Token]) -> Result<Bytes> {
let params = self.param_types();
if !Token::types_check(tokens, ¶ms) {
return Err(errors::Error::InvalidData);
}
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(&self, data: &[u8]) -> Result<Vec<Token>> {
decode(&self.param_types(), data)
}
}