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
use crate::into_type::IntoType;
use sha3::{
Digest,
Keccak256,
};
use std::borrow::Cow;
pub struct Selector {
params: Vec<Cow<'static, str>>,
}
impl Selector {
pub fn new() -> Self {
Self { params: Vec::new() }
}
pub fn push<T: IntoType>(mut self) -> Self {
self.params.push(T::into_type());
self
}
pub fn build(self, name: &str) -> [u8; 4] {
let signature = format!("{}({})", name, self.params.join(","));
let mut sig = [0; 4];
let mut hasher = Keccak256::new();
hasher.input(&signature);
sig.copy_from_slice(&hasher.result()[0..4]);
sig
}
}