serai/processor/primitives/src/payment.rs
Luke Parker eb9bce6862 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
2024-09-19 23:36:32 -07:00

56 lines
1.5 KiB
Rust

use std::io;
use scale::{Encode, Decode, IoReader};
use borsh::{BorshSerialize, BorshDeserialize};
use serai_primitives::Balance;
use serai_coins_primitives::OutInstructionWithBalance;
use crate::Address;
/// A payment to fulfill.
#[derive(Clone, BorshSerialize, BorshDeserialize)]
pub struct Payment<A: Address> {
address: A,
balance: Balance,
}
impl<A: Address> TryFrom<OutInstructionWithBalance> for Payment<A> {
type Error = ();
fn try_from(out_instruction_with_balance: OutInstructionWithBalance) -> Result<Self, ()> {
Ok(Payment {
address: out_instruction_with_balance.instruction.address.try_into().map_err(|_| ())?,
balance: out_instruction_with_balance.balance,
})
}
}
impl<A: Address> Payment<A> {
/// Create a new Payment.
pub fn new(address: A, balance: Balance) -> Self {
Payment { address, balance }
}
/// The address to pay.
pub fn address(&self) -> &A {
&self.address
}
/// The balance to transfer.
pub fn balance(&self) -> Balance {
self.balance
}
/// 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)?;
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);
Ok(())
}
}