Skip to content

Commit

Permalink
feat(builder): add api for adding spentproof
Browse files Browse the repository at this point in the history
  • Loading branch information
oetyng committed Feb 28, 2023
1 parent d75633c commit 3cfb58d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub struct DbcBuilder {
pub revealed_commitments: Vec<RevealedCommitment>,
pub output_owner_map: OutputOwnerMap,
pub revealed_tx: RevealedTransaction,

pub spent_proofs: HashSet<SpentProof>,
pub spent_proof_shares: BTreeMap<PublicKey, HashSet<SpentProofShare>>,
pub spent_transactions: BTreeMap<Hash, DbcTransaction>,
}
Expand All @@ -213,6 +213,7 @@ impl DbcBuilder {
revealed_commitments,
output_owner_map,
revealed_tx,
spent_proofs: Default::default(),
spent_proof_shares: Default::default(),
spent_transactions: Default::default(),
}
Expand All @@ -228,6 +229,12 @@ impl DbcBuilder {
.collect()
}

/// Add a SpentProof.
pub fn add_spent_proof(mut self, proof: SpentProof) -> Self {
let _ = self.spent_proofs.insert(proof);
self
}

/// Add a SpentProofShare for the given input index
pub fn add_spent_proof_share(mut self, share: SpentProofShare) -> Self {
let shares = self
Expand Down Expand Up @@ -353,6 +360,7 @@ impl DbcBuilder {
.map(|(key_image, shares)| {
SpentProof::try_from_proof_shares(*key_image, transaction_hash, shares.iter())
})
.chain(self.spent_proofs.iter().cloned().map(Ok))
.collect::<Result<_>>()?;

Ok(spent_proofs)
Expand Down
20 changes: 19 additions & 1 deletion src/spent_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub trait SpentProofKeyVerifier {

/// SpentProof's are constructed when a DBC is logged to the spentbook.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone, PartialOrd, Ord)]
pub struct SpentProof {
/// data to be signed
pub content: SpentProofContent,
Expand Down Expand Up @@ -262,3 +262,21 @@ impl SpentProof {
.map_err(|err| Error::FailedKnownKeyCheck(err.to_string()))
}
}

// impl manually to avoid clippy complaint about Hash conflict.
impl PartialEq for SpentProof {
fn eq(&self, other: &Self) -> bool {
self.content == other.content
&& self.spentbook_pub_key == other.spentbook_pub_key
&& self.spentbook_sig == other.spentbook_sig
}
}

impl Eq for SpentProof {}

impl std::hash::Hash for SpentProof {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let bytes = self.to_bytes();
bytes.hash(state);
}
}

0 comments on commit 3cfb58d

Please sign in to comment.