-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from morpho-labs/feat/permit
Feat permit
- Loading branch information
Showing
4 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,168 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.0; | ||
|
||
import "./helpers/SigUtils.sol"; | ||
import "./helpers/BaseTest.sol"; | ||
|
||
contract PermitTest is BaseTest { | ||
SigUtils internal sigUtils; | ||
|
||
uint256 internal constant OWNER_PK = 0xA11CE; | ||
uint256 internal constant SPENDER_PK = 0xB0B; | ||
|
||
address internal owner; | ||
address internal spender; | ||
|
||
function setUp() public override { | ||
super.setUp(); | ||
|
||
sigUtils = new SigUtils(vault.DOMAIN_SEPARATOR()); | ||
|
||
owner = vm.addr(OWNER_PK); | ||
spender = vm.addr(SPENDER_PK); | ||
|
||
deal(address(vault), owner, 1e18); | ||
} | ||
|
||
function testPermit() public { | ||
Permit memory permit = Permit({owner: owner, spender: spender, value: 1e18, nonce: 0, deadline: 1 days}); | ||
|
||
bytes32 digest = sigUtils.getTypedDataHash(permit); | ||
|
||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest); | ||
|
||
vault.permit(permit.owner, permit.spender, permit.value, permit.deadline, v, r, s); | ||
|
||
assertEq(vault.allowance(owner, spender), 1e18); | ||
assertEq(vault.nonces(owner), 1); | ||
} | ||
|
||
function testRevertExpiredPermit() public { | ||
Permit memory permit = | ||
Permit({owner: owner, spender: spender, value: 1e18, nonce: vault.nonces(owner), deadline: 1 days}); | ||
|
||
bytes32 digest = sigUtils.getTypedDataHash(permit); | ||
|
||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest); | ||
|
||
vm.warp(1 days + 1 seconds); // fast forward one second past the deadline | ||
|
||
vm.expectRevert("ERC20Permit: expired deadline"); | ||
vault.permit(permit.owner, permit.spender, permit.value, permit.deadline, v, r, s); | ||
} | ||
|
||
function testRevertInvalidSigner() public { | ||
Permit memory permit = | ||
Permit({owner: owner, spender: spender, value: 1e18, nonce: vault.nonces(owner), deadline: 1 days}); | ||
|
||
bytes32 digest = sigUtils.getTypedDataHash(permit); | ||
|
||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(SPENDER_PK, digest); // spender signs owner's approval | ||
|
||
vm.expectRevert("ERC20Permit: invalid signature"); | ||
vault.permit(permit.owner, permit.spender, permit.value, permit.deadline, v, r, s); | ||
} | ||
|
||
function testRevertInvalidNonce() public { | ||
Permit memory permit = Permit({ | ||
owner: owner, | ||
spender: spender, | ||
value: 1e18, | ||
nonce: 1, // owner nonce stored on-chain is 0 | ||
deadline: 1 days | ||
}); | ||
|
||
bytes32 digest = sigUtils.getTypedDataHash(permit); | ||
|
||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest); | ||
|
||
vm.expectRevert("ERC20Permit: invalid signature"); | ||
vault.permit(permit.owner, permit.spender, permit.value, permit.deadline, v, r, s); | ||
} | ||
|
||
function testRevertSignatureReplay() public { | ||
Permit memory permit = Permit({owner: owner, spender: spender, value: 1e18, nonce: 0, deadline: 1 days}); | ||
|
||
bytes32 digest = sigUtils.getTypedDataHash(permit); | ||
|
||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest); | ||
|
||
vault.permit(permit.owner, permit.spender, permit.value, permit.deadline, v, r, s); | ||
|
||
vm.expectRevert("ERC20Permit: invalid signature"); | ||
vault.permit(permit.owner, permit.spender, permit.value, permit.deadline, v, r, s); | ||
} | ||
|
||
function testTransferFromLimitedPermit() public { | ||
Permit memory permit = Permit({owner: owner, spender: spender, value: 1e18, nonce: 0, deadline: 1 days}); | ||
|
||
bytes32 digest = sigUtils.getTypedDataHash(permit); | ||
|
||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest); | ||
|
||
vault.permit(permit.owner, permit.spender, permit.value, permit.deadline, v, r, s); | ||
|
||
vm.prank(spender); | ||
vault.transferFrom(owner, spender, 1e18); | ||
|
||
assertEq(vault.balanceOf(owner), 0); | ||
assertEq(vault.balanceOf(spender), 1e18); | ||
assertEq(vault.allowance(owner, spender), 0); | ||
} | ||
|
||
function testTransferFromMaxPermit() public { | ||
Permit memory permit = | ||
Permit({owner: owner, spender: spender, value: type(uint256).max, nonce: 0, deadline: 1 days}); | ||
|
||
bytes32 digest = sigUtils.getTypedDataHash(permit); | ||
|
||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest); | ||
|
||
vault.permit(permit.owner, permit.spender, permit.value, permit.deadline, v, r, s); | ||
|
||
vm.prank(spender); | ||
vault.transferFrom(owner, spender, 1e18); | ||
|
||
assertEq(vault.balanceOf(owner), 0); | ||
assertEq(vault.balanceOf(spender), 1e18); | ||
assertEq(vault.allowance(owner, spender), type(uint256).max); | ||
} | ||
|
||
function testFailInvalidAllowance() public { | ||
Permit memory permit = Permit({ | ||
owner: owner, | ||
spender: spender, | ||
value: 5e17, // approve only 0.5 tokens | ||
nonce: 0, | ||
deadline: 1 days | ||
}); | ||
|
||
bytes32 digest = sigUtils.getTypedDataHash(permit); | ||
|
||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest); | ||
|
||
vault.permit(permit.owner, permit.spender, permit.value, permit.deadline, v, r, s); | ||
|
||
vm.prank(spender); | ||
vault.transferFrom(owner, spender, 1e18); // attempt to transfer 1 vault | ||
} | ||
|
||
function testFailInvalidBalance() public { | ||
Permit memory permit = Permit({ | ||
owner: owner, | ||
spender: spender, | ||
value: 2e18, // approve 2 tokens | ||
nonce: 0, | ||
deadline: 1 days | ||
}); | ||
|
||
bytes32 digest = sigUtils.getTypedDataHash(permit); | ||
|
||
(uint8 v, bytes32 r, bytes32 s) = vm.sign(OWNER_PK, digest); | ||
|
||
vault.permit(permit.owner, permit.spender, permit.value, permit.deadline, v, r, s); | ||
|
||
vm.prank(spender); | ||
vault.transferFrom(owner, spender, 2e18); // attempt to transfer 2 tokens (owner only owns 1) | ||
} | ||
} |
This file contains 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 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,33 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.0; | ||
|
||
struct Permit { | ||
address owner; | ||
address spender; | ||
uint256 value; | ||
uint256 nonce; | ||
uint256 deadline; | ||
} | ||
|
||
// keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); | ||
bytes32 constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; | ||
|
||
contract SigUtils { | ||
bytes32 internal DOMAIN_SEPARATOR; | ||
|
||
constructor(bytes32 _DOMAIN_SEPARATOR) { | ||
DOMAIN_SEPARATOR = _DOMAIN_SEPARATOR; | ||
} | ||
|
||
// computes the hash of a permit | ||
function getStructHash(Permit memory _permit) internal pure returns (bytes32) { | ||
return keccak256( | ||
abi.encode(PERMIT_TYPEHASH, _permit.owner, _permit.spender, _permit.value, _permit.nonce, _permit.deadline) | ||
); | ||
} | ||
|
||
// computes the hash of the fully encoded EIP-712 message for the domain, which can be used to recover the signer | ||
function getTypedDataHash(Permit memory _permit) public view returns (bytes32) { | ||
return keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, getStructHash(_permit))); | ||
} | ||
} |