mirror of
https://github.com/luxfi/erc-3643.git
synced 2026-10-30 16:58:28 +00:00
* ✨ integrate foundry alongside hardhat for dual testing framework - Add foundry.toml configuration - Reorganize test structure: test/forge/ and test/hardhat/ - Add Foundry scripts to package.json (build:forge, test:forge, coverage:forge) - Update hardhat.config.ts with evmVersion for compatibility - Add GitHub workflows and coverage scripts from template - Add Husky hooks (pre-commit, post-checkout, post-merge) - Add .vscode/settings.json for better IDE support - Disable coverage checks until tests are migrated (commented with TODO) This migration is based on TokenySolutions/contracts-template and enables gradual migration of tests from Hardhat (TypeScript) to Foundry (Solidity). Related to: BT-1418 * ✅ trex factory tests * ⚰️ remove trex suit * feat(forge): reorganize Foundry test structure and sync GitHub workflows - Move TREXFactorySetup to helpers folder - Rename TREXSuite.t.sol to TREXFactory.t.sol - Update fixture to inherit from Test for proper Foundry pattern - Transfer ownership to deployer address to match Hardhat behavior - Sync GitHub workflows with package.json scripts - Update test.yml to use npm run build and npm run test - Update push_checking.yml to use unified npm run lint - Update forge_coverage.sh to use npm run coverage * fix(ci): install Foundry and skip husky in push_checking workflow - Add Foundry installation step before npm ci - Set HUSKY=0 to skip husky setup in CI environment - Fixes 'forge: not found' error during npm prepare script * 🔧 keep hardhat settings * ✅ create addressess directly * ✅ removed test_StandardAddressesSet * ✅ fix lint * 💡 removed title natspecs * ✅ migrated trex factort tests * ✅ added test module multicall * ✅ migrate agent role test * ✅ migrate claim topics registry * ✅ migrated trusted issuer registry * ✅ migrate identity registry storage * ✅ migrated identity registry tests * ✅ migrated implementation authority test * ✅ migrated token agen restrictions * ✅ migrated token information test * ✅ migrated token recover * ✅ migrated token permit * ✅ migrated token transfer * ✅ migrated utilities upgrade * ✅ migrated freeze check * ✅ migrated compliance check in utilities test * ✅ migrated eligibility check * ✅ migrated transfer check in utilities * ✅ migrated compliance test * ✅ migrated gateway tests * 🔥 removed already defined events * 🗑️ imported Initializable error from openzeppelin * ➕ added already defined events and errors * ✅ added base test token * ✅ used idFactory * ♻️ refactored compliance test * ⚰️ remoced useless vm.prank * 🔧 deleted hardhat configurations * 💚 fixed ci * refactor: remove 100% coverage requirement from forge_coverage.sh - Remove strict 100% coverage checks that were causing workflow failures - Script now only generates and displays coverage report without failing - Aligns with previous behavior where coverage was informational only * ✅ resolve getVerifiedDetails * ✅ applied _getEligibility and remove via-ir from foundry.toml * ✅ cover tokena and token permit * ✅ cover compliance * ✅ cover identity registry * ✅ increased trex gateway coverage * ✅ increase coverage for trex factory * ✅ increased AbstractModuleUpgradeable.sol coverage * ⚰️ removed abstract module * 🔧 updted generate coverage * ✅ coverage 100% identity registry * ✅ coverage 100% trex gate way * ✅ coverage trex factory * ✅ coverage 100% AbstractModuleUpgradeable by test module * ✅ coverage IAFactory.sol * ✅ coverage 100% for implementation authority * ✅ coverage 1005 abstract proxy * ✅ coverage 100% claim topic registry * ✅ coverage 100% identity registry storage * ✅ coverage 100% modular compliance proxy * ✅ coverage trusted issuer registry proxy * ✅ coverage 100% identity registry * ✅ coverage 100% token proxy * 🚨 resolve linter in token information * ✅ coverage 100% roles * 🚨 resolve linter * 🔧 added no coverage for test, _testContracts and utils * ✅ coverage 100% for token.sol * ✅ coverage 100% for modular compliance * ✅ coverage 100% trusted issuer registry * 🔧 restore package.json * ✅ coverage 100% for utlity checker * 🚨 fix linter
174 lines
6.7 KiB
Solidity
174 lines
6.7 KiB
Solidity
// SPDX-License-Identifier: UNLICENSED
|
|
pragma solidity 0.8.30;
|
|
|
|
import { ERC2612ExpiredSignature, ERC2612InvalidSigner } from "contracts/token/TokenPermit.sol";
|
|
import { TokenTestBase } from "test/token/TokenTestBase.sol";
|
|
|
|
contract TokenPermitTest is TokenTestBase {
|
|
|
|
uint256 constant VALUE = 42;
|
|
uint256 constant NONCE = 0;
|
|
uint256 constant MAX_DEADLINE = type(uint256).max;
|
|
|
|
// Additional test addresses
|
|
uint256 public alicePrivateKey = 0x1; // Private key for alice
|
|
address public aliceSigner = vm.addr(alicePrivateKey); // alice address from private key
|
|
uint256 public bobPrivateKey = 0x2; // Private key for bob
|
|
address public bobSigner = vm.addr(bobPrivateKey); // bob address from private key
|
|
|
|
function setUp() public override {
|
|
super.setUp();
|
|
|
|
// Add tokenAgent as an agent
|
|
vm.prank(deployer);
|
|
token.addAgent(tokenAgent);
|
|
|
|
// Unpause token
|
|
vm.prank(tokenAgent);
|
|
token.unpause();
|
|
}
|
|
|
|
/// @notice Helper function to get EIP-712 domain separator
|
|
function _getDomainSeparator() internal view returns (bytes32) {
|
|
return keccak256(
|
|
abi.encode(
|
|
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
|
|
keccak256(bytes(token.name())),
|
|
keccak256(bytes(token.version())),
|
|
block.chainid,
|
|
address(token)
|
|
)
|
|
);
|
|
}
|
|
|
|
/// @notice Helper function to build permit signature
|
|
function _buildPermitSignature(
|
|
address owner,
|
|
address spender,
|
|
uint256 value,
|
|
uint256 nonce,
|
|
uint256 deadline,
|
|
uint256 privateKey
|
|
) internal view returns (uint8 v, bytes32 r, bytes32 s) {
|
|
bytes32 permitTypeHash = keccak256(
|
|
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
|
|
);
|
|
bytes32 structHash = keccak256(abi.encode(permitTypeHash, owner, spender, value, nonce, deadline));
|
|
bytes32 domainSeparator = _getDomainSeparator();
|
|
bytes32 typedDataHash = keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
|
|
|
|
return vm.sign(privateKey, typedDataHash);
|
|
}
|
|
|
|
// ============ Initial state Tests ============
|
|
|
|
/// @notice Initial nonce is 0
|
|
function test_InitialNonce_IsZero() public view {
|
|
assertEq(token.nonces(alice), 0);
|
|
assertEq(token.nonces(bob), 0);
|
|
assertEq(token.nonces(another), 0);
|
|
}
|
|
|
|
/// @notice Domain separator should match computed value
|
|
function test_DomainSeparator_Matches() public view {
|
|
bytes32 expectedDomainSeparator = _getDomainSeparator();
|
|
assertEq(token.DOMAIN_SEPARATOR(), expectedDomainSeparator);
|
|
}
|
|
|
|
// ============ Permit Tests ============
|
|
|
|
/// @notice Accepts owner signature
|
|
function test_permit_AcceptsOwnerSignature() public {
|
|
// Use alice and bob from TREXFactorySetup, but we need to sign with a known private key
|
|
// So we'll use the addresses that correspond to known private keys
|
|
address owner = aliceSigner;
|
|
address spender = bobSigner;
|
|
|
|
(uint8 v, bytes32 r, bytes32 s) =
|
|
_buildPermitSignature(owner, spender, VALUE, NONCE, MAX_DEADLINE, alicePrivateKey);
|
|
|
|
token.permit(owner, spender, VALUE, MAX_DEADLINE, v, r, s);
|
|
|
|
assertEq(token.nonces(owner), 1);
|
|
assertEq(token.allowance(owner, spender), VALUE);
|
|
}
|
|
|
|
/// @notice Rejects reused signature
|
|
function test_permit_RejectsReusedSignature() public {
|
|
address owner = aliceSigner;
|
|
address spender = bobSigner;
|
|
|
|
(uint8 v, bytes32 r, bytes32 s) =
|
|
_buildPermitSignature(owner, spender, VALUE, NONCE, MAX_DEADLINE, alicePrivateKey);
|
|
|
|
// First permit succeeds
|
|
token.permit(owner, spender, VALUE, MAX_DEADLINE, v, r, s);
|
|
|
|
// When trying to reuse the signature, the nonce has increased to 1
|
|
// The contract will check the signature against nonce=1, which will recover to a different address
|
|
// Calculate what address the signature would recover to with the new nonce
|
|
bytes32 permitTypeHash =
|
|
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
|
|
bytes32 structHash = keccak256(abi.encode(permitTypeHash, owner, spender, VALUE, 1, MAX_DEADLINE));
|
|
bytes32 domainSeparator = _getDomainSeparator();
|
|
bytes32 typedDataHash = keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
|
|
address recovered = ecrecover(typedDataHash, v, r, s);
|
|
|
|
// Try to reuse the same signature (nonce has increased, so signature recovers to wrong address)
|
|
vm.expectRevert(abi.encodeWithSelector(ERC2612InvalidSigner.selector, recovered, owner));
|
|
token.permit(owner, spender, VALUE, MAX_DEADLINE, v, r, s);
|
|
}
|
|
|
|
/// @notice Rejects other signature
|
|
function test_permit_RejectsOtherSignature() public {
|
|
address owner = aliceSigner;
|
|
address spender = bobSigner;
|
|
|
|
// Sign with bob's private key instead of alice's
|
|
(uint8 v, bytes32 r, bytes32 s) =
|
|
_buildPermitSignature(owner, spender, VALUE, NONCE, MAX_DEADLINE, bobPrivateKey);
|
|
|
|
vm.expectRevert(abi.encodeWithSelector(ERC2612InvalidSigner.selector, bobSigner, owner));
|
|
token.permit(owner, spender, VALUE, MAX_DEADLINE, v, r, s);
|
|
}
|
|
|
|
/// @notice Rejects expired permit
|
|
function test_permit_RejectsExpiredPermit() public {
|
|
address owner = aliceSigner;
|
|
address spender = bobSigner;
|
|
|
|
// Set deadline to past (1 week ago)
|
|
vm.warp(block.timestamp + 7 days);
|
|
uint256 deadline = block.timestamp - 7 days;
|
|
|
|
(uint8 v, bytes32 r, bytes32 s) = _buildPermitSignature(owner, spender, VALUE, NONCE, deadline, alicePrivateKey);
|
|
|
|
vm.expectRevert(abi.encodeWithSelector(ERC2612ExpiredSignature.selector, deadline));
|
|
token.permit(owner, spender, VALUE, deadline, v, r, s);
|
|
}
|
|
|
|
// ============ eip712Domain() Tests ============
|
|
|
|
/// @notice Should return correct EIP-712 domain information
|
|
function test_eip712Domain_ReturnsCorrectValues() public view {
|
|
(
|
|
bytes1 fields,
|
|
string memory name_,
|
|
string memory version_,
|
|
uint256 chainId,
|
|
address verifyingContract,
|
|
bytes32 salt,
|
|
uint256[] memory extensions
|
|
) = token.eip712Domain();
|
|
|
|
assertEq(fields, hex"0f");
|
|
assertEq(keccak256(bytes(name_)), keccak256(bytes(token.name())));
|
|
assertEq(keccak256(bytes(version_)), keccak256(bytes(token.version())));
|
|
assertEq(chainId, block.chainid);
|
|
assertEq(verifyingContract, address(token));
|
|
assertEq(salt, bytes32(0));
|
|
assertEq(extensions.length, 0);
|
|
}
|
|
|
|
}
|