Skip to content

Commit

Permalink
Merge branch 'main' into test-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
nonergodic authored Oct 11, 2024
2 parents ccd72ed + 5ae8bf3 commit c554595
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/interfaces/token/IWETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pragma solidity ^0.8.0;
import "IERC20/IERC20.sol";

interface IWETH is IERC20 {
event Deposit(address indexed dst, uint amount);
event Withdrawal(address indexed src, uint amount);

function deposit() external payable;
function withdraw(uint256 amount) external;
}
109 changes: 109 additions & 0 deletions src/libraries/PermitParsing.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: Apache 2

pragma solidity ^0.8.4;

import {BytesParsing} from "wormhole-sdk/libraries/BytesParsing.sol";

library PermitParsing {
using BytesParsing for bytes;

uint constant SIGNATURE_SIZE = 65;

function asPermitUnchecked(
bytes memory params,
uint offset
) internal pure returns (uint256, uint256, bytes32, bytes32, uint8, uint) {
uint256 value;
uint256 deadline;
bytes32 r;
bytes32 s;
uint8 v;
(value, offset) = params.asUint256Unchecked(offset);
(deadline, offset) = params.asUint256Unchecked(offset);
(r, offset) = params.asBytes32Unchecked(offset);
(s, offset) = params.asBytes32Unchecked(offset);
(v, offset) = params.asUint8Unchecked(offset);
return (value, deadline, r, s, v, offset);
}

function asPermitCdUnchecked(
bytes calldata params,
uint offset
) internal pure returns (uint256, uint256, bytes32, bytes32, uint8, uint) {
uint256 value;
uint256 deadline;
bytes32 r;
bytes32 s;
uint8 v;
(value, offset) = params.asUint256CdUnchecked(offset);
(deadline, offset) = params.asUint256CdUnchecked(offset);
(r, offset) = params.asBytes32CdUnchecked(offset);
(s, offset) = params.asBytes32CdUnchecked(offset);
(v, offset) = params.asUint8CdUnchecked(offset);
return (value, deadline, r, s, v, offset);
}

function asPermit2PermitUnchecked(
bytes memory params,
uint offset
) internal pure returns (uint160, uint48, uint48, uint256, bytes memory, uint) {
uint160 amount;
uint48 expiration;
uint48 nonce;
uint256 sigDeadline;
bytes memory signature;
(amount, offset) = params.asUint160Unchecked(offset);
(expiration, offset) = params.asUint48Unchecked(offset);
(nonce, offset) = params.asUint48Unchecked(offset);
(sigDeadline, offset) = params.asUint256Unchecked(offset);
(signature, offset) = params.sliceUnchecked(offset, SIGNATURE_SIZE);
return (amount, expiration, nonce, sigDeadline, signature, offset);
}

function asPermit2PermitCdUnchecked(
bytes calldata params,
uint offset
) internal pure returns (uint160, uint48, uint48, uint256, bytes memory, uint) {
uint160 amount;
uint48 expiration;
uint48 nonce;
uint256 sigDeadline;
bytes memory signature;
(amount, offset) = params.asUint160CdUnchecked(offset);
(expiration, offset) = params.asUint48CdUnchecked(offset);
(nonce, offset) = params.asUint48CdUnchecked(offset);
(sigDeadline, offset) = params.asUint256CdUnchecked(offset);
(signature, offset) = params.sliceCdUnchecked(offset, SIGNATURE_SIZE);
return (amount, expiration, nonce, sigDeadline, signature, offset);
}

function asPermit2TransferUnchecked(
bytes memory params,
uint offset
) internal pure returns (uint256, uint256, uint256, bytes memory, uint) {
uint256 amount;
uint256 nonce;
uint256 sigDeadline;
bytes memory signature;
(amount, offset) = params.asUint256Unchecked(offset);
(nonce, offset) = params.asUint256Unchecked(offset);
(sigDeadline, offset) = params.asUint256Unchecked(offset);
(signature, offset) = params.sliceUnchecked(offset, SIGNATURE_SIZE);
return (amount, nonce, sigDeadline, signature, offset);
}

function asPermit2TransferCdUnchecked(
bytes calldata params,
uint offset
) internal pure returns (uint256, uint256, uint256, bytes memory, uint) {
uint256 amount;
uint256 nonce;
uint256 sigDeadline;
bytes memory signature;
(amount, offset) = params.asUint256CdUnchecked(offset);
(nonce, offset) = params.asUint256CdUnchecked(offset);
(sigDeadline, offset) = params.asUint256CdUnchecked(offset);
(signature, offset) = params.sliceCdUnchecked(offset, SIGNATURE_SIZE);
return (amount, nonce, sigDeadline, signature, offset);
}
}
151 changes: 151 additions & 0 deletions test/PermintParsing.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// SPDX-License-Identifier: Apache 2

pragma solidity ^0.8.24;

import "forge-std/Test.sol";

import { BytesParsing } from "wormhole-sdk/libraries/BytesParsing.sol";
import { PermitParsing } from "wormhole-sdk/libraries/PermitParsing.sol";

contract PermitParsingTest is Test {
using BytesParsing for bytes;

function parsePermit(
bytes calldata params,
uint offset
) external pure returns (uint256, uint256, bytes32, bytes32, uint8, uint) {
return PermitParsing.asPermitCdUnchecked(params, offset);
}

function parsePermit2Permit(
bytes calldata params,
uint offset
) external pure returns (uint160, uint48, uint48, uint256, bytes memory, uint) {
return PermitParsing.asPermit2PermitCdUnchecked(params, offset);
}

function parsePermit2Transfer(
bytes calldata params,
uint offset
) external pure returns (uint256, uint256, uint256, bytes memory, uint) {
return PermitParsing.asPermit2TransferCdUnchecked(params, offset);
}

function testParsePermit(
uint256 value,
uint256 deadline,
bytes32 r,
bytes32 s,
uint8 v
) public {
bytes memory params = abi.encodePacked(value, deadline, r, s, v);

(
uint256 _value,
uint256 _deadline,
bytes32 _r,
bytes32 _s,
uint8 _v,
uint offset
) = PermitParsing.asPermitUnchecked(params, 0);
assertEq(_value, value);
assertEq(_deadline, deadline);
assertEq(_r, r);
assertEq(_s, s);
assertEq(_v, v);
assertEq(offset, params.length);

(
_value,
_deadline,
_r,
_s,
_v,
offset
) = this.parsePermit(params, 0);
assertEq(_value, value);
assertEq(_deadline, deadline);
assertEq(_r, r);
assertEq(_s, s);
assertEq(_v, v);
assertEq(offset, params.length);
}

function testParsePermit2Permit(
uint160 amount,
uint48 expiration,
uint48 nonce,
uint256 sigDeadline,
bytes memory signature
) public {
vm.assume(signature.length == 65);
bytes memory params = abi.encodePacked(amount, expiration, nonce, sigDeadline, signature);

(
uint160 _amount,
uint48 _expiration,
uint48 _nonce,
uint256 _sigDeadline,
bytes memory _signature,
uint offset
) = PermitParsing.asPermit2PermitUnchecked(params, 0);
assertEq(_amount, amount);
assertEq(_expiration, expiration);
assertEq(_nonce, nonce);
assertEq(_sigDeadline, sigDeadline);
assertEq(_signature, signature);
assertEq(offset, params.length);


(
_amount,
_expiration,
_nonce,
_sigDeadline,
_signature,
offset
) = this.parsePermit2Permit(params, 0);
assertEq(_amount, amount);
assertEq(_expiration, expiration);
assertEq(_nonce, nonce);
assertEq(_sigDeadline, sigDeadline);
assertEq(_signature, signature);
assertEq(offset, params.length);
}

function testParsePermit2Transfer(
uint256 amount,
uint256 nonce,
uint256 sigDeadline,
bytes memory signature
) public {
vm.assume(signature.length == 65);
bytes memory params = abi.encodePacked(amount, nonce, sigDeadline, signature);

(
uint256 _amount,
uint256 _nonce,
uint256 _sigDeadline,
bytes memory _signature,
uint offset
) = PermitParsing.asPermit2TransferUnchecked(params, 0);
assertEq(_amount, amount);
assertEq(_nonce, nonce);
assertEq(_sigDeadline, sigDeadline);
assertEq(_signature, signature);
assertEq(offset, params.length);

(
_amount,
_nonce,
_sigDeadline,
_signature,
offset
) = this.parsePermit2Transfer(params, 0);
assertEq(_amount, amount);
assertEq(_nonce, nonce);
assertEq(_sigDeadline, sigDeadline);
assertEq(_signature, signature);
assertEq(offset, params.length);
}
}

0 comments on commit c554595

Please sign in to comment.