mod expirable; mod salted; mod versioned; pub use { expirable::ExpirableNonce, salted::SaltedNonce, salted::{Salt, SaltRegistry}, versioned::VersionedNonce, }; use defuse_bitmap::{BitMap256, U248, U256}; use defuse_map_utils::{IterableMap, Map}; use near_sdk::near; use crate::{DefuseError, Result}; pub type Nonce = U256; pub type NoncePrefix = U248; /// See [permit2 nonce schema](https://docs.uniswap.org/contracts/permit2/reference/signature-transfer#nonce-schema) #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] #[near(serializers = [borsh, json])] #[derive(Debug, Clone, Default)] pub struct Nonces>(BitMap256); impl Nonces where T: Map, { #[inline] pub const fn new(bitmap: T) -> Self { Self(BitMap256::new(bitmap)) } #[inline] pub fn is_used(&self, n: Nonce) -> bool { self.0.get_bit(n) } #[inline] pub fn commit(&mut self, n: Nonce) -> Result<()> { if self.0.set_bit(n) { return Err(DefuseError::NonceUsed); } Ok(()) } #[inline] pub fn cleanup_by_prefix(&mut self, prefix: NoncePrefix) -> bool { self.0.cleanup_by_prefix(prefix) } #[inline] pub fn iter(&self) -> impl Iterator + '_ where T: IterableMap, { self.0.as_iter() } }