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-08-22 12:15:14 -04:00
|
|
|
block::Block,
|
2022-06-01 03:30:57 -04:00
|
|
|
rpc::Rpc,
|
2022-06-28 00:01:20 -04:00
|
|
|
wallet::{
|
2022-08-22 08:32:09 -04:00
|
|
|
ViewPair, Scanner,
|
|
|
|
|
address::{Network, Address},
|
2022-07-15 01:26:07 -04:00
|
|
|
Fee, SpendableOutput, SignableTransaction as MSignableTransaction, TransactionMachine,
|
|
|
|
|
},
|
2022-06-01 03:30:57 -04:00
|
|
|
};
|
2022-05-26 04:36:19 -04:00
|
|
|
|
2022-10-16 13:11:32 -04:00
|
|
|
use crate::{
|
|
|
|
|
additional_key,
|
|
|
|
|
coin::{CoinError, Output as OutputTrait, Coin},
|
|
|
|
|
};
|
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-08-22 12:15:14 -04:00
|
|
|
self.0.output.data.key.compress().to_bytes()
|
2022-05-26 04:36:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn amount(&self) -> u64 {
|
2022-08-22 12:15:14 -04:00
|
|
|
self.0.commitment().amount
|
2022-05-26 04:36:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-10-15 21:39:06 -04:00
|
|
|
pub struct SignableTransaction {
|
|
|
|
|
keys: FrostKeys<Ed25519>,
|
|
|
|
|
transcript: RecommendedTranscript,
|
|
|
|
|
// Monero height, defined as the length of the chain
|
|
|
|
|
height: usize,
|
|
|
|
|
actual: 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 {
|
2022-08-02 15:52:27 -04:00
|
|
|
pub async fn new(url: String) -> Monero {
|
2022-10-15 23:21:43 -04:00
|
|
|
Monero { rpc: Rpc::new(url), view: additional_key::<Monero>(0).0 }
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
2022-08-22 08:32:09 -04:00
|
|
|
fn scanner(&self, spend: dfg::EdwardsPoint) -> Scanner {
|
2022-08-22 08:57:36 -04:00
|
|
|
Scanner::from_view(ViewPair::new(spend.0, self.view), Network::Mainnet, None)
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-16 13:11:32 -04:00
|
|
|
#[cfg(test)]
|
2022-08-22 08:32:09 -04:00
|
|
|
fn empty_scanner() -> Scanner {
|
2022-06-28 00:01:20 -04:00
|
|
|
use group::Group;
|
2022-08-22 08:32:09 -04:00
|
|
|
Scanner::from_view(
|
|
|
|
|
ViewPair::new(*dfg::EdwardsPoint::generator(), Scalar::one()),
|
|
|
|
|
Network::Mainnet,
|
2022-08-22 08:57:36 -04:00
|
|
|
Some(std::collections::HashSet::new()),
|
2022-08-22 08:32:09 -04:00
|
|
|
)
|
2022-06-28 00:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-16 13:11:32 -04:00
|
|
|
#[cfg(test)]
|
2022-08-22 08:32:09 -04:00
|
|
|
fn empty_address() -> Address {
|
|
|
|
|
Self::empty_scanner().address()
|
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-08-22 12:15:14 -04:00
|
|
|
type Block = Block;
|
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-08-22 08:32:09 -04:00
|
|
|
self.scanner(key).address()
|
2022-06-09 02:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-15 21:39:06 -04:00
|
|
|
async fn get_latest_block_number(&self) -> Result<usize, CoinError> {
|
|
|
|
|
// Monero defines height as chain length, so subtract 1 for block number
|
|
|
|
|
Ok(self.rpc.get_height().await.map_err(|_| CoinError::ConnectionError)? - 1)
|
2022-05-26 04:36:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-15 21:39:06 -04:00
|
|
|
async fn get_block(&self, number: usize) -> Result<Self::Block, CoinError> {
|
|
|
|
|
self.rpc.get_block(number).await.map_err(|_| CoinError::ConnectionError)
|
2022-06-01 03:30:57 -04:00
|
|
|
}
|
|
|
|
|
|
2022-08-22 12:15:14 -04:00
|
|
|
async fn get_outputs(
|
|
|
|
|
&self,
|
|
|
|
|
block: &Self::Block,
|
|
|
|
|
key: dfg::EdwardsPoint,
|
|
|
|
|
) -> Result<Vec<Self::Output>, CoinError> {
|
|
|
|
|
Ok(
|
|
|
|
|
self
|
|
|
|
|
.scanner(key)
|
|
|
|
|
.scan(&self.rpc, block)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|_| CoinError::ConnectionError)?
|
|
|
|
|
.iter()
|
|
|
|
|
.flat_map(|outputs| outputs.not_locked())
|
|
|
|
|
.map(Output::from)
|
|
|
|
|
.collect(),
|
|
|
|
|
)
|
2022-05-26 04:36:19 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-15 21:39:06 -04:00
|
|
|
async fn is_confirmed(&self, tx: &[u8]) -> Result<bool, CoinError> {
|
|
|
|
|
let tx_block_number =
|
|
|
|
|
self.rpc.get_transaction_block_number(tx).await.map_err(|_| CoinError::ConnectionError)?;
|
|
|
|
|
Ok((self.get_latest_block_number().await?.saturating_sub(tx_block_number) + 1) >= 10)
|
2022-10-15 19:51:59 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-03 22:46:48 -04:00
|
|
|
async fn prepare_send(
|
2022-05-26 04:36:19 -04:00
|
|
|
&self,
|
Utilize zeroize (#76)
* Apply Zeroize to nonces used in Bulletproofs
Also makes bit decomposition constant time for a given amount of
outputs.
* Fix nonce reuse for single-signer CLSAG
* Attach Zeroize to most structures in Monero, and ZOnDrop to anything with private data
* Zeroize private keys and nonces
* Merge prepare_outputs and prepare_transactions
* Ensure CLSAG is constant time
* Pass by borrow where needed, bug fixes
The past few commitments have been one in-progress chunk which I've
broken up as best read.
* Add Zeroize to FROST structs
Still needs to zeroize internally, yet next step. Not quite as
aggressive as Monero, partially due to the limitations of HashMaps,
partially due to less concern about metadata, yet does still delete a
few smaller items of metadata (group key, context string...).
* Remove Zeroize from most Monero multisig structs
These structs largely didn't have private data, just fields with private
data, yet those fields implemented ZeroizeOnDrop making them already
covered. While there is still traces of the transaction left in RAM,
fully purging that was never the intent.
* Use Zeroize within dleq
bitvec doesn't offer Zeroize, so a manual zeroing has been implemented.
* Use Zeroize for random_nonce
It isn't perfect, due to the inability to zeroize the digest, and due to
kp256 requiring a few transformations. It does the best it can though.
Does move the per-curve random_nonce to a provided one, which is allowed
as of https://github.com/cfrg/draft-irtf-cfrg-frost/pull/231.
* Use Zeroize on FROST keygen/signing
* Zeroize constant time multiexp.
* Correct when FROST keygen zeroizes
* Move the FROST keys Arc into FrostKeys
Reduces amount of instances in memory.
* Manually implement Debug for FrostCore to not leak the secret share
* Misc bug fixes
* clippy + multiexp test bug fixes
* Correct FROST key gen share summation
It leaked our own share for ourself.
* Fix cross-group DLEq tests
2022-08-03 03:25:18 -05:00
|
|
|
keys: FrostKeys<Ed25519>,
|
2022-06-24 18:58:24 -04:00
|
|
|
transcript: RecommendedTranscript,
|
2022-10-15 21:39:06 -04:00
|
|
|
block_number: usize,
|
2022-06-05 15:10:50 -04:00
|
|
|
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-10-15 21:39:06 -04:00
|
|
|
Ok(SignableTransaction {
|
2022-07-15 01:26:07 -04:00
|
|
|
keys,
|
|
|
|
|
transcript,
|
2022-10-15 21:39:06 -04:00
|
|
|
height: block_number + 1,
|
|
|
|
|
actual: 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)),
|
2022-08-30 15:42:23 -04:00
|
|
|
None,
|
2022-07-15 01:26:07 -04:00
|
|
|
fee,
|
2022-06-05 15:10:50 -04:00
|
|
|
)
|
2022-07-15 01:26:07 -04:00
|
|
|
.map_err(|_| CoinError::ConnectionError)?,
|
2022-10-15 21:39:06 -04:00
|
|
|
})
|
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
|
2022-10-15 21:39:06 -04:00
|
|
|
.actual
|
2022-07-15 01:26:07 -04:00
|
|
|
.clone()
|
|
|
|
|
.multisig(
|
|
|
|
|
&self.rpc,
|
2022-10-15 21:39:06 -04:00
|
|
|
transaction.keys.clone(),
|
|
|
|
|
transaction.transcript.clone(),
|
|
|
|
|
transaction.height,
|
2022-07-15 01:26:07 -04:00
|
|
|
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
|
|
|
|
2022-08-30 01:52:00 -04:00
|
|
|
Ok((tx.hash().to_vec(), tx.prefix.outputs.iter().map(|output| output.key.to_bytes()).collect()))
|
2022-06-09 02:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-16 13:11:32 -04:00
|
|
|
#[cfg(test)]
|
2022-10-15 23:21:43 -04:00
|
|
|
async fn get_fee(&self) -> Self::Fee {
|
|
|
|
|
self.rpc.get_fee().await.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 13:11:32 -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": {
|
2022-08-22 08:32:09 -04:00
|
|
|
"wallet_address": Self::empty_address().to_string(),
|
2022-07-15 01:26:07 -04:00
|
|
|
"amount_of_blocks": 10
|
|
|
|
|
},
|
|
|
|
|
})),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
2022-06-09 02:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-16 13:11:32 -04:00
|
|
|
#[cfg(test)]
|
2022-06-09 02:48:53 -04:00
|
|
|
async fn test_send(&self, address: Self::Address) {
|
2022-07-27 05:43:23 -04:00
|
|
|
use rand_core::OsRng;
|
2022-06-09 02:48:53 -04:00
|
|
|
|
2022-10-15 21:39:06 -04:00
|
|
|
let new_block = self.get_latest_block_number().await.unwrap() + 1;
|
2022-06-09 02:48:53 -04:00
|
|
|
|
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-08-22 08:32:09 -04:00
|
|
|
let outputs = Self::empty_scanner()
|
2022-10-15 21:39:06 -04:00
|
|
|
.scan(&self.rpc, &self.rpc.get_block(new_block).await.unwrap())
|
2022-08-22 12:15:14 -04:00
|
|
|
.await
|
|
|
|
|
.unwrap()
|
|
|
|
|
.swap_remove(0)
|
2022-07-15 01:26:07 -04:00
|
|
|
.ignore_timelock();
|
2022-06-09 02:48:53 -04:00
|
|
|
|
2022-08-22 12:15:14 -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-08-22 08:32:09 -04:00
|
|
|
Some(Self::empty_address()),
|
2022-08-30 15:42:23 -04:00
|
|
|
None,
|
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
|
|
|
}
|
|
|
|
|
}
|