Skip to content

Commit

Permalink
refactor: use SignatureBytes instead of [u8; 65]
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <gustavo@semiotic.ai>
  • Loading branch information
gusinacio committed Aug 2, 2024
1 parent 5b461b8 commit b40df66
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
8 changes: 5 additions & 3 deletions tap_aggregator/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use alloy::{
use anyhow::{bail, Ok, Result};

use tap_core::{
rav::ReceiptAggregateVoucher, receipt::Receipt, signed_message::EIP712SignedMessage,
rav::ReceiptAggregateVoucher,
receipt::Receipt,
signed_message::{EIP712SignedMessage, SignatureBytes, SignatureBytesExt},
};

pub fn check_and_aggregate_receipts(
Expand Down Expand Up @@ -99,9 +101,9 @@ fn check_allocation_id(
}

fn check_signatures_unique(receipts: &[EIP712SignedMessage<Receipt>]) -> Result<()> {
let mut receipt_signatures: hash_set::HashSet<[u8; 65]> = hash_set::HashSet::new();
let mut receipt_signatures: hash_set::HashSet<SignatureBytes> = hash_set::HashSet::new();
for receipt in receipts.iter() {
let signature = receipt.signature.as_bytes();
let signature = receipt.signature.get_signature_bytes();
if !receipt_signatures.insert(signature) {
return Err(tap_core::Error::DuplicateReceiptSignature(format!(
"{:?}",
Expand Down
9 changes: 7 additions & 2 deletions tap_core/src/receipt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
//! let my_check: ReceiptCheck = Arc::new(MyCheck);
//! ```
use crate::signed_message::{SignatureBytes, SignatureBytesExt};

use super::{
state::{Checking, Failed},
ReceiptError, ReceiptWithState,
Expand Down Expand Up @@ -168,11 +170,14 @@ impl CheckBatch for UniqueCheck {
Vec<ReceiptWithState<Checking>>,
Vec<ReceiptWithState<Failed>>,
) {
let mut signatures: HashSet<[u8; 65]> = HashSet::new();
let mut signatures: HashSet<SignatureBytes> = HashSet::new();
let (mut checking, mut failed) = (vec![], vec![]);

for received_receipt in receipts.into_iter() {
let signature = received_receipt.signed_receipt.signature.as_bytes();
let signature = received_receipt
.signed_receipt
.signature
.get_signature_bytes();
if signatures.insert(signature) {
checking.push(received_receipt);
} else {
Expand Down
13 changes: 13 additions & 0 deletions tap_core/src/signed_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ pub struct EIP712SignedMessage<M: SolStruct> {
pub signature: Signature,
}

#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct SignatureBytes([u8; 65]);

pub trait SignatureBytesExt {
fn get_signature_bytes(&self) -> SignatureBytes;
}

impl SignatureBytesExt for Signature {
fn get_signature_bytes(&self) -> SignatureBytes {
SignatureBytes(self.as_bytes())
}
}

/// Unique identifier for a message
///
/// This is equal to the hash of the contents of a message, excluding the signature.
Expand Down

0 comments on commit b40df66

Please sign in to comment.