Nonce Management

Overview

Nonces (numbers used once) prevent replay attacks by ensuring each signed order can only be executed once. Frontier Chain uses timestamp-based nonces that enable parallel order submission while maintaining strong replay protection.

Key Features:

  • Timestamp-Based: Millisecond-precision Unix timestamps

  • Parallel Submissions: No sequential ordering required

  • 100-Nonce Window: Tracks highest 100 nonces per user

  • Time Bounds: Valid within 2-day past to 1-day future

  • Per-User Tracking: Independent nonce spaces for each user/agent


How Nonces Work

Basic Concept

Without Nonces:

User signs order: "Buy 1 BTC at $90,000"
→ Order executes
→ Attacker copies signature
→ Attacker submits same signed order
→ Order executes again! (replay attack)

With Nonces:


Timestamp-Based Nonces

Why Timestamps?

Traditional sequential nonces (0, 1, 2, 3...) require strict ordering:

Timestamp nonces allow parallel submission:


Nonce Format

Millisecond Unix Timestamp:

Generation (JavaScript):

Generation (Python):

Generation (Rust):


Nonce Validation Rules

Uniqueness Check

Rule: Nonce must not exist in user's 100-nonce window


Minimum Nonce Check

Rule: Nonce must be higher than lowest stored nonce

Purpose: Prevents very old nonces from being used after window rotates


Time Bounds Check

Past Bound: Within 2 days of current time

Future Bound: Not more than 1 day in future


100-Nonce Window

Why 100 Nonces?

Memory Efficiency:

  • Only store most recent 100 nonces

  • Older nonces automatically dropped

  • Prevents unbounded growth

  • O(1) insertion/lookup with BTreeSet

Practical Capacity:

  • 100 unique millisecond timestamps

  • 100ms minimum spacing needed

  • Effective capacity: 10 seconds of continuous submissions

  • More than sufficient for high-frequency trading


Window Management

Insertion:

Automatic Cleanup:


Per-User Nonce Tracking

Independent Nonce Spaces

Each user (and each agent) has independent nonce tracking:

Structure:

Benefits:


Agent Nonce Tracking

Each agent wallet has its own nonce tracker:


Replay Attack Prevention

Scenario: Attacker Captures Signature


Scenario: Attacker Modifies Nonce


Parallel Order Submission

High-Frequency Trading

Timestamp nonces enable parallel submissions:


Multiple Agents

Different agents can submit in parallel:


Best Practices

Nonce Generation

Do:

  • Use current timestamp in milliseconds

  • Generate fresh nonce for each order

  • Use system time (Date.now(), time.time() * 1000)

  • Add small random offset if submitting multiple orders per millisecond

Don't:

  • Reuse nonces

  • Use sequential counters

  • Generate nonces far in future

  • Use hardcoded values


Example: Multiple Orders Same Millisecond


Common Issues

"Nonce Already Used"

Cause: Submitting order with previously used nonce

Solutions:

  1. Generate new timestamp: Date.now()

  2. Don't reuse nonces

  3. Check nonce isn't in recent history

  4. Ensure clock is synchronized


"Nonce Too Low"

Cause: Nonce lower than minimum in 100-nonce window

Solutions:

  1. Use current timestamp (not old value)

  2. Check system clock is correct

  3. Don't use cached/stored nonces

  4. Generate fresh nonce


"Nonce Too Old"

Cause: Nonce more than 2 days in past

Solutions:

  1. Check system clock is accurate

  2. Use Date.now() not stored value

  3. Synchronize system time (NTP)

  4. Don't use very old nonces


"Nonce Too Far in Future"

Cause: Nonce more than 1 day ahead

Solutions:

  1. Check system clock is accurate

  2. Don't add large offsets to timestamp

  3. Synchronize system time

  4. Use actual current time


Edge Cases

Clock Skew

Problem: Different clocks may differ slightly

Mitigation:

  • 1-day future bound provides buffer

  • 2-day past bound provides buffer

  • Validators use their own clocks

  • Small skew (<1 minute) not an issue


Millisecond Collisions

Problem: Two orders same millisecond

Solutions:

  1. Add small offset (+1ms, +2ms)

  2. Most systems have microsecond precision

  3. Rare in practice

  4. Even if collision, different signatures


Window Rotation

Scenario:

Protection:

  • Time bounds prevent this

  • Nonce N now >100 milliseconds old

  • Would need 100+ orders in 100ms to trigger

  • 1000+ orders/second needed

  • Beyond typical use case

  • Time bounds provide additional protection


Technical Implementation

Data Structure (from codebase)

Operations:

  • Insert: O(log n)

  • Contains: O(log n)

  • Remove minimum: O(log n)

  • All efficient for 100-item window


Validation Algorithm


Comparison: Sequential vs Timestamp Nonces

Sequential Nonces

How They Work:

Limitations:

  • Must submit in order

  • No parallel submissions

  • Network delays cause problems

  • Nonce gaps break submission


Timestamp Nonces (Our Approach)

How They Work:

Advantages:

  • Parallel submissions work

  • Order doesn't matter

  • Network delays tolerated

  • High-frequency trading enabled


Conclusion

Timestamp-based nonces provide robust replay protection while enabling high-frequency trading. The combination of unique timestamps, time bounds, and per-user tracking creates a secure, performant nonce management system.

Key Takeaways:

  • Use millisecond timestamps as nonces

  • Each user/agent has independent nonce space

  • 100-nonce window prevents replay attacks

  • Time bounds add additional protection

  • Enables parallel order submission

Next Steps:

EIP-712 SigningAgent WalletsAuthentication

Last updated