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 #210

Merged
merged 47 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
0c159e2
allow for nonsigners to call permit
saucepoint Jul 19, 2024
d13fbe7
forge fmt
saucepoint Jul 19, 2024
ff0b56e
test permit with multicall
saucepoint Jul 19, 2024
558b621
make DOMAIN_SEPARATOR immutable
saucepoint Jul 19, 2024
36ccc28
avoid chain fork replays
saucepoint Jul 19, 2024
86e520b
misc test cleanup
saucepoint Jul 19, 2024
e0ec23c
custom errors
saucepoint Jul 19, 2024
fc539e3
move magic hex to a constant
saucepoint Jul 19, 2024
095ad00
unpayable permit
saucepoint Jul 19, 2024
85390a6
use OZ EIP712
saucepoint Jul 20, 2024
b5d18dc
separate out UnorderedNonce into a reusable contract
saucepoint Jul 20, 2024
1521e70
move token URI to posm
saucepoint Jul 21, 2024
16939fb
add back in payable permit
saucepoint Jul 21, 2024
ed59b58
fix cherry picked commits
saucepoint Jul 26, 2024
b0559b8
remove public digest getter
saucepoint Jul 26, 2024
e458882
replace range with config naming
saucepoint Jul 26, 2024
88981c1
merge in main; regenerate gas
saucepoint Jul 30, 2024
9ba2556
deprecate old test: requiring permission to increase liq
saucepoint Jul 30, 2024
3cc8407
pr feedback
saucepoint Jul 30, 2024
a229cf8
borrow pertmi2 nonce tests for UnorderedNonce
saucepoint Jul 30, 2024
76a5e47
dedicated permit and approve testing for ERC721Permit
saucepoint Jul 30, 2024
cdde9e6
pr feedback: operator should be broadcaster of permit calls
saucepoint Jul 30, 2024
c0a26b8
reorganize permit hashing and verification
saucepoint Jul 30, 2024
834f331
merge in main; fix fuzz test for SelfPermit; regenerate gas
saucepoint Jul 30, 2024
019bad1
refactor ERC721Permit signature verification with generic signature c…
saucepoint Jul 30, 2024
0d445f2
remove deprecated library
saucepoint Jul 30, 2024
741e660
fix imports
saucepoint Jul 31, 2024
8dc0ca3
merge in main; regenerate gas
saucepoint Jul 31, 2024
85eae38
formatting
saucepoint Jul 31, 2024
ff5bb68
Merge branch 'main' into posm-erc721permit-signed
saucepoint Aug 1, 2024
d188aa2
merge main; regenerate gas
saucepoint Aug 1, 2024
600dd6d
pr feedback
saucepoint Aug 1, 2024
3cab51c
optimize nonce bit flipping
saucepoint Aug 1, 2024
b15da09
Merge branch 'main' into posm-erc721permit-signed
saucepoint Aug 1, 2024
750dcba
Merge branch 'main' into posm-erc721permit-signed
saucepoint Aug 2, 2024
5c566d6
merge in main; regenerate gas
saucepoint Aug 2, 2024
6eafa63
discard public PERMIT_TYPEHASH
saucepoint Aug 2, 2024
ce52a7f
merge in main; regenerate gas
saucepoint Aug 2, 2024
e58f516
Merge branch 'main' into posm-erc721permit-signed
saucepoint Aug 2, 2024
ff6e58c
renaming
saucepoint Aug 2, 2024
33832c0
library-ify bit flipping
saucepoint Aug 2, 2024
d59bd1d
merge in main; regenerate gas
saucepoint Aug 2, 2024
42c8268
merge in main; fix conflicts; regenerate gas
saucepoint Aug 2, 2024
57542bd
yall crazy for sending through the ringer
saucepoint Aug 2, 2024
778d7f3
merge in main; regenerate gas
saucepoint Aug 2, 2024
9021dd8
nits
saucepoint Aug 3, 2024
9732562
merge in main; regenerate gas
saucepoint Aug 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_empty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50237
50219
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_empty_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50055
50036
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_nonEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135881
135863
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_nonEmpty_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128803
128784
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
158176
158153
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149328
149305
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
158176
158153
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decreaseLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123719
123696
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114937
114919
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_burnEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142224
142205
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134963
134944
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136435
136412
Original file line number Diff line number Diff line change
@@ -1 +1 @@
152896
152873
Original file line number Diff line number Diff line change
@@ -1 +1 @@
138106
138083
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144757
144734
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180911
180888
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
370660
370637
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
338770
338747
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_nativeWithSweep.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
345708
345685
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickLower.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
313342
313319
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickUpper.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
313984
313961
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
239566
239543
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319360
319337
Original file line number Diff line number Diff line change
@@ -1 +1 @@
415150
415127
1 change: 1 addition & 0 deletions .forge-snapshots/PositionManager_permit.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
76544
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
59444
1 change: 1 addition & 0 deletions .forge-snapshots/PositionManager_permit_twice.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42344
5 changes: 5 additions & 0 deletions src/PositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ contract PositionManager is
_;
}

// TODO: implement
saucepoint marked this conversation as resolved.
Show resolved Hide resolved
function tokenURI(uint256) public pure override returns (string memory) {
return "https://example.com";
}

/// @param unlockData is an encoding of actions, params, and currencies
/// @return returnData is the endocing of each actions return information
function modifyLiquidities(bytes calldata unlockData, uint256 deadline)
Expand Down
92 changes: 83 additions & 9 deletions src/base/ERC721Permit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,93 @@
pragma solidity ^0.8.24;

import {ERC721} from "solmate/src/tokens/ERC721.sol";
import {IERC1271} from "@openzeppelin/contracts/interfaces/IERC1271.sol";
import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
saucepoint marked this conversation as resolved.
Show resolved Hide resolved

/// @notice An ERC721 contract that supports permit.
/// TODO: Support permit.
contract ERC721Permit is ERC721 {
constructor(string memory name_, string memory symbol_, string memory version_) ERC721(name_, symbol_) {}
import {IERC721Permit} from "../interfaces/IERC721Permit.sol";
import {UnorderedNonce} from "./UnorderedNonce.sol";

/// @title ERC721 with permit
/// @notice Nonfungible tokens that support an approve via signature, i.e. permit
abstract contract ERC721Permit is ERC721, IERC721Permit, EIP712, UnorderedNonce {
/// @inheritdoc IERC721Permit
/// @dev Value is equal to keccak256("Permit(address spender,uint256 tokenId,uint256 nonce,uint256 deadline)");
bytes32 public constant override PERMIT_TYPEHASH =
0x49ecf333e5b8c95c40fdafc95c1ad136e8914a8fb55e9dc8bb01eaa83a2df9ad;

/// @notice Computes the nameHash and versionHash
constructor(string memory name_, string memory symbol_, string memory version_)
ERC721(name_, symbol_)
EIP712(name_, version_)
{}

/// @inheritdoc IERC721Permit
function DOMAIN_SEPARATOR() public view returns (bytes32) {
saucepoint marked this conversation as resolved.
Show resolved Hide resolved
return _domainSeparatorV4();
}

/// @inheritdoc IERC721Permit
function permit(address spender, uint256 tokenId, uint256 deadline, uint256 nonce, uint8 v, bytes32 r, bytes32 s)
external
payable
override
{
if (block.timestamp > deadline) revert DeadlineExpired();

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

bytes32 digest = getDigest(spender, tokenId, nonce, deadline);

saucepoint marked this conversation as resolved.
Show resolved Hide resolved
if (owner.code.length != 0) {
if (
IERC1271(owner).isValidSignature(digest, abi.encodePacked(r, s, v))
!= IERC1271.isValidSignature.selector
) {
revert Unauthorized();
}
} else {
address recoveredAddress = ecrecover(digest, v, r, s);
if (recoveredAddress == address(0)) revert InvalidSignature();
if (recoveredAddress != owner) revert Unauthorized();
}

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

function approve(address spender, uint256 id) public override {
saucepoint marked this conversation as resolved.
Show resolved Hide resolved
// override Solmate's ERC721 approve so approve() and permit() share the same code paths
saucepoint marked this conversation as resolved.
Show resolved Hide resolved

address owner = _ownerOf[id];

if (msg.sender != owner && !isApprovedForAll[owner][msg.sender]) revert Unauthorized();

_approve(owner, spender, id);
}

function _approve(address owner, address spender, uint256 id) internal {
getApproved[id] = spender;
emit Approval(owner, spender, id);
}

function getDigest(address spender, uint256 tokenId, uint256 _nonce, uint256 deadline)
public
view
returns (bytes32 digest)
{
digest = keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(abi.encode(PERMIT_TYPEHASH, spender, tokenId, _nonce, deadline))
)
);
saucepoint marked this conversation as resolved.
Show resolved Hide resolved
}

function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) {
return spender == ownerOf(tokenId) || getApproved[tokenId] == spender
|| isApprovedForAll[ownerOf(tokenId)][spender];
}

// TODO: Use PositionDescriptor.
function tokenURI(uint256 id) public pure override returns (string memory) {
return string(abi.encode(id));
}
}
30 changes: 30 additions & 0 deletions src/base/UnorderedNonce.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.24;

contract UnorderedNonce {
saucepoint marked this conversation as resolved.
Show resolved Hide resolved
error NonceAlreadyUsed();

mapping(address owner => mapping(uint256 word => uint256 bitmap)) public nonces;
Copy link
Member

Choose a reason for hiding this comment

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

natspec that the word is actually max type(uint248).max


/// @notice Consume a nonce, reverting if its already been used
/// @param owner address, the owner/signer of the nonce
/// @param nonce uint256, the nonce to consume
function _useUnorderedNonce(address owner, uint256 nonce) internal {
saucepoint marked this conversation as resolved.
Show resolved Hide resolved
(uint256 wordPos, uint256 bitPos) = bitmapPositions(nonce);
uint256 bit = 1 << bitPos;
hensha256 marked this conversation as resolved.
Show resolved Hide resolved
uint256 flipped = nonces[owner][wordPos] ^= bit;

if (flipped & bit == 0) revert NonceAlreadyUsed();
}

/// @notice Returns the index of the bitmap and the bit position within the bitmap. Used for unordered nonces
/// @param nonce The nonce to get the associated word and bit positions
/// @return wordPos The word position or index into the nonceBitmap
/// @return bitPos The bit position
/// @dev The first 248 bits of the nonce value is the index of the desired bitmap
/// @dev The last 8 bits of the nonce value is the position of the bit in the bitmap
function bitmapPositions(uint256 nonce) private pure returns (uint256 wordPos, uint256 bitPos) {
wordPos = uint248(nonce >> 8);
bitPos = uint8(nonce);
}
}
5 changes: 4 additions & 1 deletion src/interfaces/IERC721Permit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ pragma solidity >=0.7.5;
/// @title ERC721 with permit
/// @notice Extension to ERC721 that includes a permit function for signature based approvals
interface IERC721Permit {
error NonceAlreadyUsed();
error DeadlineExpired();
error NoSelfPermit();
error Unauthorized();
error InvalidSignature();

/// @notice The permit typehash used in the permit signature
/// @return The typehash for the permit
Expand Down
13 changes: 0 additions & 13 deletions src/libraries/ChainId.sol

This file was deleted.

Loading
Loading