mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Replace custom read/write impls in router with borsh
This commit is contained in:
24
processor/ethereum/primitives/src/borsh.rs
Normal file
24
processor/ethereum/primitives/src/borsh.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use ::borsh::{io, BorshSerialize, BorshDeserialize};
|
||||
|
||||
use alloy_primitives::{U256, Address};
|
||||
|
||||
/// Serialize a U256 with a borsh-compatible API.
|
||||
pub fn serialize_u256(value: &U256, writer: &mut impl io::Write) -> io::Result<()> {
|
||||
let value: [u8; 32] = value.to_be_bytes();
|
||||
value.serialize(writer)
|
||||
}
|
||||
|
||||
/// Deserialize an address with a borsh-compatible API.
|
||||
pub fn deserialize_u256(reader: &mut impl io::Read) -> io::Result<U256> {
|
||||
<[u8; 32]>::deserialize_reader(reader).map(|value| U256::from_be_bytes(value))
|
||||
}
|
||||
|
||||
/// Serialize an address with a borsh-compatible API.
|
||||
pub fn serialize_address(address: &Address, writer: &mut impl io::Write) -> io::Result<()> {
|
||||
<[u8; 20]>::from(address.0).serialize(writer)
|
||||
}
|
||||
|
||||
/// Deserialize an address with a borsh-compatible API.
|
||||
pub fn deserialize_address(reader: &mut impl io::Read) -> io::Result<Address> {
|
||||
<[u8; 20]>::deserialize_reader(reader).map(|address| Address(address.into()))
|
||||
}
|
||||
@@ -2,12 +2,27 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![deny(missing_docs)]
|
||||
|
||||
use ::borsh::{BorshSerialize, BorshDeserialize};
|
||||
|
||||
use group::ff::PrimeField;
|
||||
use k256::Scalar;
|
||||
|
||||
use alloy_primitives::PrimitiveSignature;
|
||||
use alloy_consensus::{SignableTransaction, Signed, TxLegacy};
|
||||
|
||||
mod borsh;
|
||||
pub use borsh::*;
|
||||
|
||||
/// An index of a log within a block.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
|
||||
#[borsh(crate = "::borsh")]
|
||||
pub struct LogIndex {
|
||||
/// The hash of the block which produced this log.
|
||||
pub block_hash: [u8; 32],
|
||||
/// The index of this log within the execution of the block.
|
||||
pub index_within_block: u64,
|
||||
}
|
||||
|
||||
/// The Keccak256 hash function.
|
||||
pub fn keccak256(data: impl AsRef<[u8]>) -> [u8; 32] {
|
||||
alloy_primitives::keccak256(data.as_ref()).into()
|
||||
|
||||
Reference in New Issue
Block a user