Skip to content

Commit

Permalink
Added U256IntoEthAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalsw committed May 28, 2023
1 parent 7b713aa commit c414774
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions corelib/src/starknet/eth_address.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use serde::Serde;
use traits::{Into, TryInto};
use zeroable::Zeroable;
use option::{Option, OptionTrait};
use integer::{u128_safe_divmod, U128TryIntoNonZero, U256TryIntoFelt252};

// An Ethereum address (160 bits).
#[derive(Copy, Drop)]
Expand All @@ -25,6 +26,15 @@ impl EthAddressIntoFelt252 of Into<EthAddress, felt252> {
self.address
}
}
impl U256IntoEthAddress of Into<u256, EthAddress> {
fn into(self: u256) -> EthAddress {
// The Ethereum address is the 20 least significant bytes of the value.
let high_32_bits = self.high % 0x100000000_u128;
let address = high_32_bits.into() * 0x100000000000000000000000000000000_felt252
+ self.low.into();
EthAddress { address }
}
}
impl EthAddressSerde of Serde<EthAddress> {
fn serialize(self: @EthAddress, ref output: Array<felt252>) {
self.address.serialize(ref output);
Expand Down

0 comments on commit c414774

Please sign in to comment.