#![cfg(feature = "selector")]
use crate::Abi;
use sha3::{Digest, Keccak256};
pub fn keccak256(input: &[u8]) -> [u8; 32] {
let mut hasher = Keccak256::new();
hasher.update(input);
hasher.finalize().into()
}
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())
}
}