2022-06-09 06:48:53 +00:00
|
|
|
use std::{marker::Send, sync::Arc, collections::HashMap};
|
2022-05-28 23:56:59 +00:00
|
|
|
|
2022-05-26 08:36:19 +00:00
|
|
|
use async_trait::async_trait;
|
|
|
|
use thiserror::Error;
|
2022-05-28 23:56:59 +00:00
|
|
|
|
2022-06-10 13:36:07 +00:00
|
|
|
use frost::{Curve, FrostError, MultisigKeys, sign::StateMachine};
|
2022-05-26 08:36:19 +00:00
|
|
|
|
2022-06-05 19:10:50 +00:00
|
|
|
pub(crate) use monero_serai::frost::Transcript;
|
|
|
|
|
2022-05-28 23:56:59 +00:00
|
|
|
mod coins;
|
|
|
|
mod wallet;
|
2022-05-26 08:36:19 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2022-06-09 06:48:53 +00:00
|
|
|
#[derive(Clone, Error, Debug)]
|
|
|
|
pub enum CoinError {
|
|
|
|
#[error("failed to connect to coin daemon")]
|
|
|
|
ConnectionError
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Error, Debug)]
|
|
|
|
pub enum NetworkError {}
|
|
|
|
|
|
|
|
#[derive(Clone, Error, Debug)]
|
|
|
|
pub enum SignError {
|
|
|
|
#[error("coin had an error {0}")]
|
|
|
|
CoinError(CoinError),
|
|
|
|
#[error("network had an error {0}")]
|
|
|
|
NetworkError(NetworkError),
|
|
|
|
#[error("FROST had an error {0}")]
|
|
|
|
FrostError(FrostError)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait Network: Send {
|
|
|
|
async fn round(&mut self, data: Vec<u8>) -> Result<HashMap<u16, Vec<u8>>, NetworkError>;
|
|
|
|
}
|
|
|
|
|
2022-06-04 02:46:48 +00:00
|
|
|
pub trait Output: Sized + Clone {
|
2022-06-05 10:00:21 +00:00
|
|
|
type Id: AsRef<[u8]>;
|
2022-05-26 08:36:19 +00:00
|
|
|
|
|
|
|
fn id(&self) -> Self::Id;
|
|
|
|
fn amount(&self) -> u64;
|
|
|
|
|
|
|
|
fn serialize(&self) -> Vec<u8>;
|
|
|
|
fn deserialize<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
2022-06-01 07:30:57 +00:00
|
|
|
pub trait Coin {
|
2022-05-28 23:56:59 +00:00
|
|
|
type Curve: Curve;
|
|
|
|
|
2022-06-19 16:19:32 +00:00
|
|
|
type Fee: Copy;
|
2022-06-10 13:36:07 +00:00
|
|
|
type Transaction;
|
2022-06-01 07:30:57 +00:00
|
|
|
type Block;
|
2022-06-10 13:36:07 +00:00
|
|
|
|
|
|
|
type Output: Output;
|
2022-05-28 23:56:59 +00:00
|
|
|
type SignableTransaction;
|
2022-06-10 13:36:07 +00:00
|
|
|
type TransactionMachine: StateMachine<Signature = Self::Transaction>;
|
2022-05-28 23:56:59 +00:00
|
|
|
|
|
|
|
type Address: Send;
|
2022-05-26 08:36:19 +00:00
|
|
|
|
2022-06-04 03:22:08 +00:00
|
|
|
const ID: &'static [u8];
|
|
|
|
const CONFIRMATIONS: usize;
|
|
|
|
const MAX_INPUTS: usize;
|
2022-06-09 06:48:53 +00:00
|
|
|
const MAX_OUTPUTS: usize; // TODO: Decide if this includes change or not
|
|
|
|
|
|
|
|
// Doesn't have to take self, enables some level of caching which is pleasant
|
|
|
|
fn address(&self, key: <Self::Curve as Curve>::G) -> Self::Address;
|
2022-05-26 08:36:19 +00:00
|
|
|
|
|
|
|
async fn get_height(&self) -> Result<usize, CoinError>;
|
2022-06-01 07:30:57 +00:00
|
|
|
async fn get_block(&self, height: usize) -> Result<Self::Block, CoinError>;
|
|
|
|
async fn get_outputs(
|
2022-05-26 08:36:19 +00:00
|
|
|
&self,
|
2022-06-01 07:30:57 +00:00
|
|
|
block: &Self::Block,
|
2022-05-28 23:56:59 +00:00
|
|
|
key: <Self::Curve as Curve>::G
|
2022-06-01 07:30:57 +00:00
|
|
|
) -> Vec<Self::Output>;
|
2022-05-28 23:56:59 +00:00
|
|
|
|
2022-06-04 02:46:48 +00:00
|
|
|
async fn prepare_send(
|
2022-05-28 23:56:59 +00:00
|
|
|
&self,
|
2022-06-04 02:46:48 +00:00
|
|
|
keys: Arc<MultisigKeys<Self::Curve>>,
|
2022-06-05 19:10:50 +00:00
|
|
|
transcript: Transcript,
|
2022-05-28 23:56:59 +00:00
|
|
|
height: usize,
|
|
|
|
inputs: Vec<Self::Output>,
|
2022-06-19 16:19:32 +00:00
|
|
|
payments: &[(Self::Address, u64)],
|
|
|
|
fee: Self::Fee
|
2022-05-28 23:56:59 +00:00
|
|
|
) -> Result<Self::SignableTransaction, CoinError>;
|
|
|
|
|
2022-06-10 13:36:07 +00:00
|
|
|
async fn attempt_send(
|
2022-05-28 23:56:59 +00:00
|
|
|
&self,
|
|
|
|
transaction: Self::SignableTransaction,
|
|
|
|
included: &[u16]
|
2022-06-10 13:36:07 +00:00
|
|
|
) -> Result<Self::TransactionMachine, CoinError>;
|
|
|
|
|
|
|
|
async fn publish_transaction(
|
|
|
|
&self,
|
|
|
|
tx: &Self::Transaction
|
|
|
|
) -> Result<(Vec<u8>, Vec<<Self::Output as Output>::Id>), CoinError>;
|
2022-06-09 06:48:53 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
async fn mine_block(&self, address: Self::Address);
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
async fn test_send(&self, key: Self::Address);
|
2022-05-28 23:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 03:22:08 +00:00
|
|
|
// Generate a static view key for a given chain in a globally consistent manner
|
|
|
|
// Doesn't consider the current group key to increase the simplicity of verifying Serai's status
|
2022-05-28 23:56:59 +00:00
|
|
|
// Takes an index, k, for more modern privacy protocols which use multiple view keys
|
2022-06-04 03:22:08 +00:00
|
|
|
pub fn view_key<C: Coin>(k: u64) -> <C::Curve as Curve>::F {
|
|
|
|
C::Curve::hash_to_F(b"Serai DEX View Key", &[C::ID, &k.to_le_bytes()].concat())
|
2022-05-26 08:36:19 +00:00
|
|
|
}
|