Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute 4 Byte selector from SolError and SolFunction #24

Merged
merged 11 commits into from
Nov 18, 2024
36 changes: 35 additions & 1 deletion crates/ast/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use alloy_primitives::U256;
use crate::ast::{SolError, SolFunction};
use crate::Spanned;
use alloy_dyn_abi::DynSolType;
use alloy_primitives::{keccak256, FixedBytes, U256};
use evm_glue::opcodes::Opcode;

pub(crate) fn u256_as_push_data<const N: usize>(value: U256) -> Result<[u8; N], String> {
Expand Down Expand Up @@ -53,3 +56,34 @@ pub fn u256_as_push(value: U256) -> Opcode {
_ => unreachable!(),
}
}

type Selector = (FixedBytes<4>, FixedBytes<4>);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused


pub fn compute_selector(name: &Spanned<&str>, args: &Box<[Spanned<DynSolType>]>) -> FixedBytes<4> {
let build_signature = |name: &Spanned<&str>, args: &Box<[Spanned<DynSolType>]>| -> Vec<u8> {
let arg_types: Vec<String> = args
.iter()
.map(|arg| {
let type_str = arg.0.to_string();
type_str
})
.collect();

let mut signature = String::new();
signature.push_str(name.0);
signature.push('(');
signature.push_str(&arg_types.join(","));
signature.push(')');

signature.into_bytes()
};

let signature = build_signature(name, args);

let hash = keccak256(signature);

let selector = FixedBytes::<4>::from_slice(&hash[..4]);

selector
}