-
Notifications
You must be signed in to change notification settings - Fork 12k
Add ERC20 Permit (EIP-2612) #2237
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
Merged
Merged
Changes from 30 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
19a413a
Initial storage-based implementation
nventuro 48c41df
Improve gas efficiency and docs
nventuro d1e0f4b
Initial sketch for tests
nventuro cf718ee
Fix encoding before hash
nventuro 53516bc
Implement nonce using Counter
nventuro be37397
Merge branch 'master' into erc20-permit
frangio 743fe9a
adjust pragma and add license
frangio 0c02fdc
adapt test to buidler
frangio 255e11a
disable eslint in wip file
frangio d5037d2
add solhint exceptions
frangio b11d7ab
use a cheaper strategy for domain separator caching
frangio 58c7098
add DOMAIN_SEPARATOR function
frangio 1bd9922
Merge branch 'master' into erc20-permit
frangio cb00f8a
add eip712 from #2418
frangio c5d01e9
move to drafts directory
frangio aacea0c
use EIP712 contract
frangio b57f655
fix test
frangio 0560ea8
emit contract in api/drafts page
frangio 4552f52
fix api/drafts page title
frangio 9a916f7
add constructor documentation
frangio 91e98a4
lint
frangio e331820
extract eip712 helpers
frangio ade3af2
test DOMAIN_SEPARATOR function
frangio 9a661c9
test permit postconditions
frangio f4b7f93
use different account for nonce test
frangio 8f7f47c
add test for rejected transaction
frangio b7b5abd
test expired permit
frangio 24e4582
remove note about domain separator and chain id
frangio 737fa3b
Merge remote-tracking branch 'upstream/master' into erc20-permit
frangio f410734
ensure deadline is before next block timestamp
frangio 9d60a71
Update contracts/token/ERC20/README.adoc
frangio 3beaf01
add test that reused signature is rejected
frangio 837586d
use valid signature when testing expired deadline
frangio add4067
Merge remote-tracking branch 'upstream/master' into erc20-permit
frangio f26f0e3
rename IERC2612Permit to IERC20Permit
frangio acd82cd
review documentation
frangio 4444d3b
Merge branch 'master' into erc20-permit
frangio 71c9e3d
add changelog entry
frangio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity >=0.6.5 <0.8.0; | ||
|
||
import "../token/ERC20/ERC20.sol"; | ||
import "./IERC2612Permit.sol"; | ||
import "../cryptography/ECDSA.sol"; | ||
import "../utils/Counters.sol"; | ||
import "./EIP712.sol"; | ||
|
||
/** | ||
* @dev Extension of {ERC20} that allows token holders to use their tokens | ||
* without sending any transactions by setting {IERC20-allowance} with a | ||
* signature using the {permit} method, and then spend them via | ||
* {IERC20-transferFrom}. | ||
* | ||
* The {permit} signature mechanism conforms to the {IERC2612Permit} interface. | ||
*/ | ||
abstract contract ERC20Permit is ERC20, IERC2612Permit, EIP712 { | ||
using Counters for Counters.Counter; | ||
|
||
mapping (address => Counters.Counter) private _nonces; | ||
|
||
// solhint-disable-next-line var-name-mixedcase | ||
bytes32 private immutable _PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); | ||
|
||
/** | ||
* @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`. | ||
* | ||
* It's a good idea to use the same `name` that is defined as the ERC20 token name. | ||
*/ | ||
constructor(string memory name) internal EIP712(name, "1") { | ||
} | ||
|
||
/** | ||
* @dev See {IERC2612Permit-permit}. | ||
*/ | ||
function permit(address owner, address spender, uint256 amount, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public virtual override { | ||
// solhint-disable-next-line not-rely-on-time | ||
require(block.timestamp <= deadline, "ERC20Permit: expired deadline"); | ||
|
||
bytes32 structHash = keccak256( | ||
abi.encode( | ||
_PERMIT_TYPEHASH, | ||
owner, | ||
spender, | ||
amount, | ||
_nonces[owner].current(), | ||
deadline | ||
) | ||
); | ||
|
||
bytes32 hash = _hashTypedDataV4(structHash); | ||
|
||
address signer = ECDSA.recover(hash, v, r, s); | ||
require(signer == owner, "ERC20Permit: invalid signature"); | ||
|
||
_nonces[owner].increment(); | ||
_approve(owner, spender, amount); | ||
} | ||
|
||
/** | ||
* @dev See {IERC2612Permit-nonces}. | ||
*/ | ||
function nonces(address owner) public view override returns (uint256) { | ||
return _nonces[owner].current(); | ||
} | ||
|
||
/** | ||
* @dev See {IERC2612Permit-DOMAIN_SEPARATOR}. | ||
*/ | ||
// solhint-disable-next-line func-name-mixedcase | ||
function DOMAIN_SEPARATOR() external view override returns (bytes32) { | ||
return _domainSeparatorV4(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity >=0.6.0 <0.8.0; | ||
|
||
/** | ||
* @dev Interface of the ERC2612 standard as defined in the EIP. | ||
* | ||
* Adds the {permit} method, which can be used to change one's | ||
* {IERC20-allowance} without having to send a transaction, by signing a | ||
* message. This allows users to spend tokens without having to hold Ether. | ||
* | ||
* See https://eips.ethereum.org/EIPS/eip-2612. | ||
*/ | ||
interface IERC2612Permit { | ||
/** | ||
* @dev Sets `amount` as the allowance of `spender` over `owner`'s tokens, | ||
* given `owner`'s signed approval. | ||
* | ||
* IMPORTANT: The same issues {IERC20-approve} has related to transaction | ||
* ordering also apply here. | ||
* | ||
* Emits an {Approval} event. | ||
* | ||
* Requirements: | ||
* | ||
* - `owner` cannot be the zero address. | ||
* - `spender` cannot be the zero address. | ||
* - `deadline` must be a timestamp in the future. | ||
* - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner` | ||
* over the EIP712-formatted function arguments. | ||
* - the signature must use ``owner``'s current nonce (see {nonces}). | ||
* | ||
* For more information on the signature format, see the | ||
* https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP | ||
* section]. | ||
*/ | ||
function permit(address owner, address spender, uint256 amount, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external; | ||
|
||
/** | ||
* @dev Returns the current ERC2612 nonce for `owner`. This value must be | ||
* included whenever a signature is generated for {permit}. | ||
* | ||
* Every successful call to {permit} increases ``owner``'s nonce by one. This | ||
frangio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* prevents a signature from being used multiple times. | ||
*/ | ||
function nonces(address owner) external view returns (uint256); | ||
|
||
/** | ||
* @dev Returns the domain separator used in the encoding of the signature for `permit`, as defined by {EIP712}. | ||
*/ | ||
// solhint-disable-next-line func-name-mixedcase | ||
function DOMAIN_SEPARATOR() external view returns (bytes32); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity >=0.6.0 <0.8.0; | ||
|
||
import "../drafts/ERC20Permit.sol"; | ||
|
||
contract ERC20PermitMock is ERC20Permit { | ||
constructor ( | ||
string memory name, | ||
string memory symbol, | ||
address initialAccount, | ||
uint256 initialBalance | ||
) public payable ERC20(name, symbol) ERC20Permit(name) { | ||
_mint(initialAccount, initialBalance); | ||
} | ||
|
||
function getChainId() external pure returns (uint256 chainId) { | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
chainId := chainid() | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.