Skip to content

Commit

Permalink
Merge pull request #66 from OffchainLabs/codehash-and-interface-fixes
Browse files Browse the repository at this point in the history
Fix codehash for EOA's and `FixedBytes` in `sol_interface!`
  • Loading branch information
rachel-bousfield authored Sep 17, 2023
2 parents d0812f2 + e682065 commit 6045222
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion stylus-proc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
21 changes: 16 additions & 5 deletions stylus-sdk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B256>;
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 {
Expand All @@ -34,9 +39,15 @@ impl AddressVM for Address {
U256::from_be_bytes(data)
}

fn codehash(&self) -> Option<B256> {
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")
}
}

0 comments on commit 6045222

Please sign in to comment.