fixed swap validations

This commit is contained in:
contractless 2026-08-02 10:40:38 -06:00
parent 15518f14c3
commit c40fb0ce4d
4 changed files with 21 additions and 14 deletions

View File

@ -2,16 +2,20 @@ use crate::DateTime;
use crate::Utc; use crate::Utc;
pub async fn is_within_30_days(timestamp: u32) -> bool { pub async fn is_within_30_days(timestamp: u32) -> bool {
// Convert the on-chain timestamp into UTC and reject values that fall is_within_30_days_at(timestamp, Utc::now().timestamp() as u32)
// more than 30 days in the past or fail to decode as valid datetimes. }
let transaction_time = DateTime::<Utc>::from_timestamp(timestamp as i64, 0);
match transaction_time { pub fn is_within_30_days_at(timestamp: u32, as_of_timestamp: u32) -> bool {
Some(transaction_time) => { // Convert the on-chain timestamp into UTC and reject values that fall
let current_time = Utc::now(); // more than 30 days before the supplied validation time or fail to decode.
let duration = current_time.signed_duration_since(transaction_time); let transaction_time = DateTime::<Utc>::from_timestamp(timestamp as i64, 0);
let as_of_time = DateTime::<Utc>::from_timestamp(as_of_timestamp as i64, 0);
match (transaction_time, as_of_time) {
(Some(transaction_time), Some(as_of_time)) => {
let duration = as_of_time.signed_duration_since(transaction_time);
duration.num_days() <= 30 duration.num_days() <= 30
} }
None => false, _ => false,
} }
} }

View File

@ -229,7 +229,7 @@ async fn verify_transaction(
} }
} }
if let Transaction::Swap(swap_tx) = &transaction { if let Transaction::Swap(swap_tx) = &transaction {
match swap_tx.verify(db).await { match swap_tx.verify_at(db, block_timestamp).await {
Ok(value) => { Ok(value) => {
reserve_verified_transaction(db, &transaction, balance_tracker, already_in_mempool) reserve_verified_transaction(db, &transaction, balance_tracker, already_in_mempool)
.await?; .await?;

View File

@ -9,7 +9,7 @@ use crate::records::wallet_registry::{
}; };
use crate::sled::Db; use crate::sled::Db;
use crate::verifications::async_funcs::checks::balance_check::balance_checkup; use crate::verifications::async_funcs::checks::balance_check::balance_checkup;
use crate::verifications::async_funcs::checks::time_checks::is_within_30_days; use crate::verifications::async_funcs::checks::time_checks::is_within_30_days_at;
use crate::verifications::async_funcs::checks::verify_db::{ use crate::verifications::async_funcs::checks::verify_db::{
db_bytes_verification, db_hex_verification, db_bytes_verification, db_hex_verification,
}; };
@ -35,6 +35,10 @@ fn validate_swap_tip(value: u64, tip: u64, is_nft: bool, sender: &str) -> Result
impl SwapTransaction { impl SwapTransaction {
pub async fn verify(&self, db: &Db) -> Result<String, String> { pub async fn verify(&self, db: &Db) -> Result<String, String> {
self.verify_at(db, Utc::now().timestamp() as u32).await
}
pub async fn verify_at(&self, db: &Db, as_of_timestamp: u32) -> Result<String, String> {
let hash = self.unsigned_swap.hash().await; let hash = self.unsigned_swap.hash().await;
// Both swap participants must provide valid wallet addresses and // Both swap participants must provide valid wallet addresses and
// matching signatures over the shared unsigned swap payload. // matching signatures over the shared unsigned swap payload.
@ -203,14 +207,13 @@ impl SwapTransaction {
// Swap offers are bounded both by signature age and by the // Swap offers are bounded both by signature age and by the
// explicit offer-expiration window carried in the transaction. // explicit offer-expiration window carried in the transaction.
if !is_within_30_days(self.unsigned_swap.timestamp).await { if !is_within_30_days_at(self.unsigned_swap.timestamp, as_of_timestamp) {
return Err( return Err(
"Timestamp is to old. Transactions must be broadcast within 30 days of signing." "Timestamp is to old. Transactions must be broadcast within 30 days of signing."
.to_string(), .to_string(),
); );
} }
let now = Utc::now().timestamp() as u32;
let offer_expiration = self.unsigned_swap.offer_expiration; let offer_expiration = self.unsigned_swap.offer_expiration;
let timestamp = self.unsigned_swap.timestamp; let timestamp = self.unsigned_swap.timestamp;
@ -227,7 +230,7 @@ impl SwapTransaction {
); );
} }
if now > offer_expiration { if as_of_timestamp > offer_expiration {
return Err("This swap offer has expired.".to_string()); return Err("This swap offer has expired.".to_string());
} }