mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-10 13:09:24 +00:00
Monero processor primitives
This commit is contained in:
54
processor/monero/src/primitives/block.rs
Normal file
54
processor/monero/src/primitives/block.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use ciphersuite::{Ciphersuite, Ed25519};
|
||||
|
||||
use monero_wallet::{transaction::Transaction, block::Block as MBlock};
|
||||
|
||||
use serai_client::networks::monero::Address;
|
||||
|
||||
use primitives::{ReceivedOutput, EventualityTracker};
|
||||
|
||||
use crate::{output::Output, transaction::Eventuality};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct BlockHeader(pub(crate) MBlock);
|
||||
impl primitives::BlockHeader for BlockHeader {
|
||||
fn id(&self) -> [u8; 32] {
|
||||
self.0.hash()
|
||||
}
|
||||
fn parent(&self) -> [u8; 32] {
|
||||
self.0.header.previous
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Block(pub(crate) MBlock, Vec<Transaction>);
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl primitives::Block for Block {
|
||||
type Header = BlockHeader;
|
||||
|
||||
type Key = <Ed25519 as Ciphersuite>::G;
|
||||
type Address = Address;
|
||||
type Output = Output;
|
||||
type Eventuality = Eventuality;
|
||||
|
||||
fn id(&self) -> [u8; 32] {
|
||||
self.0.hash()
|
||||
}
|
||||
|
||||
fn scan_for_outputs_unordered(&self, key: Self::Key) -> Vec<Self::Output> {
|
||||
todo!("TODO")
|
||||
}
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn check_for_eventuality_resolutions(
|
||||
&self,
|
||||
eventualities: &mut EventualityTracker<Self::Eventuality>,
|
||||
) -> HashMap<
|
||||
<Self::Output as ReceivedOutput<Self::Key, Self::Address>>::TransactionId,
|
||||
Self::Eventuality,
|
||||
> {
|
||||
todo!("TODO")
|
||||
}
|
||||
}
|
||||
3
processor/monero/src/primitives/mod.rs
Normal file
3
processor/monero/src/primitives/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub(crate) mod output;
|
||||
pub(crate) mod transaction;
|
||||
pub(crate) mod block;
|
||||
86
processor/monero/src/primitives/output.rs
Normal file
86
processor/monero/src/primitives/output.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
use std::io;
|
||||
|
||||
use ciphersuite::{group::Group, Ciphersuite, Ed25519};
|
||||
|
||||
use monero_wallet::WalletOutput;
|
||||
|
||||
use scale::{Encode, Decode};
|
||||
use borsh::{BorshSerialize, BorshDeserialize};
|
||||
|
||||
use serai_client::{
|
||||
primitives::{Coin, Amount, Balance},
|
||||
networks::monero::Address,
|
||||
};
|
||||
|
||||
use primitives::{OutputType, ReceivedOutput};
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[derive(
|
||||
Clone, Copy, PartialEq, Eq, Default, Hash, Debug, Encode, Decode, BorshSerialize, BorshDeserialize,
|
||||
)]
|
||||
pub(crate) struct OutputId(pub(crate) [u8; 32]);
|
||||
impl AsRef<[u8]> for OutputId {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
impl AsMut<[u8]> for OutputId {
|
||||
fn as_mut(&mut self) -> &mut [u8] {
|
||||
self.0.as_mut()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub(crate) struct Output(WalletOutput);
|
||||
|
||||
impl Output {
|
||||
pub(crate) fn new(output: WalletOutput) -> Self {
|
||||
Self(output)
|
||||
}
|
||||
}
|
||||
|
||||
impl ReceivedOutput<<Ed25519 as Ciphersuite>::G, Address> for Output {
|
||||
type Id = OutputId;
|
||||
type TransactionId = [u8; 32];
|
||||
|
||||
fn kind(&self) -> OutputType {
|
||||
todo!("TODO")
|
||||
}
|
||||
|
||||
fn id(&self) -> Self::Id {
|
||||
OutputId(self.0.key().compress().to_bytes())
|
||||
}
|
||||
|
||||
fn transaction_id(&self) -> Self::TransactionId {
|
||||
self.0.transaction()
|
||||
}
|
||||
|
||||
fn key(&self) -> <Ed25519 as Ciphersuite>::G {
|
||||
// The spend key will be a key we generated, so it'll be in the prime-order subgroup
|
||||
// The output's key is the spend key + (key_offset * G), so it's in the prime-order subgroup if
|
||||
// the spend key is
|
||||
dalek_ff_group::EdwardsPoint(
|
||||
self.0.key() - (*<Ed25519 as Ciphersuite>::G::generator() * self.0.key_offset()),
|
||||
)
|
||||
}
|
||||
|
||||
fn presumed_origin(&self) -> Option<Address> {
|
||||
None
|
||||
}
|
||||
|
||||
fn balance(&self) -> Balance {
|
||||
Balance { coin: Coin::Monero, amount: Amount(self.0.commitment().amount) }
|
||||
}
|
||||
|
||||
fn data(&self) -> &[u8] {
|
||||
self.0.arbitrary_data().first().map_or(&[], Vec::as_slice)
|
||||
}
|
||||
|
||||
fn write<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
|
||||
self.0.write(writer)
|
||||
}
|
||||
|
||||
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> {
|
||||
WalletOutput::read(reader).map(Self)
|
||||
}
|
||||
}
|
||||
137
processor/monero/src/primitives/transaction.rs
Normal file
137
processor/monero/src/primitives/transaction.rs
Normal file
@@ -0,0 +1,137 @@
|
||||
use std::io;
|
||||
|
||||
use rand_core::{RngCore, CryptoRng};
|
||||
|
||||
use ciphersuite::Ed25519;
|
||||
use frost::{dkg::ThresholdKeys, sign::PreprocessMachine};
|
||||
|
||||
use monero_wallet::{
|
||||
transaction::Transaction as MTransaction,
|
||||
send::{
|
||||
SignableTransaction as MSignableTransaction, TransactionMachine, Eventuality as MEventuality,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::output::OutputId;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Transaction(pub(crate) MTransaction);
|
||||
|
||||
impl From<MTransaction> for Transaction {
|
||||
fn from(tx: MTransaction) -> Self {
|
||||
Self(tx)
|
||||
}
|
||||
}
|
||||
|
||||
impl scheduler::Transaction for Transaction {
|
||||
fn read(reader: &mut impl io::Read) -> io::Result<Self> {
|
||||
MTransaction::read(reader).map(Self)
|
||||
}
|
||||
fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
|
||||
self.0.write(writer)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct SignableTransaction {
|
||||
id: [u8; 32],
|
||||
signable: MSignableTransaction,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ClonableTransctionMachine(MSignableTransaction, ThresholdKeys<Ed25519>);
|
||||
impl PreprocessMachine for ClonableTransctionMachine {
|
||||
type Preprocess = <TransactionMachine as PreprocessMachine>::Preprocess;
|
||||
type Signature = <TransactionMachine as PreprocessMachine>::Signature;
|
||||
type SignMachine = <TransactionMachine as PreprocessMachine>::SignMachine;
|
||||
|
||||
fn preprocess<R: RngCore + CryptoRng>(
|
||||
self,
|
||||
rng: &mut R,
|
||||
) -> (Self::SignMachine, Self::Preprocess) {
|
||||
self.0.multisig(self.1).expect("incorrect keys used for SignableTransaction").preprocess(rng)
|
||||
}
|
||||
}
|
||||
|
||||
impl scheduler::SignableTransaction for SignableTransaction {
|
||||
type Transaction = Transaction;
|
||||
type Ciphersuite = Ed25519;
|
||||
type PreprocessMachine = ClonableTransctionMachine;
|
||||
|
||||
fn read(reader: &mut impl io::Read) -> io::Result<Self> {
|
||||
let mut id = [0; 32];
|
||||
reader.read_exact(&mut id)?;
|
||||
|
||||
let signable = MSignableTransaction::read(reader)?;
|
||||
Ok(SignableTransaction { id, signable })
|
||||
}
|
||||
fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
|
||||
writer.write_all(&self.id)?;
|
||||
self.signable.write(writer)
|
||||
}
|
||||
|
||||
fn id(&self) -> [u8; 32] {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn sign(self, keys: ThresholdKeys<Self::Ciphersuite>) -> Self::PreprocessMachine {
|
||||
ClonableTransctionMachine(self.signable, keys)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub(crate) struct Eventuality {
|
||||
id: [u8; 32],
|
||||
singular_spent_output: Option<OutputId>,
|
||||
eventuality: MEventuality,
|
||||
}
|
||||
|
||||
impl primitives::Eventuality for Eventuality {
|
||||
type OutputId = OutputId;
|
||||
|
||||
fn id(&self) -> [u8; 32] {
|
||||
self.id
|
||||
}
|
||||
|
||||
// We define the lookup as our ID since the resolving transaction only has a singular possible ID
|
||||
fn lookup(&self) -> Vec<u8> {
|
||||
self.eventuality.extra()
|
||||
}
|
||||
|
||||
fn singular_spent_output(&self) -> Option<Self::OutputId> {
|
||||
self.singular_spent_output
|
||||
}
|
||||
|
||||
fn read(reader: &mut impl io::Read) -> io::Result<Self> {
|
||||
let mut id = [0; 32];
|
||||
reader.read_exact(&mut id)?;
|
||||
|
||||
let singular_spent_output = {
|
||||
let mut singular_spent_output_opt = [0xff];
|
||||
reader.read_exact(&mut singular_spent_output_opt)?;
|
||||
assert!(singular_spent_output_opt[0] <= 1);
|
||||
(singular_spent_output_opt[0] == 1)
|
||||
.then(|| -> io::Result<_> {
|
||||
let mut singular_spent_output = [0; 32];
|
||||
reader.read_exact(&mut singular_spent_output)?;
|
||||
Ok(OutputId(singular_spent_output))
|
||||
})
|
||||
.transpose()?
|
||||
};
|
||||
|
||||
let eventuality = MEventuality::read(reader)?;
|
||||
Ok(Self { id, singular_spent_output, eventuality })
|
||||
}
|
||||
fn write(&self, writer: &mut impl io::Write) -> io::Result<()> {
|
||||
writer.write_all(&self.id)?;
|
||||
|
||||
if let Some(singular_spent_output) = self.singular_spent_output {
|
||||
writer.write_all(&[1])?;
|
||||
writer.write_all(singular_spent_output.as_ref())?;
|
||||
} else {
|
||||
writer.write_all(&[0])?;
|
||||
}
|
||||
|
||||
self.eventuality.write(writer)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user