Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERC721Permit - PermitForAll #271

Merged
merged 12 commits into from
Aug 5, 2024
Original file line number Diff line number Diff line change
@@ -1 +1 @@
416622
416667
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_permit.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79445
79484
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62333
62372
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_permit_twice.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
45221
45260
52 changes: 47 additions & 5 deletions src/base/ERC721Permit_v4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,61 @@ abstract contract ERC721Permit_v4 is ERC721, IERC721Permit_v4, EIP712_v4, Unorde
/// @notice Computes the nameHash and versionHash
constructor(string memory name_, string memory symbol_) ERC721(name_, symbol_) EIP712_v4(name_) {}

modifier checkSignatureDeadline(uint256 deadline) {
if (block.timestamp > deadline) revert SignatureDeadlineExpired();
_;
}

/// @inheritdoc IERC721Permit_v4
function permit(address spender, uint256 tokenId, uint256 deadline, uint256 nonce, bytes calldata signature)
external
payable
checkSignatureDeadline(deadline)
{
if (block.timestamp > deadline) revert DeadlineExpired();

address owner = ownerOf(tokenId);
if (spender == owner) revert NoSelfPermit();
_checkNoSelfPermit(owner, spender);

bytes32 hash = ERC721PermitHashLibrary.hash(spender, tokenId, nonce, deadline);
signature.verify(_hashTypedData(hash), owner);
bytes32 digest = ERC721PermitHashLibrary.hashPermit(spender, tokenId, nonce, deadline);
signature.verify(_hashTypedData(digest), owner);

_useUnorderedNonce(owner, nonce);
_approve(owner, spender, tokenId);
}

/// @inheritdoc IERC721Permit_v4
function permitForAll(
address owner,
address operator,
bool approved,
uint256 deadline,
uint256 nonce,
bytes calldata signature
) external payable checkSignatureDeadline(deadline) {
_checkNoSelfPermit(owner, operator);

bytes32 hash = ERC721PermitHashLibrary.hashPermitForAll(operator, approved, nonce, deadline);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the variable name hash highlights as a keyword for me 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do u wanna use digest here too?

signature.verify(_hashTypedData(hash), owner);

_useUnorderedNonce(owner, nonce);
_approveForAll(owner, operator, approved);
}

/// @notice Enable or disable approval for a third party ("operator") to manage
/// all of `msg.sender`'s assets
/// @dev Emits the ApprovalForAll event. The contract MUST allow
/// multiple operators per owner.
/// @dev Override Solmate's ERC721 setApprovalForAll so setApprovalForAll() and permit() share the _approveForAll method
/// @param operator Address to add to the set of authorized operators
/// @param approved True if the operator is approved, false to revoke approval
function setApprovalForAll(address operator, bool approved) public override {
_approveForAll(msg.sender, operator, approved);
}

function _approveForAll(address owner, address operator, bool approved) internal {
isApprovedForAll[owner][operator] = approved;
emit ApprovalForAll(owner, operator, approved);
}

/// @notice Change or reaffirm the approved address for an NFT
/// @dev override Solmate's ERC721 approve so approve() and permit() share the _approve method
/// The zero address indicates there is no approved address
Expand All @@ -58,4 +96,8 @@ abstract contract ERC721Permit_v4 is ERC721, IERC721Permit_v4, EIP712_v4, Unorde
return spender == ownerOf(tokenId) || getApproved[tokenId] == spender
|| isApprovedForAll[ownerOf(tokenId)][spender];
}

function _checkNoSelfPermit(address owner, address permitted) internal pure {
if (owner == permitted) revert NoSelfPermit();
}
}
18 changes: 17 additions & 1 deletion src/interfaces/IERC721Permit_v4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.7.5;
/// @title ERC721 with permit
/// @notice Extension to ERC721 that includes a permit function for signature based approvals
interface IERC721Permit_v4 {
error DeadlineExpired();
error SignatureDeadlineExpired();
error NoSelfPermit();
error Unauthorized();

Expand All @@ -17,4 +17,20 @@ interface IERC721Permit_v4 {
function permit(address spender, uint256 tokenId, uint256 deadline, uint256 nonce, bytes calldata signature)
external
payable;

/// @notice Set an operator with full permission to an owner's tokens via signature
/// @param owner The address that is setting the operator
/// @param operator The address that will be set as an operator for the owner
/// @param approved The permission to set on the operator
/// @param deadline The deadline timestamp by which the call must be mined for the approve to work
/// @param signature Concatenated data from a valid secp256k1 signature from the holder, i.e. abi.encodePacked(r, s, v)
/// @dev payable so it can be multicalled with NATIVE related actions
function permitForAll(
address owner,
address operator,
bool approved,
uint256 deadline,
uint256 nonce,
bytes calldata signature
) external payable;
}
22 changes: 21 additions & 1 deletion src/libraries/ERC721PermitHash.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ library ERC721PermitHashLibrary {
/// @dev Value is equal to keccak256("Permit(address spender,uint256 tokenId,uint256 nonce,uint256 deadline)");
bytes32 constant PERMIT_TYPEHASH = 0x49ecf333e5b8c95c40fdafc95c1ad136e8914a8fb55e9dc8bb01eaa83a2df9ad;

function hash(address spender, uint256 tokenId, uint256 nonce, uint256 deadline)
/// @dev Value is equal to keccak256("PermitForAll(address operator,bool approved,uint256 nonce,uint256 deadline)");
bytes32 constant PERMIT_FOR_ALL_TYPEHASH = 0x6673cb397ee2a50b6b8401653d3638b4ac8b3db9c28aa6870ffceb7574ec2f76;

function hashPermit(address spender, uint256 tokenId, uint256 nonce, uint256 deadline)
internal
pure
returns (bytes32 digest)
Expand All @@ -21,4 +24,21 @@ library ERC721PermitHashLibrary {
digest := keccak256(fmp, 0xa0)
}
}

function hashPermitForAll(address operator, bool approved, uint256 nonce, uint256 deadline)
internal
pure
returns (bytes32 digest)
{
// equivalent to: keccak256(abi.encode(PERMIT_FOR_ALL_TYPEHASH, operator, approved, nonce, deadline));
assembly ("memory-safe") {
let fmp := mload(0x40)
mstore(fmp, PERMIT_FOR_ALL_TYPEHASH)
mstore(add(fmp, 0x20), operator)
mstore(add(fmp, 0x40), approved)
mstore(add(fmp, 0x60), nonce)
mstore(add(fmp, 0x80), deadline)
digest := keccak256(fmp, 0xa0)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ pragma solidity ^0.8.24;
import "forge-std/Test.sol";
import {SignatureVerification} from "permit2/src/libraries/SignatureVerification.sol";

import {ERC721PermitHashLibrary} from "../src/libraries/ERC721PermitHash.sol";
import {MockERC721Permit} from "./mocks/MockERC721Permit.sol";
import {IERC721Permit_v4} from "../src/interfaces/IERC721Permit_v4.sol";
import {ERC721PermitHashLibrary} from "../../src/libraries/ERC721PermitHash.sol";
import {MockERC721Permit} from "../mocks/MockERC721Permit.sol";
import {IERC721Permit_v4} from "../../src/interfaces/IERC721Permit_v4.sol";
import {IERC721} from "forge-std/interfaces/IERC721.sol";
import {UnorderedNonce} from "../src/base/UnorderedNonce.sol";
import {UnorderedNonce} from "../../src/base/UnorderedNonce.sol";

contract ERC721PermitTest is Test {
MockERC721Permit erc721Permit;
Expand Down Expand Up @@ -69,7 +69,7 @@ contract ERC721PermitTest is Test {
function test_fuzz_permitHash(address spender, uint256 tokenId, uint256 nonce, uint256 deadline) public pure {
bytes32 expectedHash =
keccak256(abi.encode(ERC721PermitHashLibrary.PERMIT_TYPEHASH, spender, tokenId, nonce, deadline));
assertEq(expectedHash, ERC721PermitHashLibrary.hash(spender, tokenId, nonce, deadline));
assertEq(expectedHash, ERC721PermitHashLibrary.hashPermit(spender, tokenId, nonce, deadline));
}

function test_domainSeparator() public view {
Expand All @@ -93,7 +93,7 @@ contract ERC721PermitTest is Test {
uint256 tokenId = erc721Permit.mint();

uint256 nonce = 1;
bytes32 digest = _getDigest(spender, tokenId, nonce, block.timestamp);
bytes32 digest = _getPermitDigest(spender, tokenId, nonce, block.timestamp);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePK, digest);
bytes memory signature = abi.encodePacked(r, s, v);

Expand Down Expand Up @@ -127,7 +127,7 @@ contract ERC721PermitTest is Test {
uint256 tokenId = erc721Permit.mint();

uint256 nonce = 1;
bytes32 digest = _getDigest(spender, tokenId, nonce, block.timestamp);
bytes32 digest = _getPermitDigest(spender, tokenId, nonce, block.timestamp);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePK, digest);
bytes memory signature = abi.encodePacked(r, s, v);

Expand Down Expand Up @@ -163,7 +163,7 @@ contract ERC721PermitTest is Test {
_permit(alicePK, tokenIdAlice, bob, nonce);

// alice cannot reuse the nonce
bytes32 digest = _getDigest(bob, tokenIdAlice, nonce, block.timestamp);
bytes32 digest = _getPermitDigest(bob, tokenIdAlice, nonce, block.timestamp);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePK, digest);
bytes memory signature = abi.encodePacked(r, s, v);
Expand All @@ -186,7 +186,7 @@ contract ERC721PermitTest is Test {
_permit(alicePK, tokenIdAlice, bob, nonce);

// alice cannot reuse the nonce for the second token
bytes32 digest = _getDigest(bob, tokenIdAlice2, nonce, block.timestamp);
bytes32 digest = _getPermitDigest(bob, tokenIdAlice2, nonce, block.timestamp);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePK, digest);
bytes memory signature = abi.encodePacked(r, s, v);
Expand All @@ -202,7 +202,7 @@ contract ERC721PermitTest is Test {
uint256 tokenId = erc721Permit.mint();

uint256 nonce = 1;
bytes32 digest = _getDigest(bob, tokenId, nonce, block.timestamp);
bytes32 digest = _getPermitDigest(bob, tokenId, nonce, block.timestamp);

// bob attempts signing an approval for himself
(uint8 v, bytes32 r, bytes32 s) = vm.sign(bobPK, digest);
Expand All @@ -229,13 +229,13 @@ contract ERC721PermitTest is Test {
assertEq(erc721Permit.nonces(alice, wordPos) & (1 << bitPos), 0);
}

function test_fuzz_erc721Permit_deadlineExpired(address spender) public {
function test_fuzz_erc721Permit_SignatureDeadlineExpired(address spender) public {
vm.prank(alice);
uint256 tokenId = erc721Permit.mint();

uint256 nonce = 1;
uint256 deadline = block.timestamp;
bytes32 digest = _getDigest(spender, tokenId, nonce, deadline);
bytes32 digest = _getPermitDigest(spender, tokenId, nonce, deadline);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(alicePK, digest);
bytes memory signature = abi.encodePacked(r, s, v);

Expand All @@ -252,7 +252,7 @@ contract ERC721PermitTest is Test {

// -- Permit but deadline expired -- //
vm.startPrank(spender);
vm.expectRevert(IERC721Permit_v4.DeadlineExpired.selector);
vm.expectRevert(IERC721Permit_v4.SignatureDeadlineExpired.selector);
erc721Permit.permit(spender, tokenId, deadline, nonce, signature);
vm.stopPrank();

Expand All @@ -266,7 +266,7 @@ contract ERC721PermitTest is Test {

// Helpers related to permit
function _permit(uint256 privateKey, uint256 tokenId, address operator, uint256 nonce) internal {
bytes32 digest = _getDigest(operator, tokenId, 1, block.timestamp);
bytes32 digest = _getPermitDigest(operator, tokenId, nonce, block.timestamp);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, digest);
bytes memory signature = abi.encodePacked(r, s, v);
Expand All @@ -275,7 +275,7 @@ contract ERC721PermitTest is Test {
erc721Permit.permit(operator, tokenId, block.timestamp, nonce, signature);
}

function _getDigest(address spender, uint256 tokenId, uint256 nonce, uint256 deadline)
function _getPermitDigest(address spender, uint256 tokenId, uint256 nonce, uint256 deadline)
internal
view
returns (bytes32 digest)
Expand Down
Loading
Loading