Skip to content

Commit

Permalink
Merge pull request #27 from monero-rs/thiserror
Browse files Browse the repository at this point in the history
Impl this error and move amount recovery error
  • Loading branch information
h4sh3d committed Mar 29, 2021
2 parents 724125f + b17d7af commit 9018844
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
35 changes: 12 additions & 23 deletions src/blockdata/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ use crate::consensus::encode::{self, serialize, Decodable, Encodable, VarInt};
use crate::cryptonote::hash;
use crate::cryptonote::onetime_key::{KeyGenerator, KeyRecoverer, SubKeyChecker};
use crate::cryptonote::subaddress::Index;
use crate::util::amount::RecoveryError;
use crate::util::key::{KeyPair, PrivateKey, PublicKey, ViewPair, H};
use crate::util::ringct::{RctSig, RctSigBase, RctSigPrunable, RctType, Signature};
use crate::util::ringct::{EcdhInfo, RctSig, RctSigBase, RctSigPrunable, RctType, Signature};

use curve25519_dalek::scalar::Scalar;
use hex::encode as hex_encode;
use thiserror::Error;

use std::convert::TryInto;
use std::ops::Range;
use std::{fmt, io};

use thiserror::Error;

#[cfg(feature = "serde_support")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -424,42 +427,27 @@ impl fmt::Display for Transaction {
}
}

use crate::util::ringct::EcdhInfo;
use curve25519_dalek::scalar::Scalar;
use std::convert::TryInto;

/// Error recovering the amount of a transaction
#[derive(Debug)]
pub enum AmountError {
/// index of outputs out of range
IndexOutOfRange,
/// SigMissing
SigMissing,
/// invalid commitment
FalsifiedAmount,
}

impl Transaction {
/// Calculate an output's amount
pub fn get_amount<'a>(
&self,
view_pair: &ViewPair,
out: &OwnedTxOut,
) -> Result<u64, AmountError> {
) -> Result<u64, RecoveryError> {
if out.index >= self.prefix.outputs.len() {
Err(AmountError::IndexOutOfRange)?;
Err(RecoveryError::IndexOutOfRange)?;
}

let sig = self
.rct_signatures
.sig
.as_ref()
.ok_or(AmountError::SigMissing)?;
.ok_or(RecoveryError::MissingSignature)?;

let ecdh_info = sig
.ecdh_info
.get(out.index)
.ok_or(AmountError::IndexOutOfRange)?;
.ok_or(RecoveryError::IndexOutOfRange)?;

let shared_key = KeyGenerator::from_key(view_pair, out.tx_pubkey).get_rvn_scalar(out.index);

Expand Down Expand Up @@ -516,8 +504,9 @@ impl Transaction {
let committed_amount = H * &PrivateKey::from_scalar(Scalar::from(amount));
let expected_commitment = blinding_factor + committed_amount;
let actual_commitment = PublicKey::from_slice(&sig.out_pk[out.index].mask.key);

if actual_commitment != Ok(expected_commitment) {
Err(AmountError::FalsifiedAmount)?;
Err(RecoveryError::InvalidCommitment)?;
}

Ok(amount)
Expand Down
17 changes: 17 additions & 0 deletions src/util/amount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Amounts

use thiserror::Error;

/// Error recovering the amount of a transaction
#[derive(Error, Debug, PartialEq)]
pub enum RecoveryError {
/// Index of output is out of range
#[error("The index is out of range")]
IndexOutOfRange,
/// Missing signature for the output
#[error("Missing signature for the output")]
MissingSignature,
/// Invalid commitment
#[error("Invalid commitment")]
InvalidCommitment,
}
4 changes: 4 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//!

pub mod address;
pub mod amount;
pub mod key;
pub mod ringct;

Expand Down Expand Up @@ -46,4 +47,7 @@ pub enum Error {
/// Monero transaction error
#[error("Transaction error: {0}")]
Transaction(#[from] transaction::Error),
/// Monero amount error
#[error("Amount recovery error: {0}")]
AmountRecovery(#[from] amount::RecoveryError),
}

0 comments on commit 9018844

Please sign in to comment.