2022-06-03 22:46:48 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2022-05-26 04:36:19 -04:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
2022-06-28 00:01:20 -04:00
|
|
|
use curve25519_dalek::scalar::Scalar;
|
2022-05-28 19:56:59 -04:00
|
|
|
|
|
|
|
|
use dalek_ff_group as dfg;
|
2022-06-24 18:58:24 -04:00
|
|
|
use transcript::RecommendedTranscript;
|
2022-06-28 00:06:12 -04:00
|
|
|
use frost::{curve::Ed25519, FrostKeys};
|
2022-05-28 05:24:17 -04:00
|
|
|
|
2022-06-01 03:30:57 -04:00
|
|
|
use monero_serai::{
|
2022-07-09 18:53:52 -04:00
|
|
|
transaction::Transaction,
|
2022-06-01 03:30:57 -04:00
|
|
|
rpc::Rpc,
|
2022-06-28 00:01:20 -04:00
|
|
|
wallet::{
|
2022-07-15 01:26:07 -04:00
|
|
|
ViewPair,
|
|
|
|
|
address::{Network, AddressType, Address},
|
|
|
|
|
Fee, SpendableOutput, SignableTransaction as MSignableTransaction, TransactionMachine,
|
|
|
|
|
},
|
2022-06-01 03:30:57 -04:00
|
|
|
};
|
2022-05-26 04:36:19 -04:00
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
use crate::{
|
|
|
|
|
coin::{CoinError, Output as OutputTrait, Coin},
|
|
|
|
|
view_key,
|
|
|
|
|
};
|
2022-05-26 04:36:19 -04:00
|
|
|
|
2022-06-09 02:48:53 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2022-05-26 04:36:19 -04:00
|
|
|
pub struct Output(SpendableOutput);
|
2022-07-22 02:34:36 -04:00
|
|
|
impl From<SpendableOutput> for Output {
|
|
|
|
|
fn from(output: SpendableOutput) -> Output {
|
|
|
|
|
Output(output)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 04:36:19 -04:00
|
|
|
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
|
2022-06-05 06:00:21 -04:00
|
|
|
type Id = [u8; 32];
|
2022-05-26 04:36:19 -04:00
|
|
|
|
|
|
|
|
fn id(&self) -> Self::Id {
|
2022-06-05 06:00:21 -04:00
|
|
|
self.0.key.compress().to_bytes()
|
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> {
|
2022-07-22 02:34:36 -04:00
|
|
|
SpendableOutput::deserialize(reader).map(Output)
|
2022-05-28 05:24:17 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-09 02:48:53 -04:00
|
|
|
#[derive(Debug)]
|
2022-06-05 15:10:50 -04:00
|
|
|
pub struct SignableTransaction(
|
2022-06-28 00:06:12 -04:00
|
|
|
Arc<FrostKeys<Ed25519>>,
|
2022-06-24 18:58:24 -04:00
|
|
|
RecommendedTranscript,
|
2022-06-05 15:10:50 -04:00
|
|
|
usize,
|
2022-07-15 01:26:07 -04:00
|
|
|
MSignableTransaction,
|
2022-06-05 15:10:50 -04:00
|
|
|
);
|
|
|
|
|
|
2022-06-09 02:48:53 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2022-05-28 05:24:17 -04:00
|
|
|
pub struct Monero {
|
2022-06-19 12:19:32 -04:00
|
|
|
pub(crate) rpc: Rpc,
|
2022-07-15 01:26:07 -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-06-05 15:10:50 -04:00
|
|
|
let view = view_key::<Monero>(0).0;
|
2022-06-28 00:01:20 -04:00
|
|
|
Monero { rpc: Rpc::new(url), view }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn view_pair(&self, spend: dfg::EdwardsPoint) -> ViewPair {
|
|
|
|
|
ViewPair { spend: spend.0, view: self.view }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
fn empty_view_pair(&self) -> ViewPair {
|
|
|
|
|
use group::Group;
|
|
|
|
|
self.view_pair(dfg::EdwardsPoint::generator())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
fn empty_address(&self) -> Address {
|
|
|
|
|
self.empty_view_pair().address(Network::Mainnet, AddressType::Standard, false)
|
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-06-19 12:19:32 -04:00
|
|
|
type Fee = Fee;
|
2022-06-10 09:36:07 -04:00
|
|
|
type Transaction = Transaction;
|
2022-06-01 03:30:57 -04:00
|
|
|
type Block = Vec<Transaction>;
|
2022-06-10 09:36:07 -04:00
|
|
|
|
|
|
|
|
type Output = Output;
|
2022-05-28 19:56:59 -04:00
|
|
|
type SignableTransaction = SignableTransaction;
|
2022-06-10 09:36:07 -04:00
|
|
|
type TransactionMachine = TransactionMachine;
|
2022-05-28 19:56:59 -04:00
|
|
|
|
2022-05-26 04:36:19 -04:00
|
|
|
type Address = Address;
|
|
|
|
|
|
2022-06-03 23:22:08 -04:00
|
|
|
const ID: &'static [u8] = b"Monero";
|
|
|
|
|
const 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-03 23:22:08 -04:00
|
|
|
const MAX_INPUTS: usize = 128;
|
|
|
|
|
const MAX_OUTPUTS: usize = 16;
|
2022-05-26 04:36:19 -04:00
|
|
|
|
2022-06-09 02:48:53 -04:00
|
|
|
fn address(&self, key: dfg::EdwardsPoint) -> Self::Address {
|
2022-06-28 00:01:20 -04:00
|
|
|
self.view_pair(key).address(Network::Mainnet, AddressType::Standard, true)
|
2022-06-09 02:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
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()
|
2022-07-09 18:53:52 -04:00
|
|
|
.flat_map(|tx| tx.scan(self.view_pair(key), true).not_locked())
|
2022-06-02 00:00:26 -04:00
|
|
|
.map(Output::from)
|
|
|
|
|
.collect()
|
2022-05-26 04:36:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-03 22:46:48 -04:00
|
|
|
async fn prepare_send(
|
2022-05-26 04:36:19 -04:00
|
|
|
&self,
|
2022-06-28 00:06:12 -04:00
|
|
|
keys: Arc<FrostKeys<Ed25519>>,
|
2022-06-24 18:58:24 -04:00
|
|
|
transcript: RecommendedTranscript,
|
2022-06-05 15:10:50 -04:00
|
|
|
height: usize,
|
|
|
|
|
mut inputs: Vec<Output>,
|
2022-06-19 12:19:32 -04:00
|
|
|
payments: &[(Address, u64)],
|
2022-07-15 01:26:07 -04:00
|
|
|
fee: Fee,
|
2022-05-28 19:56:59 -04:00
|
|
|
) -> Result<SignableTransaction, CoinError> {
|
2022-06-09 02:48:53 -04:00
|
|
|
let spend = keys.group_key();
|
2022-07-15 01:26:07 -04:00
|
|
|
Ok(SignableTransaction(
|
|
|
|
|
keys,
|
|
|
|
|
transcript,
|
|
|
|
|
height,
|
|
|
|
|
MSignableTransaction::new(
|
2022-07-27 04:05:43 -05:00
|
|
|
self.rpc.get_protocol().await.unwrap(), // TODO: Make this deterministic
|
2022-07-15 01:26:07 -04:00
|
|
|
inputs.drain(..).map(|input| input.0).collect(),
|
|
|
|
|
payments.to_vec(),
|
|
|
|
|
Some(self.address(spend)),
|
|
|
|
|
fee,
|
2022-06-05 15:10:50 -04:00
|
|
|
)
|
2022-07-15 01:26:07 -04:00
|
|
|
.map_err(|_| CoinError::ConnectionError)?,
|
|
|
|
|
))
|
2022-05-28 19:56:59 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-10 09:36:07 -04:00
|
|
|
async fn attempt_send(
|
2022-05-28 19:56:59 -04:00
|
|
|
&self,
|
2022-06-05 15:10:50 -04:00
|
|
|
transaction: SignableTransaction,
|
2022-07-15 01:26:07 -04:00
|
|
|
included: &[u16],
|
2022-06-10 09:36:07 -04:00
|
|
|
) -> Result<Self::TransactionMachine, CoinError> {
|
2022-07-15 01:26:07 -04:00
|
|
|
transaction
|
|
|
|
|
.3
|
|
|
|
|
.clone()
|
|
|
|
|
.multisig(
|
|
|
|
|
&self.rpc,
|
|
|
|
|
(*transaction.0).clone(),
|
|
|
|
|
transaction.1.clone(),
|
|
|
|
|
transaction.2,
|
|
|
|
|
included.to_vec(),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|_| CoinError::ConnectionError)
|
2022-06-10 09:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn publish_transaction(
|
|
|
|
|
&self,
|
2022-07-15 01:26:07 -04:00
|
|
|
tx: &Self::Transaction,
|
2022-06-10 09:36:07 -04:00
|
|
|
) -> Result<(Vec<u8>, Vec<<Self::Output as OutputTrait>::Id>), CoinError> {
|
2022-07-22 02:34:36 -04:00
|
|
|
self.rpc.publish_transaction(tx).await.map_err(|_| CoinError::ConnectionError)?;
|
2022-06-09 02:48:53 -04:00
|
|
|
|
|
|
|
|
Ok((
|
2022-06-05 15:10:50 -04:00
|
|
|
tx.hash().to_vec(),
|
2022-07-15 01:26:07 -04:00
|
|
|
tx.prefix.outputs.iter().map(|output| output.key.compress().to_bytes()).collect(),
|
2022-06-09 02:48:53 -04:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2022-06-28 00:01:20 -04:00
|
|
|
async fn mine_block(&self) {
|
2022-06-09 02:48:53 -04:00
|
|
|
#[derive(serde::Deserialize, Debug)]
|
|
|
|
|
struct EmptyResponse {}
|
2022-07-15 01:26:07 -04:00
|
|
|
let _: EmptyResponse = self
|
|
|
|
|
.rpc
|
|
|
|
|
.rpc_call(
|
|
|
|
|
"json_rpc",
|
|
|
|
|
Some(serde_json::json!({
|
|
|
|
|
"method": "generateblocks",
|
|
|
|
|
"params": {
|
|
|
|
|
"wallet_address": self.empty_address().to_string(),
|
|
|
|
|
"amount_of_blocks": 10
|
|
|
|
|
},
|
|
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
2022-06-09 02:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
async fn test_send(&self, address: Self::Address) {
|
|
|
|
|
use rand::rngs::OsRng;
|
|
|
|
|
|
|
|
|
|
let height = self.get_height().await.unwrap();
|
|
|
|
|
|
2022-06-28 00:01:20 -04:00
|
|
|
self.mine_block().await;
|
2022-06-09 02:48:53 -04:00
|
|
|
for _ in 0 .. 7 {
|
2022-06-28 00:01:20 -04:00
|
|
|
self.mine_block().await;
|
2022-06-09 02:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
let outputs = self
|
|
|
|
|
.rpc
|
|
|
|
|
.get_block_transactions_possible(height)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap()
|
|
|
|
|
.swap_remove(0)
|
|
|
|
|
.scan(self.empty_view_pair(), false)
|
|
|
|
|
.ignore_timelock();
|
2022-06-09 02:48:53 -04:00
|
|
|
|
|
|
|
|
let amount = outputs[0].commitment.amount;
|
2022-07-27 04:05:43 -05:00
|
|
|
let fee = 3000000000; // TODO
|
2022-06-09 02:48:53 -04:00
|
|
|
let tx = MSignableTransaction::new(
|
2022-07-27 04:05:43 -05:00
|
|
|
self.rpc.get_protocol().await.unwrap(),
|
2022-06-09 02:48:53 -04:00
|
|
|
outputs,
|
|
|
|
|
vec![(address, amount - fee)],
|
2022-06-28 00:01:20 -04:00
|
|
|
Some(self.empty_address()),
|
2022-07-15 01:26:07 -04:00
|
|
|
self.rpc.get_fee().await.unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.sign(&mut OsRng, &self.rpc, &Scalar::one())
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
2022-06-09 02:48:53 -04:00
|
|
|
self.rpc.publish_transaction(&tx).await.unwrap();
|
2022-06-28 00:01:20 -04:00
|
|
|
self.mine_block().await;
|
2022-05-26 04:36:19 -04:00
|
|
|
}
|
|
|
|
|
}
|