#![cfg(feature = "selector")]
use crate::Abi;
use tiny_keccak::{Hasher, Keccak};
pub fn keccak256(input: &[u8]) -> [u8; 32] {
let mut hasher = Keccak::v256();
let mut output = [0; 32];
hasher.update(input);
hasher.finalize(&mut output);
output
}
pub fn parse(bytes: &[u8]) -> [u8; 4] {
let mut selector = [0u8; 4];
selector.copy_from_slice(&keccak256(bytes)[..4]);
selector
}
impl Abi {
pub fn signature(&self) -> String {
self.name.clone()
+ "("
+ &self
.inputs
.iter()
.map(|i| i.ty.as_ref())
.collect::<Vec<_>>()
.join(",")
+ ")"
}
pub fn selector(&self) -> [u8; 4] {
parse(self.signature().as_bytes())
}
}