@@ -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
@@ -960,6 +973,14 @@ impl<A> ApplicationId<A> {
960973 }
961974}
962975
976+ impl < A > ApplicationId < A > {
977+ /// Returns whether the `ApplicationId` is the one of an EVM application.
978+ pub fn is_evm ( & self ) -> bool {
979+ let bytes = self . application_description_hash . as_bytes ( ) ;
980+ bytes. 0 [ 20 ..] == [ 0 ; 12 ]
981+ }
982+ }
983+
963984#[ cfg( with_revm) ]
964985impl From < Address > for ApplicationId {
965986 fn from ( address : Address ) -> ApplicationId {
@@ -985,18 +1006,6 @@ impl<A> ApplicationId<A> {
9851006 pub fn bytes32 ( & self ) -> B256 {
9861007 * self . application_description_hash . as_bytes ( )
9871008 }
988-
989- /// Returns whether the `ApplicationId` is the one of an EVM application.
990- pub fn is_evm ( & self ) -> bool {
991- let bytes = self . application_description_hash . as_bytes ( ) ;
992- let bytes = bytes. 0 . as_ref ( ) ;
993- for byte in & bytes[ 20 ..] {
994- if byte != & 0 {
995- return false ;
996- }
997- }
998- true
999- }
10001009}
10011010
10021011#[ derive( Serialize , Deserialize ) ]
@@ -1261,4 +1270,18 @@ mod tests {
12611270 let stream_id2 = StreamId :: from_str ( & format ! ( "{stream_id1}" ) ) . unwrap ( ) ;
12621271 assert_eq ! ( stream_id1, stream_id2) ;
12631272 }
1273+
1274+ #[ cfg( with_revm) ]
1275+ #[ test]
1276+ fn test_address_account_owner ( ) {
1277+ use alloy_primitives:: Address ;
1278+ let mut vec = Vec :: new ( ) ;
1279+ for i in 0 ..20 {
1280+ vec. push ( i as u8 ) ;
1281+ }
1282+ let address1 = Address :: from_slice ( & vec) ;
1283+ let account_owner = AccountOwner :: from ( address1) ;
1284+ let address2 = account_owner. to_evm_address ( ) . unwrap ( ) ;
1285+ assert_eq ! ( address1, address2) ;
1286+ }
12641287}
0 commit comments