mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Outline the Ethereum processor
This was only half-finished to begin with, unfortunately...
This commit is contained in:
@@ -65,6 +65,7 @@ borsh = ["serai-abi/borsh"]
|
||||
|
||||
networks = []
|
||||
bitcoin = ["networks", "dep:bitcoin"]
|
||||
ethereum = ["networks"]
|
||||
monero = ["networks", "ciphersuite/ed25519", "monero-address"]
|
||||
|
||||
# Assumes the default usage is to use Serai as a DEX, which doesn't actually
|
||||
|
||||
51
substrate/client/src/networks/ethereum.rs
Normal file
51
substrate/client/src/networks/ethereum.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use core::{str::FromStr, fmt};
|
||||
|
||||
use borsh::{BorshSerialize, BorshDeserialize};
|
||||
|
||||
use crate::primitives::ExternalAddress;
|
||||
|
||||
/// A representation of an Ethereum address.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
|
||||
pub struct Address([u8; 20]);
|
||||
|
||||
impl From<[u8; 20]> for Address {
|
||||
fn from(address: [u8; 20]) -> Self {
|
||||
Self(address)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Address> for [u8; 20] {
|
||||
fn from(address: Address) -> Self {
|
||||
address.0
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<ExternalAddress> for Address {
|
||||
type Error = ();
|
||||
fn try_from(data: ExternalAddress) -> Result<Address, ()> {
|
||||
Ok(Self(data.as_ref().try_into().map_err(|_| ())?))
|
||||
}
|
||||
}
|
||||
impl From<Address> for ExternalAddress {
|
||||
fn from(address: Address) -> ExternalAddress {
|
||||
// This is 20 bytes which is less than MAX_ADDRESS_LEN
|
||||
ExternalAddress::new(address.0.to_vec()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Address {
|
||||
type Err = ();
|
||||
fn from_str(str: &str) -> Result<Address, ()> {
|
||||
let Some(address) = str.strip_prefix("0x") else { Err(())? };
|
||||
if address.len() != 40 {
|
||||
Err(())?
|
||||
};
|
||||
Ok(Self(hex::decode(address.to_lowercase()).map_err(|_| ())?.try_into().unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Address {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "0x{}", hex::encode(self.0))
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
#[cfg(feature = "bitcoin")]
|
||||
pub mod bitcoin;
|
||||
|
||||
#[cfg(feature = "ethereum")]
|
||||
pub mod ethereum;
|
||||
|
||||
#[cfg(feature = "monero")]
|
||||
pub mod monero;
|
||||
|
||||
Reference in New Issue
Block a user