mirror of
https://github.com/serai-dex/serai.git
synced 2024-12-25 13:09:30 +00:00
Move the Ethereum Action machine to its own file
This commit is contained in:
parent
18178f3764
commit
98c3f75fa2
6 changed files with 163 additions and 158 deletions
5
Cargo.lock
generated
5
Cargo.lock
generated
|
@ -11890,8 +11890,3 @@ dependencies = [
|
||||||
"cc",
|
"cc",
|
||||||
"pkg-config",
|
"pkg-config",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[patch.unused]]
|
|
||||||
name = "alloy-sol-type-parser"
|
|
||||||
version = "0.8.0"
|
|
||||||
source = "git+https://github.com/alloy-rs/core?rev=446b9d2fbce12b88456152170709a3eaac929af0#446b9d2fbce12b88456152170709a3eaac929af0"
|
|
||||||
|
|
|
@ -204,9 +204,6 @@ directories-next = { path = "patches/directories-next" }
|
||||||
# The official pasta_curves repo doesn't support Zeroize
|
# The official pasta_curves repo doesn't support Zeroize
|
||||||
pasta_curves = { git = "https://github.com/kayabaNerve/pasta_curves", rev = "a46b5be95cacbff54d06aad8d3bbcba42e05d616" }
|
pasta_curves = { git = "https://github.com/kayabaNerve/pasta_curves", rev = "a46b5be95cacbff54d06aad8d3bbcba42e05d616" }
|
||||||
|
|
||||||
# https://github.com/alloy-rs/core/issues/717
|
|
||||||
alloy-sol-type-parser = { git = "https://github.com/alloy-rs/core", rev = "446b9d2fbce12b88456152170709a3eaac929af0" }
|
|
||||||
|
|
||||||
[workspace.lints.clippy]
|
[workspace.lints.clippy]
|
||||||
unwrap_or_default = "allow"
|
unwrap_or_default = "allow"
|
||||||
borrow_as_ptr = "deny"
|
borrow_as_ptr = "deny"
|
||||||
|
|
146
processor/ethereum/src/primitives/machine.rs
Normal file
146
processor/ethereum/src/primitives/machine.rs
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
use std::{io, collections::HashMap};
|
||||||
|
|
||||||
|
use rand_core::{RngCore, CryptoRng};
|
||||||
|
|
||||||
|
use ciphersuite::{Ciphersuite, Secp256k1};
|
||||||
|
use frost::{
|
||||||
|
dkg::{Participant, ThresholdKeys},
|
||||||
|
FrostError,
|
||||||
|
algorithm::*,
|
||||||
|
sign::*,
|
||||||
|
};
|
||||||
|
|
||||||
|
use ethereum_schnorr::{PublicKey, Signature};
|
||||||
|
|
||||||
|
use crate::transaction::{Action, Transaction};
|
||||||
|
|
||||||
|
/// The HRAm to use for the Schnorr Solidity library.
|
||||||
|
///
|
||||||
|
/// This will panic if the public key being signed for is not representable within the Schnorr
|
||||||
|
/// Solidity library.
|
||||||
|
#[derive(Clone, Default, Debug)]
|
||||||
|
pub struct EthereumHram;
|
||||||
|
impl Hram<Secp256k1> for EthereumHram {
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
fn hram(
|
||||||
|
R: &<Secp256k1 as Ciphersuite>::G,
|
||||||
|
A: &<Secp256k1 as Ciphersuite>::G,
|
||||||
|
m: &[u8],
|
||||||
|
) -> <Secp256k1 as Ciphersuite>::F {
|
||||||
|
Signature::challenge(*R, &PublicKey::new(*A).unwrap(), m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A clonable machine to sign an action.
|
||||||
|
///
|
||||||
|
/// This will panic if the public key being signed with is not representable within the Schnorr
|
||||||
|
/// Solidity library.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub(crate) struct ClonableTransctionMachine {
|
||||||
|
pub(crate) keys: ThresholdKeys<Secp256k1>,
|
||||||
|
pub(crate) action: Action,
|
||||||
|
}
|
||||||
|
|
||||||
|
type LiteralAlgorithmMachine = AlgorithmMachine<Secp256k1, IetfSchnorr<Secp256k1, EthereumHram>>;
|
||||||
|
type LiteralAlgorithmSignMachine =
|
||||||
|
AlgorithmSignMachine<Secp256k1, IetfSchnorr<Secp256k1, EthereumHram>>;
|
||||||
|
|
||||||
|
pub(crate) struct ActionSignMachine {
|
||||||
|
key: PublicKey,
|
||||||
|
action: Action,
|
||||||
|
machine: LiteralAlgorithmSignMachine,
|
||||||
|
}
|
||||||
|
|
||||||
|
type LiteralAlgorithmSignatureMachine =
|
||||||
|
AlgorithmSignatureMachine<Secp256k1, IetfSchnorr<Secp256k1, EthereumHram>>;
|
||||||
|
|
||||||
|
pub(crate) struct ActionSignatureMachine {
|
||||||
|
key: PublicKey,
|
||||||
|
action: Action,
|
||||||
|
machine: LiteralAlgorithmSignatureMachine,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PreprocessMachine for ClonableTransctionMachine {
|
||||||
|
type Preprocess = <LiteralAlgorithmMachine as PreprocessMachine>::Preprocess;
|
||||||
|
type Signature = Transaction;
|
||||||
|
type SignMachine = ActionSignMachine;
|
||||||
|
|
||||||
|
fn preprocess<R: RngCore + CryptoRng>(
|
||||||
|
self,
|
||||||
|
rng: &mut R,
|
||||||
|
) -> (Self::SignMachine, Self::Preprocess) {
|
||||||
|
let (machine, preprocess) =
|
||||||
|
AlgorithmMachine::new(IetfSchnorr::<Secp256k1, EthereumHram>::ietf(), self.keys.clone())
|
||||||
|
.preprocess(rng);
|
||||||
|
(
|
||||||
|
ActionSignMachine {
|
||||||
|
key: PublicKey::new(self.keys.group_key()).expect("signing with non-representable key"),
|
||||||
|
action: self.action,
|
||||||
|
machine,
|
||||||
|
},
|
||||||
|
preprocess,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SignMachine<Transaction> for ActionSignMachine {
|
||||||
|
type Params = <LiteralAlgorithmSignMachine as SignMachine<
|
||||||
|
<LiteralAlgorithmMachine as PreprocessMachine>::Signature,
|
||||||
|
>>::Params;
|
||||||
|
type Keys = <LiteralAlgorithmSignMachine as SignMachine<
|
||||||
|
<LiteralAlgorithmMachine as PreprocessMachine>::Signature,
|
||||||
|
>>::Keys;
|
||||||
|
type Preprocess = <LiteralAlgorithmSignMachine as SignMachine<
|
||||||
|
<LiteralAlgorithmMachine as PreprocessMachine>::Signature,
|
||||||
|
>>::Preprocess;
|
||||||
|
type SignatureShare = <LiteralAlgorithmSignMachine as SignMachine<
|
||||||
|
<LiteralAlgorithmMachine as PreprocessMachine>::Signature,
|
||||||
|
>>::SignatureShare;
|
||||||
|
type SignatureMachine = ActionSignatureMachine;
|
||||||
|
|
||||||
|
fn cache(self) -> CachedPreprocess {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
fn from_cache(
|
||||||
|
params: Self::Params,
|
||||||
|
keys: Self::Keys,
|
||||||
|
cache: CachedPreprocess,
|
||||||
|
) -> (Self, Self::Preprocess) {
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_preprocess<R: io::Read>(&self, reader: &mut R) -> io::Result<Self::Preprocess> {
|
||||||
|
self.machine.read_preprocess(reader)
|
||||||
|
}
|
||||||
|
fn sign(
|
||||||
|
self,
|
||||||
|
commitments: HashMap<Participant, Self::Preprocess>,
|
||||||
|
msg: &[u8],
|
||||||
|
) -> Result<(Self::SignatureMachine, Self::SignatureShare), FrostError> {
|
||||||
|
assert!(msg.is_empty());
|
||||||
|
self.machine.sign(commitments, &self.action.message()).map(|(machine, shares)| {
|
||||||
|
(ActionSignatureMachine { key: self.key, action: self.action, machine }, shares)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SignatureMachine<Transaction> for ActionSignatureMachine {
|
||||||
|
type SignatureShare = <LiteralAlgorithmSignatureMachine as SignatureMachine<
|
||||||
|
<LiteralAlgorithmMachine as PreprocessMachine>::Signature,
|
||||||
|
>>::SignatureShare;
|
||||||
|
|
||||||
|
fn read_share<R: io::Read>(&self, reader: &mut R) -> io::Result<Self::SignatureShare> {
|
||||||
|
self.machine.read_share(reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn complete(
|
||||||
|
self,
|
||||||
|
shares: HashMap<Participant, Self::SignatureShare>,
|
||||||
|
) -> Result<Transaction, FrostError> {
|
||||||
|
self.machine.complete(shares).map(|signature| {
|
||||||
|
let s = signature.s;
|
||||||
|
let c = Signature::challenge(signature.R, &self.key, &self.action.message());
|
||||||
|
Transaction(self.action, Signature::new(c, s))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
pub(crate) mod output;
|
pub(crate) mod output;
|
||||||
pub(crate) mod transaction;
|
pub(crate) mod transaction;
|
||||||
|
pub(crate) mod machine;
|
||||||
pub(crate) mod block;
|
pub(crate) mod block;
|
||||||
|
|
||||||
pub(crate) const DAI: [u8; 20] =
|
pub(crate) const DAI: [u8; 20] =
|
||||||
|
|
|
@ -1,14 +1,7 @@
|
||||||
use std::{io, collections::HashMap};
|
use std::io;
|
||||||
|
|
||||||
use rand_core::{RngCore, CryptoRng};
|
use ciphersuite::Secp256k1;
|
||||||
|
use frost::dkg::ThresholdKeys;
|
||||||
use ciphersuite::{Ciphersuite, Secp256k1};
|
|
||||||
use frost::{
|
|
||||||
dkg::{Participant, ThresholdKeys},
|
|
||||||
FrostError,
|
|
||||||
algorithm::*,
|
|
||||||
sign::*,
|
|
||||||
};
|
|
||||||
|
|
||||||
use alloy_core::primitives::U256;
|
use alloy_core::primitives::U256;
|
||||||
|
|
||||||
|
@ -20,7 +13,7 @@ use ethereum_primitives::keccak256;
|
||||||
use ethereum_schnorr::{PublicKey, Signature};
|
use ethereum_schnorr::{PublicKey, Signature};
|
||||||
use ethereum_router::{Coin, OutInstructions, Executed, Router};
|
use ethereum_router::{Coin, OutInstructions, Executed, Router};
|
||||||
|
|
||||||
use crate::output::OutputId;
|
use crate::{output::OutputId, machine::ClonableTransctionMachine};
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
pub(crate) enum Action {
|
pub(crate) enum Action {
|
||||||
|
@ -32,13 +25,13 @@ pub(crate) enum Action {
|
||||||
pub(crate) struct Eventuality(pub(crate) Executed);
|
pub(crate) struct Eventuality(pub(crate) Executed);
|
||||||
|
|
||||||
impl Action {
|
impl Action {
|
||||||
fn nonce(&self) -> u64 {
|
pub(crate) fn nonce(&self) -> u64 {
|
||||||
match self {
|
match self {
|
||||||
Action::SetKey { nonce, .. } | Action::Batch { nonce, .. } => *nonce,
|
Action::SetKey { nonce, .. } | Action::Batch { nonce, .. } => *nonce,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn message(&self) -> Vec<u8> {
|
pub(crate) fn message(&self) -> Vec<u8> {
|
||||||
match self {
|
match self {
|
||||||
Action::SetKey { chain_id, nonce, key } => {
|
Action::SetKey { chain_id, nonce, key } => {
|
||||||
Router::update_serai_key_message(*chain_id, *nonce, key)
|
Router::update_serai_key_message(*chain_id, *nonce, key)
|
||||||
|
@ -67,155 +60,20 @@ impl Action {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
pub(crate) struct Transaction(Action, Signature);
|
pub(crate) struct Transaction(pub(crate) Action, pub(crate) Signature);
|
||||||
impl scheduler::Transaction for Transaction {
|
impl scheduler::Transaction for Transaction {
|
||||||
fn read(reader: &mut impl io::Read) -> io::Result<Self> {
|
fn read(reader: &mut impl io::Read) -> io::Result<Self> {
|
||||||
/*
|
|
||||||
let buf: Vec<u8> = borsh::from_reader(reader)?;
|
|
||||||
// We can only read this from a &[u8], hence prior reading into a Vec<u8>
|
|
||||||
<TxLegacy as alloy_rlp::Decodable>::decode(&mut buf.as_slice())
|
|
||||||
.map(Self)
|
|
||||||
.map_err(io::Error::other)
|
|
||||||
*/
|
|
||||||
let action = Action::read(reader)?;
|
let action = Action::read(reader)?;
|
||||||
let signature = Signature::read(reader)?;
|
let signature = Signature::read(reader)?;
|
||||||
Ok(Transaction(action, signature))
|
Ok(Transaction(action, signature))
|
||||||
}
|
}
|
||||||
fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
|
fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
|
||||||
/*
|
|
||||||
let mut buf = Vec::with_capacity(256);
|
|
||||||
<TxLegacy as alloy_rlp::Encodable>::encode(&self.0, &mut buf);
|
|
||||||
borsh::BorshSerialize::serialize(&buf, writer)
|
|
||||||
*/
|
|
||||||
self.0.write(writer)?;
|
self.0.write(writer)?;
|
||||||
self.1.write(writer)?;
|
self.1.write(writer)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The HRAm to use for the Schnorr Solidity library.
|
|
||||||
///
|
|
||||||
/// This will panic if the public key being signed for is not representable within the Schnorr
|
|
||||||
/// Solidity library.
|
|
||||||
#[derive(Clone, Default, Debug)]
|
|
||||||
pub struct EthereumHram;
|
|
||||||
impl Hram<Secp256k1> for EthereumHram {
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
fn hram(
|
|
||||||
R: &<Secp256k1 as Ciphersuite>::G,
|
|
||||||
A: &<Secp256k1 as Ciphersuite>::G,
|
|
||||||
m: &[u8],
|
|
||||||
) -> <Secp256k1 as Ciphersuite>::F {
|
|
||||||
Signature::challenge(*R, &PublicKey::new(*A).unwrap(), m)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub(crate) struct ClonableTransctionMachine(ThresholdKeys<Secp256k1>, Action);
|
|
||||||
|
|
||||||
type LiteralAlgorithmMachine = AlgorithmMachine<Secp256k1, IetfSchnorr<Secp256k1, EthereumHram>>;
|
|
||||||
type LiteralAlgorithmSignMachine =
|
|
||||||
AlgorithmSignMachine<Secp256k1, IetfSchnorr<Secp256k1, EthereumHram>>;
|
|
||||||
|
|
||||||
pub(crate) struct ActionSignMachine(PublicKey, Action, LiteralAlgorithmSignMachine);
|
|
||||||
|
|
||||||
type LiteralAlgorithmSignatureMachine =
|
|
||||||
AlgorithmSignatureMachine<Secp256k1, IetfSchnorr<Secp256k1, EthereumHram>>;
|
|
||||||
|
|
||||||
pub(crate) struct ActionSignatureMachine(PublicKey, Action, LiteralAlgorithmSignatureMachine);
|
|
||||||
|
|
||||||
impl PreprocessMachine for ClonableTransctionMachine {
|
|
||||||
type Preprocess = <LiteralAlgorithmMachine as PreprocessMachine>::Preprocess;
|
|
||||||
type Signature = Transaction;
|
|
||||||
type SignMachine = ActionSignMachine;
|
|
||||||
|
|
||||||
fn preprocess<R: RngCore + CryptoRng>(
|
|
||||||
self,
|
|
||||||
rng: &mut R,
|
|
||||||
) -> (Self::SignMachine, Self::Preprocess) {
|
|
||||||
let (machine, preprocess) =
|
|
||||||
AlgorithmMachine::new(IetfSchnorr::<Secp256k1, EthereumHram>::ietf(), self.0.clone())
|
|
||||||
.preprocess(rng);
|
|
||||||
(
|
|
||||||
ActionSignMachine(
|
|
||||||
PublicKey::new(self.0.group_key()).expect("signing with non-representable key"),
|
|
||||||
self.1,
|
|
||||||
machine,
|
|
||||||
),
|
|
||||||
preprocess,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SignMachine<Transaction> for ActionSignMachine {
|
|
||||||
type Params = <LiteralAlgorithmSignMachine as SignMachine<
|
|
||||||
<LiteralAlgorithmMachine as PreprocessMachine>::Signature,
|
|
||||||
>>::Params;
|
|
||||||
type Keys = <LiteralAlgorithmSignMachine as SignMachine<
|
|
||||||
<LiteralAlgorithmMachine as PreprocessMachine>::Signature,
|
|
||||||
>>::Keys;
|
|
||||||
type Preprocess = <LiteralAlgorithmSignMachine as SignMachine<
|
|
||||||
<LiteralAlgorithmMachine as PreprocessMachine>::Signature,
|
|
||||||
>>::Preprocess;
|
|
||||||
type SignatureShare = <LiteralAlgorithmSignMachine as SignMachine<
|
|
||||||
<LiteralAlgorithmMachine as PreprocessMachine>::Signature,
|
|
||||||
>>::SignatureShare;
|
|
||||||
type SignatureMachine = ActionSignatureMachine;
|
|
||||||
|
|
||||||
fn cache(self) -> CachedPreprocess {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
fn from_cache(
|
|
||||||
params: Self::Params,
|
|
||||||
keys: Self::Keys,
|
|
||||||
cache: CachedPreprocess,
|
|
||||||
) -> (Self, Self::Preprocess) {
|
|
||||||
unimplemented!()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_preprocess<R: io::Read>(&self, reader: &mut R) -> io::Result<Self::Preprocess> {
|
|
||||||
self.2.read_preprocess(reader)
|
|
||||||
}
|
|
||||||
fn sign(
|
|
||||||
self,
|
|
||||||
commitments: HashMap<Participant, Self::Preprocess>,
|
|
||||||
msg: &[u8],
|
|
||||||
) -> Result<(Self::SignatureMachine, Self::SignatureShare), FrostError> {
|
|
||||||
assert!(msg.is_empty());
|
|
||||||
self
|
|
||||||
.2
|
|
||||||
.sign(commitments, &self.1.message())
|
|
||||||
.map(|(machine, shares)| (ActionSignatureMachine(self.0, self.1, machine), shares))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SignatureMachine<Transaction> for ActionSignatureMachine {
|
|
||||||
type SignatureShare = <LiteralAlgorithmSignatureMachine as SignatureMachine<
|
|
||||||
<LiteralAlgorithmMachine as PreprocessMachine>::Signature,
|
|
||||||
>>::SignatureShare;
|
|
||||||
|
|
||||||
fn read_share<R: io::Read>(&self, reader: &mut R) -> io::Result<Self::SignatureShare> {
|
|
||||||
self.2.read_share(reader)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn complete(
|
|
||||||
self,
|
|
||||||
shares: HashMap<Participant, Self::SignatureShare>,
|
|
||||||
) -> Result<Transaction, FrostError> {
|
|
||||||
/*
|
|
||||||
match self.1 {
|
|
||||||
Action::SetKey { chain_id: _, nonce: _, key } => self.0.update_serai_key(key, signature),
|
|
||||||
Action::Batch { chain_id: _, nonce: _, outs } => self.0.execute(outs, signature),
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
self.2.complete(shares).map(|signature| {
|
|
||||||
let s = signature.s;
|
|
||||||
let c = Signature::challenge(signature.R, &self.0, &self.1.message());
|
|
||||||
Transaction(self.1, Signature::new(c, s))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SignableTransaction for Action {
|
impl SignableTransaction for Action {
|
||||||
type Transaction = Transaction;
|
type Transaction = Transaction;
|
||||||
type Ciphersuite = Secp256k1;
|
type Ciphersuite = Secp256k1;
|
||||||
|
@ -296,7 +154,7 @@ impl SignableTransaction for Action {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sign(self, keys: ThresholdKeys<Self::Ciphersuite>) -> Self::PreprocessMachine {
|
fn sign(self, keys: ThresholdKeys<Self::Ciphersuite>) -> Self::PreprocessMachine {
|
||||||
ClonableTransctionMachine(keys, self)
|
ClonableTransctionMachine { keys, action: self }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,14 @@ impl signers::TransactionPublisher<Transaction> for TransactionPublisher {
|
||||||
&self,
|
&self,
|
||||||
tx: Transaction,
|
tx: Transaction,
|
||||||
) -> impl Send + Future<Output = Result<(), Self::EphemeralError>> {
|
) -> impl Send + Future<Output = Result<(), Self::EphemeralError>> {
|
||||||
|
// Convert from an Action (an internal representation of a signable event) to a TxLegacy
|
||||||
|
/* TODO
|
||||||
|
match tx.0 {
|
||||||
|
Action::SetKey { chain_id: _, nonce: _, key } => self.router.update_serai_key(key, tx.1),
|
||||||
|
Action::Batch { chain_id: _, nonce: _, outs } => self.router.execute(outs, tx.1),
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
/*
|
/*
|
||||||
use tokio::{
|
use tokio::{
|
||||||
|
|
Loading…
Reference in a new issue