Outline the Ethereum processor

This was only half-finished to begin with, unfortunately...
This commit is contained in:
Luke Parker
2024-09-14 07:54:18 -04:00
parent 72a18bf8bb
commit 7761798a78
19 changed files with 810 additions and 524 deletions

View File

@@ -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

View 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))
}
}

View File

@@ -1,5 +1,8 @@
#[cfg(feature = "bitcoin")]
pub mod bitcoin;
#[cfg(feature = "ethereum")]
pub mod ethereum;
#[cfg(feature = "monero")]
pub mod monero;