2022-05-26 04:36:19 -04:00
|
|
|
use async_trait::async_trait;
|
2022-05-28 19:56:59 -04:00
|
|
|
use rand_core::{RngCore, CryptoRng};
|
2022-05-26 04:36:19 -04:00
|
|
|
|
2022-06-01 03:30:57 -04:00
|
|
|
use curve25519_dalek::{scalar::Scalar, edwards::CompressedEdwardsY};
|
2022-05-28 19:56:59 -04:00
|
|
|
|
|
|
|
|
use dalek_ff_group as dfg;
|
|
|
|
|
use frost::MultisigKeys;
|
2022-05-28 05:24:17 -04:00
|
|
|
|
2022-05-26 04:36:19 -04:00
|
|
|
use monero::util::address::Address;
|
2022-06-01 03:30:57 -04:00
|
|
|
use monero_serai::{
|
|
|
|
|
frost::Ed25519,
|
2022-06-02 00:00:26 -04:00
|
|
|
transaction::{Timelock, Transaction},
|
2022-06-01 03:30:57 -04:00
|
|
|
rpc::Rpc,
|
|
|
|
|
wallet::{SpendableOutput, SignableTransaction}
|
|
|
|
|
};
|
2022-05-26 04:36:19 -04:00
|
|
|
|
2022-05-28 19:56:59 -04:00
|
|
|
use crate::{Output as OutputTrait, CoinError, Coin, view_key};
|
2022-05-26 04:36:19 -04:00
|
|
|
|
|
|
|
|
pub struct Output(SpendableOutput);
|
|
|
|
|
impl OutputTrait for Output {
|
2022-06-01 03:30:57 -04:00
|
|
|
// While we could use (tx, o), using the key ensures we won't be susceptible to the burning bug.
|
|
|
|
|
// While the Monero library offers a variant which allows senders to ensure their TXs have unique
|
|
|
|
|
// output keys, Serai can still be targeted using the classic burning bug
|
|
|
|
|
type Id = CompressedEdwardsY;
|
2022-05-26 04:36:19 -04:00
|
|
|
|
|
|
|
|
fn id(&self) -> Self::Id {
|
2022-06-01 03:30:57 -04:00
|
|
|
self.0.key.compress()
|
2022-05-26 04:36:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn amount(&self) -> u64 {
|
|
|
|
|
self.0.commitment.amount
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
|
self.0.serialize()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn deserialize<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
|
|
|
|
|
SpendableOutput::deserialize(reader).map(|o| Output(o))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 05:24:17 -04:00
|
|
|
impl From<SpendableOutput> for Output {
|
|
|
|
|
fn from(output: SpendableOutput) -> Output {
|
|
|
|
|
Output(output)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Monero {
|
|
|
|
|
rpc: Rpc,
|
2022-05-28 19:56:59 -04:00
|
|
|
view: Scalar
|
2022-05-28 05:24:17 -04:00
|
|
|
}
|
2022-05-26 04:36:19 -04:00
|
|
|
|
|
|
|
|
impl Monero {
|
|
|
|
|
pub fn new(url: String) -> Monero {
|
2022-05-28 05:24:17 -04:00
|
|
|
Monero {
|
|
|
|
|
rpc: Rpc::new(url),
|
2022-05-28 19:56:59 -04:00
|
|
|
view: dfg::Scalar::from_hash(view_key::<Monero>(0)).0
|
2022-05-28 05:24:17 -04:00
|
|
|
}
|
2022-05-26 04:36:19 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl Coin for Monero {
|
2022-05-28 19:56:59 -04:00
|
|
|
type Curve = Ed25519;
|
|
|
|
|
|
2022-05-26 04:36:19 -04:00
|
|
|
type Output = Output;
|
2022-06-01 03:30:57 -04:00
|
|
|
type Block = Vec<Transaction>;
|
2022-05-28 19:56:59 -04:00
|
|
|
type SignableTransaction = SignableTransaction;
|
|
|
|
|
|
2022-05-26 04:36:19 -04:00
|
|
|
type Address = Address;
|
|
|
|
|
|
2022-05-28 19:56:59 -04:00
|
|
|
fn id() -> &'static [u8] { b"Monero" }
|
2022-06-01 03:30:57 -04:00
|
|
|
fn confirmations() -> usize { 10 }
|
2022-05-28 05:25:00 -04:00
|
|
|
// Testnet TX bb4d188a4c571f2f0de70dca9d475abc19078c10ffa8def26dd4f63ce1bcfd79 uses 146 inputs
|
|
|
|
|
// while using less than 100kb of space, albeit with just 2 outputs (though outputs share a BP)
|
|
|
|
|
// The TX size limit is half the contextual median block weight, where said weight is >= 300,000
|
|
|
|
|
// This means any TX which fits into 150kb will be accepted by Monero
|
|
|
|
|
// 128, even with 16 outputs, should fit into 100kb. Further efficiency by 192 may be viable
|
|
|
|
|
// TODO: Get hard numbers and tune
|
2022-06-01 03:30:57 -04:00
|
|
|
fn max_inputs() -> usize { 128 }
|
|
|
|
|
fn max_outputs() -> usize { 16 }
|
2022-05-26 04:36:19 -04:00
|
|
|
|
|
|
|
|
async fn get_height(&self) -> Result<usize, CoinError> {
|
2022-05-28 05:24:17 -04:00
|
|
|
self.rpc.get_height().await.map_err(|_| CoinError::ConnectionError)
|
2022-05-26 04:36:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-01 03:30:57 -04:00
|
|
|
async fn get_block(&self, height: usize) -> Result<Self::Block, CoinError> {
|
|
|
|
|
self.rpc.get_block_transactions_possible(height).await.map_err(|_| CoinError::ConnectionError)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_outputs(&self, block: &Self::Block, key: dfg::EdwardsPoint) -> Vec<Self::Output> {
|
2022-06-02 00:00:26 -04:00
|
|
|
block
|
|
|
|
|
.iter()
|
|
|
|
|
.flat_map(|tx| {
|
|
|
|
|
let (outputs, timelock) = tx.scan(self.view, key.0);
|
|
|
|
|
if timelock == Timelock::None {
|
|
|
|
|
outputs
|
|
|
|
|
} else {
|
|
|
|
|
vec![]
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.map(Output::from)
|
|
|
|
|
.collect()
|
2022-05-26 04:36:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-28 19:56:59 -04:00
|
|
|
async fn prepare_send<R: RngCore + CryptoRng>(
|
2022-05-26 04:36:19 -04:00
|
|
|
&self,
|
2022-05-28 19:56:59 -04:00
|
|
|
_keys: MultisigKeys<Ed25519>,
|
|
|
|
|
_label: Vec<u8>,
|
|
|
|
|
_height: usize,
|
|
|
|
|
_inputs: Vec<Output>,
|
2022-05-26 04:36:19 -04:00
|
|
|
_payments: &[(Address, u64)]
|
2022-05-28 19:56:59 -04:00
|
|
|
) -> Result<SignableTransaction, CoinError> {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn attempt_send<R: RngCore + CryptoRng + std::marker::Send>(
|
|
|
|
|
&self,
|
|
|
|
|
_rng: &mut R,
|
|
|
|
|
_transaction: SignableTransaction,
|
|
|
|
|
_included: &[u16]
|
|
|
|
|
) -> Result<(Vec<u8>, Vec<<Self::Output as OutputTrait>::Id>), CoinError> {
|
2022-05-26 04:36:19 -04:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
}
|