2022-05-21 15:33:35 -04:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
|
|
use rand_core::{RngCore, CryptoRng};
|
|
|
|
|
use rand::seq::SliceRandom;
|
|
|
|
|
|
|
|
|
|
use curve25519_dalek::{
|
|
|
|
|
constants::ED25519_BASEPOINT_TABLE,
|
|
|
|
|
scalar::Scalar,
|
|
|
|
|
edwards::EdwardsPoint
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use monero::{
|
|
|
|
|
consensus::Encodable,
|
2022-06-10 02:38:19 -04:00
|
|
|
util::{key::PublicKey, address::{AddressType, Address}},
|
2022-05-21 15:33:35 -04:00
|
|
|
blockdata::transaction::SubField
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "multisig")]
|
|
|
|
|
use frost::FrostError;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
Commitment,
|
|
|
|
|
random_scalar,
|
2022-05-22 02:24:24 -04:00
|
|
|
generate_key_image,
|
|
|
|
|
ringct::{
|
|
|
|
|
clsag::{ClsagError, ClsagInput, Clsag},
|
2022-06-19 12:03:01 -04:00
|
|
|
bulletproofs::{MAX_OUTPUTS, Bulletproofs},
|
2022-05-22 02:24:24 -04:00
|
|
|
RctBase, RctPrunable, RctSignatures
|
|
|
|
|
},
|
2022-06-02 00:00:26 -04:00
|
|
|
transaction::{Input, Output, Timelock, TransactionPrefix, Transaction},
|
2022-05-21 15:33:35 -04:00
|
|
|
rpc::{Rpc, RpcError},
|
2022-05-22 01:56:17 -04:00
|
|
|
wallet::{SpendableOutput, Decoys, key_image_sort, uniqueness, shared_key, commitment_mask, amount_encryption}
|
2022-05-21 15:33:35 -04:00
|
|
|
};
|
|
|
|
|
#[cfg(feature = "multisig")]
|
|
|
|
|
use crate::frost::MultisigError;
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "multisig")]
|
|
|
|
|
mod multisig;
|
2022-06-10 09:36:07 -04:00
|
|
|
#[cfg(feature = "multisig")]
|
|
|
|
|
pub use multisig::TransactionMachine;
|
2022-05-21 15:33:35 -04:00
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
2022-05-25 00:21:01 -04:00
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
2022-05-21 15:33:35 -04:00
|
|
|
struct SendOutput {
|
|
|
|
|
R: EdwardsPoint,
|
|
|
|
|
dest: EdwardsPoint,
|
2022-06-19 12:03:01 -04:00
|
|
|
commitment: Commitment,
|
2022-05-21 15:33:35 -04:00
|
|
|
amount: [u8; 8]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SendOutput {
|
|
|
|
|
fn new<R: RngCore + CryptoRng>(
|
|
|
|
|
rng: &mut R,
|
2022-06-19 12:03:01 -04:00
|
|
|
unique: [u8; 32],
|
|
|
|
|
output: (Address, u64, bool),
|
2022-05-21 15:33:35 -04:00
|
|
|
o: usize
|
2022-06-19 12:03:01 -04:00
|
|
|
) -> SendOutput {
|
2022-05-21 15:33:35 -04:00
|
|
|
let r = random_scalar(rng);
|
|
|
|
|
let shared_key = shared_key(
|
2022-06-19 12:03:01 -04:00
|
|
|
Some(unique).filter(|_| output.2),
|
2022-05-21 15:33:35 -04:00
|
|
|
r,
|
2022-06-19 12:03:01 -04:00
|
|
|
&output.0.public_view.point.decompress().expect("SendOutput::new requires valid addresses"),
|
2022-05-21 15:33:35 -04:00
|
|
|
o
|
|
|
|
|
);
|
|
|
|
|
|
2022-06-19 12:03:01 -04:00
|
|
|
let spend = output.0.public_spend.point.decompress().expect("SendOutput::new requires valid addresses");
|
|
|
|
|
SendOutput {
|
|
|
|
|
R: match output.0.addr_type {
|
|
|
|
|
AddressType::Standard => &r * &ED25519_BASEPOINT_TABLE,
|
|
|
|
|
AddressType::SubAddress => &r * spend,
|
|
|
|
|
AddressType::Integrated(_) => panic!("SendOutput::new doesn't support Integrated addresses")
|
|
|
|
|
},
|
|
|
|
|
dest: ((&shared_key * &ED25519_BASEPOINT_TABLE) + spend),
|
|
|
|
|
commitment: Commitment::new(commitment_mask(shared_key), output.1),
|
|
|
|
|
amount: amount_encryption(output.1, shared_key)
|
|
|
|
|
}
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 05:08:37 -04:00
|
|
|
#[derive(Clone, Error, Debug)]
|
2022-05-21 15:33:35 -04:00
|
|
|
pub enum TransactionError {
|
2022-06-19 12:03:01 -04:00
|
|
|
#[error("invalid address")]
|
|
|
|
|
InvalidAddress,
|
2022-05-21 15:33:35 -04:00
|
|
|
#[error("no inputs")]
|
|
|
|
|
NoInputs,
|
|
|
|
|
#[error("no outputs")]
|
|
|
|
|
NoOutputs,
|
2022-06-19 12:03:01 -04:00
|
|
|
#[error("only one output and no change address")]
|
|
|
|
|
NoChange,
|
2022-05-21 15:33:35 -04:00
|
|
|
#[error("too many outputs")]
|
|
|
|
|
TooManyOutputs,
|
|
|
|
|
#[error("not enough funds (in {0}, out {1})")]
|
|
|
|
|
NotEnoughFunds(u64, u64),
|
2022-06-09 04:05:57 -04:00
|
|
|
#[error("wrong spend private key")]
|
|
|
|
|
WrongPrivateKey,
|
2022-05-21 15:33:35 -04:00
|
|
|
#[error("rpc error ({0})")]
|
|
|
|
|
RpcError(RpcError),
|
|
|
|
|
#[error("clsag error ({0})")]
|
|
|
|
|
ClsagError(ClsagError),
|
|
|
|
|
#[error("invalid transaction ({0})")]
|
|
|
|
|
InvalidTransaction(RpcError),
|
|
|
|
|
#[cfg(feature = "multisig")]
|
|
|
|
|
#[error("frost error {0}")]
|
|
|
|
|
FrostError(FrostError),
|
|
|
|
|
#[cfg(feature = "multisig")]
|
|
|
|
|
#[error("multisig error {0}")]
|
|
|
|
|
MultisigError(MultisigError)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn prepare_inputs<R: RngCore + CryptoRng>(
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
rpc: &Rpc,
|
|
|
|
|
inputs: &[SpendableOutput],
|
|
|
|
|
spend: &Scalar,
|
|
|
|
|
tx: &mut Transaction
|
|
|
|
|
) -> Result<Vec<(Scalar, EdwardsPoint, ClsagInput)>, TransactionError> {
|
|
|
|
|
let mut signable = Vec::with_capacity(inputs.len());
|
|
|
|
|
|
|
|
|
|
// Select decoys
|
|
|
|
|
let decoys = Decoys::select(
|
|
|
|
|
rng,
|
|
|
|
|
rpc,
|
|
|
|
|
rpc.get_height().await.map_err(|e| TransactionError::RpcError(e))? - 10,
|
|
|
|
|
inputs
|
|
|
|
|
).await.map_err(|e| TransactionError::RpcError(e))?;
|
|
|
|
|
|
|
|
|
|
for (i, input) in inputs.iter().enumerate() {
|
|
|
|
|
signable.push((
|
|
|
|
|
spend + input.key_offset,
|
|
|
|
|
generate_key_image(&(spend + input.key_offset)),
|
|
|
|
|
ClsagInput::new(
|
|
|
|
|
input.commitment,
|
|
|
|
|
decoys[i].clone()
|
|
|
|
|
).map_err(|e| TransactionError::ClsagError(e))?
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
tx.prefix.inputs.push(Input::ToKey {
|
|
|
|
|
amount: 0,
|
|
|
|
|
key_offsets: decoys[i].offsets.clone(),
|
|
|
|
|
key_image: signable[i].1
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
signable.sort_by(|x, y| x.1.compress().to_bytes().cmp(&y.1.compress().to_bytes()).reverse());
|
|
|
|
|
tx.prefix.inputs.sort_by(|x, y| if let (
|
|
|
|
|
Input::ToKey { key_image: x, ..},
|
|
|
|
|
Input::ToKey { key_image: y, ..}
|
|
|
|
|
) = (x, y) {
|
|
|
|
|
x.compress().to_bytes().cmp(&y.compress().to_bytes()).reverse()
|
|
|
|
|
} else {
|
|
|
|
|
panic!("Input wasn't ToKey")
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Ok(signable)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 12:03:01 -04:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
|
pub struct Fee {
|
|
|
|
|
pub per_weight: u64,
|
|
|
|
|
pub mask: u64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Fee {
|
|
|
|
|
pub fn calculate(&self, weight: usize) -> u64 {
|
|
|
|
|
((((self.per_weight * u64::try_from(weight).unwrap()) - 1) / self.mask) + 1) * self.mask
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 00:21:01 -04:00
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
2022-05-21 15:33:35 -04:00
|
|
|
pub struct SignableTransaction {
|
|
|
|
|
inputs: Vec<SpendableOutput>,
|
2022-06-19 12:03:01 -04:00
|
|
|
payments: Vec<(Address, u64, bool)>,
|
|
|
|
|
outputs: Vec<SendOutput>,
|
|
|
|
|
fee: u64
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SignableTransaction {
|
|
|
|
|
pub fn new(
|
|
|
|
|
inputs: Vec<SpendableOutput>,
|
|
|
|
|
payments: Vec<(Address, u64)>,
|
2022-06-19 12:03:01 -04:00
|
|
|
change_address: Option<Address>,
|
|
|
|
|
fee_rate: Fee
|
2022-05-21 15:33:35 -04:00
|
|
|
) -> Result<SignableTransaction, TransactionError> {
|
2022-06-19 12:03:01 -04:00
|
|
|
// Make sure all addresses are valid
|
|
|
|
|
let test = |addr: Address| {
|
|
|
|
|
if !(
|
|
|
|
|
addr.public_view.point.decompress().is_some() &&
|
|
|
|
|
addr.public_spend.point.decompress().is_some()
|
|
|
|
|
) {
|
|
|
|
|
Err(TransactionError::InvalidAddress)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match addr.addr_type {
|
|
|
|
|
AddressType::Standard => Ok(()),
|
|
|
|
|
AddressType::Integrated(..) => Err(TransactionError::InvalidAddress),
|
|
|
|
|
AddressType::SubAddress => Ok(())
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for payment in &payments {
|
|
|
|
|
test(payment.0)?;
|
|
|
|
|
}
|
|
|
|
|
if let Some(change) = change_address {
|
|
|
|
|
test(change)?;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
if inputs.len() == 0 {
|
|
|
|
|
Err(TransactionError::NoInputs)?;
|
|
|
|
|
}
|
|
|
|
|
if payments.len() == 0 {
|
|
|
|
|
Err(TransactionError::NoOutputs)?;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 12:03:01 -04:00
|
|
|
// TODO TX MAX SIZE
|
|
|
|
|
|
|
|
|
|
// If we don't have two outputs, as required by Monero, add a second
|
|
|
|
|
let mut change = payments.len() == 1;
|
|
|
|
|
if change && change_address.is_none() {
|
|
|
|
|
Err(TransactionError::NoChange)?;
|
|
|
|
|
}
|
|
|
|
|
let mut outputs = payments.len() + (if change { 1 } else { 0 });
|
|
|
|
|
|
2022-06-19 12:19:57 -04:00
|
|
|
// Calculate the extra length.
|
|
|
|
|
// Type, length, value, with 1 field for the first key and 1 field for the rest
|
|
|
|
|
let extra = (outputs * (2 + 32)) - (outputs.saturating_sub(2) * 2);
|
|
|
|
|
|
2022-06-19 12:03:01 -04:00
|
|
|
// Calculate the fee.
|
|
|
|
|
let mut fee = fee_rate.calculate(Transaction::fee_weight(inputs.len(), outputs, extra));
|
|
|
|
|
|
|
|
|
|
// Make sure we have enough funds
|
|
|
|
|
let in_amount = inputs.iter().map(|input| input.commitment.amount).sum::<u64>();
|
|
|
|
|
let mut out_amount = payments.iter().map(|payment| payment.1).sum::<u64>() + fee;
|
|
|
|
|
if in_amount < out_amount {
|
|
|
|
|
Err(TransactionError::NotEnoughFunds(in_amount, out_amount))?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we have yet to add a change output, do so if it's economically viable
|
|
|
|
|
if (!change) && change_address.is_some() && (in_amount != out_amount) {
|
|
|
|
|
// Check even with the new fee, there's remaining funds
|
|
|
|
|
let change_fee = fee_rate.calculate(Transaction::fee_weight(inputs.len(), outputs + 1, extra)) - fee;
|
|
|
|
|
if (out_amount + change_fee) < in_amount {
|
|
|
|
|
change = true;
|
|
|
|
|
outputs += 1;
|
|
|
|
|
out_amount += change_fee;
|
|
|
|
|
fee += change_fee;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if outputs > MAX_OUTPUTS {
|
|
|
|
|
Err(TransactionError::TooManyOutputs)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut payments = payments.iter().map(|(address, amount)| (*address, *amount, false)).collect::<Vec<_>>();
|
|
|
|
|
if change {
|
|
|
|
|
// Always use a unique key image for the change output
|
|
|
|
|
// TODO: Make this a config option
|
|
|
|
|
payments.push((change_address.unwrap(), in_amount - out_amount, true));
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
Ok(
|
|
|
|
|
SignableTransaction {
|
|
|
|
|
inputs,
|
|
|
|
|
payments,
|
2022-06-19 12:03:01 -04:00
|
|
|
outputs: vec![],
|
|
|
|
|
fee
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn prepare_outputs<R: RngCore + CryptoRng>(
|
|
|
|
|
&mut self,
|
|
|
|
|
rng: &mut R,
|
2022-05-22 01:56:17 -04:00
|
|
|
uniqueness: [u8; 32]
|
2022-06-19 12:03:01 -04:00
|
|
|
) -> (Vec<Commitment>, Scalar) {
|
|
|
|
|
// Shuffle the payments
|
|
|
|
|
self.payments.shuffle(rng);
|
2022-05-21 15:33:35 -04:00
|
|
|
|
|
|
|
|
// Actually create the outputs
|
2022-06-19 12:03:01 -04:00
|
|
|
self.outputs = Vec::with_capacity(self.payments.len() + 1);
|
|
|
|
|
for (o, output) in self.payments.iter().enumerate() {
|
|
|
|
|
self.outputs.push(SendOutput::new(rng, uniqueness, *output, o));
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-19 12:03:01 -04:00
|
|
|
let commitments = self.outputs.iter().map(|output| output.commitment).collect::<Vec<_>>();
|
|
|
|
|
let sum = commitments.iter().map(|commitment| commitment.mask).sum();
|
|
|
|
|
(commitments, sum)
|
2022-05-21 15:33:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn prepare_transaction(
|
|
|
|
|
&self,
|
|
|
|
|
commitments: &[Commitment],
|
|
|
|
|
bp: Bulletproofs
|
|
|
|
|
) -> Transaction {
|
|
|
|
|
// Create the TX extra
|
2022-06-10 02:38:19 -04:00
|
|
|
// TODO: Review this for canonicity with Monero
|
2022-05-21 15:33:35 -04:00
|
|
|
let mut extra = vec![];
|
|
|
|
|
SubField::TxPublicKey(
|
|
|
|
|
PublicKey { point: self.outputs[0].R.compress() }
|
|
|
|
|
).consensus_encode(&mut extra).unwrap();
|
|
|
|
|
SubField::AdditionalPublickKey(
|
2022-05-30 02:14:34 -04:00
|
|
|
self.outputs[1 ..].iter().map(|output| PublicKey { point: output.R.compress() }).collect()
|
2022-05-21 15:33:35 -04:00
|
|
|
).consensus_encode(&mut extra).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut tx_outputs = Vec::with_capacity(self.outputs.len());
|
|
|
|
|
let mut ecdh_info = Vec::with_capacity(self.outputs.len());
|
|
|
|
|
for o in 0 .. self.outputs.len() {
|
|
|
|
|
tx_outputs.push(Output {
|
|
|
|
|
amount: 0,
|
|
|
|
|
key: self.outputs[o].dest,
|
|
|
|
|
tag: None
|
|
|
|
|
});
|
|
|
|
|
ecdh_info.push(self.outputs[o].amount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Transaction {
|
|
|
|
|
prefix: TransactionPrefix {
|
|
|
|
|
version: 2,
|
2022-06-02 00:00:26 -04:00
|
|
|
timelock: Timelock::None,
|
2022-05-21 15:33:35 -04:00
|
|
|
inputs: vec![],
|
|
|
|
|
outputs: tx_outputs,
|
|
|
|
|
extra
|
|
|
|
|
},
|
|
|
|
|
rct_signatures: RctSignatures {
|
|
|
|
|
base: RctBase {
|
|
|
|
|
fee: self.fee,
|
|
|
|
|
ecdh_info,
|
|
|
|
|
commitments: commitments.iter().map(|commitment| commitment.calculate()).collect()
|
|
|
|
|
},
|
|
|
|
|
prunable: RctPrunable::Clsag {
|
|
|
|
|
bulletproofs: vec![bp],
|
|
|
|
|
clsags: vec![],
|
|
|
|
|
pseudo_outs: vec![]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn sign<R: RngCore + CryptoRng>(
|
|
|
|
|
&mut self,
|
|
|
|
|
rng: &mut R,
|
|
|
|
|
rpc: &Rpc,
|
|
|
|
|
spend: &Scalar
|
|
|
|
|
) -> Result<Transaction, TransactionError> {
|
2022-05-21 21:44:57 -04:00
|
|
|
let mut images = Vec::with_capacity(self.inputs.len());
|
|
|
|
|
for input in &self.inputs {
|
2022-06-09 04:05:57 -04:00
|
|
|
let offset = spend + input.key_offset;
|
|
|
|
|
if (&offset * &ED25519_BASEPOINT_TABLE) != input.key {
|
|
|
|
|
Err(TransactionError::WrongPrivateKey)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
images.push(generate_key_image(&offset));
|
2022-05-21 21:44:57 -04:00
|
|
|
}
|
2022-05-22 01:56:17 -04:00
|
|
|
images.sort_by(key_image_sort);
|
2022-05-21 21:44:57 -04:00
|
|
|
|
2022-05-21 15:33:35 -04:00
|
|
|
let (commitments, mask_sum) = self.prepare_outputs(
|
|
|
|
|
rng,
|
2022-05-22 01:56:17 -04:00
|
|
|
uniqueness(
|
|
|
|
|
&images.iter().map(|image| Input::ToKey {
|
|
|
|
|
amount: 0,
|
|
|
|
|
key_offsets: vec![],
|
|
|
|
|
key_image: *image
|
|
|
|
|
}).collect::<Vec<_>>()
|
2022-05-21 15:33:35 -04:00
|
|
|
)
|
2022-06-19 12:03:01 -04:00
|
|
|
);
|
2022-05-21 15:33:35 -04:00
|
|
|
|
2022-05-22 01:56:17 -04:00
|
|
|
let mut tx = self.prepare_transaction(&commitments, Bulletproofs::new(rng, &commitments)?);
|
2022-05-21 15:33:35 -04:00
|
|
|
|
|
|
|
|
let signable = prepare_inputs(rng, rpc, &self.inputs, spend, &mut tx).await?;
|
|
|
|
|
|
|
|
|
|
let clsag_pairs = Clsag::sign(rng, &signable, mask_sum, tx.signature_hash());
|
|
|
|
|
match tx.rct_signatures.prunable {
|
|
|
|
|
RctPrunable::Null => panic!("Signing for RctPrunable::Null"),
|
|
|
|
|
RctPrunable::Clsag { ref mut clsags, ref mut pseudo_outs, .. } => {
|
|
|
|
|
clsags.append(&mut clsag_pairs.iter().map(|clsag| clsag.0.clone()).collect::<Vec<_>>());
|
|
|
|
|
pseudo_outs.append(&mut clsag_pairs.iter().map(|clsag| clsag.1.clone()).collect::<Vec<_>>());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(tx)
|
|
|
|
|
}
|
|
|
|
|
}
|