From e68206562d8dc339dc6132bad803265b97049a5e Mon Sep 17 00:00:00 2001 From: Rachel Bousfield Date: Sun, 17 Sep 2023 15:26:13 -0600 Subject: [PATCH] fix codehash for EOA's and sol_interface! for bytesX --- stylus-proc/src/types.rs | 2 +- stylus-sdk/src/types.rs | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/stylus-proc/src/types.rs b/stylus-proc/src/types.rs index bbaf960c..22006f1f 100644 --- a/stylus-proc/src/types.rs +++ b/stylus-proc/src/types.rs @@ -82,7 +82,7 @@ pub fn solidity_type_info(ty: &Type) -> (Cow<'static, str>, Cow<'static, str>) { Type::String(_) => simple!(String), Type::Bytes(_) => simple!(Bytes), Type::FixedBytes(_, size) => ( - "stylus_sdk::abi::FixedBytesSolType<{size}>".into(), + format!("stylus_sdk::abi::FixedBytesSolType<{size}>").into(), abi!("bytes[{size}]"), ), Type::Uint(_, size) => { diff --git a/stylus-sdk/src/types.rs b/stylus-sdk/src/types.rs index f22a1caa..e20539ca 100644 --- a/stylus-sdk/src/types.rs +++ b/stylus-sdk/src/types.rs @@ -14,17 +14,22 @@ //! ``` use crate::hostio; -use alloy_primitives::{Address, B256, U256}; +use alloy_primitives::{b256, Address, B256, U256}; /// Trait that allows the [`Address`] type to inspect the corresponding account's balance and codehash. pub trait AddressVM { /// The balance in wei of the account. fn balance(&self) -> U256; - /// The codehash of the contract at the given address, or `None` when an [`EOA`]. + /// The codehash of the contract or [`EOA`] at the given address. /// /// [`EOA`]: https://ethereum.org/en/developers/docs/accounts/#types-of-account - fn codehash(&self) -> Option; + fn codehash(&self) -> B256; + + /// Determines if an account is an [`EOA`]. + /// + /// [`EOA`]: https://ethereum.org/en/developers/docs/accounts/#types-of-account + fn is_eoa(&self) -> bool; } impl AddressVM for Address { @@ -34,9 +39,15 @@ impl AddressVM for Address { U256::from_be_bytes(data) } - fn codehash(&self) -> Option { + fn codehash(&self) -> B256 { let mut data = [0; 32]; unsafe { hostio::account_codehash(self.0.as_ptr(), data.as_mut_ptr()) }; - (data != [0; 32]).then_some(data.into()) + data.into() + } + + fn is_eoa(&self) -> bool { + let hash = self.codehash(); + hash.is_zero() + || hash == b256!("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") } }