Remove OutInstruction's data field

It makes sense for networks which support arbitrary data to do as part of their
address. This reduces the ability to perform DoSs, achieves better performance,
and better uses the type system (as now networks we don't support data on don't
have a data field).

Updates the Ethereum address definition in serai-client accordingly
This commit is contained in:
Luke Parker
2024-09-15 12:48:09 -04:00
parent 39be23d807
commit eb9bce6862
18 changed files with 121 additions and 150 deletions

View File

@@ -3,7 +3,7 @@ use std::io;
use scale::{Encode, Decode, IoReader};
use borsh::{BorshSerialize, BorshDeserialize};
use serai_primitives::{Balance, Data};
use serai_primitives::Balance;
use serai_coins_primitives::OutInstructionWithBalance;
use crate::Address;
@@ -13,7 +13,6 @@ use crate::Address;
pub struct Payment<A: Address> {
address: A,
balance: Balance,
data: Option<Vec<u8>>,
}
impl<A: Address> TryFrom<OutInstructionWithBalance> for Payment<A> {
@@ -22,15 +21,14 @@ impl<A: Address> TryFrom<OutInstructionWithBalance> for Payment<A> {
Ok(Payment {
address: out_instruction_with_balance.instruction.address.try_into().map_err(|_| ())?,
balance: out_instruction_with_balance.balance,
data: out_instruction_with_balance.instruction.data.map(Data::consume),
})
}
}
impl<A: Address> Payment<A> {
/// Create a new Payment.
pub fn new(address: A, balance: Balance, data: Option<Vec<u8>>) -> Self {
Payment { address, balance, data }
pub fn new(address: A, balance: Balance) -> Self {
Payment { address, balance }
}
/// The address to pay.
@@ -41,24 +39,18 @@ impl<A: Address> Payment<A> {
pub fn balance(&self) -> Balance {
self.balance
}
/// The data to associate with this payment.
pub fn data(&self) -> &Option<Vec<u8>> {
&self.data
}
/// Read a Payment.
pub fn read(reader: &mut impl io::Read) -> io::Result<Self> {
let address = A::deserialize_reader(reader)?;
let reader = &mut IoReader(reader);
let balance = Balance::decode(reader).map_err(io::Error::other)?;
let data = Option::<Vec<u8>>::decode(reader).map_err(io::Error::other)?;
Ok(Self { address, balance, data })
Ok(Self { address, balance })
}
/// Write the Payment.
pub fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
self.address.serialize(writer)?;
self.balance.encode_to(writer);
self.data.encode_to(writer);
Ok(())
}
}