solid_core/
selector.rs

1use crate::into_type::IntoType;
2use sha3::{
3    Digest,
4    Keccak256,
5};
6use std::borrow::Cow;
7
8/// Function signature builder
9pub struct Selector {
10    params: Vec<Cow<'static, str>>,
11}
12
13impl Selector {
14    pub fn new() -> Self {
15        Self { params: Vec::new() }
16    }
17
18    pub fn push<T: IntoType>(mut self) -> Self {
19        self.params.push(T::into_type());
20        self
21    }
22
23    pub fn build(self, name: &str) -> [u8; 4] {
24        let signature = format!("{}({})", name, self.params.join(","));
25        let mut sig = [0; 4];
26        let mut hasher = Keccak256::new();
27        hasher.input(&signature);
28        sig.copy_from_slice(&hasher.result()[0..4]);
29        sig
30    }
31}