Skip to content

Commit

Permalink
refactor(builder)!: keep revealedoutputs
Browse files Browse the repository at this point in the history
- Instead of revealed amounts, we keep the revealed outputs, which
allows us to find the revealed amount by output key.
  • Loading branch information
oetyng committed Mar 27, 2023
1 parent 6b96c08 commit d3f0c43
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 25 deletions.
17 changes: 9 additions & 8 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{
};

use crate::transaction::{
DbcTransaction, Output, RevealedAmount, RevealedInput, RevealedTransaction,
DbcTransaction, Output, RevealedAmount, RevealedInput, RevealedOutput, RevealedTransaction,
};
use crate::{
rand::{CryptoRng, RngCore},
Expand Down Expand Up @@ -177,11 +177,11 @@ impl TransactionBuilder {
/// Build the DbcTransaction by signing the inputs,
/// and generating the blinded outputs. Return a DbcBuilder.
pub fn build(self, rng: impl RngCore + CryptoRng) -> Result<DbcBuilder> {
let (transaction, revealed_amounts) = self.revealed_tx.sign(rng)?;
let (transaction, revealed_outputs) = self.revealed_tx.sign(rng)?;

Ok(DbcBuilder::new(
transaction,
revealed_amounts,
revealed_outputs,
self.output_owner_map,
self.revealed_tx,
))
Expand All @@ -193,7 +193,7 @@ impl TransactionBuilder {
#[derive(Debug, Clone)]
pub struct DbcBuilder {
pub transaction: DbcTransaction,
pub revealed_amounts: Vec<RevealedAmount>,
pub revealed_outputs: Vec<RevealedOutput>,
pub output_owner_map: OutputOwnerMap,
pub revealed_tx: RevealedTransaction,
pub spent_proofs: HashSet<SpentProof>,
Expand All @@ -205,13 +205,13 @@ impl DbcBuilder {
/// Create a new DbcBuilder
pub fn new(
transaction: DbcTransaction,
revealed_amounts: Vec<RevealedAmount>,
revealed_outputs: Vec<RevealedOutput>,
output_owner_map: OutputOwnerMap,
revealed_tx: RevealedTransaction,
) -> Self {
Self {
transaction,
revealed_amounts,
revealed_outputs,
output_owner_map,
revealed_tx,
spent_proofs: Default::default(),
Expand Down Expand Up @@ -305,9 +305,10 @@ impl DbcBuilder {
) -> Result<Vec<(Dbc, OwnerOnce, RevealedAmount)>> {
let pc_gens = PedersenGens::default();
let output_blinded_and_revealed_amounts: Vec<(BlindedAmount, RevealedAmount)> = self
.revealed_amounts
.revealed_outputs
.iter()
.map(|r| (r.blinded_amount(&pc_gens), *r))
.map(|output| output.revealed_amount)
.map(|r| (r.blinded_amount(&pc_gens), r))
.collect();

let owner_once_list: Vec<&OwnerOnce> = self
Expand Down
11 changes: 6 additions & 5 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ pub(crate) mod tests {
let input_content = DbcContent::from((
owner_once.owner_base.clone(),
owner_once.derivation_index,
revealed_amounts[0],
revealed_amounts[0].revealed_amount,
));
let public_key = owner_once
.owner_base
Expand Down Expand Up @@ -469,7 +469,7 @@ pub(crate) mod tests {
let input_content = DbcContent::from((
owner_once.owner_base.clone(),
owner_once.derivation_index,
revealed_amounts[0],
revealed_amounts[0].revealed_amount,
));
let public_key = owner_once
.owner_base
Expand Down Expand Up @@ -568,7 +568,7 @@ pub(crate) mod tests {
let input_content = DbcContent::from((
owner_once.owner_base.clone(),
owner_once.derivation_index,
revealed_amounts[0],
revealed_amounts[0].revealed_amount,
));

let public_key = owner_once
Expand Down Expand Up @@ -684,9 +684,10 @@ pub(crate) mod tests {
let output = dbc_builder.transaction.outputs.get(0).unwrap();
let pc_gens = PedersenGens::default();
let output_blinded_and_revealed_amounts: Vec<(BlindedAmount, RevealedAmount)> = dbc_builder
.revealed_amounts
.revealed_outputs
.iter()
.map(|r| (r.blinded_amount(&pc_gens), *r))
.map(|output| output.revealed_amount)
.map(|r| (r.blinded_amount(&pc_gens), r))
.collect();
let revealed_amount_list: Vec<RevealedAmount> = output_blinded_and_revealed_amounts
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ mod tests {
.add_output_by_amount(Token::from_nano(100), output1_owner.clone())
.build(&mut rng)?;

let revealed_amount = dbc_builder.revealed_amounts[0];
let revealed_amount = dbc_builder.revealed_outputs[0].revealed_amount;
let secret_key = output1_owner.as_owner().secret_key()?;

let output2_owner =
Expand Down
13 changes: 3 additions & 10 deletions src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ mod error;
mod input;
mod output;

use output::RevealedOutput;

use crate::rand::{CryptoRng, RngCore};
use crate::BlindedAmount;

Expand All @@ -28,7 +26,7 @@ use serde::{Deserialize, Serialize};
pub use amount::{Amount, RevealedAmount};
pub(crate) use error::Error;
pub use input::{BlindedInput, RevealedInput};
pub use output::{BlindedOutput, Output};
pub use output::{BlindedOutput, Output, RevealedOutput};

pub(super) const RANGE_PROOF_BITS: usize = 64; // note: Range Proof max-bits is 64. allowed are: 8, 16, 32, 64 (only)
// This limits our amount field to 64 bits also.
Expand Down Expand Up @@ -169,7 +167,7 @@ impl RevealedTransaction {
pub fn sign(
&self,
mut rng: impl RngCore + CryptoRng,
) -> Result<(DbcTransaction, Vec<RevealedAmount>)> {
) -> Result<(DbcTransaction, Vec<RevealedOutput>)> {
// We need to gather a bunch of things for our message to sign.
// All public keys in all inputs
// All input blinded amounts
Expand Down Expand Up @@ -199,17 +197,12 @@ impl RevealedTransaction {
.map(|input| input.sign(&msg, &Self::pc_gens()))
.collect();

let revealed_output_amounts = adjusted_revealed_outputs
.iter()
.map(|r| r.revealed_amount)
.collect::<Vec<_>>();

Ok((
DbcTransaction {
inputs: blinded_inputs,
outputs: blinded_outputs,
},
revealed_output_amounts,
adjusted_revealed_outputs,
))
}

Expand Down
2 changes: 1 addition & 1 deletion src/transaction/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Output {
/// blinding factor included (in the revealed amount instance).
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub(super) struct RevealedOutput {
pub struct RevealedOutput {
pub public_key: PublicKey,
pub revealed_amount: RevealedAmount,
}
Expand Down

0 comments on commit d3f0c43

Please sign in to comment.