Skip to content

Commit bd2eebb

Browse files
authored
Add Permit2Parsing lib (#64)
* add Permit2Parsing lib * fix solidity version * rename namings * add memory versions
1 parent 5a2da37 commit bd2eebb

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

src/libraries/PermitParsing.sol

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// SPDX-License-Identifier: Apache 2
2+
3+
pragma solidity ^0.8.4;
4+
5+
import {BytesParsing} from "wormhole-sdk/libraries/BytesParsing.sol";
6+
7+
library PermitParsing {
8+
using BytesParsing for bytes;
9+
10+
uint constant SIGNATURE_SIZE = 65;
11+
12+
function asPermitUnchecked(
13+
bytes memory params,
14+
uint offset
15+
) internal pure returns (uint256, uint256, bytes32, bytes32, uint8, uint) {
16+
uint256 value;
17+
uint256 deadline;
18+
bytes32 r;
19+
bytes32 s;
20+
uint8 v;
21+
(value, offset) = params.asUint256Unchecked(offset);
22+
(deadline, offset) = params.asUint256Unchecked(offset);
23+
(r, offset) = params.asBytes32Unchecked(offset);
24+
(s, offset) = params.asBytes32Unchecked(offset);
25+
(v, offset) = params.asUint8Unchecked(offset);
26+
return (value, deadline, r, s, v, offset);
27+
}
28+
29+
function asPermitCdUnchecked(
30+
bytes calldata params,
31+
uint offset
32+
) internal pure returns (uint256, uint256, bytes32, bytes32, uint8, uint) {
33+
uint256 value;
34+
uint256 deadline;
35+
bytes32 r;
36+
bytes32 s;
37+
uint8 v;
38+
(value, offset) = params.asUint256CdUnchecked(offset);
39+
(deadline, offset) = params.asUint256CdUnchecked(offset);
40+
(r, offset) = params.asBytes32CdUnchecked(offset);
41+
(s, offset) = params.asBytes32CdUnchecked(offset);
42+
(v, offset) = params.asUint8CdUnchecked(offset);
43+
return (value, deadline, r, s, v, offset);
44+
}
45+
46+
function asPermit2PermitUnchecked(
47+
bytes memory params,
48+
uint offset
49+
) internal pure returns (uint160, uint48, uint48, uint256, bytes memory, uint) {
50+
uint160 amount;
51+
uint48 expiration;
52+
uint48 nonce;
53+
uint256 sigDeadline;
54+
bytes memory signature;
55+
(amount, offset) = params.asUint160Unchecked(offset);
56+
(expiration, offset) = params.asUint48Unchecked(offset);
57+
(nonce, offset) = params.asUint48Unchecked(offset);
58+
(sigDeadline, offset) = params.asUint256Unchecked(offset);
59+
(signature, offset) = params.sliceUnchecked(offset, SIGNATURE_SIZE);
60+
return (amount, expiration, nonce, sigDeadline, signature, offset);
61+
}
62+
63+
function asPermit2PermitCdUnchecked(
64+
bytes calldata params,
65+
uint offset
66+
) internal pure returns (uint160, uint48, uint48, uint256, bytes memory, uint) {
67+
uint160 amount;
68+
uint48 expiration;
69+
uint48 nonce;
70+
uint256 sigDeadline;
71+
bytes memory signature;
72+
(amount, offset) = params.asUint160CdUnchecked(offset);
73+
(expiration, offset) = params.asUint48CdUnchecked(offset);
74+
(nonce, offset) = params.asUint48CdUnchecked(offset);
75+
(sigDeadline, offset) = params.asUint256CdUnchecked(offset);
76+
(signature, offset) = params.sliceCdUnchecked(offset, SIGNATURE_SIZE);
77+
return (amount, expiration, nonce, sigDeadline, signature, offset);
78+
}
79+
80+
function asPermit2TransferUnchecked(
81+
bytes memory params,
82+
uint offset
83+
) internal pure returns (uint256, uint256, uint256, bytes memory, uint) {
84+
uint256 amount;
85+
uint256 nonce;
86+
uint256 sigDeadline;
87+
bytes memory signature;
88+
(amount, offset) = params.asUint256Unchecked(offset);
89+
(nonce, offset) = params.asUint256Unchecked(offset);
90+
(sigDeadline, offset) = params.asUint256Unchecked(offset);
91+
(signature, offset) = params.sliceUnchecked(offset, SIGNATURE_SIZE);
92+
return (amount, nonce, sigDeadline, signature, offset);
93+
}
94+
95+
function asPermit2TransferCdUnchecked(
96+
bytes calldata params,
97+
uint offset
98+
) internal pure returns (uint256, uint256, uint256, bytes memory, uint) {
99+
uint256 amount;
100+
uint256 nonce;
101+
uint256 sigDeadline;
102+
bytes memory signature;
103+
(amount, offset) = params.asUint256CdUnchecked(offset);
104+
(nonce, offset) = params.asUint256CdUnchecked(offset);
105+
(sigDeadline, offset) = params.asUint256CdUnchecked(offset);
106+
(signature, offset) = params.sliceCdUnchecked(offset, SIGNATURE_SIZE);
107+
return (amount, nonce, sigDeadline, signature, offset);
108+
}
109+
}

0 commit comments

Comments
 (0)