Files
impersonator/__tests__/nonceManager.test.ts

111 lines
3.2 KiB
TypeScript
Raw Normal View History

feat: comprehensive project improvements and fixes - Fix all TypeScript compilation errors (40+ fixes) - Add missing type definitions (TransactionRequest, SafeInfo) - Fix TransactionRequestStatus vs TransactionStatus confusion - Fix import paths and provider type issues - Fix test file errors and mock providers - Implement comprehensive security features - AES-GCM encryption with PBKDF2 key derivation - Input validation and sanitization - Rate limiting and nonce management - Replay attack prevention - Access control and authorization - Add comprehensive test suite - Integration tests for transaction flow - Security validation tests - Wallet management tests - Encryption and rate limiter tests - E2E tests with Playwright - Add extensive documentation - 12 numbered guides (setup, development, API, security, etc.) - Security documentation and audit reports - Code review and testing reports - Project organization documentation - Update dependencies - Update axios to latest version (security fix) - Update React types to v18 - Fix peer dependency warnings - Add development tooling - CI/CD workflows (GitHub Actions) - Pre-commit hooks (Husky) - Linting and formatting (Prettier, ESLint) - Security audit workflow - Performance benchmarking - Reorganize project structure - Move reports to docs/reports/ - Clean up root directory - Organize documentation - Add new features - Smart wallet management (Gnosis Safe, ERC4337) - Transaction execution and approval workflows - Balance management and token support - Error boundary and monitoring (Sentry) - Fix WalletConnect configuration - Handle missing projectId gracefully - Add environment variable template
2026-01-14 02:17:26 -08:00
/**
* Nonce manager tests
* Note: These tests require a mock provider
*/
import { NonceManager } from "../utils/security";
import { ethers } from "ethers";
// Mock provider
class MockProvider extends ethers.providers.BaseProvider {
private transactionCounts: Map<string, number> = new Map();
constructor() {
super(ethers.providers.getNetwork(1)); // Mainnet network
}
setTransactionCount(address: string, count: number) {
this.transactionCounts.set(address.toLowerCase(), count);
}
async getTransactionCount(address: string, blockTag?: string): Promise<number> {
return this.transactionCounts.get(address.toLowerCase()) || 0;
}
// Required by BaseProvider
async perform(method: string, params: any): Promise<any> {
throw new Error("Not implemented");
}
}
describe("NonceManager", () => {
let provider: MockProvider;
let nonceManager: NonceManager;
beforeEach(() => {
provider = new MockProvider();
nonceManager = new NonceManager(provider as any);
});
it("should get next nonce for new address", async () => {
const address = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb";
provider.setTransactionCount(address, 0);
const nonce = await nonceManager.getNextNonce(address);
expect(nonce).toBe(0);
});
it("should increment nonce after use", async () => {
const address = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb";
provider.setTransactionCount(address, 5);
const nonce1 = await nonceManager.getNextNonce(address);
expect(nonce1).toBe(5);
const nonce2 = await nonceManager.getNextNonce(address);
expect(nonce2).toBe(6);
const nonce3 = await nonceManager.getNextNonce(address);
expect(nonce3).toBe(7);
});
it("should use higher value between stored and on-chain", async () => {
const address = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb";
// Set stored nonce to 10
await nonceManager.getNextNonce(address);
await nonceManager.getNextNonce(address);
// Now stored should be 2
// Set on-chain to 5
provider.setTransactionCount(address, 5);
// Should use 5 (higher)
const nonce = await nonceManager.getNextNonce(address);
expect(nonce).toBe(5);
});
it("should refresh nonce from chain", async () => {
const address = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb";
// Set initial nonce
provider.setTransactionCount(address, 3);
await nonceManager.getNextNonce(address);
// Update on-chain
provider.setTransactionCount(address, 10);
// Refresh
const refreshed = await nonceManager.refreshNonce(address);
expect(refreshed).toBe(10);
// Next nonce should be 11
const next = await nonceManager.getNextNonce(address);
expect(next).toBe(11);
});
it("should track multiple addresses independently", async () => {
const address1 = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb";
const address2 = "0x8ba1f109551bD432803012645Hac136c22C9e8";
provider.setTransactionCount(address1, 0);
provider.setTransactionCount(address2, 5);
const nonce1 = await nonceManager.getNextNonce(address1);
const nonce2 = await nonceManager.getNextNonce(address2);
expect(nonce1).toBe(0);
expect(nonce2).toBe(5);
});
});