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

BT-235: ERC2612 - Add ERC-20 Permit function compatibility #236

Merged
merged 7 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
25 changes: 17 additions & 8 deletions contracts/token/Token.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@

pragma solidity 0.8.27;

import "./IToken.sol";
import "@onchain-id/solidity/contracts/interface/IIdentity.sol";
import "./TokenStorage.sol";
import "../roles/AgentRoleUpgradeable.sol";
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "../roles/IERC173.sol";
import "../errors/InvalidArgumentErrors.sol";
import "./IToken.sol";
import "./TokenPermit.sol";
import "./TokenStorage.sol";
import "../errors/CommonErrors.sol";
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "../errors/InvalidArgumentErrors.sol";
import "../roles/AgentRoleUpgradeable.sol";

/// errors

Expand Down Expand Up @@ -119,7 +120,13 @@ error DefaultAllowanceAlreadyDisabled(address _user);
error DefaultAllowanceAlreadySet(address _target);


contract Token is IToken, AgentRoleUpgradeable, TokenStorage, IERC165 {
contract Token is IToken, AgentRoleUpgradeable, TokenStorage, IERC165, TokenPermit {

bytes32 private constant _TYPE_HASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");

bytes32 private constant _PERMIT_TYPEHASH =
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");

/// modifiers

Expand Down Expand Up @@ -172,6 +179,7 @@ contract Token is IToken, AgentRoleUpgradeable, TokenStorage, IERC165 {
, EmptyString());
require(0 <= _decimals && _decimals <= 18, DecimalsOutOfRange(_decimals));
__Ownable_init();
__TokenPermit_init(_name);
_tokenName = _name;
_tokenSymbol = _symbol;
_tokenDecimals = _decimals;
Expand Down Expand Up @@ -674,7 +682,8 @@ contract Token is IToken, AgentRoleUpgradeable, TokenStorage, IERC165 {
interfaceId == type(IToken).interfaceId ||
interfaceId == type(IERC173).interfaceId ||
interfaceId == type(IERC165).interfaceId ||
interfaceId == type(IERC3643).interfaceId;
interfaceId == type(IERC3643).interfaceId ||
interfaceId == type(IERC20Permit).interfaceId;
}

/**
Expand Down Expand Up @@ -728,7 +737,7 @@ contract Token is IToken, AgentRoleUpgradeable, TokenStorage, IERC165 {
address _owner,
address _spender,
uint256 _amount
) internal virtual {
) internal virtual override {
require(_owner != address(0), ERC20InvalidSender(_owner));
require(_spender != address(0), ERC20InvalidSpender(_spender));

Expand Down
121 changes: 121 additions & 0 deletions contracts/token/TokenPermit.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// SPDX-License-Identifier: GPL-3.0
//
// :+#####%%%%%%%%%%%%%%+
// .-*@@@%+.:+%@@@@@%%#***%@@%=
// :=*%@@@#=. :#@@% *@@@%=
// .-+*%@%*-.:+%@@@@@@+. -*+: .=#. :%@@@%-
// :=*@@@@%%@@@@@@@@@%@@@- .=#@@@%@%= =@@@@#.
// -=+#%@@%#*=:. :%@@@@%. -*@@#*@@@@@@@#=:- *@@@@+
// =@@%=:. :=: *@@@@@%#- =%*%@@@@#+-. =+ :%@@@%-
// -@@%. .+@@@ =+=-. @@#- +@@@%- =@@@@%:
// :@@@. .+@@#%: : .=*=-::.-%@@@+*@@= +@@@@#.
// %@@: +@%%* =%@@@@@@@@@@@#. .*@%- +@@@@*.
// #@@= .+@@@@%:=*@@@@@- :%@%: .*@@@@+
// *@@* +@@@#-@@%-:%@@* +@@#. :%@@@@-
// -@@% .:-=++*##%%%@@@@@@@@@@@@*. :@+.@@@%: .#@@+ =@@@@#:
// .@@@*-+*#%%%@@@@@@@@@@@@@@@@%%#**@@%@@@. *@=*@@# :#@%= .#@@@@#-
// -%@@@@@@@@@@@@@@@*+==-:-@@@= *@# .#@*-=*@@@@%= -%@@@* =@@@@@%-
// -+%@@@#. %@%%= -@@:+@: -@@* *@@*-:: -%@@%=. .*@@@@@#
// *@@@* +@* *@@##@@- #@*@@+ -@@= . :+@@@#: .-+@@@%+-
// +@@@%*@@:..=@@@@* .@@@* .#@#. .=+- .=%@@@*. :+#@@@@*=:
// =@@@@%@@@@@@@@@@@@@@@@@@@@@@%- :+#*. :*@@@%=. .=#@@@@%+:
// .%@@= ..... .=#@@+. .#@@@*: -*%@@@@%+.
// +@@#+===---:::... .=%@@*- +@@@+. -*@@@@@%+.
// -@@@@@@@@@@@@@@@@@@@@@@%@@@@= -@@@+ -#@@@@@#=.
// ..:::---===+++***###%%%@@@#- .#@@+ -*@@@@@#=.
// @@@@@@+. +@@*. .+@@@@@%=.
// -@@@@@= =@@%: -#@@@@%+.
// +@@@@@. =@@@= .+@@@@@*:
// #@@@@#:%@@#. :*@@@@#-
// @@@@@%@@@= :#@@@@+.
// :@@@@@@@#.:#@@@%-
// +@@@@@@-.*@@@*:
// #@@@@#.=@@@+.
// @@@@+-%@%=
// :@@@#%@%=
// +@@@@%-
// :#%%=
//

/**
* NOTICE
*
* The T-REX software is licensed under a proprietary license or the GPL v.3.
* If you choose to receive it under the GPL v.3 license, the following applies:
* T-REX is a suite of smart contracts implementing the ERC-3643 standard and
* developed by Tokeny to manage and transfer financial assets on EVM blockchains
*
* Copyright (C) 2023, Tokeny sàrl.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

pragma solidity 0.8.27;

import { IERC20Permit } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol";
import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import { EIP712Upgradeable } from "../utils/EIP712Upgradeable.sol";
import { NoncesUpgradeable } from "../utils/NoncesUpgradeable.sol";

error ERC2612ExpiredSignature(uint256 deadline);
error ERC2612InvalidSigner(address signer, address owner);


abstract contract TokenPermit is IERC20Permit, EIP712Upgradeable, NoncesUpgradeable {

bytes32 private constant _PERMIT_TYPEHASH =
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");

/// @inheritdoc IERC20Permit
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external {
require(block.timestamp <= deadline, ERC2612ExpiredSignature(deadline));

bytes32 structHash = keccak256(abi.encode(_PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));

bytes32 hash = _hashTypedDataV4(structHash);

address signer = ECDSA.recover(hash, v, r, s);
require(signer == owner, ERC2612InvalidSigner(signer, owner));

_approve(owner, spender, value);
}

/// @inheritdoc IERC20Permit
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() external view returns (bytes32) {
return _domainSeparatorV4();
}

/// @inheritdoc IERC20Permit
function nonces(address owner) public view override(IERC20Permit, NoncesUpgradeable) returns (uint256) {
return super.nonces(owner);
}

// solhint-disable-next-line func-name-mixedcase
function __TokenPermit_init(string memory name) internal {
__EIP712_init_unchained(name, "1");
}

/// @dev Implemented in Token.sol
function _approve(address owner, address spender, uint256 value) internal virtual;

}
Loading
Loading