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

adding permit #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
61 changes: 59 additions & 2 deletions WSYS.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,63 @@ contract WSYS {

mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
mapping (address => uint256) public nonces;

bytes32 public PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
bytes32 public DOMAIN_SEPARATOR;

constructor() public {
uint256 chainId;
assembly {
chainId := chainid()
}

DOMAIN_SEPARATOR = keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
chainId,
address(this)
)
);
}

function permit(address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s) external {
require(deadline >= block.timestamp, "ERC20Permit: expired deadline");

bytes32 hashStruct = keccak256(
abi.encode(
PERMIT_TYPEHASH,
owner,
spender,
amount,
nonces[owner]++,
deadline
)
);

bytes32 hash = keccak256(
abi.encodePacked(
'\x19\x01',
DOMAIN_SEPARATOR,
hashStruct
)
);

address signer = ecrecover(hash, v, r, s);
require(
signer != address(0) && signer == owner,
"ERC20Permit: invalid signature"
);

approve(spender, amount);
}

function() external payable {
deposit();
Expand Down Expand Up @@ -60,7 +117,7 @@ contract WSYS {
return transferFrom(msg.sender, dst, wad);
}

function transferFrom(
function transferFrom(
address src,
address dst,
uint256 wad
Expand Down Expand Up @@ -757,4 +814,4 @@ the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

*/
*/