@@ -79,6 +79,14 @@ impl AccountOwner {
7979 }
8080}
8181
82+ #[ cfg( with_revm) ]
83+ impl From < Address > for AccountOwner {
84+ fn from ( address : Address ) -> Self {
85+ let address = address. into_array ( ) ;
86+ AccountOwner :: Address20 ( address)
87+ }
88+ }
89+
8290#[ cfg( with_testing) ]
8391impl From < CryptoHash > for AccountOwner {
8492 fn from ( address : CryptoHash ) -> Self {
@@ -416,7 +424,12 @@ impl GenericApplicationId {
416424
417425impl < A > From < ApplicationId < A > > for AccountOwner {
418426 fn from ( app_id : ApplicationId < A > ) -> Self {
419- AccountOwner :: Address32 ( app_id. application_description_hash )
427+ if app_id. is_evm ( ) {
428+ let hash_bytes = app_id. application_description_hash . as_bytes ( ) ;
429+ AccountOwner :: Address20 ( hash_bytes[ ..20 ] . try_into ( ) . unwrap ( ) )
430+ } else {
431+ AccountOwner :: Address32 ( app_id. application_description_hash )
432+ }
420433 }
421434}
422435
@@ -966,6 +979,14 @@ impl<A> ApplicationId<A> {
966979 }
967980}
968981
982+ impl < A > ApplicationId < A > {
983+ /// Returns whether the `ApplicationId` is the one of an EVM application.
984+ pub fn is_evm ( & self ) -> bool {
985+ let bytes = self . application_description_hash . as_bytes ( ) ;
986+ bytes. 0 [ 20 ..] == [ 0 ; 12 ]
987+ }
988+ }
989+
969990#[ cfg( with_revm) ]
970991impl From < Address > for ApplicationId {
971992 fn from ( address : Address ) -> ApplicationId {
@@ -991,18 +1012,6 @@ impl<A> ApplicationId<A> {
9911012 pub fn bytes32 ( & self ) -> B256 {
9921013 * self . application_description_hash . as_bytes ( )
9931014 }
994-
995- /// Returns whether the `ApplicationId` is the one of an EVM application.
996- pub fn is_evm ( & self ) -> bool {
997- let bytes = self . application_description_hash . as_bytes ( ) ;
998- let bytes = bytes. 0 . as_ref ( ) ;
999- for byte in & bytes[ 20 ..] {
1000- if byte != & 0 {
1001- return false ;
1002- }
1003- }
1004- true
1005- }
10061015}
10071016
10081017#[ derive( Serialize , Deserialize ) ]
@@ -1267,4 +1276,18 @@ mod tests {
12671276 let stream_id2 = StreamId :: from_str ( & format ! ( "{stream_id1}" ) ) . unwrap ( ) ;
12681277 assert_eq ! ( stream_id1, stream_id2) ;
12691278 }
1279+
1280+ #[ cfg( with_revm) ]
1281+ #[ test]
1282+ fn test_address_account_owner ( ) {
1283+ use alloy_primitives:: Address ;
1284+ let mut vec = Vec :: new ( ) ;
1285+ for i in 0 ..20 {
1286+ vec. push ( i as u8 ) ;
1287+ }
1288+ let address1 = Address :: from_slice ( & vec) ;
1289+ let account_owner = AccountOwner :: from ( address1) ;
1290+ let address2 = account_owner. to_evm_address ( ) . unwrap ( ) ;
1291+ assert_eq ! ( address1, address2) ;
1292+ }
12701293}
0 commit comments