2024-09-18 01:09:42 -04:00
|
|
|
use std::io;
|
2024-09-14 07:54:18 -04:00
|
|
|
|
2024-09-18 01:09:42 -04:00
|
|
|
use ciphersuite::Secp256k1;
|
|
|
|
|
use frost::dkg::ThresholdKeys;
|
2024-09-14 07:54:18 -04:00
|
|
|
|
2024-09-18 00:54:20 -04:00
|
|
|
use alloy_core::primitives::U256;
|
|
|
|
|
|
|
|
|
|
use serai_client::networks::ethereum::Address;
|
|
|
|
|
|
|
|
|
|
use scheduler::SignableTransaction;
|
|
|
|
|
|
|
|
|
|
use ethereum_primitives::keccak256;
|
|
|
|
|
use ethereum_schnorr::{PublicKey, Signature};
|
|
|
|
|
use ethereum_router::{Coin, OutInstructions, Executed, Router};
|
2024-09-14 07:54:18 -04:00
|
|
|
|
2024-09-18 01:09:42 -04:00
|
|
|
use crate::{output::OutputId, machine::ClonableTransctionMachine};
|
2024-09-14 07:54:18 -04:00
|
|
|
|
2024-09-18 00:54:20 -04:00
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
|
|
|
|
pub(crate) enum Action {
|
|
|
|
|
SetKey { chain_id: U256, nonce: u64, key: PublicKey },
|
2024-09-20 00:12:54 -04:00
|
|
|
Batch { chain_id: U256, nonce: u64, coin: Coin, fee_per_gas: U256, outs: Vec<(Address, U256)> },
|
2024-09-18 00:54:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
|
|
|
pub(crate) struct Eventuality(pub(crate) Executed);
|
|
|
|
|
|
|
|
|
|
impl Action {
|
2024-09-18 01:09:42 -04:00
|
|
|
pub(crate) fn nonce(&self) -> u64 {
|
2024-09-18 00:54:20 -04:00
|
|
|
match self {
|
|
|
|
|
Action::SetKey { nonce, .. } | Action::Batch { nonce, .. } => *nonce,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 01:09:42 -04:00
|
|
|
pub(crate) fn message(&self) -> Vec<u8> {
|
2024-09-18 00:54:20 -04:00
|
|
|
match self {
|
2024-09-18 00:57:10 -04:00
|
|
|
Action::SetKey { chain_id, nonce, key } => {
|
|
|
|
|
Router::update_serai_key_message(*chain_id, *nonce, key)
|
|
|
|
|
}
|
2024-09-20 00:12:54 -04:00
|
|
|
Action::Batch { chain_id, nonce, coin, fee_per_gas, outs } => Router::execute_message(
|
|
|
|
|
*chain_id,
|
|
|
|
|
*nonce,
|
|
|
|
|
*coin,
|
|
|
|
|
*fee_per_gas,
|
|
|
|
|
OutInstructions::from(outs.as_ref()),
|
|
|
|
|
),
|
2024-09-18 00:54:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
|
2024-09-18 00:54:20 -04:00
|
|
|
pub(crate) fn eventuality(&self) -> Eventuality {
|
|
|
|
|
Eventuality(match self {
|
|
|
|
|
Self::SetKey { chain_id: _, nonce, key } => {
|
|
|
|
|
Executed::SetKey { nonce: *nonce, key: key.eth_repr() }
|
|
|
|
|
}
|
2024-09-20 00:12:54 -04:00
|
|
|
Self::Batch { nonce, .. } => Executed::Batch {
|
2024-09-18 00:54:20 -04:00
|
|
|
nonce: *nonce,
|
2024-09-20 00:12:54 -04:00
|
|
|
message_hash: keccak256(self.message()),
|
2024-09-18 00:54:20 -04:00
|
|
|
},
|
|
|
|
|
})
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 00:54:20 -04:00
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
2024-09-18 01:09:42 -04:00
|
|
|
pub(crate) struct Transaction(pub(crate) Action, pub(crate) Signature);
|
2024-09-14 07:54:18 -04:00
|
|
|
impl scheduler::Transaction for Transaction {
|
|
|
|
|
fn read(reader: &mut impl io::Read) -> io::Result<Self> {
|
2024-09-18 00:54:20 -04:00
|
|
|
let action = Action::read(reader)?;
|
|
|
|
|
let signature = Signature::read(reader)?;
|
|
|
|
|
Ok(Transaction(action, signature))
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
|
2024-09-18 00:54:20 -04:00
|
|
|
self.0.write(writer)?;
|
|
|
|
|
self.1.write(writer)?;
|
|
|
|
|
Ok(())
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 00:54:20 -04:00
|
|
|
impl SignableTransaction for Action {
|
2024-09-14 07:54:18 -04:00
|
|
|
type Transaction = Transaction;
|
|
|
|
|
type Ciphersuite = Secp256k1;
|
|
|
|
|
type PreprocessMachine = ClonableTransctionMachine;
|
|
|
|
|
|
|
|
|
|
fn read(reader: &mut impl io::Read) -> io::Result<Self> {
|
2024-09-18 00:54:20 -04:00
|
|
|
let mut kind = [0xff];
|
|
|
|
|
reader.read_exact(&mut kind)?;
|
|
|
|
|
if kind[0] >= 2 {
|
|
|
|
|
Err(io::Error::other("unrecognized Action type"))?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut chain_id = [0; 32];
|
|
|
|
|
reader.read_exact(&mut chain_id)?;
|
|
|
|
|
let chain_id = U256::from_le_bytes(chain_id);
|
|
|
|
|
|
|
|
|
|
let mut nonce = [0; 8];
|
|
|
|
|
reader.read_exact(&mut nonce)?;
|
|
|
|
|
let nonce = u64::from_le_bytes(nonce);
|
|
|
|
|
|
|
|
|
|
Ok(match kind[0] {
|
|
|
|
|
0 => {
|
|
|
|
|
let mut key = [0; 32];
|
|
|
|
|
reader.read_exact(&mut key)?;
|
|
|
|
|
let key =
|
|
|
|
|
PublicKey::from_eth_repr(key).ok_or_else(|| io::Error::other("invalid key in Action"))?;
|
|
|
|
|
|
|
|
|
|
Action::SetKey { chain_id, nonce, key }
|
|
|
|
|
}
|
|
|
|
|
1 => {
|
2024-09-20 00:12:54 -04:00
|
|
|
let coin = Coin::read(reader)?;
|
|
|
|
|
|
|
|
|
|
let mut fee_per_gas = [0; 32];
|
|
|
|
|
reader.read_exact(&mut fee_per_gas)?;
|
|
|
|
|
let fee_per_gas = U256::from_le_bytes(fee_per_gas);
|
|
|
|
|
|
2024-09-18 00:54:20 -04:00
|
|
|
let mut outs_len = [0; 4];
|
|
|
|
|
reader.read_exact(&mut outs_len)?;
|
|
|
|
|
let outs_len = usize::try_from(u32::from_le_bytes(outs_len)).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut outs = vec![];
|
|
|
|
|
for _ in 0 .. outs_len {
|
|
|
|
|
let address = borsh::from_reader(reader)?;
|
|
|
|
|
|
|
|
|
|
let mut amount = [0; 32];
|
|
|
|
|
reader.read_exact(&mut amount)?;
|
|
|
|
|
let amount = U256::from_le_bytes(amount);
|
|
|
|
|
|
2024-09-20 00:12:54 -04:00
|
|
|
outs.push((address, amount));
|
2024-09-18 00:54:20 -04:00
|
|
|
}
|
2024-09-20 00:12:54 -04:00
|
|
|
Action::Batch { chain_id, nonce, coin, fee_per_gas, outs }
|
2024-09-18 00:54:20 -04:00
|
|
|
}
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
})
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
|
2024-09-18 00:54:20 -04:00
|
|
|
match self {
|
|
|
|
|
Self::SetKey { chain_id, nonce, key } => {
|
|
|
|
|
writer.write_all(&[0])?;
|
|
|
|
|
writer.write_all(&chain_id.as_le_bytes())?;
|
|
|
|
|
writer.write_all(&nonce.to_le_bytes())?;
|
|
|
|
|
writer.write_all(&key.eth_repr())
|
|
|
|
|
}
|
2024-09-20 00:12:54 -04:00
|
|
|
Self::Batch { chain_id, nonce, coin, fee_per_gas, outs } => {
|
2024-09-18 00:54:20 -04:00
|
|
|
writer.write_all(&[1])?;
|
|
|
|
|
writer.write_all(&chain_id.as_le_bytes())?;
|
|
|
|
|
writer.write_all(&nonce.to_le_bytes())?;
|
2024-09-20 00:12:54 -04:00
|
|
|
coin.write(writer)?;
|
|
|
|
|
writer.write_all(&fee_per_gas.as_le_bytes())?;
|
2024-09-18 00:54:20 -04:00
|
|
|
writer.write_all(&u32::try_from(outs.len()).unwrap().to_le_bytes())?;
|
2024-09-20 00:12:54 -04:00
|
|
|
for (address, amount) in outs {
|
2024-09-18 00:54:20 -04:00
|
|
|
borsh::BorshSerialize::serialize(address, writer)?;
|
|
|
|
|
writer.write_all(&amount.as_le_bytes())?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn id(&self) -> [u8; 32] {
|
|
|
|
|
let mut res = [0; 32];
|
2024-09-18 00:54:20 -04:00
|
|
|
res[.. 8].copy_from_slice(&self.nonce().to_le_bytes());
|
2024-09-14 07:54:18 -04:00
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sign(self, keys: ThresholdKeys<Self::Ciphersuite>) -> Self::PreprocessMachine {
|
2024-09-18 01:09:42 -04:00
|
|
|
ClonableTransctionMachine { keys, action: self }
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl primitives::Eventuality for Eventuality {
|
|
|
|
|
type OutputId = OutputId;
|
|
|
|
|
|
|
|
|
|
fn id(&self) -> [u8; 32] {
|
|
|
|
|
let mut res = [0; 32];
|
2024-09-18 00:54:20 -04:00
|
|
|
res[.. 8].copy_from_slice(&self.0.nonce().to_le_bytes());
|
2024-09-14 07:54:18 -04:00
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn lookup(&self) -> Vec<u8> {
|
2024-09-18 00:54:20 -04:00
|
|
|
self.0.nonce().to_le_bytes().to_vec()
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn singular_spent_output(&self) -> Option<Self::OutputId> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read(reader: &mut impl io::Read) -> io::Result<Self> {
|
2024-09-18 00:54:20 -04:00
|
|
|
Executed::read(reader).map(Self)
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
|
2024-09-18 00:54:20 -04:00
|
|
|
self.0.write(writer)
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
}
|