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
use crate::into_type::IntoType;
use sha3::{
    Digest,
    Keccak256,
};

#[derive(Default)]
pub struct Selector {
    params: Vec<String>,
}

impl Selector {
    pub fn new() -> Self {
        Selector::default()
    }

    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
    }
}